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.Messages;
29 import com.quantum.QuantumPlugin;
30 import com.quantum.actions.CustomCopyAction;
31 import com.quantum.extensions.ExtensionAction;
32 import com.quantum.extensions.ProcessServiceMembers;
34 public class BookmarkView extends ViewPart implements PropertyChangeListener {
35 private CustomCopyAction customCopyAction1;
36 private CustomCopyAction customCopyAction2;
37 private CustomCopyAction customCopyAction3;
38 private Vector extensionVector;
40 private BookmarkViewActionGroup actionGroup;
42 private TreeViewer treeViewer;
43 private BookmarkLabelProvider labelProvider = new BookmarkLabelProvider();
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() ? ).
51 public synchronized static BookmarkView getInstance() {
52 return (BookmarkView) QuantumPlugin.getDefault().getView("com.quantum.view.bookmarkview");
55 * Returns the current selected object in the tree. If it's a multiple selection, return the first.
58 public Object getCurrent() {
59 if (treeViewer == null) return null;
60 return ((StructuredSelection) treeViewer.getSelection())
64 * Returns the current selected objects in the tree, in the form of a StructuredSelection.
67 public StructuredSelection getSelection() {
68 if (treeViewer == null) return null;
69 return ((StructuredSelection) treeViewer.getSelection());
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.
77 public BookmarkNode getCurrentBookmark() {
78 TreeNode current = (TreeNode) getCurrent();
79 return getRoot(current);
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))
87 node = (TreeNode) 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());
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);
169 // openDataEditorAction = new OpenDataEditorAction();
170 // openDataEditorAction.setText("Edit Data");
171 // openDataEditorAction.setImageDescriptor(
172 // QuantumPlugin.getImageDescriptor("greentable.gif")); //$NON-NLS-1$
173 // openDataEditorAction.init(this);
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$
189 extensionVector = new Vector();
191 ProcessServiceMembers.process(this, extensionVector);
192 } catch (WorkbenchException e) {
198 public void dispose(){
200 BookmarkListNode.getInstance().removePropertyChangeListener(this);
203 public Shell getShell() {
204 return getSite().getShell();
208 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
210 public void propertyChange(PropertyChangeEvent event) {
211 if ("bookmarks".equals(event.getPropertyName())) {
213 } else if ("name".equals(event.getPropertyName()) &&
214 event.getSource() instanceof BookmarkNode) {
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);
222 treeViewer.refresh(event.getSource());
226 private void fillContextMenu(IMenuManager mgr) {
227 // TODO: this method is pretty barfy... make it cleaner
229 IStructuredSelection selection = getSelection();
230 ActionContext context = new ActionContext(selection);
231 this.actionGroup.setContext(context);
232 this.actionGroup.fillContextMenu(mgr);
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);
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);
251 if (extensionVector.size() > 0) mgr.add(subMenuExtension);
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);
263 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
265 public void setFocus() {