new sql.gif
[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.ImageStore;
8 import com.quantum.Messages;
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 ToolItem previous;
37         private ToolItem next;
38         
39         ToolItem filter;
40
41         public TableViewToolBar(
42                 final TableView view,
43                 final ToolBar toolBar, 
44                 final Table table,
45                 final TableAdapter ta,
46                 final Label label) {
47
48                 this.toolBar = toolBar;
49                 
50                 final Action copyAction = new CopyAction(view, table);
51                 final Action selectAllAction = new Action() {
52                         public void run() {
53                                 table.selectAll();
54                         }
55                 };
56                 ToolItem toolItem = new ToolItem(toolBar, SWT.PUSH);
57                 toolItem.setImage(ImageStore.getImage(ImageStore.COPY));
58                 toolItem.setToolTipText(Messages.getString("tableview.copy")); //$NON-NLS-1$
59                 toolItem.addSelectionListener(new SelectionListener() {
60                         public void widgetDefaultSelected(SelectionEvent e) {
61                         }
62                         public void widgetSelected(SelectionEvent e) {
63                                 copyAction.run();
64                         }
65                 });
66                 toolItem = new ToolItem(toolBar, SWT.PUSH);
67                 toolItem.setImage(ImageStore.getImage(ImageStore.OPEN_TABLE)); //$NON-NLS-1$
68                 toolItem.setToolTipText(Messages.getString("tableview.selectAll")); //$NON-NLS-1$
69                 toolItem.addSelectionListener(new SelectionListener() {
70                         public void widgetDefaultSelected(SelectionEvent e) {
71                         }
72                         public void widgetSelected(SelectionEvent e) {
73                                 selectAllAction.run();
74                         }
75                 });
76
77                 filter = new ToolItem(toolBar, SWT.PUSH);
78                 filter.setImage(ImageStore.getImage(ImageStore.FILTER));
79                 filter.setToolTipText(Messages.getString("tableview.filterSort")); //$NON-NLS-1$
80
81                 toolItem = new ToolItem(toolBar, SWT.SEPARATOR);
82
83                 final ToolItem fullMode = new ToolItem(toolBar, SWT.PUSH | SWT.CHECK);
84
85                 previous = new ToolItem(toolBar, SWT.PUSH);
86                 next = new ToolItem(toolBar, SWT.PUSH);
87
88                 fullMode.setImage(ImageStore.getImage(ImageStore.FULLDATA));
89                 fullMode.setToolTipText(Messages.getString("tableview.showAll")); //$NON-NLS-1$
90                 fullMode.setSelection(false);
91                 fullMode.addSelectionListener(new SelectionListener() {
92                         public void widgetDefaultSelected(SelectionEvent e) {
93                         }
94                         public void widgetSelected(SelectionEvent event) {
95                 try {
96                                 if (ta.getPageSize() == Integer.MAX_VALUE) {
97                                         ta.resetMode();
98                                 } else {
99                                         ta.fullMode();
100                                 }
101                                 ta.loadData();
102                                 table.removeAll();
103                                 for (int i = table.getColumnCount() - 1; i >= 0; i--) {
104                                         table.getColumn(i).dispose();
105                                 }
106                                 ta.loadTable(table);
107                                 label.setText(ta.getStatusString());
108                                 previous.setEnabled(ta.hasPreviousPage());
109                                 next.setEnabled(ta.hasNextPage());
110                 } catch (NotConnectedException e) {
111                     view.handleException(e);
112                 }
113                         }
114                 });
115                 previous.setImage(ImageStore.getImage(ImageStore.PREVIOUS));
116                 previous.setToolTipText("Previous"); //$NON-NLS-1$
117                 previous.addSelectionListener(new SelectionListener() {
118                         public void widgetDefaultSelected(SelectionEvent e) {
119                         }
120                         public void widgetSelected(SelectionEvent event) {
121                 try {
122                                 ta.previousPage();
123                                 ta.loadData();
124                                 table.removeAll();
125                                 for (int i = table.getColumnCount() - 1; i >= 0; i--) {
126                                         table.getColumn(i).dispose();
127                                 }
128                                 ta.loadTable(table);
129                                 label.setText(ta.getStatusString());
130                                 previous.setEnabled(ta.hasPreviousPage());
131                                 next.setEnabled(ta.hasNextPage());
132                 } catch (NotConnectedException e) {
133                     view.handleException(e);
134                 }
135                         }
136                 });
137                 next.setImage(ImageStore.getImage(ImageStore.NEXT));
138                 next.setToolTipText("Next"); //$NON-NLS-1$
139                 next.addSelectionListener(new SelectionListener() {
140                         public void widgetDefaultSelected(SelectionEvent e) {
141                         }
142                         public void widgetSelected(SelectionEvent event) {
143                 try {
144                                 ta.nextPage();
145                                 ta.loadData();
146                                 table.removeAll();
147                                 for (int i = table.getColumnCount() - 1; i >= 0; i--) {
148                                         table.getColumn(i).dispose();
149                                 }
150                                 ta.loadTable(table);
151                                 label.setText(ta.getStatusString());
152                                 previous.setEnabled(ta.hasPreviousPage());
153                                 next.setEnabled(ta.hasNextPage());
154                 } catch (NotConnectedException e) {
155                     view.handleException(e);
156                 }
157                         }
158                 });
159
160         }
161
162         public void setColumns( final TableView view,   final TableAdapter ta,  Table table) {
163
164                 // We create a matrix of TableColumn (columns in the Table widget) with
165                 // the columns in the table object
166                 TableColumn[] columns = table.getColumns();
167                 String columnNames[] = new String[columns.length];
168                 for (int i = 0; i < columns.length; i++) {
169                         columnNames[i] = columns[i].getText();
170                 }
171                 // Copy in data the values of the data columns of the selected rows
172                 StringMatrix data = new StringMatrix();
173                 data.addMatrixHeader(columnNames);
174                         // Create dummy values in case nothing selected
175                         for (int i = 0; i < columns.length; i++) {
176                                 data.addAt(columnNames[i], "", 0); //$NON-NLS-1$
177                 }
178                 final TableRow emptyRow = new TableRow(ta.getEntity(), ta.getBookmark(), ta.getTable(), data);
179
180                 filter.addSelectionListener(new SelectionListener() {
181                         public void widgetDefaultSelected(SelectionEvent e) {
182                         }
183                         public void widgetSelected(SelectionEvent e) {
184                                 SortFilterPage page = new SortFilterPage(""); //$NON-NLS-1$
185                                 SQLRowWizard wizard = new SQLRowWizard();
186                                 wizard.init(Messages.getString("TableView.FilterAndSort"), page, emptyRow, ta); //$NON-NLS-1$
187                                 WizardDialog dialog = new WizardDialog(view.getSite().getShell(), wizard);
188                                 dialog.open();
189                         }
190                 });
191         }
192         
193         
194
195         /**
196          * @return
197          */
198         public ToolItem getNext() {
199                 return next;
200         }
201
202         /**
203          * @return
204          */
205         public ToolItem getPrevious() {
206                 return previous;
207         }
208
209 }