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