Skip to content
This repository has been archived by the owner on Apr 3, 2018. It is now read-only.

Commit

Permalink
Tool download functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-medeiros committed Dec 28, 2015
1 parent c10233f commit 5e87705
Show file tree
Hide file tree
Showing 19 changed files with 631 additions and 107 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -52,18 +52,23 @@ public Path getSDKToolPath(IProject project) throws CommonException {
return getSDKToolPathValidator().getValidatedPath(getSDKPathPreference(project)); return getSDKToolPathValidator().getValidatedPath(getSDKPathPreference(project));
} }


protected String getSDKPathPreference(IProject project) { public String getSDKPathPreference(IProject project) {
return ToolchainPreferences.SDK_PATH2.getEffectiveValue(project); return ToolchainPreferences.SDK_PATH2.getEffectiveValue(project);
} }


protected abstract PathValidator getSDKToolPathValidator(); public abstract PathValidator getSDKToolPathValidator();


/* ----------------- ----------------- */ /* ----------------- ----------------- */


public ProcessBuilder createSDKProcessBuilder(IProject project, String... sdkOptions) public ProcessBuilder createSDKProcessBuilder(IProject project, String... sdkOptions)
throws CoreException, CommonException { throws CommonException {
Location projectLocation = ResourceUtils.getProjectLocation(project);
Path sdkToolPath = getSDKToolPath(project); Path sdkToolPath = getSDKToolPath(project);
return createToolProcessBuilder(project, sdkToolPath, sdkOptions);
}

public ProcessBuilder createToolProcessBuilder(IProject project, Path sdkToolPath, String... sdkOptions)
throws CommonException {
Location projectLocation = project == null ? null : ResourceUtils.getProjectLocation2(project);
return createToolProcessBuilder(sdkToolPath, projectLocation, sdkOptions); return createToolProcessBuilder(sdkToolPath, projectLocation, sdkOptions);
} }


Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,25 @@
/*******************************************************************************
* Copyright (c) 2015 Bruno Medeiros and other Contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Bruno Medeiros - initial API and implementation
*******************************************************************************/
package melnorme.lang.ide.core.utils.operation;

import java.util.concurrent.Callable;

import org.eclipse.core.runtime.CoreException;

import melnorme.utilbox.concurrency.OperationCancellation;
import melnorme.utilbox.core.CommonException;

public interface CommonOperationCallable<RET> extends Callable<RET> {

@Override
RET call() throws CoreException, CommonException, OperationCancellation;

}
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,22 @@
/*******************************************************************************
* Copyright (c) 2015 Bruno Medeiros and other Contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Bruno Medeiros - initial API and implementation
*******************************************************************************/
package melnorme.lang.ide.core.utils.operation;

import org.eclipse.core.runtime.IProgressMonitor;

import melnorme.utilbox.concurrency.OperationCancellation;
import melnorme.utilbox.core.CommonException;

public interface CommonProgressRunnable {

void run(IProgressMonitor pm) throws CommonException, OperationCancellation;

}
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@


import melnorme.utilbox.concurrency.ICancelMonitor; import melnorme.utilbox.concurrency.ICancelMonitor;


import static melnorme.utilbox.core.Assert.AssertNamespace.assertNotNull;

import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IProgressMonitor;


public final class EclipseCancelMonitor implements ICancelMonitor { public final class EclipseCancelMonitor implements ICancelMonitor {


protected final IProgressMonitor monitor; protected final IProgressMonitor monitor;


public EclipseCancelMonitor(IProgressMonitor monitor) { public EclipseCancelMonitor(IProgressMonitor monitor) {
this.monitor = monitor; this.monitor = assertNotNull(monitor);
} }


@Override @Override
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ public static String getFormattedString(String message, Object... args) {
return MessageFormat.format(message, args); return MessageFormat.format(message, args);
} }


public static String Error =
"Error: ";
public static String InternalError = public static String InternalError =
"Internal Error"; "Internal Error: ";
public static String InternalErrorOccured =
"An internal error has occured.";



public static String LangPlugin_error = public static String LangPlugin_error =
"UI plugin Error"; "UI plugin Error";
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,118 @@
/*******************************************************************************
* Copyright (c) 2015 Bruno Medeiros and other Contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Bruno Medeiros - initial API and implementation
*******************************************************************************/
package melnorme.lang.ide.ui.operations;


import static melnorme.utilbox.core.Assert.AssertNamespace.assertNotNull;

import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IconAndMessageDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import melnorme.lang.ide.core.LangCore;
import melnorme.lang.ide.core.operations.AbstractToolManager;
import melnorme.lang.ide.core.utils.operation.EclipseCancelMonitor;
import melnorme.lang.ide.ui.utils.WorkbenchUtils;
import melnorme.lang.ide.ui.utils.operations.BasicUIOperation;
import melnorme.lang.ide.ui.utils.operations.RunOperationAsJob;
import melnorme.util.swt.SWTFactory;
import melnorme.utilbox.misc.ArrayUtil;

public abstract class StartBundleDownloadOperation extends BasicUIOperation {

protected final AbstractToolManager toolMgr = LangCore.getToolManager();

public StartBundleDownloadOperation(String operationName) {
super(operationName);
}

protected void startProcessUnderJob(ProcessBuilder pb, String toolLocation) {
String cmdLineRender = DebugPlugin.renderArguments(ArrayUtil.createFrom(pb.command(), String.class), null);

startProcessUnderJob(pb, cmdLineRender, toolLocation);
}

protected void startProcessUnderJob(ProcessBuilder pb, String cmdLineRender, String toolLocation) {
boolean confirm = openIntroDialog(cmdLineRender, toolLocation);

if(confirm) {
scheduleDownloadJob(pb);
}
}

protected boolean openIntroDialog(String cmdLineRender, String toolLocation) {
Shell shell = WorkbenchUtils.getActiveWorkbenchShell();
IntroDialog introDialog = new IntroDialog(shell, operationName, cmdLineRender, toolLocation);
boolean confirm = introDialog.open() == Dialog.OK;
return confirm;
}

protected void scheduleDownloadJob(ProcessBuilder pb) {
new RunOperationAsJob(operationName, (pm) -> {
toolMgr.new RunEngineClientOperation(pb, new EclipseCancelMonitor(pm)).runProcess(null);
Display.getDefault().asyncExec(() -> afterDownloadJobCompletes_inUI());
}).schedule();
}

protected void afterDownloadJobCompletes_inUI() {
}

protected static class IntroDialog extends IconAndMessageDialog {

protected final String dialogTitle;
protected final String commandLine;
protected final String binLocation;

public IntroDialog(Shell parentShell, String dialogTitle, String commandLine, String binLocation) {
super(parentShell);
this.dialogTitle = assertNotNull(dialogTitle);
this.commandLine = assertNotNull(commandLine);
this.binLocation = assertNotNull(binLocation);;
}

@Override
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setText(dialogTitle);
}

@Override
protected Image getImage() {
return Display.getCurrent().getSystemImage(SWT.ICON_INFORMATION);
}

@Override
protected Control createDialogArea(Composite parent) {
Composite dialogArea = (Composite) super.createDialogArea(parent);
SWTFactory.createLabel(dialogArea, SWT.NONE,
"The following process will be started in the background:");

SWTFactory.createReadonlyText(dialogArea, commandLine,
GridDataFactory.fillDefaults().hint(600, SWT.DEFAULT).create());

SWTFactory.createLabel(dialogArea, SWT.NONE,
"The executable will be located at:");

SWTFactory.createReadonlyText(dialogArea, binLocation, GridDataFactory.fillDefaults().create());

return dialogArea;
}

}

}
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,88 @@
/*******************************************************************************
* Copyright (c) 2015 Bruno Medeiros and other Contributors.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Bruno Medeiros - initial API and implementation
*******************************************************************************/
package melnorme.lang.ide.ui.preferences.pages;

import static melnorme.utilbox.core.CoreUtil.array;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

import melnorme.lang.ide.ui.utils.ControlUtils;
import melnorme.lang.ide.ui.utils.operations.BasicUIOperation;
import melnorme.util.swt.SWTFactoryUtil;
import melnorme.util.swt.SWTLayoutUtil;
import melnorme.util.swt.SWTUtil;
import melnorme.util.swt.WidgetSelectedRunner;
import melnorme.util.swt.components.fields.ButtonTextField;
import melnorme.util.swt.components.fields.FileTextField;
import melnorme.util.swt.components.fields.SetFieldValueOperation;
import melnorme.utilbox.concurrency.OperationCancellation;

public abstract class DownloadToolTextField extends ButtonTextField {

protected String downloadButtonLabel;
protected Button downloadButton;

public DownloadToolTextField(String label, String downloadButtonLabel) {
this(label, FileTextField.DEFAULT_BUTTON_LABEL, downloadButtonLabel);
}

public DownloadToolTextField(String label, String buttonlabel, String downloadButtonLabel) {
super(label, buttonlabel);
this.downloadButtonLabel = downloadButtonLabel;
}

@Override
public int getPreferredLayoutColumns() {
return 4;
}

@Override
protected void createContents_all(Composite topControl) {
super.createContents_all(topControl);

createContents_DownloadButton(topControl);
}

@Override
protected void createContents_layout() {
SWTLayoutUtil.layoutControls(array(label, text, button, downloadButton), text, text);
}

@Override
protected void doSetEnabled(boolean enabled) {
super.doSetEnabled(enabled);
SWTUtil.setEnabledIfOk(downloadButton, enabled);
}

/* ----------------- Button ----------------- */

@Override
protected BasicUIOperation getButtonHandler() {
return new SetFieldValueOperation<String>(this, this::getNewValueFromButtonSelection2);
}

@Override
protected String getNewValueFromButtonSelection2() throws OperationCancellation {
return ControlUtils.openFileDialog(getFieldValue(), button.getShell());
}

/* ----------------- Download Button ----------------- */

protected void createContents_DownloadButton(Composite topControl) {
downloadButton = SWTFactoryUtil.createPushButton(topControl, downloadButtonLabel, null);
downloadButton.addSelectionListener(new WidgetSelectedRunner(getDownloadButtonHandler()));
}


protected abstract BasicUIOperation getDownloadButtonHandler();

}
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import melnorme.lang.ide.core.LangCore; import melnorme.lang.ide.core.LangCore;
import melnorme.lang.ide.core.LangCore.StatusExt; import melnorme.lang.ide.core.LangCore.StatusExt;
import melnorme.lang.ide.core.utils.EclipseUtils; import melnorme.lang.ide.core.utils.EclipseUtils;
import melnorme.lang.ide.ui.LangUIMessages;
import melnorme.lang.tooling.data.StatusException; import melnorme.lang.tooling.data.StatusException;
import melnorme.lang.tooling.data.StatusLevel; import melnorme.lang.tooling.data.StatusLevel;


Expand Down Expand Up @@ -78,10 +77,6 @@ public void displayStatusMessage(String title, StatusLevel statusLevel, String m
handleStatus(false, null, title, new StatusException(statusLevel, message)); handleStatus(false, null, title, new StatusException(statusLevel, message));
} }


public final void handleInternalError(Shell shell, String message, Throwable exception) {
assertNotNull(message);
handleStatus(true, shell, LangUIMessages.InternalError, message, exception);
}




/* ----------------- ----------------- */ /* ----------------- ----------------- */
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@


import melnorme.lang.ide.core.LangCore; import melnorme.lang.ide.core.LangCore;
import melnorme.lang.ide.core.LangCoreMessages; import melnorme.lang.ide.core.LangCoreMessages;
import melnorme.lang.ide.ui.LangUIMessages;
import melnorme.lang.tooling.data.StatusException; import melnorme.lang.tooling.data.StatusException;
import melnorme.lang.tooling.data.StatusLevel; import melnorme.lang.tooling.data.StatusLevel;
import melnorme.utilbox.core.CommonException; import melnorme.utilbox.core.CommonException;
Expand Down Expand Up @@ -45,16 +46,16 @@ public static void handleStatus(String title, StatusException status) {


/* ----------------- ----------------- */ /* ----------------- ----------------- */


public static void handleStatus(boolean logError, String dialogTitle, CoreException ce) { public static void handleStatus(boolean logError, String title, CoreException ce) {
handleStatus(logError, dialogTitle, LangCore.createCommonException(ce)); handleStatus(logError, title, LangCore.createCommonException(ce));
} }


public static void handleStatus(boolean logError, String dialogTitle, CommonException ce) { public static void handleStatus(boolean logError, String title, CommonException ce) {
handleStatus(logError, dialogTitle, ce.toStatusException(StatusLevel.ERROR)); handleStatus(logError, title, ce.toStatusException(StatusLevel.ERROR));
} }


public static void handleStatus(String dialogTitle, CommonException ce) { public static void handleStatus(String title, CommonException ce) {
handleStatus(false, dialogTitle, ce); handleStatus(false, title, ce);
} }


/* ----------------- ----------------- */ /* ----------------- ----------------- */
Expand All @@ -68,11 +69,11 @@ public static void displayStatusMessage(String title, StatusLevel statusLevel, S
} }


public static void handleInternalError(String message, Throwable exception) { public static void handleInternalError(String message, Throwable exception) {
handler.handleInternalError(null, message, exception); handleInternalError(null, message, exception);
} }


public static void handleInternalError(Shell shell, String message, Throwable exception) { public static void handleInternalError(Shell shell, String message, Throwable exception) {
handler.handleInternalError(shell, message, exception); handler.handleStatus(true, shell, LangUIMessages.InternalError, message, exception);
} }


public static void handleOperationStatus(String opName, CoreException ce) { public static void handleOperationStatus(String opName, CoreException ce) {
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public BasicEditorOperation(String operationName, IEditorPart editorPart) {
} }


@Override @Override
protected void handleStatus(CommonException ce) { protected void handleError(CommonException ce) {
UIOperationsStatusHandler.handleOperationStatus(getOperationName(), ce); UIOperationsStatusHandler.handleOperationStatus(getOperationName(), ce);
} }


Expand Down
Loading

0 comments on commit 5e87705

Please sign in to comment.