Skip to content

Commit

Permalink
JBIDE-21791: Prompt user to save open current open editor before exec…
Browse files Browse the repository at this point in the history
…uting command

This change introduces a new preference option in the Forge preference page.
  • Loading branch information
gastaldi committed Mar 8, 2016
1 parent 76e6beb commit b96c2bb
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class ForgeCorePreferences {
public static final String PREF_FORGE_START_IN_DEBUG = "org.jboss.tools.forge.core.startInDebug";
public static final String PREF_FORGE_VM_ARGS = "org.jboss.tools.forge.core.vmArgs";
private static final String PREF_FORGE_ADDON_DIR = "org.jboss.tools.forge.ext.core.addon_dir";
private static final String PREF_FORGE_SAVE_ON_COMMAND_MENU = "org.jboss.tools.forge.ext.core.save_on_command_menu";

public static final ForgeCorePreferences INSTANCE = new ForgeCorePreferences();

Expand All @@ -67,8 +68,7 @@ public ForgeRuntime[] getRuntimes() {
if (runtimes == null) {
initializeRuntimes();
}
return (ForgeRuntime[]) runtimes.toArray(new ForgeRuntime[runtimes
.size()]);
return runtimes.toArray(new ForgeRuntime[runtimes.size()]);
}

public ForgeRuntime getDefaultRuntime() {
Expand All @@ -84,8 +84,7 @@ public boolean getStartup() {
}

public boolean getStartInDebug() {
return getForgeCorePreferences().getBoolean(PREF_FORGE_START_IN_DEBUG,
false);
return getForgeCorePreferences().getBoolean(PREF_FORGE_START_IN_DEBUG, false);
}

public String getVmArgs() {
Expand All @@ -107,7 +106,7 @@ private void initializeRuntimes() {
addFromXml(getForgeRuntimesPreference());
initializeDefaultRuntime();
}

private void initializeDefaultRuntime() {
for (ForgeRuntime runtime : runtimes) {
if (runtime.getName().equals(defaultRuntimeName)) {
Expand All @@ -123,9 +122,11 @@ private void initializeDefaultRuntime() {

private void addFromXml(String xml) {
DocumentBuilder documentBuilder = newDocumentBuilder();
if (documentBuilder == null) return;
if (documentBuilder == null)
return;
InputStream inputStream = createInputStream(xml);
if (inputStream == null) return;
if (inputStream == null)
return;
Document document = parseRuntimes(documentBuilder, inputStream);
Element runtimeElement = document.getDocumentElement();
defaultRuntimeName = runtimeElement.getAttribute("default");
Expand All @@ -135,8 +136,7 @@ private void addFromXml(String xml) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
String type = element.getAttribute("type").toUpperCase();
if (ForgeRuntimeType.valueOf(type).equals(
ForgeRuntimeType.EXTERNAL)) {
if (ForgeRuntimeType.valueOf(type).equals(ForgeRuntimeType.EXTERNAL)) {
String name = element.getAttribute("name");
String location = element.getAttribute("location");
runtimes.add(new ForgeExternalRuntime(name, location));
Expand All @@ -145,8 +145,7 @@ private void addFromXml(String xml) {
}
}

private Document parseRuntimes(DocumentBuilder documentBuilder,
InputStream inputStream) {
private Document parseRuntimes(DocumentBuilder documentBuilder, InputStream inputStream) {
Document result = null;
try {
result = documentBuilder.parse(inputStream);
Expand All @@ -161,8 +160,7 @@ private Document parseRuntimes(DocumentBuilder documentBuilder,
private InputStream createInputStream(String string) {
InputStream result = null;
try {
result = new BufferedInputStream(new ByteArrayInputStream(
string.getBytes("UTF8")));
result = new BufferedInputStream(new ByteArrayInputStream(string.getBytes("UTF8")));
} catch (UnsupportedEncodingException e) {
ForgeCorePlugin.log(e);
}
Expand Down Expand Up @@ -217,8 +215,19 @@ public void setVmArgs(String vmArgsText) {
}
}

private void setBoolean(String prefKey, boolean startup)
throws BackingStoreException {
public void setSaveOnCommandMenu(boolean saveOnMenu) {
try {
setBoolean(PREF_FORGE_SAVE_ON_COMMAND_MENU, saveOnMenu);
} catch (BackingStoreException e) {
ForgeCorePlugin.log(e);
}
}

public boolean isSaveOnCommandMenu() {
return getForgeCorePreferences().getBoolean(PREF_FORGE_SAVE_ON_COMMAND_MENU, false);
}

private void setBoolean(String prefKey, boolean startup) throws BackingStoreException {
IEclipsePreferences eclipsePreferences = getForgeCorePreferences();
eclipsePreferences.putBoolean(prefKey, startup);
eclipsePreferences.flush();
Expand All @@ -228,8 +237,7 @@ public void addPreferenceChangeListener(IPreferenceChangeListener listener) {
getForgeCorePreferences().addPreferenceChangeListener(listener);
}

public void removePreferenceChangeListener(
IPreferenceChangeListener listener) {
public void removePreferenceChangeListener(IPreferenceChangeListener listener) {
getForgeCorePreferences().removePreferenceChangeListener(listener);
}

Expand Down Expand Up @@ -276,8 +284,7 @@ private Document createEmptyDocument() {
}
}

private static String serializeDocument(Document doc)
throws TransformerException, IOException {
private static String serializeDocument(Document doc) throws TransformerException, IOException {
ByteArrayOutputStream s = new ByteArrayOutputStream();
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
Expand All @@ -292,8 +299,7 @@ private static String serializeDocument(Document doc)
public String getAddonDir() {
IEclipsePreferences prefs = getForgeCorePreferences();
return prefs.get(PREF_FORGE_ADDON_DIR,
new File(OperatingSystemUtils.getUserForgeDir(), "addons")
.getAbsolutePath());
new File(OperatingSystemUtils.getUserForgeDir(), "addons").getAbsolutePath());
}

public void setAddonDir(String addonDir) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import org.jboss.tools.forge.core.furnace.FurnaceRuntime;
Expand All @@ -24,37 +27,46 @@
import org.jboss.tools.forge.ui.util.ForgeHelper;

public class ForgeCommandHandler extends AbstractHandler {

@Override
public Object execute(ExecutionEvent event) {
ForgeRuntime runtime = ForgeCorePreferences.INSTANCE.getDefaultRuntime();
if (runtime == FurnaceRuntime.INSTANCE) {
handleFurnace(event);
} else {
startForgeRuntime(runtime);
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
if (saveCurrentEditor(window)) {
ForgeRuntime runtime = ForgeCorePreferences.INSTANCE.getDefaultRuntime();
if (runtime == FurnaceRuntime.INSTANCE) {
handleFurnace(window);
} else {
startForgeRuntime(runtime);
}
}
return null;
}

private void handleFurnace(ExecutionEvent event) {

private boolean saveCurrentEditor(IWorkbenchWindow window) throws ExecutionException {
IWorkbenchPage page = window.getActivePage();
boolean result = true;
if (page != null) {
IEditorPart editor = page.getActiveEditor();
if (editor != null) {
boolean confirm = (ForgeCorePreferences.INSTANCE.isSaveOnCommandMenu() == false);
result = page.saveEditor(editor, confirm);
}
}
return result;
}

private void handleFurnace(IWorkbenchWindow window) {
try {
ForgeConsoleView forgeConsoleView = ForgeHelper.findForgeConsoleView();
if (forgeConsoleView != null && forgeConsoleView.isShowing()) {
ForgeHelper.showRuntime(FurnaceRuntime.INSTANCE);
}
final IWorkbenchWindow window = HandlerUtil
.getActiveWorkbenchWindowChecked(event);
if (!ForgeRuntimeState.RUNNING.equals(FurnaceRuntime.INSTANCE.getState())) {
Job job = ForgeHelper.createStartRuntimeJob(FurnaceRuntime.INSTANCE);
job.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
new UICommandListDialog(window).open();
}
});
Display.getDefault().asyncExec(() -> new UICommandListDialog(window).open());
}
});
job.schedule();
Expand All @@ -65,12 +77,12 @@ public void run() {
ForgeUIPlugin.log(e);
}
}

private void startForgeRuntime(ForgeRuntime runtime) {
ForgeHelper.showForgeConsole(runtime);
if (!ForgeRuntimeState.RUNNING.equals(runtime.getState())) {
ForgeHelper.start(runtime);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,26 @@
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.jboss.tools.forge.core.preferences.ForgeCorePreferences;

public class ForgeMainPreferencePage extends PreferencePage implements
IWorkbenchPreferencePage {

public class ForgeMainPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {

private Text addonDirText;

private Button saveOnCommandMenu;

@Override
protected Control createContents(Composite parent) {
noDefaultAndApplyButton();
Composite clientArea = createClientArea(parent);
createAddonDirText(clientArea);
createSaveOnCommandMenu(clientArea);
return clientArea;
}



private void createSaveOnCommandMenu(Composite parent) {
saveOnCommandMenu = new Button(parent, SWT.CHECK);
saveOnCommandMenu.setText("Automatically save the active dirty editor before launching the command menu");
saveOnCommandMenu.setSelection(ForgeCorePreferences.INSTANCE.isSaveOnCommandMenu());
}

private void createAddonDirText(Composite parent) {
Label addonDirLabel = new Label(parent, SWT.NONE);
addonDirLabel.setText("Forge Addon Repository Location: ");
Expand All @@ -57,8 +64,7 @@ private void createAddonDirText(Composite parent) {
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
DirectoryDialog dialog = new DirectoryDialog(getShell(),
SWT.OPEN);
DirectoryDialog dialog = new DirectoryDialog(getShell(), SWT.OPEN);
dialog.setText("Select a directory");
dialog.setFilterPath(addonDirText.getText());
String selectedPath = dialog.open();
Expand All @@ -79,10 +85,14 @@ private Composite createClientArea(Composite parent) {
return clientArea;
}

public void init(IWorkbench workbench) {}

@Override
public void init(IWorkbench workbench) {
}

@Override
public boolean performOk() {
ForgeCorePreferences.INSTANCE.setAddonDir(addonDirText.getText());
ForgeCorePreferences.INSTANCE.setSaveOnCommandMenu(saveOnCommandMenu.getSelection());
return true;
}
}

0 comments on commit b96c2bb

Please sign in to comment.