package org.simantics.district.selection.ui.handlers; import java.util.Collection; import javax.inject.Inject; 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.ui.model.application.ui.basic.MPart; import org.eclipse.e4.ui.services.IServiceConstants; import org.eclipse.e4.ui.workbench.IWorkbench; import org.eclipse.e4.ui.workbench.modeling.EPartService; import org.eclipse.e4.ui.workbench.modeling.ESelectionService; import org.eclipse.jface.window.Window; import org.eclipse.swt.widgets.Shell; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; import org.simantics.district.selection.ElementSelector; import org.simantics.district.selection.ElementSelector.ExplicitGenerator; import org.simantics.district.selection.ui.parts.EditSelectorDialog; import org.simantics.district.selection.ui.parts.ElementSelectionView; import org.simantics.utils.ui.AdaptionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class EditElementSelector { private static final Logger LOGGER = LoggerFactory.getLogger(EditElementSelector.class); @Inject EPartService partService; @CanExecute public boolean canExecute(ESelectionService selectionService) { MPart part = partService.getActivePart(); if (part == null) return false; Object object = part.getObject(); if (object == null || !(object instanceof ElementSelectionView)) return false; ElementSelectionView view = (ElementSelectionView)object; return view.getSelectedItem() != null; } @Execute public void editElementSelector(IEclipseContext context, IWorkbench workbench) { Object currentSelection = context.get(IServiceConstants.ACTIVE_SELECTION); MPart part = partService.getActivePart(); if (part == null) return; Object object = part.getObject(); if (object == null || !(object instanceof ElementSelectionView)) return; ElementSelectionView view = (ElementSelectionView)object; Collection selection; ElementSelector selectedItem = view.getSelectedItem(); if (selectedItem.getGenerator() instanceof ExplicitGenerator) { selection = ((ExplicitGenerator)selectedItem.getGenerator()).elements; } else { selection = AdaptionUtils.adaptToCollection(currentSelection, Resource.class); } Shell shell = context.getActive(Shell.class); EditSelectorDialog dialog = new EditSelectorDialog(shell, selectedItem, selection); LOGGER.debug("Opening dialog"); int result = dialog.open(); LOGGER.debug("Dialog closed with result code " + result); if (result == Window.OK) { try { dialog.writeSelection(); } catch (DatabaseException e) { LOGGER.error("Writing new element selection failed", e); } } } }