]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/handlers/CreateNewElementSelector.java
Selection queries from explicit sets of elements.
[simantics/district.git] / org.simantics.district.selection.ui / src / org / simantics / district / selection / ui / handlers / CreateNewElementSelector.java
1 package org.simantics.district.selection.ui.handlers;
2
3 import org.eclipse.e4.core.contexts.IEclipseContext;
4 import org.eclipse.e4.core.di.annotations.CanExecute;
5 import org.eclipse.e4.core.di.annotations.Execute;
6 import org.eclipse.e4.ui.services.IServiceConstants;
7 import org.eclipse.e4.ui.workbench.IWorkbench;
8 import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
9 import org.eclipse.jface.window.Window;
10 import org.eclipse.swt.widgets.Shell;
11 import org.simantics.db.Resource;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.district.selection.ui.parts.EditSelectorDialog;
14 import org.simantics.utils.ui.AdaptionUtils;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 public class CreateNewElementSelector {
19
20         private static final Logger LOGGER = LoggerFactory.getLogger(CreateNewElementSelector.class);
21
22         @CanExecute
23         public boolean canExecute(ESelectionService selectionService) {
24                 return true;
25         }
26         
27         @Execute
28         public void createNewElementSelector(IEclipseContext context, IWorkbench workbench) {
29                 Object currentSelection = context.get(IServiceConstants.ACTIVE_SELECTION);
30                 
31                 Shell shell = context.getActive(Shell.class);
32                 EditSelectorDialog dialog = new EditSelectorDialog(shell, null, AdaptionUtils.adaptToCollection(currentSelection, Resource.class));
33                 LOGGER.debug("Opening dialog");
34                 int result = dialog.open();
35                 LOGGER.debug("Dialog closed with result code " + result);
36                 if (result == Window.OK) {
37                         try {
38                                 dialog.writeSelection();
39                         } catch (DatabaseException e) {
40                                 LOGGER.error("Writing new element selection failed", e);
41                         }
42                 }
43         }
44 }