448ff51f1be1fb87e1f2cae24bcf5a47be57ab0b
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / subset / SubsetView.java
1 package com.quantum.view.subset;
2
3 import java.io.IOException;
4 import java.io.StringReader;
5 import java.util.Vector;
6
7 import javax.xml.parsers.DocumentBuilder;
8 import javax.xml.parsers.DocumentBuilderFactory;
9 import javax.xml.parsers.ParserConfigurationException;
10
11 import com.quantum.Messages;
12 import com.quantum.QuantumPlugin;
13 import com.quantum.actions.CustomCopyAction;
14 import com.quantum.actions.DeleteColumnAction;
15 import com.quantum.actions.DeleteObjectAction;
16 import com.quantum.actions.DeleteSubsetAction;
17 import com.quantum.actions.ExportXMLAction;
18 import com.quantum.actions.NewSubsetAction;
19 import com.quantum.actions.ViewTableAction;
20 import com.quantum.view.bookmark.ColumnNode;
21 import com.quantum.view.bookmark.TreeNode;
22
23 import org.eclipse.jface.action.Action;
24 import org.eclipse.jface.action.IMenuListener;
25 import org.eclipse.jface.action.IMenuManager;
26 import org.eclipse.jface.action.IToolBarManager;
27 import org.eclipse.jface.action.MenuManager;
28 import org.eclipse.jface.viewers.DoubleClickEvent;
29 import org.eclipse.jface.viewers.IDoubleClickListener;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.jface.viewers.StructuredSelection;
32 import org.eclipse.jface.viewers.TreeViewer;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.dnd.TextTransfer;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.layout.GridLayout;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Menu;
40 import org.eclipse.ui.IActionBars;
41 import org.eclipse.ui.IWorkbenchActionConstants;
42 import org.eclipse.ui.part.ViewPart;
43 import org.w3c.dom.Document;
44 import org.w3c.dom.Element;
45 import org.xml.sax.InputSource;
46 import org.xml.sax.SAXException;
47
48 /**
49  * @author panic
50  *
51  * View for subsets
52  * */
53 public class SubsetView extends ViewPart {
54         
55         private SubsetContentProvider provider = SubsetContentProvider.getInstance();
56         private NewSubsetAction newSubsetAction;
57         private DeleteSubsetAction deleteSubsetAction;
58         private DeleteObjectAction deleteObjectAction;
59         private DeleteColumnAction deleteColumnAction;
60         private CustomCopyAction customCopyAction;
61
62         private ViewTableAction viewTableAction;
63         private ExportXMLAction exportXMLAction;
64         private static SubsetView instance = null;
65         private TreeViewer treeViewer;
66         private Label status;
67         public synchronized static SubsetView getInstance() {
68                 return instance;
69         }
70         /**
71          * Returns the current selected object in the tree. If it's a multiple selection, return the first.
72          * @return
73          */
74         public Object getCurrent() {
75                 if (treeViewer == null) return null;
76                 return ((StructuredSelection) treeViewer.getSelection())
77                                         .getFirstElement();
78         }
79         /**
80          * Returns the current selected objects in the tree, in the form of a StructuredSelection.
81          * @return
82          */
83         public StructuredSelection getSelection() {
84                 if (treeViewer == null) return null;
85                 return ((StructuredSelection) treeViewer.getSelection());
86         }
87         
88         /** 
89          * Navigates the tree to get the current subset (root) of the selected element.
90          * If it's a multiple selection, it takes the first one.
91          * @return
92          */
93         public SubsetNode getCurrentSubset() {
94                 TreeNode current = (TreeNode) getCurrent();
95                 
96                 return getRoot(current);
97         }
98
99         /**
100          * Navigates a given TreeNode till finds a SubsetNode
101          * @param node
102          * @return
103          */
104         private static SubsetNode getRoot(TreeNode node){
105                 while (!( node instanceof SubsetNode))
106                 {
107                         node = (TreeNode) node.getParent();
108                 }
109                 return (SubsetNode) node;
110
111         }
112
113  
114         /**
115          * Deletes the current node (first selected) in the tree
116          */
117         public void deleteCurrent() {
118                 provider.removeSubset(getCurrentSubset());
119                 treeViewer.refresh();
120         }
121         
122         public void refreshSubsetData(){
123                 //if (treeViewer == null) return;
124                 //SubsetContentProvider provider = (SubsetContentProvider) treeViewer.getContentProvider();
125                 //Object[] objects = provider.getElements(Root.ROOT);
126                 //for (int i = 0; i < objects.length; i++) {
127                         //SubsetNode current = (SubsetNode) objects[i];
128                         
129                 //}
130         }
131         
132         public void expandCurrent(SubsetNode node) {
133                 treeViewer.setExpandedState(node, true);
134                 treeViewer.refresh(node, false);
135         }
136         public void refresh() {
137                 treeViewer.refresh();
138         }
139         public void disconnect() {
140         }
141         
142         public void createPartControl(org.eclipse.swt.widgets.Composite parent) {
143                 instance = this;
144                 initActions();
145                 Composite main = new Composite(parent, SWT.NONE);
146                 GridLayout layout = new GridLayout(1, false);
147                 layout.horizontalSpacing = 0;
148                 layout.verticalSpacing = 0;
149                 main.setLayout(layout);
150
151         
152                 treeViewer = new TreeViewer(main);
153                 treeViewer.setContentProvider(provider);
154                 treeViewer.setLabelProvider(new SubsetLabelProvider());
155                 treeViewer.setInput(SubsetRoot.ROOT);
156                 MenuManager manager = new MenuManager();
157                 manager.setRemoveAllWhenShown(true);
158                 Menu fTextContextMenu =
159                         manager.createContextMenu(treeViewer.getControl());
160                 treeViewer.getControl().setMenu(fTextContextMenu);
161                 treeViewer.addDoubleClickListener(new IDoubleClickListener() {
162                         public void doubleClick(DoubleClickEvent event) {
163                                 Object sel = getCurrent();
164                                 if (sel instanceof ObjectNode) {
165                                         viewTableAction.run();
166                                 }
167                         }
168                 });
169
170                 manager.addMenuListener(new IMenuListener() {
171                         public void menuAboutToShow(IMenuManager mgr) {
172                                 Object sel = getCurrent();
173                                 if (sel instanceof SubsetNode) {
174                                         mgr.add(deleteSubsetAction);
175                                         deleteSubsetAction.setText(Messages.getString("SubsetView.Delete")); //$NON-NLS-1$
176                                         deleteSubsetAction.setImageDescriptor(
177                                                 QuantumPlugin.getImageDescriptor("delete.gif")); //$NON-NLS-1$
178                                         mgr.add(pasteAction);
179                                         pasteAction.setText(Messages.getString("SubsetView.Paste")); //$NON-NLS-1$
180                                         pasteAction.setImageDescriptor(QuantumPlugin.getImageDescriptor("paste.gif")); //$NON-NLS-1$
181                                         mgr.add(exportXMLAction);
182                                         exportXMLAction.setText(Messages.getString("bookmarkview.exportXML")); //$NON-NLS-1$
183                                         exportXMLAction.setImageDescriptor(QuantumPlugin.getImageDescriptor("xml.gif")); //$NON-NLS-1$
184                 
185                                 } else  if (sel instanceof ObjectNode) {
186                                         mgr.add(deleteObjectAction);
187                                         deleteObjectAction.setText(Messages.getString("SubsetView.Delete")); //$NON-NLS-1$
188                                         deleteObjectAction.setImageDescriptor(QuantumPlugin.getImageDescriptor("delete.gif")); //$NON-NLS-1$
189                                         mgr.add(viewTableAction);
190                                         viewTableAction.setText(Messages.getString("bookmarkview.viewTable")); //$NON-NLS-1$
191                                         viewTableAction.setImageDescriptor(QuantumPlugin.getImageDescriptor("table.gif")); //$NON-NLS-1$
192                                         mgr.add(exportXMLAction);
193                                         exportXMLAction.setText(Messages.getString("bookmarkview.exportXML")); //$NON-NLS-1$
194                                         exportXMLAction.setImageDescriptor(QuantumPlugin.getImageDescriptor("xml.gif")); //$NON-NLS-1$
195                     customCopyAction.selectionChanged(
196                         (IStructuredSelection) treeViewer.getSelection());
197                                         mgr.add(customCopyAction);
198                                 } else if (sel instanceof ColumnNode){
199                                         mgr.add(deleteColumnAction);
200                                         deleteColumnAction.setText(Messages.getString("SubsetView.Delete")); //$NON-NLS-1$
201                                         deleteColumnAction.setImageDescriptor(QuantumPlugin.getImageDescriptor("delete.gif")); //$NON-NLS-1$
202                     customCopyAction.selectionChanged(
203                         (IStructuredSelection) treeViewer.getSelection());
204                                         mgr.add(customCopyAction);
205                                                 
206                                 } else {
207                                         mgr.add(newSubsetAction);
208                                 }
209                         }
210                 });
211
212                 GridData gridData = new GridData();
213                 gridData.horizontalAlignment = GridData.FILL;
214                 gridData.verticalAlignment = GridData.FILL;
215                 gridData.grabExcessHorizontalSpace = true;
216                 gridData.grabExcessVerticalSpace = true;
217                 treeViewer.getControl().setLayoutData(gridData);
218                 status = new Label(main, SWT.NONE);
219                 gridData = new GridData();
220                 gridData.horizontalAlignment = GridData.FILL;
221                 gridData.grabExcessHorizontalSpace = true;
222                 status.setLayoutData(gridData);
223                 
224                 IActionBars bars = getViewSite().getActionBars();
225                 bars.setGlobalActionHandler(IWorkbenchActionConstants.PASTE, pasteAction);
226                 bars.setGlobalActionHandler(IWorkbenchActionConstants.DELETE, deleteSubsetAction);
227
228                 IToolBarManager toolBar = getViewSite().getActionBars().getToolBarManager();
229                 toolBar.add(newSubsetAction);
230                 
231                 status.setText(Messages.getString("bookmarkview.done")); //$NON-NLS-1$
232         }
233         public void initActions() {
234                 newSubsetAction = new NewSubsetAction();
235                 newSubsetAction.setText("New Subset"); //$NON-NLS-1$
236                 newSubsetAction.setToolTipText(Messages.getString("SubsetView.CreatesANewEmptySubset")); //$NON-NLS-1$
237                 newSubsetAction.setImageDescriptor(
238                         QuantumPlugin.getImageDescriptor("subset.gif")); //$NON-NLS-1$
239                 newSubsetAction.init(this);
240                 deleteColumnAction = new DeleteColumnAction();
241                 deleteColumnAction.setText("Delete Column"); //$NON-NLS-1$
242                 deleteColumnAction.setToolTipText(Messages.getString("SubsetView.DeletesTheSelectedColumns")); //$NON-NLS-1$
243                 deleteColumnAction.setImageDescriptor(
244                         QuantumPlugin.getImageDescriptor("delete.gif")); //$NON-NLS-1$
245                 deleteColumnAction.init(this);
246                 deleteObjectAction = new DeleteObjectAction();
247                 deleteObjectAction.setText("Delete Object"); //$NON-NLS-1$
248                 deleteObjectAction.setToolTipText(Messages.getString("SubsetView.DeletesTheSelectedObject")); //$NON-NLS-1$
249                 deleteObjectAction.setImageDescriptor(
250                         QuantumPlugin.getImageDescriptor("delete.gif")); //$NON-NLS-1$
251                 deleteObjectAction.init(this);
252                 deleteSubsetAction = new DeleteSubsetAction();
253                 deleteSubsetAction.setText("Delete Subset"); //$NON-NLS-1$
254                 deleteSubsetAction.setToolTipText(Messages.getString("SubsetView.DeletesTheSelectedSubset")); //$NON-NLS-1$
255                 deleteSubsetAction.setImageDescriptor(
256                         QuantumPlugin.getImageDescriptor("delete.gif")); //$NON-NLS-1$
257                 deleteSubsetAction.init(this);
258 //              viewTableAction = new ViewTableAction();
259 //              viewTableAction.setText(Messages.getString("bookmarkview.viewTable")); //$NON-NLS-1$
260 //              viewTableAction.setImageDescriptor(
261 //                      QuantumPlugin.getImageDescriptor("table.gif")); //$NON-NLS-1$
262 //              viewTableAction.init(this);
263                 exportXMLAction = new ExportXMLAction();
264                 exportXMLAction.setText(Messages.getString("bookmarkview.exportXML")); //$NON-NLS-1$
265                 exportXMLAction.setImageDescriptor(
266                         QuantumPlugin.getImageDescriptor("table.gif")); //$NON-NLS-1$
267                 exportXMLAction.init(this);
268
269                 this.customCopyAction = new CustomCopyAction(this,1); // 1 is unused, just in case more custom copies are defined        
270                 this.customCopyAction.setText(Messages.getString("bookmarkview.customCopyAction")); //$NON-NLS-1$
271                 this.customCopyAction.setImageDescriptor(
272                         QuantumPlugin.getImageDescriptor("copy.gif")); //$NON-NLS-1$
273
274
275                 
276         }
277         public void setFocus() {
278         }
279         
280         private Action pasteAction = new Action() {
281                 public void run() {
282                         Object sel = getCurrent();
283                         if (sel == null) return;
284                         if (!(sel instanceof SubsetNode)) return;
285                         
286                         SubsetNode subset = (SubsetNode) sel;
287                         
288                         TextTransfer transfer = TextTransfer.getInstance();
289                         String xmlMetaData = (String) QuantumPlugin.getDefault().getSysClip().getContents(transfer);
290                         StringReader text = new StringReader(xmlMetaData);
291                         InputSource source = new InputSource(text);
292                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
293                         DocumentBuilder parser;
294             Document doc; 
295                         try {
296                                 parser = factory.newDocumentBuilder();
297                                 doc = parser.parse(source);
298                         } catch (ParserConfigurationException e) {
299                                 e.printStackTrace();
300                                 return;
301                         } catch (SAXException e) {
302                                 e.printStackTrace();
303                                 return;
304                         } catch (IOException e) {
305                                 e.printStackTrace();
306                                 return;
307                         }
308                         Element root = doc.getDocumentElement();
309                         subset.importXML(root, false);
310                         treeViewer.refresh();
311                         provider.setHasChanged(true);
312
313                 }
314         };
315         
316         public void addNewSubset(SubsetNode subset) {
317                 provider.addSubset(subset);
318                 treeViewer.refresh();
319         }
320         public void setStatus(String text) {
321                 status.setText(text);
322         }
323         public void dispose(){
324                 super.dispose();
325         }
326
327         /**
328          * Returs a Vector with all the elements of the treeViewer
329          * @return
330          */
331         public Vector getElements(){
332                 Vector result = new Vector();
333                 if (treeViewer == null) return result;
334                 SubsetContentProvider provider = (SubsetContentProvider) treeViewer.getContentProvider();
335                 Object[] objects = provider.getElements(SubsetRoot.ROOT);
336                 for (int i = 0; i < objects.length; i++) {
337                         SubsetNode current = (SubsetNode) objects[i];
338                         result.add(current);
339                 }
340                 return result;
341         }
342         
343         public void deleteColumn (ColumnNode column){
344                 provider.deleteColumn(column);
345                 treeViewer.refresh();
346         }
347
348         public void deleteObject (ObjectNode object){
349                 provider.deleteObject(object);
350                 treeViewer.refresh();
351         }
352
353
354 }