8e23888f81d4f5cb033e632f3de85cad60710cde
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / tableview / TableView.java
1 package com.quantum.view.tableview;
2
3 import java.util.Vector;
4
5 import org.eclipse.jface.action.Action;
6 import org.eclipse.jface.action.IMenuListener;
7 import org.eclipse.jface.action.MenuManager;
8 import org.eclipse.jface.viewers.ISelection;
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.events.SelectionEvent;
11 import org.eclipse.swt.events.SelectionListener;
12 import org.eclipse.swt.layout.GridData;
13 import org.eclipse.swt.layout.GridLayout;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.swt.widgets.Label;
16 import org.eclipse.swt.widgets.Menu;
17 import org.eclipse.swt.widgets.TabFolder;
18 import org.eclipse.swt.widgets.TabItem;
19 import org.eclipse.swt.widgets.Table;
20 import org.eclipse.swt.widgets.ToolBar;
21 import org.eclipse.ui.ISelectionListener;
22 import org.eclipse.ui.IWorkbenchPart;
23 import org.eclipse.ui.WorkbenchException;
24 import org.eclipse.ui.part.ViewPart;
25
26 import com.quantum.Messages;
27 import com.quantum.QuantumPlugin;
28 import com.quantum.extensions.ProcessServiceMembers;
29 import com.quantum.model.Bookmark;
30 import com.quantum.model.Entity;
31 import com.quantum.model.NotConnectedException;
32 import com.quantum.sql.SQLResults;
33 import com.quantum.ui.dialog.ExceptionDisplayDialog;
34 import com.quantum.view.LogProxy;
35
36 /**
37  * The Table View. Displays tables and Queries.
38  */
39 public class TableView extends ViewPart implements ISelectionListener {
40
41         private class DefaultEncodingAction extends Action {
42         private final TableAdapter ta;
43         private DefaultEncodingAction(TableAdapter ta) {
44             super();
45             this.ta = ta;
46         }
47         public void run() {
48                 ta.setEncoding(TableAdapter.DEFAULT);
49         }
50     }
51         private TabFolder tabs = null;
52         private Composite parent;
53         private Vector extensionVector;
54  
55         /**
56          * Generic contructor
57          */
58         public TableView() {
59         }
60
61
62         public void setFocus() {
63                 setQualifiedTitle();
64         }
65         
66     /**
67      * Gets the instance of the TableView.  This view can appear on multiple 
68      * perspectives, but tabs within the view are shared no matter which perspective
69      * is open.
70      * 
71      * @return the TableView instance.
72      */
73         public static TableView getInstance() {
74                 return (TableView) QuantumPlugin.getDefault().getView("com.quantum.view.tableview.TableView");
75         }
76         
77         /**
78          * Close the current tab, disposing of it
79          */
80         public void closeCurrent() {
81                 if (tabs == null) return;
82                 
83                 if (tabs.getSelectionIndex() >= 0) {
84                         try {
85                                 TabItem item = tabs.getItem(tabs.getSelectionIndex());
86                                 item.dispose();
87                         } catch (Throwable e) {
88                                 LogProxy.getInstance().addText(LogProxy.ERROR, "Error Closing Current: " + e.toString()); //$NON-NLS-1$
89                                 e.printStackTrace();
90                         }
91                 } 
92                 if (tabs.getItemCount() == 0) {
93                         setTitle(Messages.getString("tableview.QuantumTableViewName")); //$NON-NLS-1$
94                 }
95         }
96         
97         /**
98          * Reload table or query data into the selected tab
99          */
100         public void refreshCurrent() {
101                 TabItem item = tabs.getItem(tabs.getSelectionIndex());
102                 TableAdapter adapter = (TableAdapter) item.getData();
103                 Bookmark bookmark = adapter.getBookmark();
104                 String table = adapter.getTable();
105                 if (table == null) {
106                         loadTable(bookmark, item, null, null, true, true);
107                 } else {
108                         loadTable(bookmark, item, null, null, true, true);
109                 }
110                 String title = Messages.getString("tableview.QuantumTableViewName"); //$NON-NLS-1$
111                 if (bookmark != null)
112                         title = bookmark.getName() + Messages.getString("tableview.ViewNameInitialDecoration") + title + Messages.getString("tableview.ViewNameFinalDecoration"); //$NON-NLS-1$ //$NON-NLS-2$
113                 setTitle(title);
114         }
115         
116     public void loadQuery(Bookmark bookmark, SQLResults results) {
117         loadTable(bookmark, null, null, results, true, false);
118     }
119         public void loadTable(Entity entity) {
120                 loadTable(entity.getBookmark(), null, entity, null, false, true);
121         }
122         public void loadTable(Bookmark bookmark, TabItem tabItem, Entity entity, SQLResults results, boolean query, boolean reload) {
123         try {
124                 TableAdapter adapter;
125                 // If no TabItem is given we have to create a new one, with the info of the table or view.
126                 if (tabItem == null) {
127                         tabItem = new TabItem(tabs, SWT.NONE);
128                         // Data is stored in a TableAdapter object
129                         if (query) {
130                                 adapter = TableAdapter.createFromQuery(bookmark, results);
131                         } else {
132                                 adapter = TableAdapter.createFromTable(entity);
133                         }
134                         // That is stored in the tabItem, so it won't get lost
135                         tabItem.setData(adapter);
136                         // This does not really belong here, but it'll fail if done before the creation of the
137                         // first TabItem, so it remains here till a better place found.
138                         // We set a function to change the Title of the window depending on the selected tab.
139                         tabs.addSelectionListener(new SelectionListener() {
140                                 public void widgetDefaultSelected(SelectionEvent e) {
141                                 }
142                                 public void widgetSelected(SelectionEvent e) {
143                                         setQualifiedTitle();
144                                 }
145                         });
146                 } else {
147                         // If there is already a TabItem, we take its TableAdapter object
148                         adapter = (TableAdapter) tabItem.getData();
149                 }
150                 
151                 // We create a Composite widget (main) to display our data, with a GridLayout
152                 Composite main = new Composite(tabs, SWT.NONE);
153                 GridLayout layout = new GridLayout(1, false);
154                 layout.horizontalSpacing = 0;
155                 layout.verticalSpacing = 0;
156                 main.setLayout(layout);
157     
158                 
159                 // load widgets, the order of loading them determines the appearance in screen
160                 ToolBar widgetToolBar = new ToolBar(main, SWT.HORIZONTAL);
161                 // We fill up our Composite widget, the main table display, etc.
162                 final Table table = new Table(main, SWT.FULL_SELECTION | SWT.MULTI);
163                 final Label label = new Label(main, SWT.NULL);
164                 TableViewToolBar toolBar = new TableViewToolBar(this, widgetToolBar, table, adapter, label);
165                 
166         
167                 // load table
168                 if (reload) {
169                         adapter.resetOffset();
170                         adapter.loadData();
171                 }
172                 // Load the table data from the adapter into the widget
173                 adapter.loadTable(table);
174                 // Experimental, won't make it into 2.2
175                 // final TableViewer viewer = adapter.addTableViewer(table);
176                 
177                 String tableName = adapter.getTable();
178                 if (tableName != null) {
179                         tabItem.setText(bookmark.getName() + ":" + tableName);
180                 } else {
181                         tabItem.setText(bookmark.getName() + ": SQL");
182                         tabItem.setToolTipText(bookmark.getName() + ":\n" + adapter.getQuery());
183                 }
184     
185                 toolBar.getPrevious().setEnabled(adapter.hasPreviousPage());
186                 toolBar.getNext().setEnabled(adapter.hasNextPage());
187                 label.setText(adapter.getStatusString());
188                 
189                 GridData gridData = new GridData();
190                 gridData.horizontalAlignment = GridData.FILL;
191                 gridData.verticalAlignment = GridData.FILL;
192                 gridData.grabExcessHorizontalSpace = true;
193                 gridData.grabExcessVerticalSpace = true;
194                 table.setLayoutData(gridData);
195     
196                 gridData = new GridData();
197                 gridData.horizontalAlignment = GridData.FILL;
198                 label.setLayoutData(gridData);
199     
200                 
201                 toolBar.setColumns(this, adapter, table);
202                 final TableAdapter ta = adapter;
203                 final Action defaultEncodingAction = new DefaultEncodingAction(ta);
204                 defaultEncodingAction.setText(Messages.getString("tableview.defaultEncoding")); //$NON-NLS-1$
205                 final Action UTF8EncodingAction = new Action() {
206                         public void run() {
207                                 ta.setEncoding(TableAdapter.UTF_8);
208                         }
209                 };
210                 UTF8EncodingAction.setText(Messages.getString("tableview.UTF8Encoding")); //$NON-NLS-1$
211                 final Action UTF16EncodingAction = new Action() {
212                         public void run() {
213                                 ta.setEncoding(TableAdapter.UTF_16);
214                         }
215                 };
216                 UTF16EncodingAction.setText(Messages.getString("tableview.UTF16Encoding")); //$NON-NLS-1$
217     
218                 IMenuListener menuListener = new TableViewMenuListener(this, table, UTF16EncodingAction, ta, defaultEncodingAction, UTF8EncodingAction, extensionVector);
219     
220                 // final setup
221                 MenuManager manager = new MenuManager();
222                 manager.setRemoveAllWhenShown(true);
223                 Menu fTextContextMenu = manager.createContextMenu(table);
224                 table.setMenu(fTextContextMenu);
225                 table.setLinesVisible(true);
226                 manager.addMenuListener(menuListener);
227                 
228                 tabItem.setControl(main);
229                 tabs.setSelection(tabs.indexOf(tabItem));
230     
231                 setQualifiedTitle();            
232         } catch (NotConnectedException e) {
233             e.printStackTrace();
234             handleException(e);
235         } catch (Exception e) {
236             e.printStackTrace();
237         }
238         }
239     
240     protected void handleException(Exception e) {
241         ExceptionDisplayDialog.openError(getSite().getShell(), null, null, e);
242     }
243
244         /**
245          * @return
246          */
247         /**
248          * Sets the title of the window to the text of the selected tab
249          */
250         private void setQualifiedTitle() {
251                 if (tabs.getSelectionIndex() < 0) return;
252                 TabItem item = tabs.getItem(tabs.getSelectionIndex());
253                 String defTitle = Messages.getString("tableview.QuantumTableViewName"); //$NON-NLS-1$
254                 String title = item.getText();
255                 int ind = title.indexOf(Messages.getString("tableview.BookmarkSeparator")); //$NON-NLS-1$
256                 if (ind > 0) defTitle = title.substring(0,ind) 
257                                                                 + Messages.getString("tableview.ViewNameInitialDecoration")   //$NON-NLS-1$
258                                                                 + defTitle 
259                                                                 + Messages.getString("tableview.ViewNameFinalDecoration"); //$NON-NLS-1$
260                 setTitle(defTitle);
261         }
262
263         public void createPartControl(Composite parent) {
264                 this.parent = parent;
265                 initActions();
266                 tabs = new TabFolder(parent, SWT.NONE);
267         }
268         
269         public void initActions() {
270                 extensionVector = new Vector();
271                 try {
272                         ProcessServiceMembers.process(this, extensionVector);
273                 } catch (WorkbenchException e) {
274                         e.printStackTrace();
275                 }
276
277         }
278
279         public void selectionChanged(IWorkbenchPart part, ISelection selection) {
280
281         }
282 }