001: /*******************************************************************************
002: * Copyright (c) 2005, 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: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.actions;
011:
012: import org.eclipse.jface.action.Action;
013: import org.eclipse.jface.text.Region;
014: import org.eclipse.jface.text.hyperlink.IHyperlink;
015: import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;
016: import org.eclipse.pde.internal.ui.PDEPluginImages;
017: import org.eclipse.pde.internal.ui.PDEUIMessages;
018: import org.eclipse.pde.internal.ui.editor.PDESourcePage;
019: import org.eclipse.pde.internal.ui.editor.plugin.ExtensionHyperLink;
020: import org.eclipse.pde.internal.ui.editor.text.BundleHyperlink;
021: import org.eclipse.pde.internal.ui.editor.text.JavaHyperlink;
022: import org.eclipse.pde.internal.ui.editor.text.PackageHyperlink;
023: import org.eclipse.pde.internal.ui.editor.text.ResourceHyperlink;
024: import org.eclipse.pde.internal.ui.editor.text.SchemaHyperlink;
025: import org.eclipse.pde.internal.ui.editor.text.TranslationHyperlink;
026: import org.eclipse.swt.custom.StyledText;
027: import org.eclipse.swt.events.KeyEvent;
028: import org.eclipse.swt.events.KeyListener;
029: import org.eclipse.swt.events.MouseEvent;
030: import org.eclipse.swt.events.MouseListener;
031: import org.eclipse.swt.graphics.Point;
032: import org.eclipse.ui.texteditor.ITextEditor;
033:
034: public class HyperlinkAction extends Action implements MouseListener,
035: KeyListener {
036:
037: protected IHyperlinkDetector fDetector;
038: protected StyledText fStyledText;
039: protected IHyperlink fLink;
040:
041: public HyperlinkAction() {
042: setImageDescriptor(PDEPluginImages.DESC_LINK_OBJ);
043: setEnabled(false);
044: }
045:
046: public void run() {
047: if (fLink != null)
048: fLink.open();
049: }
050:
051: public IHyperlink getHyperLink() {
052: return fLink;
053: }
054:
055: protected void removeListeners() {
056: if (!hasDetector() || isTextDisposed())
057: return;
058: fStyledText.removeMouseListener(this );
059: fStyledText.removeKeyListener(this );
060: }
061:
062: protected void addListeners() {
063: if (!hasDetector() || isTextDisposed())
064: return;
065: fStyledText.addMouseListener(this );
066: fStyledText.addKeyListener(this );
067: }
068:
069: public boolean detectHyperlink() {
070: fLink = null;
071: if (!hasDetector() || isTextDisposed())
072: return false;
073:
074: Point p = fStyledText.getSelection();
075: IHyperlink[] links = fDetector.detectHyperlinks(null,
076: new Region(p.x, p.y - p.x), false);
077:
078: if (links == null || links.length == 0)
079: return false;
080:
081: fLink = links[0];
082: return true;
083: }
084:
085: public void setTextEditor(ITextEditor editor) {
086: StyledText newText = editor instanceof PDESourcePage ? ((PDESourcePage) editor)
087: .getViewer().getTextWidget()
088: : null;
089: if (fStyledText != null && fStyledText.equals(newText))
090: return;
091:
092: // remove the previous listeners if there were any
093: removeListeners();
094: fStyledText = newText;
095: fDetector = editor instanceof PDESourcePage ? (IHyperlinkDetector) ((PDESourcePage) editor)
096: .getAdapter(IHyperlinkDetector.class)
097: : null;
098: // Add new listeners, if hyperlinks are present
099: addListeners();
100:
101: setEnabled(detectHyperlink());
102: generateActionText();
103: }
104:
105: protected boolean hasDetector() {
106: return fDetector != null;
107: }
108:
109: private boolean isTextDisposed() {
110: return fStyledText == null || fStyledText.isDisposed();
111: }
112:
113: public void generateActionText() {
114: String text = PDEUIMessages.HyperlinkActionNoLinksAvailable;
115: if (fLink instanceof JavaHyperlink)
116: text = PDEUIMessages.HyperlinkActionOpenType;
117: else if (fLink instanceof ExtensionHyperLink)
118: text = PDEUIMessages.HyperlinkActionOpenDescription;
119: else if (fLink instanceof BundleHyperlink)
120: text = PDEUIMessages.HyperlinkActionOpenBundle;
121: else if (fLink instanceof PackageHyperlink)
122: text = PDEUIMessages.HyperlinkActionOpenPackage;
123: else if (fLink instanceof ResourceHyperlink)
124: text = PDEUIMessages.HyperlinkActionOpenResource;
125: else if (fLink instanceof SchemaHyperlink)
126: text = PDEUIMessages.HyperlinkActionOpenSchema;
127: else if (fLink instanceof TranslationHyperlink)
128: text = PDEUIMessages.HyperlinkActionOpenTranslation;
129: setText(text);
130: setToolTipText(text);
131: }
132:
133: public void mouseDoubleClick(MouseEvent e) {
134: // Ignore
135: }
136:
137: public void mouseDown(MouseEvent e) {
138: // Ignore
139: }
140:
141: public void mouseUp(MouseEvent e) {
142: setEnabled(detectHyperlink());
143: generateActionText();
144: }
145:
146: public void keyPressed(KeyEvent e) {
147: setEnabled(detectHyperlink());
148: generateActionText();
149: }
150:
151: public void keyReleased(KeyEvent e) {
152: // Ignore
153: }
154:
155: }
|