001: /**
002: * Name........: ImportButton
003: * Description.: Button allowing to import an Eclipse project configuration.
004: * Author......: Daniel Kasmeroglu
005: * E-Mail......: costamojan@users.sourceforge.net
006: */package abbot.editor.editors;
007:
008: import net.sf.ant4eclipse.tools.resolver.*;
009: import net.sf.ant4eclipse.tools.*;
010:
011: import net.sf.ant4eclipse.model.project.*;
012: import net.sf.ant4eclipse.model.launch.*;
013: import net.sf.ant4eclipse.model.*;
014:
015: import abbot.script.*;
016: import abbot.i18n.*;
017:
018: import javax.swing.*;
019:
020: import java.awt.event.*;
021:
022: import java.io.*;
023:
024: /**
025: * Button allowing to import an Eclipse project configuration.
026: */
027: public class ImportButton extends JButton implements ActionListener {
028:
029: private JFileChooser launchfilechooser;
030: private JFileChooser workspacefilechooser;
031: private LaunchFileFilter launchfilefilter;
032: private WorkspaceFileFilter workspacefilefilter;
033: private LaunchEditor editor;
034: private Launch launch;
035:
036: /**
037: * Initialises this button which will be placed on the supplied editor.
038: *
039: * @param launcheditor
040: * The editor which receives this button.
041: * @param launchstep
042: * The step that will be edited.
043: */
044: public ImportButton(LaunchEditor launcheditor, Launch launchstep) {
045: super (Strings.get("eclipse.import"));
046: editor = launcheditor;
047: launch = launchstep;
048: launchfilechooser = new JFileChooser();
049: workspacefilechooser = new JFileChooser();
050: launchfilefilter = new LaunchFileFilter();
051: launchfilechooser.setFileFilter(launchfilefilter);
052: workspacefilechooser.setFileFilter(workspacefilefilter);
053: workspacefilechooser
054: .setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
055: addActionListener(this );
056: }
057:
058: /**
059: * {@inheritDoc}
060: */
061: public void actionPerformed(ActionEvent evt) {
062: if (importEclipse()) {
063: editor.fireStepChanged();
064: }
065: }
066:
067: /**
068: * Enforces the user to select the workspace.
069: *
070: * @param projectname
071: * The name of the project used within the launch configuration.
072: * @return The location of the workspace or null if none has been selected.
073: */
074: private File selectWorkspace(String projectname) {
075: workspacefilefilter.setProjectName(projectname);
076: File result = null;
077: int option = workspacefilechooser.showOpenDialog(null);
078: if (option == JFileChooser.APPROVE_OPTION) {
079: result = workspacefilechooser.getSelectedFile();
080: }
081: return (result);
082: }
083:
084: /**
085: * Locates the workspace directory which may be parental to the Java launch
086: * configuration.
087: *
088: * @param file
089: * The directory to start from.
090: * @return The location of the workspace directory or null.
091: */
092: private File locateWorkspace(File file) {
093: if (file == null) {
094: return (null);
095: }
096: File metadata = new File(file, ".metadata");
097: if (metadata.isDirectory()) {
098: return (file);
099: }
100: return (locateWorkspace(file.getParentFile()));
101: }
102:
103: /**
104: * Runs the import process of an Eclipse launch configuration.
105: *
106: * @return true <=> A launch configuration could be imported.
107: */
108: private boolean importEclipse() {
109:
110: boolean result = false;
111:
112: // load the launch configuration first
113: JavaApplicationLaunchConfiguration launchconfig = selectLaunchConfiguration();
114: if (launchconfig != null) {
115:
116: // try to locate the workspace (potentially a parental directory)
117: File workspacedir = locateWorkspace(launchconfig
118: .getLaunchFile().getParentFile());
119: if (workspacedir == null) {
120: // the user must select the workspace directory
121: workspacedir = selectWorkspace(launchconfig
122: .getProjectName());
123: }
124:
125: if (workspacedir != null) {
126:
127: // try to resolve the classpath
128: Workspace workspace = Workspace
129: .getWorkspace(workspacedir);
130: String cp = loadClasspath(workspace, launchconfig);
131: File projectlocation = workspace.getChild(launchconfig
132: .getProjectName());
133:
134: String progargs = launchconfig.getProgramArguments();
135: if (progargs == null) {
136: progargs = "";
137: }
138: String[] splitted = progargs.split(" ");
139: StringBuffer argsbuffer = new StringBuffer();
140: for (int i = 0; i < splitted.length; i++) {
141: if (i > 0) {
142: argsbuffer.append(" ");
143: }
144: File file = new File(projectlocation, splitted[i]);
145: if (file.exists()) {
146: splitted[i] = "[" + file.getAbsolutePath()
147: + "]";
148: } else {
149: splitted[i] = "[" + splitted[i] + "]";
150: }
151: argsbuffer.append(splitted[i]);
152: }
153:
154: // modify the step
155: launch.setArguments(argsbuffer.toString());
156: launch.setClasspath(cp);
157: launch.setTargetClassName(launchconfig.getMainType());
158:
159: // ant4eclipse assures the File.pathSeparator
160: editor.classpath
161: .setValues(cp.split(File.pathSeparator));
162: editor.target.setText(launchconfig.getMainType());
163: editor.arguments.setValues(splitted);
164:
165: result = true;
166:
167: }
168:
169: }
170:
171: return (result);
172:
173: }
174:
175: /**
176: * Resolves the classpath from a Java launch configuration.
177: *
178: * @param workspace
179: * The workspace which is used for the Eclipse project.
180: * @param launchconfig
181: * The launch configuration providing the necessary data.
182: * @return The resolved classpath.
183: * @pre workspace != null
184: * @pre launchconfig != null
185: * @post result != null
186: */
187: private String loadClasspath(Workspace workspace,
188: JavaApplicationLaunchConfiguration launchconfig) {
189: ResolvedPathEntry[] entries = null;
190: try {
191: entries = RuntimeClasspathResolver.resolveRuntimeClasspath(
192: workspace, launchconfig, false);
193: } catch (FileParserException ex) {
194: /**
195: * @todo [15-Apr-2006:KASI] There should be a proper error message
196: * here.
197: */
198: return ("");
199: }
200: StringBuffer buffer = new StringBuffer();
201: boolean added = false;
202: for (int i = 0; i < entries.length; i++) {
203: // we're checking for resolved entries since containers like JRE
204: // don't need to be added here, since Abbot doesn't support specific
205: // runtime information
206: if (entries[i].isResolved()) {
207: if (added) {
208: buffer.append(File.pathSeparator);
209: }
210: buffer.append(entries[i].getResolvedEntryAsFile()
211: .getAbsolutePath());
212: added = true;
213: }
214: }
215: return (buffer.toString());
216: }
217:
218: /**
219: * Selects and loads launch configuration file.
220: *
221: * @return The launching configuration information.
222: */
223: private JavaApplicationLaunchConfiguration selectLaunchConfiguration() {
224: JavaApplicationLaunchConfiguration result = null;
225: int option = launchfilechooser.showOpenDialog(null);
226: if (option == JFileChooser.APPROVE_OPTION) {
227: File location = launchfilechooser.getSelectedFile();
228: if (location.isFile()) {
229: try {
230: LaunchFileParser parser = new LaunchFileParser(
231: location);
232: AbstractLaunchConfiguration config = parser
233: .getLaunchConfiguration();
234: if (config instanceof JavaApplicationLaunchConfiguration) {
235: result = (JavaApplicationLaunchConfiguration) config;
236: }
237: } catch (FileParserException ex) {
238: ex.printStackTrace();
239: }
240: }
241: }
242: return (result);
243: }
244:
245: /**
246: * FileFilter implementation used to select Eclipse launch configuration
247: * files.
248: */
249: private static class LaunchFileFilter extends
250: javax.swing.filechooser.FileFilter {
251:
252: /**
253: * {@inheritDoc}
254: */
255: public boolean accept(File file) {
256: if (file.isDirectory()) {
257: return (true);
258: }
259: /**
260: * @todo [13-Apr-2006:KASI] There should be OS specific comparison
261: * (upper/lower/ignore).
262: */
263: String name = file.getName();
264: return (name.endsWith(".launch"));
265: }
266:
267: /**
268: * {@inheritDoc}
269: */
270: public String getDescription() {
271: return ("Eclipse Launch Configuration (*.launch)");
272: }
273:
274: } /* ENDCLASS */
275:
276: /**
277: * FileFilter implementation used to select an Eclipse workspace.
278: */
279: private static class WorkspaceFileFilter extends
280: javax.swing.filechooser.FileFilter {
281:
282: private String project;
283:
284: private String relative;
285:
286: /**
287: * Initializes this FileFilter which is used to select a Workspace.
288: */
289: public WorkspaceFileFilter() {
290: project = null;
291: relative = null;
292: }
293:
294: /**
295: * Changes the name of the currently used projectname.
296: *
297: * @param newproject
298: * The name of the currently used projectname.
299: * @pre newproject.length() > 0
300: */
301: public void setProjectName(String newproject) {
302: project = newproject;
303: relative = ".plugins/org.eclipse.core.resources/.projects/"
304: + project;
305: }
306:
307: /**
308: * {@inheritDoc}
309: */
310: public boolean accept(File file) {
311: if (file.isDirectory()) {
312: File child = new File(file, ".metadata");
313: if (child.isDirectory()) {
314: File projectdir = new File(child, relative);
315: return (projectdir.isDirectory());
316: }
317: }
318: return (false);
319: }
320:
321: /**
322: * {@inheritDoc}
323: */
324: public String getDescription() {
325: return ("Eclipse Workspace");
326: }
327:
328: } /* ENDCLASS */
329:
330: } /* ENDCLASS */
|