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