c9b151a5984ad0e006491cf26e4525f2fd9c2ed1
[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 com.quantum.Messages;
6 import com.quantum.QuantumPlugin;
7 import com.quantum.extensions.ProcessServiceMembers;
8 import com.quantum.model.Bookmark;
9 import com.quantum.model.Entity;
10 import com.quantum.model.NotConnectedException;
11 import com.quantum.sql.SQLResults;
12 import com.quantum.ui.dialog.ExceptionDisplayDialog;
13 import com.quantum.view.LogProxy;
14
15 import org.eclipse.jface.action.Action;
16 import org.eclipse.jface.action.IMenuListener;
17 import org.eclipse.jface.action.MenuManager;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.events.SelectionListener;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.swt.widgets.Menu;
27 import org.eclipse.swt.widgets.TabFolder;
28 import org.eclipse.swt.widgets.TabItem;
29 import org.eclipse.swt.widgets.Table;
30 import org.eclipse.swt.widgets.ToolBar;
31 import org.eclipse.ui.ISelectionListener;
32 import org.eclipse.ui.IWorkbenchPart;
33 import org.eclipse.ui.WorkbenchException;
34 import org.eclipse.ui.part.ViewPart;
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 System.out.println("Refresh?");
102                 if (tabs.getSelectionIndex() >= 0) {
103 System.out.println("Refresh!");
104                         TabItem item = tabs.getItem(tabs.getSelectionIndex());
105                         TableAdapter adapter = (TableAdapter) item.getData();
106                         Bookmark bookmark = adapter.getBookmark();
107                         String table = adapter.getTable();
108                         if (table == null) {
109                                 loadTable(bookmark, item, null, null, true, true);
110                         } else {
111                                 loadTable(bookmark, item, null, null, true, true);
112                         }
113                         String title = Messages.getString("tableview.QuantumTableViewName"); //$NON-NLS-1$
114                         if (bookmark != null)
115                                 title = bookmark.getName() + Messages.getString("tableview.ViewNameInitialDecoration") + title + Messages.getString("tableview.ViewNameFinalDecoration"); //$NON-NLS-1$ //$NON-NLS-2$
116                         setTitle(title);
117                 }
118         }
119         
120     public void loadQuery(Bookmark bookmark, SQLResults results) {
121         loadTable(bookmark, null, null, results, true, false);
122     }
123         public void loadTable(Entity entity) {
124                 loadTable(entity.getBookmark(), null, entity, null, false, true);
125         }
126         public void loadTable(Bookmark bookmark, TabItem tabItem, Entity entity, SQLResults results, boolean query, boolean reload) {
127         try {
128                 TableAdapter adapter;
129                 // If no TabItem is given we have to create a new one, with the info of the table or view.
130                 if (tabItem == null) {
131                         tabItem = new TabItem(tabs, SWT.NONE);
132                         // Data is stored in a TableAdapter object
133                         if (query) {
134                                 adapter = TableAdapter.createFromQuery(bookmark, results);
135                         } else {
136                                 adapter = TableAdapter.createFromTable(entity);
137                         }
138                         // That is stored in the tabItem, so it won't get lost
139                         tabItem.setData(adapter);
140                         // This does not really belong here, but it'll fail if done before the creation of the
141                         // first TabItem, so it remains here till a better place found.
142                         // We set a function to change the Title of the window depending on the selected tab.
143                         tabs.addSelectionListener(new SelectionListener() {
144                                 public void widgetDefaultSelected(SelectionEvent e) {
145                                 }
146                                 public void widgetSelected(SelectionEvent e) {
147                                         setQualifiedTitle();
148                                 }
149                         });
150                 } else {
151                         // If there is already a TabItem, we take its TableAdapter object
152                         adapter = (TableAdapter) tabItem.getData();
153                 }
154                 
155                 // We create a Composite widget (main) to display our data, with a GridLayout
156                 Composite main = new Composite(tabs, SWT.NONE);
157                 GridLayout layout = new GridLayout(1, false);
158                 layout.horizontalSpacing = 0;
159                 layout.verticalSpacing = 0;
160                 main.setLayout(layout);
161     
162                 
163                 // load widgets, the order of loading them determines the appearance in screen
164                 ToolBar widgetToolBar = new ToolBar(main, SWT.HORIZONTAL);
165                 // We fill up our Composite widget, the main table display, etc.
166                 final Table table = new Table(main, SWT.FULL_SELECTION | SWT.MULTI);
167                 final Label label = new Label(main, SWT.NULL);
168                 TableViewToolBar toolBar = new TableViewToolBar(this, widgetToolBar, table, adapter, label);
169                 
170         
171                 // load table
172                 if (reload) {
173                         adapter.resetOffset();
174                         adapter.loadData();
175                 }
176                 // Load the table data from the adapter into the widget
177                 adapter.loadTable(table);
178                 // Experimental, won't make it into 2.2
179                 // final TableViewer viewer = adapter.addTableViewer(table);
180                 
181                 String tableName = adapter.getTable();
182                 if (tableName != null) {
183                         tabItem.setText(bookmark.getName() + ":" + tableName);
184                 } else {
185                         tabItem.setText(bookmark.getName() + ": SQL");
186                         tabItem.setToolTipText(bookmark.getName() + ":\n" + adapter.getQuery());
187                 }
188     
189                 toolBar.getPrevious().setEnabled(adapter.hasPreviousPage());
190                 toolBar.getNext().setEnabled(adapter.hasNextPage());
191                 label.setText(adapter.getStatusString());
192                 
193                 GridData gridData = new GridData();
194                 gridData.horizontalAlignment = GridData.FILL;
195                 gridData.verticalAlignment = GridData.FILL;
196                 gridData.grabExcessHorizontalSpace = true;
197                 gridData.grabExcessVerticalSpace = true;
198                 table.setLayoutData(gridData);
199     
200                 gridData = new GridData();
201                 gridData.horizontalAlignment = GridData.FILL;
202                 label.setLayoutData(gridData);
203     
204                 
205                 toolBar.setColumns(this, adapter, table);
206                 final TableAdapter ta = adapter;
207                 final Action defaultEncodingAction = new DefaultEncodingAction(ta);
208                 defaultEncodingAction.setText(Messages.getString("tableview.defaultEncoding")); //$NON-NLS-1$
209                 final Action UTF8EncodingAction = new Action() {
210                         public void run() {
211                                 ta.setEncoding(TableAdapter.UTF_8);
212                         }
213                 };
214                 UTF8EncodingAction.setText(Messages.getString("tableview.UTF8Encoding")); //$NON-NLS-1$
215                 final Action UTF16EncodingAction = new Action() {
216                         public void run() {
217                                 ta.setEncoding(TableAdapter.UTF_16);
218                         }
219                 };
220                 UTF16EncodingAction.setText(Messages.getString("tableview.UTF16Encoding")); //$NON-NLS-1$
221     
222                 IMenuListener menuListener = new TableViewMenuListener(this, table, UTF16EncodingAction, ta, defaultEncodingAction, UTF8EncodingAction, extensionVector);
223     
224                 // final setup
225                 MenuManager manager = new MenuManager();
226                 manager.setRemoveAllWhenShown(true);
227                 Menu fTextContextMenu = manager.createContextMenu(table);
228                 table.setMenu(fTextContextMenu);
229                 table.setLinesVisible(true);
230                 manager.addMenuListener(menuListener);
231                 
232                 tabItem.setControl(main);
233                 tabs.setSelection(tabs.indexOf(tabItem));
234     
235                 setQualifiedTitle();            
236         } catch (NotConnectedException e) {
237             e.printStackTrace();
238             handleException(e);
239         } catch (Exception e) {
240             e.printStackTrace();
241         }
242         }
243     
244     protected void handleException(Exception e) {
245         ExceptionDisplayDialog.openError(getSite().getShell(), null, null, e);
246     }
247
248         /**
249          * @return
250          */
251         /**
252          * Sets the title of the window to the text of the selected tab
253          */
254         private void setQualifiedTitle() {
255                 if (tabs.getSelectionIndex() < 0) return;
256                 TabItem item = tabs.getItem(tabs.getSelectionIndex());
257                 String defTitle = Messages.getString("tableview.QuantumTableViewName"); //$NON-NLS-1$
258                 String title = item.getText();
259                 int ind = title.indexOf(Messages.getString("tableview.BookmarkSeparator")); //$NON-NLS-1$
260                 if (ind > 0) defTitle = title.substring(0,ind) 
261                                                                 + Messages.getString("tableview.ViewNameInitialDecoration")   //$NON-NLS-1$
262                                                                 + defTitle 
263                                                                 + Messages.getString("tableview.ViewNameFinalDecoration"); //$NON-NLS-1$
264                 setTitle(defTitle);
265         }
266
267         public void createPartControl(Composite parent) {
268                 this.parent = parent;
269                 initActions();
270                 tabs = new TabFolder(parent, SWT.NONE);
271         }
272         
273         public void initActions() {
274                 extensionVector = new Vector();
275                 try {
276                         ProcessServiceMembers.process(this, extensionVector);
277                 } catch (WorkbenchException e) {
278                         e.printStackTrace();
279                 }
280
281         }
282
283         public void selectionChanged(IWorkbenchPart part, ISelection selection) {
284         }
285 }