fixed ID bug
[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                 if (! (node instanceof TreeNode)) return null;
85                 while (!( node instanceof BookmarkNode))
86                 {
87                         node = (TreeNode) 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());
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 //              openDataEditorAction = new OpenDataEditorAction();
170 //              openDataEditorAction.setText("Edit Data"); 
171 //              openDataEditorAction.setImageDescriptor(
172 //                      QuantumPlugin.getImageDescriptor("greentable.gif")); //$NON-NLS-1$
173 //              openDataEditorAction.init(this);
174
175                 IPreferenceStore store = QuantumPlugin.getDefault().getPreferenceStore();
176                 this.customCopyAction1 = new CustomCopyAction(this,1); // 1 is unused, just in case more custom copies are defined        
177                 this.customCopyAction1.setText(store.getString("customCopyName1")); //$NON-NLS-1$
178                 this.customCopyAction1.setImageDescriptor(
179                         QuantumPlugin.getImageDescriptor("copy.gif")); //$NON-NLS-1$
180                 this.customCopyAction2 = new CustomCopyAction(this,2); // 1 is unused, just in case more custom copies are defined        
181                 this.customCopyAction2.setText(store.getString("customCopyName2")); //$NON-NLS-1$
182                 this.customCopyAction2.setImageDescriptor(
183                         QuantumPlugin.getImageDescriptor("copy.gif")); //$NON-NLS-1$
184                 this.customCopyAction3 = new CustomCopyAction(this,3); // 1 is unused, just in case more custom copies are defined        
185                 this.customCopyAction3.setText(store.getString("customCopyName3")); //$NON-NLS-1$
186                 this.customCopyAction3.setImageDescriptor(
187                         QuantumPlugin.getImageDescriptor("copy.gif")); //$NON-NLS-1$
188
189                 extensionVector = new Vector();
190                 try {
191                         ProcessServiceMembers.process(this, extensionVector);
192                 } catch (WorkbenchException e) {
193                         e.printStackTrace();
194                 }
195
196                 }
197         
198         public void dispose(){
199                 super.dispose();
200         BookmarkListNode.getInstance().removePropertyChangeListener(this);
201         }
202
203         public Shell getShell() {
204                 return getSite().getShell();
205         }
206
207     /**
208      * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
209      */
210     public void propertyChange(PropertyChangeEvent event) {
211         if ("bookmarks".equals(event.getPropertyName())) {
212             refresh();
213         } else if ("name".equals(event.getPropertyName()) && 
214             event.getSource() instanceof BookmarkNode) {
215             refresh();
216         } else if ("connected".equals(event.getPropertyName())) {
217             treeViewer.refresh(event.getSource());
218             if (Boolean.TRUE.equals(event.getNewValue())) {
219                 treeViewer.setExpandedState(event.getSource(), true);
220             }
221         } else {
222             treeViewer.refresh(event.getSource());
223         }
224     }
225     
226     private void fillContextMenu(IMenuManager mgr) {
227     // TODO: this method is pretty barfy... make it cleaner
228     
229         IStructuredSelection selection = getSelection();
230         ActionContext context = new ActionContext(selection);
231         this.actionGroup.setContext(context);
232         this.actionGroup.fillContextMenu(mgr);
233         
234         Object sel = getCurrent();
235         // If selection is a BookmarkNode
236         if (sel instanceof EntityNode) {
237             EntityNode entityNode = (EntityNode) sel;
238             if (!entityNode.isSequence()) {
239                 mgr.add(new Separator());
240                 MenuManager subMenu = new MenuManager(Messages.getString("bookmarkview.customCopyAction")); 
241                 subMenu.add(customCopyAction1);
242                 subMenu.add(customCopyAction2);
243                 subMenu.add(customCopyAction3);
244                 mgr.add(subMenu);
245
246                                 MenuManager subMenuExtension = new MenuManager("Extensions"); 
247                                 for (int i = 0; i < extensionVector.size(); i++) {
248                                         ExtensionAction extensionAction = (ExtensionAction) extensionVector.get(i);
249                                         subMenuExtension.add(extensionAction);
250                                 }
251                                 if (extensionVector.size() > 0) mgr.add(subMenuExtension);
252             }
253         } else if (sel instanceof ColumnNode) {
254             MenuManager subMenu = new MenuManager(Messages.getString("bookmarkview.customCopyAction")); 
255             subMenu.add(customCopyAction1);
256             subMenu.add(customCopyAction2);
257             subMenu.add(customCopyAction3);
258             mgr.add(subMenu);
259
260         }
261     }
262     /**
263      * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
264      */
265     public void setFocus() {
266     }
267     
268
269 }