KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > propertiesfileeditor > OpenAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.propertiesfileeditor;
12
13 import org.eclipse.jface.text.IRegion;
14 import org.eclipse.jface.text.ITextSelection;
15 import org.eclipse.jface.text.Region;
16 import org.eclipse.jface.text.hyperlink.IHyperlink;
17
18 import org.eclipse.ui.IFileEditorInput;
19
20 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
21
22 /**
23  * This action opens a tool (internal editor or view or an external
24  * application) for the element at the given location.
25  * <p>
26  * XXX: This does not work for properties files coming from a JAR due to
27  * missing J Core functionality. For details see:
28  * https://bugs.eclipse.org/bugs/show_bug.cgi?id=22376
29  * </p>
30  * <p>
31  * This class may be instantiated; it is not intended to be subclassed.
32  * </p>
33  *
34  * @since 3.1
35  */

36 public class OpenAction extends SelectionDispatchAction {
37
38
39     private PropertiesFileEditor fEditor;
40
41
42     /**
43      * Creates a new <code>OpenAction</code>.
44      *
45      * @param editor the Properties file editor which provides the context information for this action
46      */

47     public OpenAction(PropertiesFileEditor editor) {
48         super(editor.getEditorSite());
49         fEditor= editor;
50         setText(PropertiesFileEditorMessages.OpenAction_label);
51         setToolTipText(PropertiesFileEditorMessages.OpenAction_tooltip);
52
53          // XXX: Must be removed once support for JARs is available (see class Javadoc for details).
54
setEnabled(fEditor.getEditorInput() instanceof IFileEditorInput);
55     }
56
57     /*
58      * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#selectionChanged(org.eclipse.jface.text.ITextSelection)
59      */

60     public void selectionChanged(ITextSelection selection) {
61         setEnabled(checkEnabled(selection));
62     }
63
64     private boolean checkEnabled(ITextSelection selection) {
65         if (selection == null || selection.isEmpty())
66             return false;
67
68         // XXX: Must be changed to IStorageEditorInput once support for JARs is available (see class Javadoc for details)
69
return fEditor.getEditorInput() instanceof IFileEditorInput;
70     }
71
72     public void run(ITextSelection selection) {
73
74         if (!checkEnabled(selection))
75             return;
76
77         IRegion region= new Region(selection.getOffset(), selection.getLength());
78         PropertyKeyHyperlinkDetector detector= new PropertyKeyHyperlinkDetector();
79         detector.setContext(fEditor);
80         IHyperlink[]hyperlinks= detector.detectHyperlinks(fEditor.internalGetSourceViewer(), region, false);
81
82         if (hyperlinks != null && hyperlinks.length == 1)
83             hyperlinks[0].open();
84
85     }
86 }
87
Popular Tags