KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > actions > OpenExternalDocAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.ant.internal.ui.editor.actions;
12
13 import java.net.MalformedURLException JavaDoc;
14 import java.net.URL JavaDoc;
15 import org.apache.tools.ant.AntTypeDefinition;
16 import org.apache.tools.ant.ComponentHelper;
17 import org.apache.tools.ant.Project;
18 import org.eclipse.ant.internal.ui.AntUIPlugin;
19 import org.eclipse.ant.internal.ui.AntUtil;
20 import org.eclipse.ant.internal.ui.IAntUIPreferenceConstants;
21 import org.eclipse.ant.internal.ui.editor.AntEditor;
22 import org.eclipse.ant.internal.ui.model.AntElementNode;
23 import org.eclipse.ant.internal.ui.model.AntModel;
24 import org.eclipse.ant.internal.ui.model.AntProjectNode;
25 import org.eclipse.ant.internal.ui.model.AntTargetNode;
26 import org.eclipse.ant.internal.ui.model.AntTaskNode;
27 import org.eclipse.jface.action.Action;
28 import org.eclipse.jface.action.IAction;
29 import org.eclipse.jface.commands.ActionHandler;
30 import org.eclipse.jface.preference.IPreferenceStore;
31 import org.eclipse.jface.text.ITextSelection;
32 import org.eclipse.jface.viewers.ISelection;
33 import org.eclipse.swt.widgets.Shell;
34 import org.eclipse.ui.IEditorActionDelegate;
35 import org.eclipse.ui.IEditorPart;
36 import org.eclipse.ui.handlers.IHandlerService;
37
38 /**
39  * This action opens the selected tasks manual page in an external
40  * browser.
41  */

42 public class OpenExternalDocAction extends Action implements IEditorActionDelegate {
43         
44     private static final String JavaDoc COMMAND_ID = "org.eclipse.ant.ui.openExternalDoc"; //$NON-NLS-1$
45
private AntEditor fEditor;
46     
47     public OpenExternalDocAction() {
48     }
49     
50     public OpenExternalDocAction(AntEditor antEditor) {
51         fEditor= antEditor;
52         setActionDefinitionId(COMMAND_ID);
53         IHandlerService handlerService= (IHandlerService) antEditor.getSite().getService(IHandlerService.class);
54         handlerService.activateHandler(COMMAND_ID, new ActionHandler(this));
55         setText(AntEditorActionMessages.getString("OpenExternalDocAction.1")); //$NON-NLS-1$
56
setDescription(AntEditorActionMessages.getString("OpenExternalDocAction.2")); //$NON-NLS-1$
57
setToolTipText(AntEditorActionMessages.getString("OpenExternalDocAction.2")); //$NON-NLS-1$
58
}
59     
60     private Shell getShell() {
61        return fEditor.getEditorSite().getShell();
62     }
63     
64     private void doAction(AntElementNode node) {
65         Shell shell= getShell();
66         try {
67             URL JavaDoc url= getExternalLocation(node);
68             if (url != null) {
69                 AntUtil.openBrowser(url.toString(), shell, getTitle());
70             }
71         } catch (MalformedURLException JavaDoc e) {
72            AntUIPlugin.log(e);
73         }
74     }
75     
76     public URL JavaDoc getExternalLocation(AntElementNode node) throws MalformedURLException JavaDoc {
77         URL JavaDoc baseLocation= getBaseLocation();
78         if (baseLocation == null) {
79             return null;
80         }
81
82         String JavaDoc urlString= baseLocation.toExternalForm();
83
84         StringBuffer JavaDoc pathBuffer= new StringBuffer JavaDoc(urlString);
85         if (!urlString.endsWith("/")) { //$NON-NLS-1$
86
pathBuffer.append('/');
87         }
88
89         if (node instanceof AntProjectNode) {
90             pathBuffer.append("using.html#projects"); //$NON-NLS-1$
91
} else if (node instanceof AntTargetNode) {
92             pathBuffer.append("using.html#targets"); //$NON-NLS-1$
93
} else if (node instanceof AntTaskNode) {
94             AntTaskNode taskNode= (AntTaskNode) node;
95             if (fEditor.getAntModel().getDefininingTaskNode(taskNode.getTask().getTaskName()) == null) {
96                 //not a user defined task
97
appendTaskPath(taskNode, pathBuffer);
98             }
99         }
100
101         try {
102             return new URL JavaDoc(pathBuffer.toString());
103         } catch (MalformedURLException JavaDoc e) {
104             AntUIPlugin.log(e);
105         }
106         return null;
107     }
108
109     private void appendTaskPath(AntTaskNode node, StringBuffer JavaDoc buffer) {
110         String JavaDoc taskName= node.getTask().getTaskName();
111         String JavaDoc taskPart= null;
112         if (taskName.equalsIgnoreCase("path")) { //$NON-NLS-1$
113
buffer.append("using.html#path"); //$NON-NLS-1$
114
return;
115         }
116         taskPart= getTaskTypePart(node);
117         if (taskPart == null) {
118             return;
119         }
120         buffer.append(taskPart);
121         buffer.append('/');
122         buffer.append(taskName);
123         buffer.append(".html"); //$NON-NLS-1$
124
}
125
126     private URL JavaDoc getBaseLocation() throws MalformedURLException JavaDoc {
127         IPreferenceStore prefs = AntUIPlugin.getDefault().getPreferenceStore();
128         String JavaDoc base= prefs.getString(IAntUIPreferenceConstants.DOCUMENTATION_URL);
129         return new URL JavaDoc(base);
130     }
131     
132     private String JavaDoc getTitle() {
133         return AntEditorActionMessages.getString("OpenExternalDocAction.0"); //$NON-NLS-1$
134
}
135
136     /* (non-Javadoc)
137      * @see org.eclipse.ui.IEditorActionDelegate#setActiveEditor(org.eclipse.jface.action.IAction, org.eclipse.ui.IEditorPart)
138      */

139     public void setActiveEditor(IAction action, IEditorPart targetEditor) {
140         fEditor= (AntEditor) targetEditor;
141         if (fEditor != null) {
142             IHandlerService handlerService= (IHandlerService) fEditor.getSite().getService(IHandlerService.class);
143             handlerService.activateHandler(COMMAND_ID, new ActionHandler(this));
144         }
145     }
146
147     /* (non-Javadoc)
148      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
149      */

150     public void run(IAction action) {
151         ISelection selection= fEditor.getSelectionProvider().getSelection();
152         AntElementNode node= null;
153         if (selection instanceof ITextSelection) {
154             ITextSelection textSelection= (ITextSelection)selection;
155             int textOffset= textSelection.getOffset();
156             AntModel model= fEditor.getAntModel();
157             if (model != null) {
158                 node= model.getNode(textOffset, false);
159             }
160             if (node != null) {
161                 doAction(node);
162             }
163         }
164     }
165
166
167     /* (non-Javadoc)
168      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
169      */

170     public void selectionChanged(IAction action, ISelection selection) {
171     }
172     
173     
174     private String JavaDoc getTaskTypePart(AntTaskNode node) {
175         AntProjectNode projectNode= node.getProjectNode();
176         if (projectNode != null) {
177             Project antProject= projectNode.getProject();
178             AntTypeDefinition definition= ComponentHelper.getComponentHelper(antProject).getDefinition(node.getTask().getTaskName());
179             if (definition == null) {
180                 return null;
181             }
182             String JavaDoc className= definition.getClassName();
183             if (className.indexOf("taskdef") != -1) { //$NON-NLS-1$
184
if (className.indexOf("optional") != -1) { //$NON-NLS-1$
185
return "OptionalTasks"; //$NON-NLS-1$
186
}
187                 return "CoreTasks"; //$NON-NLS-1$
188
} else if (className.indexOf("optional") != -1) { //$NON-NLS-1$
189
return "OptionalTypes"; //$NON-NLS-1$
190
} else {
191                 return "CoreTypes"; //$NON-NLS-1$
192
}
193         }
194         
195         return null;
196     }
197     /* (non-Javadoc)
198      * @see org.eclipse.jface.action.IAction#run()
199      */

200     public void run() {
201         run(null);
202     }
203 }
204
Popular Tags