use of org.eclipse.e4.ui.internal.workbench.PartServiceSaveHandler in project eclipse.platform.ui by eclipse-platform.
the class WBWRenderer method createWidget.
@Override
public Object createWidget(MUIElement element, Object parent) {
final Widget newWidget;
if (!(element instanceof MWindow) || (parent != null && !(parent instanceof Control))) {
return null;
}
MWindow wbwModel = (MWindow) element;
MApplication appModel = wbwModel.getContext().get(MApplication.class);
Boolean rtlMode = (Boolean) appModel.getTransientData().get(E4Workbench.RTL_MODE);
int rtlStyle = (rtlMode != null && rtlMode.booleanValue()) ? SWT.RIGHT_TO_LEFT : 0;
Shell parentShell = parent == null ? null : ((Control) parent).getShell();
final Shell wbwShell;
int styleOverride = getStyleOverride(wbwModel) | rtlStyle;
if (parentShell == null) {
int style = styleOverride == -1 ? SWT.SHELL_TRIM | rtlStyle : styleOverride;
wbwShell = new Shell(display, style);
// $NON-NLS-1$
wbwModel.getTags().add("topLevel");
} else {
int style = SWT.TITLE | SWT.RESIZE | SWT.MAX | SWT.CLOSE | rtlStyle;
style = styleOverride == -1 ? style : styleOverride;
if (wbwModel.getTags().contains(IPresentationEngine.WINDOW_TOP_LEVEL)) {
wbwShell = new Shell(display, style);
} else {
wbwShell = new Shell(parentShell, style);
}
// Prevent ESC from closing the DW
wbwShell.addTraverseListener(e -> {
if (e.detail == SWT.TRAVERSE_ESCAPE) {
e.doit = false;
}
});
}
wbwShell.setBackgroundMode(SWT.INHERIT_DEFAULT);
Rectangle modelBounds = wbwShell.getBounds();
if (wbwModel.isSetX()) {
modelBounds.x = wbwModel.getX();
}
if (wbwModel.isSetY()) {
modelBounds.y = wbwModel.getY();
}
if (wbwModel.isSetHeight()) {
modelBounds.height = wbwModel.getHeight();
}
if (wbwModel.isSetWidth()) {
modelBounds.width = wbwModel.getWidth();
}
// Force the shell onto the display if it would be invisible otherwise
Display display = Display.getCurrent();
Monitor closestMonitor = Util.getClosestMonitor(display, Geometry.centerPoint(modelBounds));
Rectangle displayBounds = closestMonitor.getClientArea();
if (!modelBounds.intersects(displayBounds)) {
Geometry.moveInside(modelBounds, displayBounds);
}
wbwShell.setBounds(modelBounds);
setCSSInfo(wbwModel, wbwShell);
// set up context
IEclipseContext localContext = getContext(wbwModel);
// We need to retrieve specific CSS properties for our layout.
CSSEngineHelper helper = new CSSEngineHelper(localContext, wbwShell);
TrimmedPartLayout tl = new TrimmedPartLayout(wbwShell);
tl.gutterTop = helper.getMarginTop(0);
tl.gutterBottom = helper.getMarginBottom(0);
tl.gutterLeft = helper.getMarginLeft(0);
tl.gutterRight = helper.getMarginRight(0);
wbwShell.setLayout(tl);
newWidget = wbwShell;
bindWidget(element, newWidget);
// Add the shell into the WBW's context
localContext.set(Shell.class, wbwShell);
localContext.set(E4Workbench.LOCAL_ACTIVE_SHELL, wbwShell);
setCloseHandler(wbwModel);
localContext.set(IShellProvider.class, () -> wbwShell);
final PartServiceSaveHandler saveHandler = new PartServiceSaveHandler() {
@Override
public Save promptToSave(MPart dirtyPart) {
Shell shell = (Shell) context.get(IServiceConstants.ACTIVE_SHELL);
Object[] elements = promptForSave(shell, Collections.singleton(dirtyPart));
if (elements == null) {
return Save.CANCEL;
}
return elements.length == 0 ? Save.NO : Save.YES;
}
@Override
public Save[] promptToSave(Collection<MPart> dirtyParts) {
List<MPart> parts = new ArrayList<>(dirtyParts);
Shell shell = (Shell) context.get(IServiceConstants.ACTIVE_SHELL);
Save[] response = new Save[dirtyParts.size()];
Object[] elements = promptForSave(shell, parts);
if (elements == null) {
Arrays.fill(response, Save.CANCEL);
} else {
Arrays.fill(response, Save.NO);
for (Object element : elements) {
response[parts.indexOf(element)] = Save.YES;
}
}
return response;
}
};
saveHandler.logger = logger;
localContext.set(ISaveHandler.class, saveHandler);
if (wbwModel.getLabel() != null) {
wbwShell.setText(wbwModel.getLocalizedLabel());
}
Image windowImage = getImage(wbwModel);
if (windowImage != null) {
wbwShell.setImage(windowImage);
} else {
// TODO: This should be added to the model, see bug 308494
// it allows for a range of icon sizes that the platform gets to
// choose from
wbwShell.setImages(Window.getDefaultImages());
}
return newWidget;
}
use of org.eclipse.e4.ui.internal.workbench.PartServiceSaveHandler in project eclipse.platform.ui by eclipse-platform.
the class WorkbenchWindow method setup.
@PostConstruct
public void setup() {
try {
// if workbench window is opened as a result of command execution,
// the context in which the new workbench window's commands are
// initialized has to to match the workbench context
final IEclipseContext windowContext = model.getContext();
HandlerServiceImpl.push(windowContext.getParent(), null);
// update the preference store.
if (getModel().getPersistedState().containsKey(IPreferenceConstants.COOLBAR_VISIBLE)) {
this.coolBarVisible = Boolean.parseBoolean(getModel().getPersistedState().get(IPreferenceConstants.COOLBAR_VISIBLE));
} else {
this.coolBarVisible = PrefUtil.getInternalPreferenceStore().getBoolean(IPreferenceConstants.COOLBAR_VISIBLE);
getModel().getPersistedState().put(IPreferenceConstants.COOLBAR_VISIBLE, Boolean.toString(this.coolBarVisible));
}
if (getModel().getPersistedState().containsKey(IPreferenceConstants.PERSPECTIVEBAR_VISIBLE)) {
this.perspectiveBarVisible = Boolean.parseBoolean(getModel().getPersistedState().get(IPreferenceConstants.PERSPECTIVEBAR_VISIBLE));
} else {
this.perspectiveBarVisible = PrefUtil.getInternalPreferenceStore().getBoolean(IPreferenceConstants.PERSPECTIVEBAR_VISIBLE);
getModel().getPersistedState().put(IPreferenceConstants.PERSPECTIVEBAR_VISIBLE, Boolean.toString(this.perspectiveBarVisible));
}
IServiceLocatorCreator slc = workbench.getService(IServiceLocatorCreator.class);
this.serviceLocator = (ServiceLocator) slc.createServiceLocator(workbench, null, () -> {
final Shell shell = getShell();
if (shell != null && !shell.isDisposed()) {
close();
}
}, windowContext);
windowContext.set(IExtensionTracker.class.getName(), new ContextFunction() {
@Override
public Object compute(IEclipseContext context, String contextKey) {
if (tracker == null) {
tracker = new UIExtensionTracker(getWorkbench().getDisplay());
}
return tracker;
}
});
windowContext.set(IWindowCloseHandler.class.getName(), (IWindowCloseHandler) window -> getWindowAdvisor().preWindowShellClose() && WorkbenchWindow.this.close());
final ISaveHandler defaultSaveHandler = windowContext.get(ISaveHandler.class);
final PartServiceSaveHandler localSaveHandler = new WWinPartServiceSaveHandler() {
@Override
public Save promptToSave(MPart dirtyPart) {
Object object = dirtyPart.getObject();
if (object instanceof CompatibilityPart) {
IWorkbenchPart part = ((CompatibilityPart) object).getPart();
ISaveablePart saveable = SaveableHelper.getSaveable(part);
if (saveable != null) {
if (!saveable.isSaveOnCloseNeeded()) {
return Save.NO;
}
return SaveableHelper.savePart(saveable, part, WorkbenchWindow.this, true) ? Save.NO : Save.CANCEL;
}
}
return defaultSaveHandler.promptToSave(dirtyPart);
}
@Override
public Save[] promptToSave(Collection<MPart> dirtyParts) {
LabelProvider labelProvider = new LabelProvider() {
@Override
public String getText(Object element) {
return ((MPart) element).getLocalizedLabel();
}
};
List<MPart> parts = new ArrayList<>(dirtyParts);
ListSelectionDialog dialog = new ListSelectionDialog(getShell(), parts, ArrayContentProvider.getInstance(), labelProvider, WorkbenchMessages.EditorManager_saveResourcesMessage);
dialog.setInitialSelections(parts.toArray());
dialog.setTitle(WorkbenchMessages.EditorManager_saveResourcesTitle);
if (dialog.open() == IDialogConstants.CANCEL_ID) {
return new Save[] { Save.CANCEL };
}
Object[] toSave = dialog.getResult();
Save[] retSaves = new Save[parts.size()];
Arrays.fill(retSaves, Save.NO);
for (int i = 0; i < retSaves.length; i++) {
MPart part = parts.get(i);
for (Object o : toSave) {
if (o == part) {
retSaves[i] = Save.YES;
break;
}
}
}
return retSaves;
}
@Override
public boolean save(MPart dirtyPart, boolean confirm) {
Object object = dirtyPart.getObject();
if (object instanceof CompatibilityPart) {
IWorkbenchPart workbenchPart = ((CompatibilityPart) object).getPart();
if (SaveableHelper.isSaveable(workbenchPart)) {
SaveablesList saveablesList = (SaveablesList) PlatformUI.getWorkbench().getService(ISaveablesLifecycleListener.class);
Object saveResult = saveablesList.preCloseParts(Collections.singletonList(workbenchPart), true, WorkbenchWindow.this);
return saveResult != null;
}
} else if (isSaveOnCloseNotNeededSplitEditorPart(dirtyPart)) {
return true;
}
return super.save(dirtyPart, confirm);
}
private boolean saveParts(ArrayList<MPart> dirtyParts, Save[] decisions) {
if (decisions == null || decisions.length == 0) {
super.saveParts(dirtyParts, true);
}
if (dirtyParts.size() != decisions.length) {
for (Save decision : decisions) {
if (decision == Save.CANCEL) {
return false;
}
}
}
List<MPart> dirtyPartsList = Collections.unmodifiableList(new ArrayList<>(dirtyParts));
for (Save decision : decisions) {
if (decision == Save.CANCEL) {
return false;
}
}
for (int i = 0; i < decisions.length; i++) {
if (decisions[i] == Save.YES) {
if (!save(dirtyPartsList.get(i), false)) {
return false;
}
}
}
return true;
}
private boolean saveMixedParts(ArrayList<MPart> nonCompParts, ArrayList<IWorkbenchPart> compParts, boolean confirm, boolean addNonPartSources) {
SaveablesList saveablesList = (SaveablesList) PlatformUI.getWorkbench().getService(ISaveablesLifecycleListener.class);
if (!confirm) {
boolean saved = super.saveParts(nonCompParts, confirm);
Object saveResult = saveablesList.preCloseParts(compParts, true, WorkbenchWindow.this);
return ((saveResult != null) && saved);
}
LabelProvider labelProvider = new LabelProvider() {
WorkbenchPartLabelProvider workbenchLabelProvider = new WorkbenchPartLabelProvider();
@Override
public String getText(Object element) {
if (element instanceof Saveable) {
return workbenchLabelProvider.getText(element);
}
return ((MPart) element).getLocalizedLabel();
}
};
ArrayList<Object> listParts = new ArrayList<>();
Map<IWorkbenchPart, List<Saveable>> saveableMap = saveablesList.getSaveables(compParts);
listParts.addAll(nonCompParts);
LinkedHashSet<Saveable> saveablesSet = new LinkedHashSet<>();
for (IWorkbenchPart workbenchPart : compParts) {
List<Saveable> list = saveableMap.get(workbenchPart);
if (list != null) {
saveablesSet.addAll(list);
}
}
if (addNonPartSources) {
for (ISaveablesSource nonPartSource : saveablesList.getNonPartSources()) {
Saveable[] saveables = nonPartSource.getSaveables();
for (Saveable saveable : saveables) {
if (saveable.isDirty()) {
saveablesSet.add(saveable);
}
}
}
}
listParts.addAll(saveablesSet);
ListSelectionDialog dialog = new ListSelectionDialog(getShell(), listParts, ArrayContentProvider.getInstance(), labelProvider, WorkbenchMessages.EditorManager_saveResourcesMessage);
dialog.setInitialSelections(listParts.toArray());
dialog.setTitle(WorkbenchMessages.EditorManager_saveResourcesTitle);
if (dialog.open() == IDialogConstants.CANCEL_ID) {
return false;
}
Object[] toSave = dialog.getResult();
Save[] nonCompatSaves = new Save[nonCompParts.size()];
Save[] compatSaves = new Save[saveablesSet.size()];
Arrays.fill(nonCompatSaves, Save.NO);
Arrays.fill(compatSaves, Save.NO);
for (int i = 0; i < nonCompatSaves.length; i++) {
MPart part = nonCompParts.get(i);
for (Object o : toSave) {
if (o == part) {
nonCompatSaves[i] = Save.YES;
break;
}
}
}
Map<Saveable, Save> saveOptionMap = new HashMap<>();
for (Saveable saveable : saveablesSet) {
boolean found = false;
for (Object o : toSave) {
if (o == saveable) {
saveOptionMap.put(saveable, Save.YES);
found = true;
break;
}
}
if (!found) {
saveOptionMap.put(saveable, Save.NO);
}
}
boolean saved = saveParts(nonCompParts, nonCompatSaves);
if (!saved) {
return saved;
}
Object saveResult = saveablesList.preCloseParts(compParts, false, true, WorkbenchWindow.this, saveOptionMap);
return ((saveResult != null) && saved);
}
private void removeSaveOnCloseNotNeededParts(List<IWorkbenchPart> parts) {
for (Iterator<IWorkbenchPart> it = parts.iterator(); it.hasNext(); ) {
IWorkbenchPart part = it.next();
ISaveablePart saveable = SaveableHelper.getSaveable(part);
if (saveable == null || !saveable.isSaveOnCloseNeeded()) {
it.remove();
}
}
}
private void removeSaveOnCloseNotNeededSplitEditorParts(List<MPart> parts) {
for (Iterator<MPart> it = parts.iterator(); it.hasNext(); ) {
MPart part = it.next();
if (isSaveOnCloseNotNeededSplitEditorPart(part)) {
it.remove();
}
}
}
private boolean isSaveOnCloseNotNeededSplitEditorPart(MPart part) {
boolean notNeeded = false;
if (part instanceof MCompositePart && SplitHost.SPLIT_HOST_CONTRIBUTOR_URI.equals(part.getContributionURI())) {
MCompositePart compPart = (MCompositePart) part;
List<MPart> elements = modelService.findElements(compPart, null, MPart.class);
if (elements != null && elements.size() > 1) {
elements.remove(0);
for (MPart mpart : elements) {
Object object = mpart.getObject();
if (object instanceof CompatibilityPart) {
IWorkbenchPart workbenchPart = ((CompatibilityPart) object).getPart();
if (!SaveableHelper.isSaveable(workbenchPart)) {
notNeeded = true;
} else {
ISaveablePart saveable = SaveableHelper.getSaveable(workbenchPart);
if (saveable == null || !saveable.isSaveOnCloseNeeded()) {
notNeeded = true;
} else {
notNeeded = false;
break;
}
}
} else {
notNeeded = false;
break;
}
}
}
}
return notNeeded;
}
@Override
public boolean saveParts(Collection<MPart> dirtyParts, boolean confirm, boolean closing, boolean addNonPartSources) {
ArrayList<IWorkbenchPart> saveableParts = new ArrayList<>();
ArrayList<MPart> nonCompatibilityParts = new ArrayList<>();
for (MPart part : dirtyParts) {
Object object = part.getObject();
if (object instanceof CompatibilityPart) {
IWorkbenchPart workbenchPart = ((CompatibilityPart) object).getPart();
if (SaveableHelper.isSaveable(workbenchPart)) {
saveableParts.add(workbenchPart);
}
} else {
nonCompatibilityParts.add(part);
}
}
if (!saveableParts.isEmpty() && closing) {
removeSaveOnCloseNotNeededParts(saveableParts);
}
if (!nonCompatibilityParts.isEmpty() && closing) {
removeSaveOnCloseNotNeededSplitEditorParts(nonCompatibilityParts);
}
if (saveableParts.isEmpty()) {
if (nonCompatibilityParts.isEmpty()) {
// nothing to save
return true;
}
return super.saveParts(nonCompatibilityParts, confirm);
} else if (!nonCompatibilityParts.isEmpty()) {
return saveMixedParts(nonCompatibilityParts, saveableParts, confirm, addNonPartSources);
}
SaveablesList saveablesList = (SaveablesList) PlatformUI.getWorkbench().getService(ISaveablesLifecycleListener.class);
Object saveResult = saveablesList.preCloseParts(saveableParts, addNonPartSources, true, WorkbenchWindow.this, WorkbenchWindow.this);
return (saveResult != null);
}
@Override
public boolean saveParts(Collection<MPart> dirtyParts, boolean confirm) {
return saveParts(dirtyParts, confirm, false, false);
}
};
localSaveHandler.logger = logger;
windowContext.set(ISaveHandler.class, localSaveHandler);
windowContext.set(IWorkbenchWindow.class.getName(), this);
windowContext.set(IPageService.class, this);
windowContext.set(IPartService.class, partService);
windowContext.set(ISources.ACTIVE_WORKBENCH_WINDOW_NAME, this);
windowContext.set(ISources.ACTIVE_WORKBENCH_WINDOW_SHELL_NAME, getShell());
EContextService cs = windowContext.get(EContextService.class);
cs.activateContext(IContextService.CONTEXT_ID_WINDOW);
cs.getActiveContextIds();
initializeDefaultServices();
/*
* Remove the second QuickAccess control if an older workspace is opened.
*
* An older workspace will create an ApplicationModel which already contains the
* QuickAccess elements, from the old "popuolateTopTrimContribution()" method.
* The new implementation of this method doesn't add the QuickAccess elements
* anymore but an old workbench.xmi still has these entries in it and so they
* need to be removed.
*/
cleanLegacyQuickAccessContribution();
// register with the tracker
fireWindowOpening();
configureShell(getShell(), windowContext);
try {
page = new WorkbenchPage(this, input);
} catch (WorkbenchException e) {
WorkbenchPlugin.log(e);
}
menuOverride = new MenuOverrides(page);
toolbarOverride = new ToolbarOverrides(page);
ContextInjectionFactory.inject(page, model.getContext());
windowContext.set(IWorkbenchPage.class, page);
menuManager.setOverrides(menuOverride);
((CoolBarToTrimManager) getCoolBarManager2()).setOverrides(toolbarOverride);
// Fill the action bars
fillActionBars(FILL_ALL_ACTION_BARS);
firePageOpened();
populateTopTrimContributions();
populateBottomTrimContributions();
// Trim gets populated during rendering (?) so make sure we have al/
// sides. See bug 383269 for details
modelService.getTrim(model, SideValue.LEFT);
modelService.getTrim(model, SideValue.RIGHT);
// move the QuickAccess ToolControl to the correct position (only if
// it exists)
positionQuickAccess();
Shell shell = (Shell) model.getWidget();
if (model.getMainMenu() == null) {
mainMenu = modelService.createModelElement(MMenu.class);
mainMenu.setElementId(IWorkbenchConstants.MAIN_MENU_ID);
mainMenu.getPersistedState().put(org.eclipse.e4.ui.workbench.IWorkbench.PERSIST_STATE, Boolean.FALSE.toString());
renderer = (MenuManagerRenderer) rendererFactory.getRenderer(mainMenu, null);
renderer.linkModelToManager(mainMenu, menuManager);
renderer.reconcileManagerToModel(menuManager, mainMenu);
model.setMainMenu(mainMenu);
final Menu menu = (Menu) engine.createGui(mainMenu, model.getWidget(), model.getContext());
shell.setMenuBar(menu);
menuUpdater = () -> {
try {
if (model.getMainMenu() == null || model.getWidget() == null || menu.isDisposed() || mainMenu.getWidget() == null) {
return;
}
MenuManagerRendererFilter.updateElementVisibility(mainMenu, renderer, menuManager, windowContext.getActiveLeaf(), 1, false);
menuManager.update(true);
} finally {
canUpdateMenus = true;
}
};
RunAndTrack menuChangeManager = new RunAndTrack() {
@Override
public boolean changed(IEclipseContext context) {
ExpressionInfo info = new ExpressionInfo();
IEclipseContext leafContext = windowContext.getActiveLeaf();
MenuManagerRendererFilter.collectInfo(info, mainMenu, renderer, leafContext, true);
// if one of these variables change, re-run the RAT
for (String name : info.getAccessedVariableNames()) {
leafContext.get(name);
}
if (canUpdateMenus && workbench.getDisplay() != null) {
canUpdateMenus = false;
workbench.getDisplay().asyncExec(menuUpdater);
}
return manageChanges;
}
};
windowContext.runAndTrack(menuChangeManager);
}
eventBroker.subscribe(UIEvents.UIElement.TOPIC_WIDGET, windowWidgetHandler);
boolean newWindow = setupPerspectiveStack(windowContext);
partService.setPage(page);
page.setPerspective(perspective);
firePageActivated();
if (newWindow) {
page.fireInitialPartVisibilityEvents();
} else {
page.updatePerspectiveActionSets();
}
updateActionSets();
IPreferenceStore preferenceStore = PrefUtil.getAPIPreferenceStore();
boolean enableAnimations = preferenceStore.getBoolean(IWorkbenchPreferenceConstants.ENABLE_ANIMATIONS);
preferenceStore.setValue(IWorkbenchPreferenceConstants.ENABLE_ANIMATIONS, false);
// Hack!! don't show the intro if there's more than one open
// perspective
List<MPerspective> persps = modelService.findElements(model, null, MPerspective.class, null);
if (persps.size() > 1) {
PrefUtil.getAPIPreferenceStore().setValue(IWorkbenchPreferenceConstants.SHOW_INTRO, false);
PrefUtil.saveAPIPrefs();
}
if (Boolean.parseBoolean(getModel().getPersistedState().get(PERSISTED_STATE_RESTORED))) {
SafeRunnable.run(new SafeRunnable() {
@Override
public void run() throws Exception {
getWindowAdvisor().postWindowRestore();
}
});
} else {
getModel().getPersistedState().put(PERSISTED_STATE_RESTORED, Boolean.TRUE.toString());
}
getWindowAdvisor().postWindowCreate();
getWindowAdvisor().openIntro();
preferenceStore.setValue(IWorkbenchPreferenceConstants.ENABLE_ANIMATIONS, enableAnimations);
getShell().setData(this);
trackShellActivation();
/**
* When SWT zoom changes for primary monitor, prompt user to restart Eclipse to
* apply the changes.
*/
getShell().addListener(SWT.ZoomChanged, event -> {
if (getShell().getDisplay().getPrimaryMonitor().equals(getShell().getMonitor())) {
int dialogResponse = MessageDialog.open(MessageDialog.QUESTION, getShell(), WorkbenchMessages.Workbench_zoomChangedTitle, WorkbenchMessages.Workbench_zoomChangedMessage, SWT.NONE, WorkbenchMessages.Workbench_RestartButton, WorkbenchMessages.Workbench_DontRestartButton);
if (event.doit && dialogResponse == 0) {
getWorkbenchImpl().restart(true);
}
}
});
} finally {
HandlerServiceImpl.pop();
}
}
use of org.eclipse.e4.ui.internal.workbench.PartServiceSaveHandler in project eclipse.platform.ui by eclipse-platform.
the class EPartServiceTest method testSavePart.
private void testSavePart(final Save returnValue, boolean confirm, boolean beforeDirty, boolean afterDirty, boolean success, boolean saveCalled, boolean throwException) {
MWindow window = ems.createModelElement(MWindow.class);
application.getChildren().add(window);
MPart saveablePart = ems.createModelElement(MPart.class);
saveablePart.setDirty(beforeDirty);
saveablePart.setContributionURI("bundleclass://org.eclipse.e4.ui.tests/org.eclipse.e4.ui.tests.application.ClientEditor");
window.getChildren().add(saveablePart);
initialize();
getEngine().createGui(window);
ClientEditor editor = (ClientEditor) saveablePart.getObject();
editor.setThrowException(throwException);
window.getContext().set(ISaveHandler.class.getName(), new PartServiceSaveHandler() {
@Override
public Save[] promptToSave(Collection<MPart> saveablePart) {
return null;
}
@Override
public Save promptToSave(MPart saveablePart) {
return returnValue;
}
});
EPartService partService = window.getContext().get(EPartService.class);
if (beforeDirty) {
assertEquals(success, partService.savePart(saveablePart, confirm));
} else {
assertTrue("The part is not dirty, the save operation should complete successfully", partService.savePart(saveablePart, confirm));
}
assertEquals(afterDirty, saveablePart.isDirty());
assertEquals(saveCalled, editor.wasSaveCalled());
}
use of org.eclipse.e4.ui.internal.workbench.PartServiceSaveHandler in project eclipse.platform.ui by eclipse-platform.
the class EPartServiceTest method testSaveAll.
private void testSaveAll(final Save[] returnValues, boolean confirm, boolean[] beforeDirty, boolean[] afterDirty, boolean success, boolean[] saveCalled, boolean[] throwException) {
MWindow window = ems.createModelElement(MWindow.class);
application.getChildren().add(window);
final MPart saveablePart = createSaveablePart(window, beforeDirty[0]);
final MPart saveablePart2 = createSaveablePart(window, beforeDirty[1]);
// setup the context
initialize();
getEngine().createGui(window);
ClientEditor editor = (ClientEditor) saveablePart.getObject();
editor.setThrowException(throwException[0]);
ClientEditor editor2 = (ClientEditor) saveablePart2.getObject();
editor2.setThrowException(throwException[1]);
window.getContext().set(ISaveHandler.class.getName(), new PartServiceSaveHandler() {
@Override
public Save[] promptToSave(Collection<MPart> saveableParts) {
int index = 0;
Save[] prompt = new Save[saveableParts.size()];
Iterator<MPart> it = saveableParts.iterator();
while (it.hasNext()) {
prompt[index] = prompt(returnValues, it.next(), saveablePart);
index++;
}
return prompt;
}
@Override
public Save promptToSave(MPart saveablePart) {
return null;
}
});
EPartService partService = window.getContext().get(EPartService.class);
assertEquals(success, partService.saveAll(confirm));
assertEquals(afterDirty[0], saveablePart.isDirty());
assertEquals(saveCalled[0], editor.wasSaveCalled());
assertEquals(afterDirty[1], saveablePart2.isDirty());
assertEquals(saveCalled[1], editor2.wasSaveCalled());
}
use of org.eclipse.e4.ui.internal.workbench.PartServiceSaveHandler in project eclipse.platform.ui by eclipse-platform.
the class EPartServiceTest method initialize.
private void initialize() {
final UIEventPublisher ep = new UIEventPublisher(applicationContext);
((Notifier) application).eAdapters().add(ep);
applicationContext.set(UIEventPublisher.class, ep);
applicationContext.set(ISaveHandler.class.getName(), new PartServiceSaveHandler() {
@Override
public Save[] promptToSave(Collection<MPart> saveablePart) {
Save[] ret = new Save[saveablePart.size()];
Arrays.fill(ret, ISaveHandler.Save.YES);
return ret;
}
@Override
public Save promptToSave(MPart saveablePart) {
return ISaveHandler.Save.YES;
}
});
}
Aggregations