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