View Javadoc

1   /*
2    * Created on 06.12.2004
3    *
4    */
5   package org.apache.commons.jelly.eclipse.ui;
6   
7   
8   import org.eclipse.core.resources.IFile;
9   import org.eclipse.core.resources.IProject;
10  import org.eclipse.core.runtime.CoreException;
11  import org.eclipse.debug.core.DebugPlugin;
12  import org.eclipse.debug.core.ILaunchConfiguration;
13  import org.eclipse.debug.core.ILaunchConfigurationType;
14  import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
15  import org.eclipse.debug.core.ILaunchManager;
16  import org.eclipse.debug.ui.ILaunchShortcut;
17  import org.eclipse.jface.viewers.ISelection;
18  import org.eclipse.jface.viewers.IStructuredSelection;
19  import org.eclipse.ui.IEditorPart;
20  
21  
22  /***
23   * Launcher shortcut implementation for the Eclipse 3 platform
24   * @author Juergen Mayrbaeurl
25   * @since 0.9
26   */
27  public class JellyLauncherShortcutImpl implements ILaunchShortcut 
28  {
29  	/***
30  	 * Default constructor
31  	 */
32  	public JellyLauncherShortcutImpl() {
33  		super();
34  	}
35  
36  	// ILaunchshortcut Interface implementation
37  	/* (non-Javadoc)
38  	 * @see org.eclipse.debug.ui.ILaunchShortcut#launch(org.eclipse.jface.viewers.ISelection, java.lang.String)
39  	 */
40  	public void launch(ISelection selection, String mode) 
41  	{		
42  		// Only structured selections are supported
43  		if (selection instanceof IStructuredSelection) {
44  			searchAndLaunch(((IStructuredSelection)selection).toArray(), mode);
45  		} 
46  	}
47  	
48  	// IMPLEMENTATION
49  	/***
50  	 * Launches the first entry in the <code>search</code> array of IFile entries
51  	 * @param search array of IFile entries (selected project Jelly script file)
52  	 * @param mode run or debug (selected by user)
53  	 */
54  	protected void searchAndLaunch(Object[] search, String mode) 
55  	{
56  		if (search != null) 
57  		{
58  			// We'll launch the first IFile entry, if it exists
59  			if (search.length == 0) {
60  				return;
61  			}	
62  			
63  			// Get the first entry in the array and check if it's a file resource
64  			Object object = search[0];
65  		    if (object instanceof IFile) 
66  		    {
67  		         IFile file = (IFile) object;
68  		         this.launch(file, mode);
69  		    }
70  		}
71  	}
72  
73  	/* (non-Javadoc)
74  	 * @see org.eclipse.debug.ui.ILaunchShortcut#launch(org.eclipse.ui.IEditorPart, java.lang.String)
75  	 */
76  	public void launch(IEditorPart arg0, String arg1) 
77  	{
78  		// Launching from the editor is currently not supported
79  		JellyLauncherPlugin.getDefault().showNotImplemented();
80  	}
81  
82  	/***
83  	 * Launches Jelly with the specified Jelly script file and user selected mode
84  	 * @param script Jelly script file
85  	 * @param mode run or debug mode, selected by the user
86  	 */
87  	private void launch(IFile script, String mode)
88  	{
89  		try {
90  			// Find or create launch configuration for the Jelly script file
91  			ILaunchConfiguration config = this.findLaunchConfiguration(script, mode);
92  			if(config != null)
93  				config.launch(mode, null);
94  		} catch (CoreException e) {
95  			// Log and show error message
96  			JellyLauncherPlugin.getDefault().showErrorForException(JellyLaunchConfigurationConstants.ERR_LAUNCHING_SHORTCUT, e);
97  		}
98  	}
99  	
100 	/***
101 	 * Finds the launch configuration for the Jelly script file. If no launch configuration
102 	 * exists, a new launch configuration will be created and added to the launch
103 	 * configuration types list.
104 	 * @param script Jelly script file
105 	 * @param mode run or debug
106 	 * @return the Eclipse launch configuration for the Jelly script
107 	 * @throws CoreException Eclipse platform exception
108 	 */
109 	private ILaunchConfiguration findLaunchConfiguration(IFile script, String mode)
110 		throws CoreException
111 	{
112 		// Get the launch manager from the Debug plugin of Eclipse
113 		ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
114 		// Get the launch configuration type for Jelly
115 		ILaunchConfigurationType type = manager.getLaunchConfigurationType(JellyLauncherPlugin.getResourceString("jelly.launchConfigurationType"));
116 		
117 		// Create the absolute file path for the Jelly script file
118 		String loc = script.getLocation().toOSString();
119 		
120 		// Now check, if there's already a launch configuration for the Jelly script
121 		// This is done, by comparing the absolute file path of the Jelly script
122 		ILaunchConfiguration[] configurations = manager.getLaunchConfigurations(type);
123 		for(int i = 0; i < configurations.length; i++)
124 		{
125 			ILaunchConfiguration config = configurations[i];
126 			if(config.getAttribute(JellyLaunchConfigurationConstants.ATTR_JELLYSCRIPT_PATH, "").equals(loc))
127 			{
128 				return config;
129 			}
130 		} 
131 		
132 		// It seems, there's no existing launch configuration for the Jelly script
133 		// Therefore we'll create a new one for the Jelly script
134 		ILaunchConfigurationWorkingCopy workingCopy = type.newInstance(null, this.createLaunchConfigurationname(script, mode));
135 		
136 		// Set the defaults for the launch configuration of the Jelly script
137 		JellyLaunchUtils.setDefaults(workingCopy, script);
138 		// Store the absolute file path of the Jelly script in the appropriate attribute of the launch configuration
139 		workingCopy.setAttribute(JellyLaunchConfigurationConstants.ATTR_JELLYSCRIPT_PATH, loc);
140 
141 		// Save the new launch configuration for the Jelly script
142 		return workingCopy.doSave();
143 	}
144 	
145 	/***
146 	 * Creates a name/title for the launch configuration of the Jelly script, that
147 	 * gets displayed in the launch configuration dialog and various menus. The name
148 	 * is created by taking the project name and appending the Jelly script file
149 	 * name, separated by a dot.
150 	 * @param script Jelly script file
151 	 * @param mode run or debug
152 	 * @return name of the launch configuration
153 	 */
154 	private String createLaunchConfigurationname(IFile script, String mode)
155 	{
156 		String result = script.getFullPath().toString();
157 		
158 		// If the Jelly script file doesn't belong to a project, simply use the full path
159 		IProject proj = script.getProject();
160 		if(proj != null)
161 			result = proj.getName() + "." + script.getName();
162 		
163 		return result;
164 	}
165 }