KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > actions > HyperlinkAction


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.pde.internal.ui.editor.actions;
12
13 import org.eclipse.jface.action.Action;
14 import org.eclipse.jface.text.Region;
15 import org.eclipse.jface.text.hyperlink.IHyperlink;
16 import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;
17 import org.eclipse.pde.internal.ui.PDEPluginImages;
18 import org.eclipse.pde.internal.ui.PDEUIMessages;
19 import org.eclipse.pde.internal.ui.editor.PDESourcePage;
20 import org.eclipse.pde.internal.ui.editor.plugin.ExtensionHyperLink;
21 import org.eclipse.pde.internal.ui.editor.text.BundleHyperlink;
22 import org.eclipse.pde.internal.ui.editor.text.JavaHyperlink;
23 import org.eclipse.pde.internal.ui.editor.text.PackageHyperlink;
24 import org.eclipse.pde.internal.ui.editor.text.ResourceHyperlink;
25 import org.eclipse.pde.internal.ui.editor.text.SchemaHyperlink;
26 import org.eclipse.pde.internal.ui.editor.text.TranslationHyperlink;
27 import org.eclipse.swt.custom.StyledText;
28 import org.eclipse.swt.events.KeyEvent;
29 import org.eclipse.swt.events.KeyListener;
30 import org.eclipse.swt.events.MouseEvent;
31 import org.eclipse.swt.events.MouseListener;
32 import org.eclipse.swt.graphics.Point;
33 import org.eclipse.ui.texteditor.ITextEditor;
34
35 public class HyperlinkAction extends Action implements MouseListener, KeyListener {
36
37     protected IHyperlinkDetector fDetector;
38     protected StyledText fStyledText;
39     protected IHyperlink fLink;
40
41     public HyperlinkAction() {
42         setImageDescriptor(PDEPluginImages.DESC_LINK_OBJ);
43         setEnabled(false);
44     }
45
46     public void run() {
47         if (fLink != null)
48             fLink.open();
49     }
50
51     public IHyperlink getHyperLink() {
52         return fLink;
53     }
54
55     protected void removeListeners() {
56         if (!hasDetector() || isTextDisposed())
57             return;
58         fStyledText.removeMouseListener(this);
59         fStyledText.removeKeyListener(this);
60     }
61
62     protected void addListeners() {
63         if (!hasDetector() || isTextDisposed())
64             return;
65         fStyledText.addMouseListener(this);
66         fStyledText.addKeyListener(this);
67     }
68
69     public boolean detectHyperlink() {
70         fLink = null;
71         if (!hasDetector() || isTextDisposed())
72             return false;
73
74         Point p = fStyledText.getSelection();
75         IHyperlink[] links = fDetector.detectHyperlinks(null, new Region(p.x, p.y - p.x), false);
76
77         if (links == null || links.length == 0)
78             return false;
79
80         fLink = links[0];
81         return true;
82     }
83
84     public void setTextEditor(ITextEditor editor) {
85         StyledText newText = editor instanceof PDESourcePage ?
86                 ((PDESourcePage)editor).getViewer().getTextWidget() : null;
87                 if (fStyledText != null && fStyledText.equals(newText))
88                     return;
89
90                 // remove the previous listeners if there were any
91
removeListeners();
92                 fStyledText = newText;
93                 fDetector = editor instanceof PDESourcePage ? (IHyperlinkDetector)((PDESourcePage)editor).getAdapter(IHyperlinkDetector.class) : null;
94                 // Add new listeners, if hyperlinks are present
95
addListeners();
96
97                 setEnabled(detectHyperlink());
98                 generateActionText();
99     }
100
101     protected boolean hasDetector() {
102         return fDetector != null;
103     }
104
105     private boolean isTextDisposed() {
106         return fStyledText == null || fStyledText.isDisposed();
107     }
108
109     public void generateActionText() {
110         String JavaDoc text = PDEUIMessages.HyperlinkActionNoLinksAvailable;
111         if (fLink instanceof JavaHyperlink)
112             text = PDEUIMessages.HyperlinkActionOpenType;
113         else if (fLink instanceof ExtensionHyperLink)
114             text = PDEUIMessages.HyperlinkActionOpenDescription;
115         else if (fLink instanceof BundleHyperlink)
116             text = PDEUIMessages.HyperlinkActionOpenBundle;
117         else if (fLink instanceof PackageHyperlink)
118             text = PDEUIMessages.HyperlinkActionOpenPackage;
119         else if (fLink instanceof ResourceHyperlink)
120             text = PDEUIMessages.HyperlinkActionOpenResource;
121         else if (fLink instanceof SchemaHyperlink)
122             text = PDEUIMessages.HyperlinkActionOpenSchema;
123         else if (fLink instanceof TranslationHyperlink)
124             text = PDEUIMessages.HyperlinkActionOpenTranslation;
125         setText(text);
126         setToolTipText(text);
127     }
128
129     public void mouseDoubleClick(MouseEvent e) {
130         // Ignore
131
}
132
133     public void mouseDown(MouseEvent e) {
134         // Ignore
135
}
136
137     public void mouseUp(MouseEvent e) {
138         setEnabled(detectHyperlink());
139         generateActionText();
140     }
141
142     public void keyPressed(KeyEvent e) {
143         setEnabled(detectHyperlink());
144         generateActionText();
145     }
146
147     public void keyReleased(KeyEvent e) {
148         // Ignore
149
}
150
151 }
152
Popular Tags