Created a separated 'externaltools' plugin
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / view / bookmark / BookmarkView.java
1 package com.quantum.view.bookmark;
2
3
4 import java.beans.PropertyChangeEvent;
5 import java.beans.PropertyChangeListener;
6 import java.util.Vector;
7
8 import org.eclipse.jface.action.Action;
9 import org.eclipse.jface.action.IAction;
10 import org.eclipse.jface.action.IMenuListener;
11 import org.eclipse.jface.action.IMenuManager;
12 import org.eclipse.jface.action.MenuManager;
13 import org.eclipse.jface.action.Separator;
14 import org.eclipse.jface.preference.IPreferenceStore;
15 import org.eclipse.jface.viewers.IOpenListener;
16 import org.eclipse.jface.viewers.ISelectionProvider;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.jface.viewers.OpenEvent;
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.widgets.Menu;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.ui.IActionBars;
25 import org.eclipse.ui.WorkbenchException;
26 import org.eclipse.ui.actions.ActionContext;
27 import org.eclipse.ui.part.ViewPart;
28
29 import com.quantum.ImageStore;
30 import com.quantum.Messages;
31 import com.quantum.QuantumPlugin;
32 import com.quantum.actions.CustomCopyAction;
33 import com.quantum.extensions.ExtensionAction;
34 import com.quantum.extensions.ProcessServiceMembers;
35
36 public class BookmarkView extends ViewPart implements PropertyChangeListener {
37         private CustomCopyAction customCopyAction1;
38         private CustomCopyAction customCopyAction2;
39         private CustomCopyAction customCopyAction3;
40         private Vector extensionVector;
41     
42     private BookmarkViewActionGroup actionGroup;
43
44         private TreeViewer treeViewer;
45     private BookmarkLabelProvider labelProvider = new BookmarkLabelProvider();
46     
47         /**
48          * @return      -
49          *      The instance of the BookmarkView. There is no guarantee of it being a singleton
50          *      due to the workspace creating a new one (does the workspace call getInstance() ? ).
51          *      It seems to work.
52          */
53         public synchronized static BookmarkView getInstance() {
54                 return (BookmarkView) QuantumPlugin.getDefault().getView("com.quantum.view.bookmarkview");
55         }
56         /**
57          * Returns the current selected object in the tree. If it's a multiple selection, return the first.
58          * @return
59          */
60         public Object getCurrent() {
61                 if (treeViewer == null) return null;
62                 return ((StructuredSelection) treeViewer.getSelection())
63                                         .getFirstElement();
64         }
65         /**
66          * Returns the current selected objects in the tree, in the form of a StructuredSelection.
67          * @return
68          */
69         public StructuredSelection getSelection() {
70                 if (treeViewer == null) return null;
71                 return ((StructuredSelection) treeViewer.getSelection());
72         }
73         
74         /** 
75          * Navigates the tree to get the current bookmark (root) of the selected element.
76          * If it's a multiple selection, it takes the first one.
77          * @return
78          */
79         public BookmarkNode getCurrentBookmark() {
80                 TreeNode current = (TreeNode) getCurrent();
81                 return getRoot(current);
82         }
83
84         private static BookmarkNode getRoot(TreeNode node){
85                 if (node == null) return null;
86                 while (!( node instanceof BookmarkNode))
87                 {
88                         node = node.getParent();
89                 }
90                 if (node instanceof BookmarkNode) return (BookmarkNode) node;
91                 else return null;
92
93         }
94         public void refresh() {
95                 treeViewer.refresh();
96         }
97         public void createPartControl(org.eclipse.swt.widgets.Composite parent) {
98
99                 treeViewer = new TreeViewer(
100             parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
101                 treeViewer.setContentProvider(new BookmarkContentProvider(this));
102                 treeViewer.setLabelProvider(this.labelProvider);
103         BookmarkListNode input = BookmarkListNode.getInstance();
104                 treeViewer.setInput(input);
105         initActions();
106         
107         input.addPropertyChangeListener(this);
108         
109         initializePopUpMenu();
110         fillActionBars();
111         
112                 treeViewer.addOpenListener(new IOpenListener() {
113                         public void open(OpenEvent event) {
114                 ActionContext context = new ActionContext(
115                     getSelection());
116                 BookmarkView.this.actionGroup.setContext(context);
117                 IAction action = actionGroup.getOpenAction();
118                 if (action != null) {
119                     action.run();
120                 }
121                         }
122                 });
123                 
124         }
125     private void fillActionBars() {
126         Action enableTableSizes = new Action() {
127                 public void run() {
128                         labelProvider.getLabelDecorationInstructions().setSizeVisible(isChecked());
129                 treeViewer.refresh();
130                 }
131         };
132         enableTableSizes.setText(Messages.getString("BookmarkView.ShowTableSizes")); //$NON-NLS-1$
133         enableTableSizes.setChecked(false);
134
135         IActionBars actionBars = getViewSite().getActionBars();
136         actionBars.getMenuManager().add(enableTableSizes);
137         
138         Action showDatabaseData = new Action() {
139             public void run() {
140                 labelProvider.getLabelDecorationInstructions().setDatabaseDataVisible(isChecked());
141                 treeViewer.refresh();
142             }
143         };
144         showDatabaseData.setText(Messages.getString("BookmarkView.ShowDatabaseData")); //$NON-NLS-1$
145         showDatabaseData.setChecked(false);
146         actionBars.getMenuManager().add(showDatabaseData);
147
148         this.actionGroup.fillActionBars(actionBars);
149     }
150     
151     private void initializePopUpMenu() {
152         MenuManager manager = new MenuManager();
153         manager.setRemoveAllWhenShown(true);
154         manager.addMenuListener(new IMenuListener() {
155             public void menuAboutToShow(IMenuManager mgr) {
156                 fillContextMenu(mgr);
157             }
158         });
159         Menu fTextContextMenu =
160             manager.createContextMenu(treeViewer.getControl());
161         treeViewer.getControl().setMenu(fTextContextMenu);
162         // register the menu to the site so that we can allow 
163         // actions to be plugged in
164         getSite().registerContextMenu(manager, this.treeViewer);
165     }
166         public void initActions() {
167         
168         this.actionGroup = new BookmarkViewActionGroup(this, this.treeViewer);
169         
170
171                 extensionVector = new Vector();
172                 try {
173                         ProcessServiceMembers.process(this, extensionVector);
174                 } catch (WorkbenchException e) {
175                         e.printStackTrace();
176                 }
177
178         }
179         
180         /**
181          * 
182          */
183         private void initCustomCopyActions() {
184                 IPreferenceStore store = QuantumPlugin.getDefault().getPreferenceStore();
185         String text1 = store.getString("customCopyName1");
186         if (text1 != null && text1.trim().length() > 0) {
187                         this.customCopyAction1 = new CustomCopyAction(1); // 1 is unused, just in case more custom copies are defined        
188                         this.customCopyAction1.setText(text1); //$NON-NLS-1$
189                         this.customCopyAction1.setImageDescriptor(
190                                         ImageStore.getImageDescriptor(ImageStore.COPY)); //$NON-NLS-1$
191         }
192         String text2 = store.getString("customCopyName2");
193         if (text2 != null && text1.trim().length() > 0) {
194                         this.customCopyAction2 = new CustomCopyAction(2); // 1 is unused, just in case more custom copies are defined        
195                         this.customCopyAction2.setText(text2); //$NON-NLS-1$
196                         this.customCopyAction2.setImageDescriptor(
197                                         ImageStore.getImageDescriptor(ImageStore.COPY)); //$NON-NLS-1$
198         }
199         String text3 = store.getString("customCopyName3");
200         if (text3 != null && text1.trim().length() > 0) {
201                         this.customCopyAction3 = new CustomCopyAction(3); // 1 is unused, just in case more custom copies are defined        
202                         this.customCopyAction3.setText(text3); //$NON-NLS-1$
203                         this.customCopyAction3.setImageDescriptor(
204                                         ImageStore.getImageDescriptor(ImageStore.COPY)); //$NON-NLS-1$
205         }
206         }
207         public void dispose(){
208                 super.dispose();
209         BookmarkListNode.getInstance().removePropertyChangeListener(this);
210         }
211
212         public Shell getShell() {
213                 return getSite().getShell();
214         }
215
216     /**
217      * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
218      */
219     public void propertyChange(PropertyChangeEvent event) {
220         if ("bookmarks".equals(event.getPropertyName())) {
221             refresh();
222         } else if ("name".equals(event.getPropertyName()) && 
223             event.getSource() instanceof BookmarkNode) {
224             refresh();
225         } else if ("connected".equals(event.getPropertyName())) {
226             treeViewer.refresh(event.getSource());
227             if (Boolean.TRUE.equals(event.getNewValue())) {
228                 treeViewer.setExpandedState(event.getSource(), true);
229             }
230         } else {
231             treeViewer.refresh(event.getSource());
232         }
233     }
234     
235     private void fillContextMenu(IMenuManager mgr) {
236     // TODO: this method is pretty barfy... make it cleaner
237     
238                 initCustomCopyActions();
239         IStructuredSelection selection = getSelection();
240         ActionContext context = new ActionContext(selection);
241         this.actionGroup.setContext(context);
242         this.actionGroup.fillContextMenu(mgr);
243         
244         Object sel = getCurrent();
245         // If selection is a BookmarkNode
246         if (sel instanceof EntityNode) {
247             EntityNode entityNode = (EntityNode) sel;
248             if (!entityNode.isSequence()) {
249                 if (this.customCopyAction1 != null 
250                                 || this.customCopyAction2 != null 
251                                                 || this.customCopyAction3 != null) {
252                         mgr.add(new Separator());
253                         MenuManager subMenu = new MenuManager(Messages.getString("bookmarkview.customCopyAction")); 
254                         if (this.customCopyAction1 != null) {
255                             customCopyAction1.selectionChanged(selection);
256                                 subMenu.add(customCopyAction1);
257                         }
258                         if (this.customCopyAction2 != null) {
259                                 customCopyAction2.selectionChanged(selection);
260                                 subMenu.add(customCopyAction2);
261                         }
262                         if (this.customCopyAction3 != null) {
263                                 customCopyAction3.selectionChanged(selection);
264                                 subMenu.add(customCopyAction3);
265                         }
266                         mgr.add(subMenu);
267                 }
268
269                                 MenuManager subMenuExtension = new MenuManager("Extensions"); 
270                                 for (int i = 0; i < extensionVector.size(); i++) {
271                                         ExtensionAction extensionAction = (ExtensionAction) extensionVector.get(i);
272                                         subMenuExtension.add(extensionAction);
273                                 }
274                                 if (extensionVector.size() > 0) mgr.add(subMenuExtension);
275             }
276         } else if ((sel instanceof ColumnNode) && (this.customCopyAction1 != null 
277                                 || this.customCopyAction2 != null 
278                                         || this.customCopyAction3 != null)) {
279             MenuManager subMenu = new MenuManager(Messages.getString("bookmarkview.customCopyAction")); 
280             if (this.customCopyAction1 != null) {
281                 customCopyAction1.selectionChanged(selection);
282                 subMenu.add(customCopyAction1);
283             }
284             if (this.customCopyAction2 != null) {
285                 customCopyAction2.selectionChanged(selection);
286                 subMenu.add(customCopyAction2);
287             }
288             if (this.customCopyAction3 != null) {
289                 customCopyAction3.selectionChanged(selection);
290                 subMenu.add(customCopyAction3);
291             }
292             mgr.add(subMenu);
293         }
294     }
295     /**
296      * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
297      */
298     public void setFocus() {
299     }
300     
301
302 }