1 14 package org.eclipse.ui.actions; 15 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.Map ; 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 44 public class AddBookmarkAction extends SelectionListenerAction { 45 46 49 public static final String ID = PlatformUI.PLUGIN_ID + ".AddBookmarkAction"; 51 54 private Shell shell; 55 56 59 private boolean promptForName = true; 60 61 68 public AddBookmarkAction(Shell shell) { 69 this(shell, true); 70 } 71 72 80 public AddBookmarkAction(Shell shell, boolean promptForName) { 81 super(IDEWorkbenchMessages.AddBookmarkLabel); 82 setId(ID); 83 if (shell == null) { 84 throw new IllegalArgumentException (); 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 96 public void run() { 97 IStructuredSelection selection = getStructuredSelection(); 98 for (Iterator i = selection.iterator(); i.hasNext();) { 99 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).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 attrs = new HashMap (); 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); } 128 } 129 } 130 } 131 132 } 133 134 139 protected boolean updateSelection(IStructuredSelection selection) { 140 return super.updateSelection(selection) && !selection.isEmpty() 142 && selectionIsOfType(IResource.FILE); 143 } 144 } 145 | Popular Tags |