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.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;
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;
35 public class BookmarkView extends ViewPart implements PropertyChangeListener {
36 private CustomCopyAction customCopyAction1;
37 private CustomCopyAction customCopyAction2;
38 private CustomCopyAction customCopyAction3;
39 private Vector extensionVector;
41 private BookmarkViewActionGroup actionGroup;
43 private TreeViewer treeViewer;
44 private BookmarkLabelProvider labelProvider = new BookmarkLabelProvider();
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() ? ).
52 public synchronized static BookmarkView getInstance() {
53 return (BookmarkView) QuantumPlugin.getDefault().getView("com.quantum.view.bookmarkview");
56 * Returns the current selected object in the tree. If it's a multiple selection, return the first.
59 public Object getCurrent() {
60 if (treeViewer == null) return null;
61 return ((StructuredSelection) treeViewer.getSelection())
65 * Returns the current selected objects in the tree, in the form of a StructuredSelection.
68 public StructuredSelection getSelection() {
69 if (treeViewer == null) return null;
70 return ((StructuredSelection) treeViewer.getSelection());
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.
78 public BookmarkNode getCurrentBookmark() {
79 TreeNode current = (TreeNode) getCurrent();
80 return getRoot(current);
83 private static BookmarkNode getRoot(TreeNode node){
84 if (node == null) return null;
85 while (!( node instanceof BookmarkNode))
87 node = node.getParent();
89 if (node instanceof BookmarkNode) return (BookmarkNode) node;
93 public void refresh() {
96 public void createPartControl(org.eclipse.swt.widgets.Composite parent) {
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);
106 input.addPropertyChangeListener(this);
108 initializePopUpMenu();
111 treeViewer.addOpenListener(new IOpenListener() {
112 public void open(OpenEvent event) {
113 ActionContext context = new ActionContext(
115 BookmarkView.this.actionGroup.setContext(context);
116 IAction action = actionGroup.getOpenAction();
117 if (action != null) {
124 private void fillActionBars() {
125 Action enableTableSizes = new Action() {
127 labelProvider.getLabelDecorationInstructions().setSizeVisible(isChecked());
128 treeViewer.refresh();
131 enableTableSizes.setText(Messages.getString("BookmarkView.ShowTableSizes")); //$NON-NLS-1$
132 enableTableSizes.setChecked(false);
134 IActionBars actionBars = getViewSite().getActionBars();
135 actionBars.getMenuManager().add(enableTableSizes);
137 Action showDatabaseData = new Action() {
139 labelProvider.getLabelDecorationInstructions().setDatabaseDataVisible(isChecked());
140 treeViewer.refresh();
143 showDatabaseData.setText(Messages.getString("BookmarkView.ShowDatabaseData")); //$NON-NLS-1$
144 showDatabaseData.setChecked(false);
145 actionBars.getMenuManager().add(showDatabaseData);
147 this.actionGroup.fillActionBars(actionBars);
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);
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);
165 public void initActions() {
167 this.actionGroup = new BookmarkViewActionGroup(this, this.treeViewer);
170 extensionVector = new Vector();
172 ProcessServiceMembers.process(this, extensionVector);
173 } catch (WorkbenchException e) {
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$
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$
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$
206 public void dispose(){
208 BookmarkListNode.getInstance().removePropertyChangeListener(this);
211 public Shell getShell() {
212 return getSite().getShell();
216 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
218 public void propertyChange(PropertyChangeEvent event) {
219 if ("bookmarks".equals(event.getPropertyName())) {
221 } else if ("name".equals(event.getPropertyName()) &&
222 event.getSource() instanceof BookmarkNode) {
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);
230 treeViewer.refresh(event.getSource());
234 private void fillContextMenu(IMenuManager mgr) {
235 // TODO: this method is pretty barfy... make it cleaner
237 initCustomCopyActions();
238 IStructuredSelection selection = getSelection();
239 ActionContext context = new ActionContext(selection);
240 this.actionGroup.setContext(context);
241 this.actionGroup.fillContextMenu(mgr);
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);
256 if (this.customCopyAction2 != null) {
257 subMenu.add(customCopyAction2);
259 if (this.customCopyAction3 != null) {
260 subMenu.add(customCopyAction3);
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);
270 if (extensionVector.size() > 0) mgr.add(subMenuExtension);
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);
279 if (this.customCopyAction2 != null) {
280 subMenu.add(customCopyAction2);
282 if (this.customCopyAction3 != null) {
283 subMenu.add(customCopyAction3);
289 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
291 public void setFocus() {