001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Sebastian Davids <sdavids@gmx.de>
011: * - Fix for bug 20510 - Add Bookmark action has wrong label in navigator or
012: * packages view
013: *******************************************************************************/package org.eclipse.ui.actions;
014:
015: import java.util.HashMap;
016: import java.util.Iterator;
017: import java.util.Map;
018:
019: import org.eclipse.core.commands.ExecutionException;
020: import org.eclipse.core.resources.IFile;
021: import org.eclipse.core.resources.IMarker;
022: import org.eclipse.core.resources.IResource;
023: import org.eclipse.core.runtime.IAdaptable;
024: import org.eclipse.jface.viewers.IStructuredSelection;
025:
026: import org.eclipse.swt.widgets.Shell;
027: import org.eclipse.ui.PlatformUI;
028: import org.eclipse.ui.ide.undo.CreateMarkersOperation;
029: import org.eclipse.ui.ide.undo.WorkspaceUndoUtil;
030: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
031: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
032: import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
033: import org.eclipse.ui.internal.views.bookmarkexplorer.BookmarkMessages;
034: import org.eclipse.ui.views.bookmarkexplorer.BookmarkPropertiesDialog;
035:
036: /**
037: * Standard action for adding a bookmark to the currently selected file
038: * resource(s).
039: * <p>
040: * This class may be instantiated; it is not intended to be subclassed.
041: * </p>
042: */
043: public class AddBookmarkAction extends SelectionListenerAction {
044:
045: /**
046: * The id of this action.
047: */
048: public static final String ID = PlatformUI.PLUGIN_ID
049: + ".AddBookmarkAction"; //$NON-NLS-1$
050:
051: /**
052: * The shell in which to show any dialogs.
053: */
054: private Shell shell;
055:
056: /**
057: * Whether to prompt the user for the bookmark name.
058: */
059: private boolean promptForName = true;
060:
061: /**
062: * Creates a new bookmark action. By default, prompts the user for the
063: * bookmark name.
064: *
065: * @param shell
066: * the shell for any dialogs
067: */
068: public AddBookmarkAction(Shell shell) {
069: this (shell, true);
070: }
071:
072: /**
073: * Creates a new bookmark action.
074: *
075: * @param shell
076: * the shell for any dialogs
077: * @param promptForName
078: * whether to ask the user for the bookmark name
079: */
080: public AddBookmarkAction(Shell shell, boolean promptForName) {
081: super (IDEWorkbenchMessages.AddBookmarkLabel);
082: setId(ID);
083: if (shell == null) {
084: throw new IllegalArgumentException();
085: }
086: this .shell = shell;
087: this .promptForName = promptForName;
088: setToolTipText(IDEWorkbenchMessages.AddBookmarkToolTip);
089: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
090: IIDEHelpContextIds.ADD_BOOKMARK_ACTION);
091: }
092:
093: /*
094: * (non-Javadoc) Method declared on IAction.
095: */
096: public void run() {
097: IStructuredSelection selection = getStructuredSelection();
098: for (Iterator i = selection.iterator(); i.hasNext();) {
099: Object o = i.next();
100: IFile file = null;
101: if (o instanceof IFile) {
102: file = (IFile) o;
103: } else if (o instanceof IAdaptable) {
104: Object resource = ((IAdaptable) o)
105: .getAdapter(IResource.class);
106: if (resource instanceof IFile) {
107: file = (IFile) resource;
108: }
109: }
110: if (file != null) {
111: if (promptForName) {
112: BookmarkPropertiesDialog dialog = new BookmarkPropertiesDialog(
113: shell);
114: dialog.setResource(file);
115: dialog.open();
116: } else {
117: Map attrs = new HashMap();
118: attrs.put(IMarker.MESSAGE, file.getName());
119: CreateMarkersOperation op = new CreateMarkersOperation(
120: IMarker.BOOKMARK, attrs, file,
121: BookmarkMessages.CreateBookmark_undoText);
122: try {
123: PlatformUI
124: .getWorkbench()
125: .getOperationSupport()
126: .getOperationHistory()
127: .execute(
128: op,
129: null,
130: WorkspaceUndoUtil
131: .getUIInfoAdapter(shell));
132: } catch (ExecutionException e) {
133: IDEWorkbenchPlugin.log(null, e); // We don't care
134: }
135: }
136: }
137: }
138:
139: }
140:
141: /**
142: * The <code>AddBookmarkAction</code> implementation of this
143: * <code>SelectionListenerAction</code> method enables the action only if
144: * the selection is not empty and contains just file resources.
145: */
146: protected boolean updateSelection(IStructuredSelection selection) {
147: // @issue typed selections
148: return super.updateSelection(selection) && !selection.isEmpty()
149: && selectionIsOfType(IResource.FILE);
150: }
151: }
|