KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > actions > AddBookmarkAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Sebastian Davids <sdavids@gmx.de>
11  * - Fix for bug 20510 - Add Bookmark action has wrong label in navigator or
12  * packages view
13  *******************************************************************************/

14 package org.eclipse.ui.actions;
15
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.core.commands.ExecutionException;
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IMarker;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.runtime.IAdaptable;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26
27 import org.eclipse.swt.widgets.Shell;
28 import org.eclipse.ui.PlatformUI;
29 import org.eclipse.ui.ide.undo.CreateMarkersOperation;
30 import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
31 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
32 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
33 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
34 import org.eclipse.ui.internal.views.bookmarkexplorer.BookmarkMessages;
35 import org.eclipse.ui.views.bookmarkexplorer.BookmarkPropertiesDialog;
36
37 /**
38  * Standard action for adding a bookmark to the currently selected file
39  * resource(s).
40  * <p>
41  * This class may be instantiated; it is not intended to be subclassed.
42  * </p>
43  */

44 public class AddBookmarkAction extends SelectionListenerAction {
45
46     /**
47      * The id of this action.
48      */

49     public static final String JavaDoc ID = PlatformUI.PLUGIN_ID + ".AddBookmarkAction"; //$NON-NLS-1$
50

51     /**
52      * The shell in which to show any dialogs.
53      */

54     private Shell shell;
55
56     /**
57      * Whether to prompt the user for the bookmark name.
58      */

59     private boolean promptForName = true;
60
61     /**
62      * Creates a new bookmark action. By default, prompts the user for the
63      * bookmark name.
64      *
65      * @param shell
66      * the shell for any dialogs
67      */

68     public AddBookmarkAction(Shell shell) {
69         this(shell, true);
70     }
71
72     /**
73      * Creates a new bookmark action.
74      *
75      * @param shell
76      * the shell for any dialogs
77      * @param promptForName
78      * whether to ask the user for the bookmark name
79      */

80     public AddBookmarkAction(Shell shell, boolean promptForName) {
81         super(IDEWorkbenchMessages.AddBookmarkLabel);
82         setId(ID);
83         if (shell == null) {
84             throw new IllegalArgumentException JavaDoc();
85         }
86         this.shell = shell;
87         this.promptForName = promptForName;
88         setToolTipText(IDEWorkbenchMessages.AddBookmarkToolTip);
89         PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
90                 IIDEHelpContextIds.ADD_BOOKMARK_ACTION);
91     }
92
93     /*
94      * (non-Javadoc) Method declared on IAction.
95      */

96     public void run() {
97         IStructuredSelection selection = getStructuredSelection();
98         for (Iterator JavaDoc i = selection.iterator(); i.hasNext();) {
99             Object JavaDoc o = i.next();
100             IFile file = null;
101             if (o instanceof IFile) {
102                 file = (IFile) o;
103             } else if (o instanceof IAdaptable) {
104                 Object JavaDoc resource = ((IAdaptable) o).getAdapter(IResource.class);
105                 if (resource instanceof IFile) {
106                     file = (IFile) resource;
107                 }
108             }
109             if (file != null) {
110                 if (promptForName) {
111                     BookmarkPropertiesDialog dialog = new BookmarkPropertiesDialog(
112                             shell);
113                     dialog.setResource(file);
114                     dialog.open();
115                 } else {
116                     Map JavaDoc attrs = new HashMap JavaDoc();
117                     attrs.put(IMarker.MESSAGE, file.getName());
118                     CreateMarkersOperation op = new CreateMarkersOperation(
119                             IMarker.BOOKMARK, attrs, file,
120                             BookmarkMessages.CreateBookmark_undoText);
121                     try {
122                         PlatformUI.getWorkbench().getOperationSupport()
123                                 .getOperationHistory().execute(op, null,
124                                         WorkspaceUndoUtil.getUIInfoAdapter(shell));
125                     } catch (ExecutionException e) {
126                         IDEWorkbenchPlugin.log(null, e); // We don't care
127
}
128                 }
129             }
130         }
131
132     }
133
134     /**
135      * The <code>AddBookmarkAction</code> implementation of this
136      * <code>SelectionListenerAction</code> method enables the action only if
137      * the selection is not empty and contains just file resources.
138      */

139     protected boolean updateSelection(IStructuredSelection selection) {
140         // @issue typed selections
141
return super.updateSelection(selection) && !selection.isEmpty()
142                 && selectionIsOfType(IResource.FILE);
143     }
144 }
145
Popular Tags