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

Bug 564256 - [Passage] support unregister for multiple resources at once #240

Merged
merged 2 commits into from
Jun 13, 2020
Merged
Show file tree
Hide file tree
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
5 changes: 2 additions & 3 deletions bundles/org.eclipse.passage.loc.edit.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Automatic-Module-Name: org.eclipse.passage.loc.edit.ui
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.eclipse.passage.loc.edit.ui;singleton:=true
Bundle-Version: 0.6.0.qualifier
Bundle-Version: 0.6.100.qualifier
Bundle-Name: %Bundle-Name
Bundle-Vendor: %Bundle-Vendor
Bundle-Copyright: %Bundle-Copyright
Expand All @@ -21,5 +21,4 @@ Require-Bundle: org.eclipse.jface;bundle-version="0.0.0",
org.eclipse.passage.loc.workbench;bundle-version="0.0.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Export-Package: org.eclipse.passage.loc.edit.ui;x-internal:=true,
org.eclipse.passage.loc.edit.ui.handlers;x-internal:=true,
org.eclipse.passage.loc.internal.edit.ui.i18n;x-internal:=true
org.eclipse.passage.loc.edit.ui.handlers;x-internal:=true
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,10 @@
import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.e4.ui.di.UIEventTopic;
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.GroupMarker;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ISelection;
Expand All @@ -38,14 +34,13 @@
import org.eclipse.passage.lic.emf.ecore.EditingDomainRegistry;
import org.eclipse.passage.lic.emf.edit.BaseDomainRegistry;
import org.eclipse.passage.lic.emf.edit.EditingDomainRegistryAccess;
import org.eclipse.passage.loc.internal.edit.ui.i18n.EditUiMessages;
import org.eclipse.passage.loc.internal.workbench.LocDomainRegistryAccess;
import org.eclipse.passage.loc.internal.workbench.registry.UnregisterAction;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchActionConstants;

//FIXME: rewrite to avoid restriction warnings
Expand Down Expand Up @@ -121,31 +116,9 @@ private void createContextMenu(Control control) {

private void fillContextMenu(IMenuManager contextMenu) {
contextMenu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
contextMenu.add(new Action(EditUiMessages.DomainRegistryRemoveHandler_title) {
@Override
public void run() {
// FIXME: duplicates DomainRegistryRemoveHandler
Object first = viewer.getStructuredSelection().getFirstElement();
if (first instanceof Resource) {
Resource resource = (Resource) first;
URI uri = resource.getURI();
if (uri != null) {
access.domainRegistryForExtension(uri.fileExtension())//
.filter(BaseDomainRegistry.class::isInstance)//
.map(BaseDomainRegistry.class::cast)//
.ifPresent(r -> unregister(r, uri, viewer.getControl().getShell()));
}
}
}
});
}

private void unregister(BaseDomainRegistry<?> registry, URI uri, Shell shell) {
String message = String.format(EditUiMessages.DomainRegistryRemoveHandler_mesage, uri.toFileString());
String title = EditUiMessages.DomainRegistryRemoveHandler_title;
if (MessageDialog.openConfirm(shell, title, message)) {
registry.unregisterSource(uri.toFileString());
}
contextMenu.add(new UnregisterAction(access, //
() -> viewer.getStructuredSelection().toArray(), //
() -> viewer.getControl().getShell()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,36 @@
*******************************************************************************/
package org.eclipse.passage.loc.edit.ui.handlers;

import java.util.Objects;

import javax.inject.Named;

import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.di.annotations.CanExecute;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.e4.ui.services.IServiceConstants;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.passage.lic.emf.edit.BaseDomainRegistry;
import org.eclipse.passage.lic.emf.edit.EditingDomainRegistryAccess;
import org.eclipse.passage.loc.internal.edit.ui.i18n.EditUiMessages;
import org.eclipse.passage.loc.internal.workbench.LocDomainRegistryAccess;
import org.eclipse.passage.loc.internal.workbench.registry.UnregisterConfirmation;
import org.eclipse.passage.loc.internal.workbench.registry.UnregisterSelected;
import org.eclipse.passage.loc.internal.workbench.registry.UnregisterUri;
import org.eclipse.swt.widgets.Shell;

//FIXME: rewrite to avoid restriction warnings
@SuppressWarnings("restriction")
public class DomainRegistryRemoveHandler {

@CanExecute
public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) @Optional Resource resource) {
return resource != null;
public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection) {
return Objects.nonNull(selection);
}

@Execute
public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) Resource resource, IEclipseContext context) {
URI uri = resource.getURI();
if (uri != null) {
LocDomainRegistryAccess access = (LocDomainRegistryAccess) context.get(EditingDomainRegistryAccess.class);
access.domainRegistryForExtension(uri.fileExtension())//
.filter(BaseDomainRegistry.class::isInstance)//
.map(BaseDomainRegistry.class::cast)//
.ifPresent(r -> unregister(r, uri, context.get(Shell.class)));
}
}

private void unregister(BaseDomainRegistry<?> registry, URI uri, Shell shell) {
String message = String.format(EditUiMessages.DomainRegistryRemoveHandler_mesage, uri.toFileString());
String title = EditUiMessages.DomainRegistryRemoveHandler_title;
if (MessageDialog.openConfirm(shell, title, message)) {
registry.unregisterSource(uri.toFileString());
}
public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) Object selection, IEclipseContext context) {
new UnregisterSelected(//
new UnregisterUri((LocDomainRegistryAccess) context.get(EditingDomainRegistryAccess.class)), //
new UnregisterConfirmation(() -> context.get(Shell.class))//
).unregister(selection);
}

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Automatic-Module-Name: org.eclipse.passage.loc.workbench
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.eclipse.passage.loc.workbench;singleton:=true
Bundle-Version: 0.6.0.qualifier
Bundle-Version: 0.6.100.qualifier
Bundle-Name: %Bundle-Name
Bundle-Vendor: %Bundle-Vendor
Bundle-Copyright: %Bundle-Copyright
Expand Down Expand Up @@ -36,6 +36,7 @@ Export-Package: org.eclipse.passage.loc.internal.workbench;
org.eclipse.passage.loc.dashboard.ui,
org.eclipse.passage.loc.products.ui",
org.eclipse.passage.loc.internal.workbench.i18n;x-internal:=true,
org.eclipse.passage.loc.internal.workbench.registry;x-internal:=true,
org.eclipse.passage.loc.jface,
org.eclipse.passage.loc.jface.dialogs,
org.eclipse.passage.loc.workbench,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public class WorkbenchMessages extends NLS {
public static String SelectRoot_e_null_root_request;
public static String UndoHandler_label_base;
public static String UndoHandler_label_handler;
public static String UnregisterConfirmation_message;
public static String UnregisterConfirmation_title;
static {
// initialize resource bundle
NLS.initializeMessages(BUNDLE_NAME, WorkbenchMessages.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,5 @@ SelectRoot_e_null_inner_request=Request for inner must not be null
SelectRoot_e_null_root_request=Request for root must not be null
UndoHandler_label_base=Undo
UndoHandler_label_handler={0} {1}
UnregisterConfirmation_message=Unregister domain resources ({0})?
UnregisterConfirmation_title=Unregister Resources
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.loc.internal.workbench.registry;

import java.util.Objects;
import java.util.function.Supplier;

import org.eclipse.jface.action.Action;
import org.eclipse.passage.loc.internal.workbench.LocDomainRegistryAccess;
import org.eclipse.passage.loc.internal.workbench.i18n.WorkbenchMessages;
import org.eclipse.swt.widgets.Shell;

public final class UnregisterAction extends Action {

private final LocDomainRegistryAccess access;
private final Supplier<Object[]> selection;
private final Supplier<Shell> shell;

public UnregisterAction(LocDomainRegistryAccess access, Supplier<Object[]> selection, Supplier<Shell> shell) {
super(WorkbenchMessages.UnregisterConfirmation_title);
Objects.requireNonNull(access, "LocDomainRegistryAccess access"); //$NON-NLS-1$
Objects.requireNonNull(selection, "Supplier<Object[]> selection"); //$NON-NLS-1$
Objects.requireNonNull(shell, "Supplier<Shell> shell"); //$NON-NLS-1$
this.access = access;
this.selection = selection;
this.shell = shell;
}

@Override
public void run() {
new UnregisterSelected(//
new UnregisterUri(access), //
new UnregisterConfirmation(shell))//
.unregister(selection.get());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.loc.internal.workbench.registry;

import java.text.MessageFormat;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.function.Supplier;

import org.eclipse.emf.common.util.URI;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.passage.loc.internal.workbench.i18n.WorkbenchMessages;
import org.eclipse.swt.widgets.Shell;

public final class UnregisterConfirmation implements Predicate<List<URI>> {

private final Supplier<Shell> shell;

public UnregisterConfirmation(Supplier<Shell> shell) {
Objects.requireNonNull(shell, "Supplier<Shell> shell"); //$NON-NLS-1$
this.shell = shell;
}

@Override
public boolean test(List<URI> uris) {
return MessageDialog.openConfirm(shell.get(), WorkbenchMessages.UnregisterConfirmation_title,
MessageFormat.format(WorkbenchMessages.UnregisterConfirmation_message, uris.size()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.loc.internal.workbench.registry;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;

public final class UnregisterSelected {

private final Consumer<URI> unregister;
private final Predicate<List<URI>> accept;

public UnregisterSelected(Consumer<URI> unregister, Predicate<List<URI>> accept) {
Objects.requireNonNull(unregister, "Consumer<URI> unregister"); //$NON-NLS-1$
Objects.requireNonNull(accept, "Predicate<List<URI>> accept"); //$NON-NLS-1$
this.unregister = unregister;
this.accept = accept;
}

public void unregister(Object selection) {
List<URI> remove = Arrays.stream(//
Optional.of(selection)//
.filter(Object[].class::isInstance)//
.map(Object[].class::cast)//
.orElse(new Object[] { selection }))//
.filter(Resource.class::isInstance)//
.map(Resource.class::cast).map(Resource::getURI)//
.filter(Objects::nonNull).collect(Collectors.toList());
if (remove.isEmpty() || !accept.test(remove)) {
return;
}
remove.forEach(unregister);
}

}
Loading