Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save in main thread #45

Merged
merged 5 commits into from
Apr 26, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@
package com.hpe.octane.ideplugins.eclipse.ui.entitydetail;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.JFacePreferences;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.EditorPart;

import com.hpe.adm.nga.sdk.exception.OctaneException;
import com.hpe.adm.nga.sdk.model.ErrorModel;
import com.hpe.adm.nga.sdk.model.FieldModel;
import com.hpe.adm.octane.ideplugins.services.EntityService;
import com.hpe.octane.ideplugins.eclipse.Activator;
Expand All @@ -40,18 +40,17 @@
import com.hpe.octane.ideplugins.eclipse.ui.util.LoadingComposite;
import com.hpe.octane.ideplugins.eclipse.ui.util.StackLayoutComposite;
import com.hpe.octane.ideplugins.eclipse.ui.util.icon.EntityIconFactory;
import com.hpe.octane.ideplugins.eclipse.ui.util.resource.PlatformResourcesManager;
import com.hpe.octane.ideplugins.eclipse.ui.util.resource.SWTResourceManager;

public class EntityModelEditor extends EditorPart {

private static final EntityIconFactory entityIconFactoryForTabInfo = new EntityIconFactory(20, 20, 7);
private static EntityService entityService = Activator.getInstance(EntityService.class);

private static final Color BACKGROUND_COLOR = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme()
.getColorRegistry().get(JFacePreferences.CONTENT_ASSIST_BACKGROUND_COLOR);

public static final String ID = "com.hpe.octane.ideplugins.eclipse.ui.entitydetail.EntityModelEditor"; //$NON-NLS-1$
private static final EntityIconFactory entityIconFactoryForTabInfo = new EntityIconFactory(20, 20, 7);
private static final String SAVE_FAILED_DIALOG_TITLE = "Saving entity failed";

private static EntityService entityService = Activator.getInstance(EntityService.class);

public EntityModelEditorInput input;
private EntityModelWrapper entityModelWrapper;
private EntityComposite entityComposite;
Expand Down Expand Up @@ -79,7 +78,7 @@ public void createPartControl(Composite parent) {
rootComposite = new StackLayoutComposite(parent, SWT.NONE);
rootComposite.setBackgroundMode(SWT.INHERIT_FORCE);
rootComposite.setForeground(SWTResourceManager.getColor(SWT.COLOR_LIST_SELECTION));
rootComposite.setBackground(BACKGROUND_COLOR);
rootComposite.setBackground(PlatformResourcesManager.getPlatformBackgroundColor());

loadingComposite = new LoadingComposite(rootComposite, SWT.NONE);
rootComposite.showControl(loadingComposite);
Expand Down Expand Up @@ -143,50 +142,54 @@ public void fieldModelChanged(@SuppressWarnings("rawtypes") FieldModel fieldMode
private void setIsDirty(boolean isDirty) {
Display.getDefault().syncExec(() -> {
EntityModelEditor.this.isDirty = isDirty;
firePropertyChange(PROP_DIRTY);
firePropertyChange(IEditorPart.PROP_DIRTY);
});
}

@Override
public void setFocus() {
}
public void setFocus() {}

@Override
public void doSave(IProgressMonitor monitor) {
UpdateEntityJob updateEntityJob = new UpdateEntityJob("Saving " + entityModelWrapper.getEntityType(), entityModelWrapper.getEntityModel());

updateEntityJob.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
OctaneException octaneException = updateEntityJob.getOctaneException();

if (octaneException == null) {
loadEntity();

} else {
Display.getDefault().asyncExec(() -> {

EntityDetailErrorDialog errorDialog = new EntityDetailErrorDialog(rootComposite.getShell());

errorDialog.addButton("Back", () -> errorDialog.close());

errorDialog.addButton("Refresh", () -> {
loadEntity();
errorDialog.close();
});
errorDialog.addButton("Open in browser", () -> {
entityService.openInBrowser(entityModelWrapper.getReadOnlyEntityModel());
errorDialog.close();
});

errorDialog.open(octaneException, "Saving entity failed");

});
}
updateEntityJob.schedule();

try {
updateEntityJob.join(1000 * 10, monitor);
OctaneException octaneException = updateEntityJob.getOctaneException();
if (octaneException != null) {
throw octaneException;
} else {
loadEntity(); //reload entity from server if save was successful
}
});

updateEntityJob.schedule();
} catch (OperationCanceledException | InterruptedException | OctaneException ex) {
EntityDetailErrorDialog errorDialog = new EntityDetailErrorDialog(rootComposite.getShell());
errorDialog.addButton("Back", () -> errorDialog.close());
errorDialog.addButton("Refresh", () -> {
loadEntity();
errorDialog.close();
});
errorDialog.addButton("Open in browser", () -> {
entityService.openInBrowser(entityModelWrapper.getReadOnlyEntityModel());
errorDialog.close();
});

if(ex instanceof OperationCanceledException || ex instanceof InterruptedException) {
errorDialog.open(new OctaneException(new ErrorModel("Save timeout")), SAVE_FAILED_DIALOG_TITLE);
}
else if(ex instanceof OctaneException) {
errorDialog.open((OctaneException) ex, SAVE_FAILED_DIALOG_TITLE);
}

// This would stop the editor from closing, if the editor was being closed before the save
// The monitor can be null because of doSaveAs()
// The monitor being null will not affect the editor b4 close,
// because when the platform saves b4 close, it will always use the doSave method, i. e. the monitor won't be null
if(monitor != null) {
monitor.setCanceled(true);
}
}
}

@Override
Expand Down