01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.examples.readmetool;
11:
12: import org.eclipse.core.resources.IFile;
13: import org.eclipse.core.resources.IMarker;
14: import org.eclipse.core.runtime.CoreException;
15: import org.eclipse.jface.dialogs.MessageDialog;
16: import org.eclipse.jface.text.BadLocationException;
17: import org.eclipse.jface.text.IDocument;
18: import org.eclipse.ui.IEditorPart;
19: import org.eclipse.ui.IFileEditorInput;
20: import org.eclipse.ui.IMarkerResolution;
21: import org.eclipse.ui.IWorkbenchPage;
22: import org.eclipse.ui.IWorkbenchWindow;
23: import org.eclipse.ui.PartInitException;
24: import org.eclipse.ui.PlatformUI;
25: import org.eclipse.ui.ide.IDE;
26: import org.eclipse.ui.part.FileEditorInput;
27:
28: /**
29: * A resolution which inserts a sentence into the readme file
30: */
31: public class AddSentenceResolution implements IMarkerResolution {
32: /* (non-Javadoc)
33: * Method declared on IMarkerResolution.
34: */
35: public String getLabel() {
36: return MessageUtil.getString("Add_Sentence"); //$NON-NLS-1$
37: }
38:
39: /* (non-Javadoc)
40: * Method declared on IMarkerResolution.
41: */
42: public void run(IMarker marker) {
43: // Se if there is an open editor on the file containing the marker
44: IWorkbenchWindow w = PlatformUI.getWorkbench()
45: .getActiveWorkbenchWindow();
46: if (w == null)
47: return;
48: IWorkbenchPage page = w.getActivePage();
49: if (page == null)
50: return;
51: IFileEditorInput input = new FileEditorInput((IFile) marker
52: .getResource());
53: IEditorPart editorPart = page.findEditor(input);
54:
55: if (editorPart == null) {
56: // open an editor
57: try {
58: editorPart = IDE.openEditor(page, (IFile) marker
59: .getResource(), true);
60: } catch (PartInitException e) {
61: MessageDialog
62: .openError(w.getShell(),
63: MessageUtil
64: .getString("Resolution_Error"), //$NON-NLS-1$
65: MessageUtil
66: .getString("Unable_to_open_file_editor")); //$NON-NLS-1$
67: }
68: }
69: if (editorPart == null || !(editorPart instanceof ReadmeEditor))
70: return;
71: // insert the sentence
72: ReadmeEditor editor = (ReadmeEditor) editorPart;
73: IDocument doc = editor.getDocumentProvider().getDocument(
74: editor.getEditorInput());
75: String s = MessageUtil.getString("Simple_sentence"); //$NON-NLS-1$
76: try {
77: doc.replace(marker.getAttribute(IMarker.CHAR_START, -1), 0,
78: s);
79: } catch (BadLocationException e) {
80: // ignore
81: return;
82: }
83: // delete the marker
84: try {
85: marker.delete();
86: } catch (CoreException e) {
87: e.printStackTrace();
88: // ignore
89: }
90:
91: }
92:
93: }
|