1 package com.quantum.view.bookmark;
4 import java.beans.PropertyChangeEvent;
5 import java.beans.PropertyChangeListener;
6 import java.util.Vector;
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;
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;
36 public class BookmarkView extends ViewPart implements PropertyChangeListener {
37 private CustomCopyAction customCopyAction1;
38 private CustomCopyAction customCopyAction2;
39 private CustomCopyAction customCopyAction3;
40 private Vector extensionVector;
42 private BookmarkViewActionGroup actionGroup;
44 private TreeViewer treeViewer;
45 private BookmarkLabelProvider labelProvider = new BookmarkLabelProvider();
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() ? ).
53 public synchronized static BookmarkView getInstance() {
54 return (BookmarkView) QuantumPlugin.getDefault().getView("com.quantum.view.bookmarkview");
57 * Returns the current selected object in the tree. If it's a multiple selection, return the first.
60 public Object getCurrent() {
61 if (treeViewer == null) return null;
62 return ((StructuredSelection) treeViewer.getSelection())
66 * Returns the current selected objects in the tree, in the form of a StructuredSelection.
69 public StructuredSelection getSelection() {
70 if (treeViewer == null) return null;
71 return ((StructuredSelection) treeViewer.getSelection());
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.
79 public BookmarkNode getCurrentBookmark() {
80 TreeNode current = (TreeNode) getCurrent();
81 return getRoot(current);
84 private static BookmarkNode getRoot(TreeNode node){
85 if (node == null) return null;
86 while (!( node instanceof BookmarkNode))
88 node = node.getParent();
90 if (node instanceof BookmarkNode) return (BookmarkNode) node;
94 public void refresh() {
97 public void createPartControl(org.eclipse.swt.widgets.Composite parent) {
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);
107 input.addPropertyChangeListener(this);
109 initializePopUpMenu();
112 treeViewer.addOpenListener(new IOpenListener() {
113 public void open(OpenEvent event) {
114 ActionContext context = new ActionContext(
116 BookmarkView.this.actionGroup.setContext(context);
117 IAction action = actionGroup.getOpenAction();
118 if (action != null) {
125 private void fillActionBars() {
126 Action enableTableSizes = new Action() {
128 labelProvider.getLabelDecorationInstructions().setSizeVisible(isChecked());
129 treeViewer.refresh();
132 enableTableSizes.setText(Messages.getString("BookmarkView.ShowTableSizes")); //$NON-NLS-1$
133 enableTableSizes.setChecked(false);
135 IActionBars actionBars = getViewSite().getActionBars();
136 actionBars.getMenuManager().add(enableTableSizes);
138 Action showDatabaseData = new Action() {
140 labelProvider.getLabelDecorationInstructions().setDatabaseDataVisible(isChecked());
141 treeViewer.refresh();
144 showDatabaseData.setText(Messages.getString("BookmarkView.ShowDatabaseData")); //$NON-NLS-1$
145 showDatabaseData.setChecked(false);
146 actionBars.getMenuManager().add(showDatabaseData);
148 this.actionGroup.fillActionBars(actionBars);
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);
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);
166 public void initActions() {
168 this.actionGroup = new BookmarkViewActionGroup(this, this.treeViewer);
171 extensionVector = new Vector();
173 ProcessServiceMembers.process(this, extensionVector);
174 } catch (WorkbenchException e) {
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$
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$
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$
207 public void dispose(){
209 BookmarkListNode.getInstance().removePropertyChangeListener(this);
212 public Shell getShell() {
213 return getSite().getShell();
217 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
219 public void propertyChange(PropertyChangeEvent event) {
220 if ("bookmarks".equals(event.getPropertyName())) {
222 } else if ("name".equals(event.getPropertyName()) &&
223 event.getSource() instanceof BookmarkNode) {
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);
231 treeViewer.refresh(event.getSource());
235 private void fillContextMenu(IMenuManager mgr) {
236 // TODO: this method is pretty barfy... make it cleaner
238 initCustomCopyActions();
239 IStructuredSelection selection = getSelection();
240 ActionContext context = new ActionContext(selection);
241 this.actionGroup.setContext(context);
242 this.actionGroup.fillContextMenu(mgr);
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);
258 if (this.customCopyAction2 != null) {
259 customCopyAction2.selectionChanged(selection);
260 subMenu.add(customCopyAction2);
262 if (this.customCopyAction3 != null) {
263 customCopyAction3.selectionChanged(selection);
264 subMenu.add(customCopyAction3);
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);
274 if (extensionVector.size() > 0) mgr.add(subMenuExtension);
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);
284 if (this.customCopyAction2 != null) {
285 customCopyAction2.selectionChanged(selection);
286 subMenu.add(customCopyAction2);
288 if (this.customCopyAction3 != null) {
289 customCopyAction3.selectionChanged(selection);
290 subMenu.add(customCopyAction3);
296 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
298 public void setFocus() {