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: * Joe Bowbeer (jozart@blarg.net) - removed dependency on runtime compatibility layer (bug 74528)
011: *******************************************************************************/package org.eclipse.ui.examples.readmetool;
012:
013: import java.util.HashMap;
014: import java.util.Map;
015:
016: import org.eclipse.core.resources.IResource;
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IAdaptable;
019: import org.eclipse.core.runtime.Platform;
020: import org.eclipse.jface.action.Action;
021: import org.eclipse.jface.dialogs.ErrorDialog;
022: import org.eclipse.jface.text.ITextSelection;
023: import org.eclipse.swt.widgets.Shell;
024: import org.eclipse.ui.IEditorInput;
025: import org.eclipse.ui.texteditor.ITextEditor;
026: import org.eclipse.ui.texteditor.MarkerUtilities;
027: import org.osgi.framework.Bundle;
028:
029: /**
030: * Action for creating a readme marker with a specfic id
031: * attribute value.
032: */
033: public class AddReadmeMarkerAction extends Action {
034: private ITextEditor textEditor;
035:
036: private Object[][] customAttributes;
037:
038: private String message;
039:
040: private final static String MARKER_TYPE = "org.eclipse.ui.examples.readmetool.readmemarker"; //$NON-NLS-1$
041:
042: /**
043: * Creates a new action for the given text editor.
044: *
045: * @param editor the text editor
046: * @param label the label for the action
047: * @param attributes the custom attributes for this marker
048: * @param message the message for the marker
049: */
050: public AddReadmeMarkerAction(ITextEditor editor, String label,
051: Object[][] attributes, String message) {
052: textEditor = editor;
053: setText(label);
054: this .customAttributes = attributes;
055: this .message = message;
056: }
057:
058: /*
059: * @see IAction#run()
060: */
061: public void run() {
062: Map attributes = new HashMap(11);
063:
064: ITextSelection selection = (ITextSelection) textEditor
065: .getSelectionProvider().getSelection();
066: if (!selection.isEmpty()) {
067:
068: int start = selection.getOffset();
069: int length = selection.getLength();
070:
071: if (length < 0) {
072: length = -length;
073: start -= length;
074: }
075:
076: MarkerUtilities.setCharStart(attributes, start);
077: MarkerUtilities.setCharEnd(attributes, start + length);
078:
079: // marker line numbers are 1-based
080: int line = selection.getStartLine();
081: MarkerUtilities.setLineNumber(attributes, line == -1 ? -1
082: : line + 1);
083:
084: // set custom attribute values
085: for (int i = 0; i < customAttributes.length; i++) {
086: attributes.put(customAttributes[i][0],
087: customAttributes[i][1]);
088: }
089:
090: MarkerUtilities.setMessage(attributes, message);
091: }
092:
093: try {
094: MarkerUtilities.createMarker(getResource(), attributes,
095: MARKER_TYPE);
096: } catch (CoreException x) {
097: Bundle bundle = ReadmePlugin.getDefault().getBundle();
098: Platform.getLog(bundle).log(x.getStatus());
099:
100: Shell shell = textEditor.getSite().getShell();
101: String title = MessageUtil
102: .getString("Add_readme_marker_error_title"); //$NON-NLS-1$
103: String msg = MessageUtil
104: .getString("Add_readme_marker_error_message"); //$NON-NLS-1$
105:
106: ErrorDialog.openError(shell, title, msg, x.getStatus());
107: }
108: }
109:
110: /**
111: * Returns the resource on which to create the marker,
112: * or <code>null</code> if there is no applicable resource. This
113: * queries the editor's input using <code>getAdapter(IResource.class)</code>.
114: *
115: * @return the resource to which to attach the newly created marker
116: */
117: protected IResource getResource() {
118: IEditorInput input = textEditor.getEditorInput();
119: return (IResource) ((IAdaptable) input)
120: .getAdapter(IResource.class);
121: }
122: }
|