1 11 package org.eclipse.ant.internal.ui.editor.actions; 12 13 import java.net.MalformedURLException ; 14 import java.net.URL ; 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 42 public class OpenExternalDocAction extends Action implements IEditorActionDelegate { 43 44 private static final String COMMAND_ID = "org.eclipse.ant.ui.openExternalDoc"; 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")); setDescription(AntEditorActionMessages.getString("OpenExternalDocAction.2")); setToolTipText(AntEditorActionMessages.getString("OpenExternalDocAction.2")); } 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 url= getExternalLocation(node); 68 if (url != null) { 69 AntUtil.openBrowser(url.toString(), shell, getTitle()); 70 } 71 } catch (MalformedURLException e) { 72 AntUIPlugin.log(e); 73 } 74 } 75 76 public URL getExternalLocation(AntElementNode node) throws MalformedURLException { 77 URL baseLocation= getBaseLocation(); 78 if (baseLocation == null) { 79 return null; 80 } 81 82 String urlString= baseLocation.toExternalForm(); 83 84 StringBuffer pathBuffer= new StringBuffer (urlString); 85 if (!urlString.endsWith("/")) { pathBuffer.append('/'); 87 } 88 89 if (node instanceof AntProjectNode) { 90 pathBuffer.append("using.html#projects"); } else if (node instanceof AntTargetNode) { 92 pathBuffer.append("using.html#targets"); } else if (node instanceof AntTaskNode) { 94 AntTaskNode taskNode= (AntTaskNode) node; 95 if (fEditor.getAntModel().getDefininingTaskNode(taskNode.getTask().getTaskName()) == null) { 96 appendTaskPath(taskNode, pathBuffer); 98 } 99 } 100 101 try { 102 return new URL (pathBuffer.toString()); 103 } catch (MalformedURLException e) { 104 AntUIPlugin.log(e); 105 } 106 return null; 107 } 108 109 private void appendTaskPath(AntTaskNode node, StringBuffer buffer) { 110 String taskName= node.getTask().getTaskName(); 111 String taskPart= null; 112 if (taskName.equalsIgnoreCase("path")) { buffer.append("using.html#path"); 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"); } 125 126 private URL getBaseLocation() throws MalformedURLException { 127 IPreferenceStore prefs = AntUIPlugin.getDefault().getPreferenceStore(); 128 String base= prefs.getString(IAntUIPreferenceConstants.DOCUMENTATION_URL); 129 return new URL (base); 130 } 131 132 private String getTitle() { 133 return AntEditorActionMessages.getString("OpenExternalDocAction.0"); } 135 136 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 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 170 public void selectionChanged(IAction action, ISelection selection) { 171 } 172 173 174 private String 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 className= definition.getClassName(); 183 if (className.indexOf("taskdef") != -1) { if (className.indexOf("optional") != -1) { return "OptionalTasks"; } 187 return "CoreTasks"; } else if (className.indexOf("optional") != -1) { return "OptionalTypes"; } else { 191 return "CoreTypes"; } 193 } 194 195 return null; 196 } 197 200 public void run() { 201 run(null); 202 } 203 } 204 | Popular Tags |