cb06fbdcefc89ae81ba79f52db3275a15d0990b7
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / tableview / TableViewToolBar.java
1 /*
2  * Created on 28-jul-2003
3  *
4  */
5 package com.quantum.view.tableview;
6
7 import com.quantum.Messages;
8 import com.quantum.QuantumPlugin;
9 import com.quantum.actions.CloseTableAction;
10 import com.quantum.actions.RefreshTableAction;
11 import com.quantum.model.NotConnectedException;
12 import com.quantum.sql.TableRow;
13 import com.quantum.util.StringMatrix;
14 import com.quantum.view.CopyAction;
15 import com.quantum.wizards.SQLRowWizard;
16 import com.quantum.wizards.SortFilterPage;
17
18 import org.eclipse.jface.action.Action;
19 import org.eclipse.jface.wizard.WizardDialog;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.SelectionEvent;
22 import org.eclipse.swt.events.SelectionListener;
23 import org.eclipse.swt.widgets.Label;
24 import org.eclipse.swt.widgets.Table;
25 import org.eclipse.swt.widgets.TableColumn;
26 import org.eclipse.swt.widgets.ToolBar;
27 import org.eclipse.swt.widgets.ToolItem;
28
29 /**
30  * @author panic
31  *
32  */
33 public class TableViewToolBar {
34
35         private ToolBar toolBar;
36         private RefreshTableAction refreshTableAction;
37         private CloseTableAction closeTableAction;
38         private ToolItem previous;
39         private ToolItem next;
40         
41         ToolItem filter;
42
43         public TableViewToolBar(
44                 final TableView view,
45                 final ToolBar toolBar, 
46                 final Table table,
47                 final TableAdapter ta,
48                 final Label label) {
49
50                 this.toolBar = toolBar;
51                 
52                 refreshTableAction = new RefreshTableAction();
53                 refreshTableAction.setText(Messages.getString("tableview.refresh")); //$NON-NLS-1$
54                 refreshTableAction.setImageDescriptor(
55                         QuantumPlugin.getImageDescriptor("refresh.gif")); //$NON-NLS-1$
56                 refreshTableAction.init(view);
57                 closeTableAction = new CloseTableAction();
58                 closeTableAction.setText(Messages.getString("tableview.close")); //$NON-NLS-1$
59                 closeTableAction.setImageDescriptor(
60                         QuantumPlugin.getImageDescriptor("close.gif")); //$NON-NLS-1$
61                 closeTableAction.init(view);
62
63                 ToolItem toolItem = new ToolItem(toolBar, SWT.PUSH);
64                 toolItem.setImage(QuantumPlugin.getImage("refresh.gif")); //$NON-NLS-1$
65                 toolItem.setToolTipText(Messages.getString("tableview.refresh")); //$NON-NLS-1$
66                 toolItem.addSelectionListener(new SelectionListener() {
67                         public void widgetDefaultSelected(SelectionEvent e) {
68                         }
69                         public void widgetSelected(SelectionEvent e) {
70                                 refreshTableAction.run();
71                         }
72                 });
73
74                 final Action copyAction = new CopyAction(view, table);
75                 final Action selectAllAction = new Action() {
76                         public void run() {
77                                 table.selectAll();
78                         }
79                 };
80                 toolItem = new ToolItem(toolBar, SWT.PUSH);
81                 toolItem.setImage(QuantumPlugin.getImage("copy.gif")); //$NON-NLS-1$
82                 toolItem.setToolTipText(Messages.getString("tableview.copy")); //$NON-NLS-1$
83                 toolItem.addSelectionListener(new SelectionListener() {
84                         public void widgetDefaultSelected(SelectionEvent e) {
85                         }
86                         public void widgetSelected(SelectionEvent e) {
87                                 copyAction.run();
88                         }
89                 });
90                 toolItem = new ToolItem(toolBar, SWT.PUSH);
91                 toolItem.setImage(QuantumPlugin.getImage("table.gif")); //$NON-NLS-1$
92                 toolItem.setToolTipText(Messages.getString("tableview.selectAll")); //$NON-NLS-1$
93                 toolItem.addSelectionListener(new SelectionListener() {
94                         public void widgetDefaultSelected(SelectionEvent e) {
95                         }
96                         public void widgetSelected(SelectionEvent e) {
97                                 selectAllAction.run();
98                         }
99                 });
100
101                 filter = new ToolItem(toolBar, SWT.PUSH);
102                 filter.setImage(QuantumPlugin.getImage("filter.gif")); //$NON-NLS-1$
103                 filter.setToolTipText(Messages.getString("tableview.filterSort")); //$NON-NLS-1$
104
105                 toolItem = new ToolItem(toolBar, SWT.SEPARATOR);
106
107                 final ToolItem fullMode = new ToolItem(toolBar, SWT.PUSH | SWT.CHECK);
108
109                 previous = new ToolItem(toolBar, SWT.PUSH);
110                 next = new ToolItem(toolBar, SWT.PUSH);
111
112                 fullMode.setImage(QuantumPlugin.getImage("fulldata.gif")); //$NON-NLS-1$
113                 fullMode.setToolTipText(Messages.getString("tableview.showAll")); //$NON-NLS-1$
114                 fullMode.setSelection(false);
115                 fullMode.addSelectionListener(new SelectionListener() {
116                         public void widgetDefaultSelected(SelectionEvent e) {
117                         }
118                         public void widgetSelected(SelectionEvent event) {
119                 try {
120                                 if (ta.getPageSize() == Integer.MAX_VALUE) {
121                                         ta.resetMode();
122                                 } else {
123                                         ta.fullMode();
124                                 }
125                                 ta.loadData();
126                                 table.removeAll();
127                                 for (int i = table.getColumnCount() - 1; i >= 0; i--) {
128                                         table.getColumn(i).dispose();
129                                 }
130                                 ta.loadTable(table);
131                                 label.setText(ta.getStatusString());
132                                 previous.setEnabled(ta.hasPreviousPage());
133                                 next.setEnabled(ta.hasNextPage());
134                 } catch (NotConnectedException e) {
135                     view.handleException(e);
136                 }
137                         }
138                 });
139                 previous.setImage(QuantumPlugin.getImage("previous.gif")); //$NON-NLS-1$
140                 previous.setToolTipText("Previous"); //$NON-NLS-1$
141                 previous.addSelectionListener(new SelectionListener() {
142                         public void widgetDefaultSelected(SelectionEvent e) {
143                         }
144                         public void widgetSelected(SelectionEvent event) {
145                 try {
146                                 ta.previousPage();
147                                 ta.loadData();
148                                 table.removeAll();
149                                 for (int i = table.getColumnCount() - 1; i >= 0; i--) {
150                                         table.getColumn(i).dispose();
151                                 }
152                                 ta.loadTable(table);
153                                 label.setText(ta.getStatusString());
154                                 previous.setEnabled(ta.hasPreviousPage());
155                                 next.setEnabled(ta.hasNextPage());
156                 } catch (NotConnectedException e) {
157                     view.handleException(e);
158                 }
159                         }
160                 });
161                 next.setImage(QuantumPlugin.getImage("next.gif")); //$NON-NLS-1$
162                 next.setToolTipText("Next"); //$NON-NLS-1$
163                 next.addSelectionListener(new SelectionListener() {
164                         public void widgetDefaultSelected(SelectionEvent e) {
165                         }
166                         public void widgetSelected(SelectionEvent event) {
167                 try {
168                                 ta.nextPage();
169                                 ta.loadData();
170                                 table.removeAll();
171                                 for (int i = table.getColumnCount() - 1; i >= 0; i--) {
172                                         table.getColumn(i).dispose();
173                                 }
174                                 ta.loadTable(table);
175                                 label.setText(ta.getStatusString());
176                                 previous.setEnabled(ta.hasPreviousPage());
177                                 next.setEnabled(ta.hasNextPage());
178                 } catch (NotConnectedException e) {
179                     view.handleException(e);
180                 }
181                         }
182                 });
183
184                 toolItem = new ToolItem(toolBar, SWT.SEPARATOR);
185
186                 toolItem = new ToolItem(toolBar, SWT.PUSH);
187                 toolItem.setImage(QuantumPlugin.getImage("close.gif")); //$NON-NLS-1$
188                 toolItem.setToolTipText(Messages.getString("tableview.close")); //$NON-NLS-1$
189                 toolItem.addSelectionListener(new SelectionListener() {
190                         public void widgetDefaultSelected(SelectionEvent e) {
191                         }
192                         public void widgetSelected(SelectionEvent e) {
193                                 closeTableAction.run();
194                         }
195                 });
196
197         }
198
199         public void setColumns( final TableView view,   final TableAdapter ta,  Table table) {
200
201                 // We create a matrix of TableColumn (columns in the Table widget) with
202                 // the columns in the table object
203                 TableColumn[] columns = table.getColumns();
204                 String columnNames[] = new String[columns.length];
205                 for (int i = 0; i < columns.length; i++) {
206                         columnNames[i] = columns[i].getText();
207                 }
208                 // Copy in data the values of the data columns of the selected rows
209                 StringMatrix data = new StringMatrix();
210                 data.addMatrixHeader(columnNames);
211                         // Create dummy values in case nothing selected
212                         for (int i = 0; i < columns.length; i++) {
213                                 data.addAt(columnNames[i], "", 0); //$NON-NLS-1$
214                 }
215                 final TableRow emptyRow = new TableRow(ta.getEntity(), ta.getBookmark(), ta.getTable(), data);
216
217                 filter.addSelectionListener(new SelectionListener() {
218                         public void widgetDefaultSelected(SelectionEvent e) {
219                         }
220                         public void widgetSelected(SelectionEvent e) {
221                                 SortFilterPage page = new SortFilterPage(""); //$NON-NLS-1$
222                                 SQLRowWizard wizard = new SQLRowWizard();
223                                 wizard.init(Messages.getString("TableView.FilterAndSort"), page, emptyRow, ta); //$NON-NLS-1$
224                                 WizardDialog dialog = new WizardDialog(view.getSite().getShell(), wizard);
225                                 dialog.open();
226                         }
227                 });
228         }
229         
230         
231
232         /**
233          * @return
234          */
235         public ToolItem getNext() {
236                 return next;
237         }
238
239         /**
240          * @return
241          */
242         public ToolItem getPrevious() {
243                 return previous;
244         }
245
246 }