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