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 while (!( node instanceof BookmarkNode))
86 node = node.getParent();
88 if (node instanceof BookmarkNode) return (BookmarkNode) node;
92 public void refresh() {
95 public void createPartControl(org.eclipse.swt.widgets.Composite parent) {
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);
105 input.addPropertyChangeListener(this);
107 initializePopUpMenu();
110 treeViewer.addOpenListener(new IOpenListener() {
111 public void open(OpenEvent event) {
112 ActionContext context = new ActionContext(
114 BookmarkView.this.actionGroup.setContext(context);
115 IAction action = actionGroup.getOpenAction();
116 if (action != null) {
123 private void fillActionBars() {
124 Action enableTableSizes = new Action() {
126 labelProvider.getLabelDecorationInstructions().setSizeVisible(isChecked());
127 treeViewer.refresh();
130 enableTableSizes.setText(Messages.getString("BookmarkView.ShowTableSizes")); //$NON-NLS-1$
131 enableTableSizes.setChecked(false);
133 IActionBars actionBars = getViewSite().getActionBars();
134 actionBars.getMenuManager().add(enableTableSizes);
136 Action showDatabaseData = new Action() {
138 labelProvider.getLabelDecorationInstructions().setDatabaseDataVisible(isChecked());
139 treeViewer.refresh();
142 showDatabaseData.setText(Messages.getString("BookmarkView.ShowDatabaseData")); //$NON-NLS-1$
143 showDatabaseData.setChecked(false);
144 actionBars.getMenuManager().add(showDatabaseData);
146 this.actionGroup.fillActionBars(actionBars);
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);
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);
164 public void initActions() {
166 this.actionGroup = new BookmarkViewActionGroup(this, this.treeViewer);
169 extensionVector = new Vector();
171 ProcessServiceMembers.process(this, extensionVector);
172 } catch (WorkbenchException e) {
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$
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$
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$
205 public void dispose(){
207 BookmarkListNode.getInstance().removePropertyChangeListener(this);
210 public Shell getShell() {
211 return getSite().getShell();
215 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
217 public void propertyChange(PropertyChangeEvent event) {
218 if ("bookmarks".equals(event.getPropertyName())) {
220 } else if ("name".equals(event.getPropertyName()) &&
221 event.getSource() instanceof BookmarkNode) {
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);
229 treeViewer.refresh(event.getSource());
233 private void fillContextMenu(IMenuManager mgr) {
234 // TODO: this method is pretty barfy... make it cleaner
236 initCustomCopyActions();
237 IStructuredSelection selection = getSelection();
238 ActionContext context = new ActionContext(selection);
239 this.actionGroup.setContext(context);
240 this.actionGroup.fillContextMenu(mgr);
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);
255 if (this.customCopyAction2 != null) {
256 subMenu.add(customCopyAction2);
258 if (this.customCopyAction3 != null) {
259 subMenu.add(customCopyAction3);
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);
269 if (extensionVector.size() > 0) mgr.add(subMenuExtension);
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);
278 if (this.customCopyAction2 != null) {
279 subMenu.add(customCopyAction2);
281 if (this.customCopyAction3 != null) {
282 subMenu.add(customCopyAction3);
288 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
290 public void setFocus() {