001: package org.enhydra.kelp.eclipse.actions;
002:
003: import java.util.HashMap;
004: import org.eclipse.core.resources.IProject;
005: import org.eclipse.core.resources.IResource;
006: import org.eclipse.jface.action.IAction;
007: import org.eclipse.ui.IWorkbenchWindow;
008: import org.eclipse.ui.externaltools.internal.registry.ExternalToolMigration;
009: import org.eclipse.ui.externaltools.internal.model.IExternalToolConstants;
010: import org.enhydra.tool.ToolBoxInfo;
011: import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
012: import org.eclipse.debug.core.ILaunchConfigurationType;
013: import org.eclipse.debug.ui.IDebugUIConstants;
014: import org.eclipse.debug.core.DebugPlugin;
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.debug.core.ILaunchConfiguration;
017: import org.eclipse.debug.core.ILaunchManager;
018: import org.eclipse.debug.ui.DebugUITools;
019: import java.text.MessageFormat;
020: import org.eclipse.jface.dialogs.MessageDialog;
021: import org.eclipse.debug.internal.ui.actions.ActionMessages;
022: import org.eclipse.debug.internal.ui.DebugUIPlugin;
023: import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationsDialog;
024: import org.eclipse.core.resources.IWorkspace;
025: import org.eclipse.core.resources.IWorkspaceDescription;
026: import org.eclipse.core.resources.ResourcesPlugin;
027: import org.eclipse.debug.internal.ui.actions.RunLastAction;
028:
029: /**
030: * Insert the type's description here.
031: * @see IWorkbenchWindowActionDelegate
032: */
033: public class AntRebuildAction extends RunLastAction {
034:
035: /**
036: * Insert the method's description here.
037: * @see IWorkbenchWindowActionDelegate#run
038: */
039: public void run(IAction action) {
040: IProject project = null;
041:
042: try {
043: IResource res = DebugUITools.getSelectedResource();
044: project = res.getProject();
045:
046: if (project != null) {
047: String location = ToolBoxInfo.getEnhydraRoot()
048: + "/bin/enhydra-ant";
049: if (System.getProperty("os.name").toLowerCase()
050: .startsWith("windows"))
051: location += ".bat";
052:
053: ILaunchConfiguration conf;
054:
055: try {
056: ILaunchManager manager = DebugPlugin.getDefault()
057: .getLaunchManager();
058:
059: ILaunchConfigurationType configType = manager
060: .getLaunchConfigurationType(IExternalToolConstants.ID_PROGRAM_BUILDER_LAUNCH_CONFIGURATION_TYPE);
061: ILaunchConfigurationWorkingCopy config = configType
062: .newInstance(null, "enhydra-ant rebuild");
063: ;
064:
065: config.setAttribute(
066: IExternalToolConstants.ATTR_LOCATION,
067: location);
068: config.setAttribute(
069: IExternalToolConstants.ATTR_TOOL_ARGUMENTS,
070: "rebuild");
071:
072: config.setAttribute(
073: IExternalToolConstants.ATTR_CAPTURE_OUTPUT,
074: "true");
075: config.setAttribute(
076: DebugPlugin.ATTR_CAPTURE_OUTPUT, true);
077: config.setAttribute(
078: IDebugUIConstants.ATTR_CAPTURE_IN_CONSOLE,
079: true);
080:
081: config.setAttribute(
082: IExternalToolConstants.ATTR_SHOW_CONSOLE,
083: true);
084: config
085: .setAttribute(
086: IDebugUIConstants.ATTR_LAUNCH_IN_BACKGROUND,
087: false);
088:
089: StringBuffer buffer = new StringBuffer(
090: IExternalToolConstants.BUILD_TYPE_FULL);
091: buffer.append(',');
092: buffer
093: .append(IExternalToolConstants.BUILD_TYPE_INCREMENTAL);
094: buffer.append(',');
095: config
096: .setAttribute(
097: IExternalToolConstants.ATTR_RUN_BUILD_KINDS,
098: (String) null);
099: config
100: .setAttribute(
101: IExternalToolConstants.ATTR_WORKING_DIRECTORY,
102: "${workspace_loc:/"
103: + project.getName() + "}");
104:
105: conf = config;
106: } catch (Exception cex) {
107: HashMap args = new HashMap();
108: args.put("!{tool_name}", "ant rebuild");
109: args
110: .put("!{tool_type}",
111: "org.eclipse.ui.externaltools.type.program");
112: args.put("!{tool_args}", "rebuild");
113: args.put("!{tool_show_log}", "true");
114: args.put("!{tool_dir}", "${workspace_loc:/"
115: + project.getName() + "}");
116: args.put("!{tool_loc}", location);
117: args.put("!{tool_block}", "true");
118:
119: conf = ExternalToolMigration
120: .configFrom20ArgumentMap(args);
121: }
122:
123: if (conf != null) {
124: if (conf.supportsMode(ILaunchManager.RUN_MODE)) {
125: DebugUITools.launch(conf,
126: ILaunchManager.RUN_MODE);
127: } else {
128: String configName = conf.getName();
129: String title = ActionMessages.RelaunchLastAction_Cannot_relaunch_1; //$NON-NLS-1$
130: String message = MessageFormat
131: .format(
132: ActionMessages.RelaunchLastAction_Cannot_relaunch___0___because_it_does_not_support__2__mode_2,
133: new String[] { configName,
134: getMode() }); //$NON-NLS-1$
135: MessageDialog.openError(getShell(), title,
136: message);
137: }
138: } else {
139: // If the history is empty, just open the launch config dialog
140: openLaunchConfigurationDialog();
141: }
142:
143: } else
144: DebugUIPlugin
145: .errorDialog(
146: getShell(),
147: "Error getting application project",
148: "Please, sellect valid application project before starting this action!",
149: new Exception("Project is null!"));
150:
151: } catch (CoreException ce) {
152: DebugUIPlugin
153: .errorDialog(
154: getShell(),
155: "Error relaunching", "Error encountered while attempting to launch enhydra-ant rebild action", ce); //$NON-NLS-1$ //$NON-NLS-2$
156: }
157: }
158:
159: /**
160: * Open the launch configuration dialog, passing in the current workbench selection.
161: */
162: private void openLaunchConfigurationDialog() {
163: IWorkbenchWindow dwindow = DebugUIPlugin
164: .getActiveWorkbenchWindow();
165: if (dwindow == null) {
166: return;
167: }
168: LaunchConfigurationsDialog dialog = new LaunchConfigurationsDialog(
169: DebugUIPlugin.getShell(), DebugUIPlugin.getDefault()
170: .getLaunchConfigurationManager()
171: .getDefaultLaunchGroup(getMode()));
172: dialog
173: .setOpenMode(LaunchConfigurationsDialog.LAUNCH_CONFIGURATION_DIALOG_OPEN_ON_LAST_LAUNCHED);
174: dialog.open();
175: }
176:
177: /**
178: * Turns autobuilding on or off in the workspace.
179: */
180: private void setAutobuild(boolean newState) throws CoreException {
181: IWorkspace workspace = ResourcesPlugin.getWorkspace();
182: IWorkspaceDescription wsDescription = workspace
183: .getDescription();
184: boolean oldState = wsDescription.isAutoBuilding();
185: if (oldState != newState) {
186: wsDescription.setAutoBuilding(newState);
187: workspace.setDescription(wsDescription);
188: }
189: }
190: }
|