]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.selection.ui/src/org/simantics/district/selection/ui/parts/EditSelectorDialog.java
Selection of highlight color for District Finder queries
[simantics/district.git] / org.simantics.district.selection.ui / src / org / simantics / district / selection / ui / parts / EditSelectorDialog.java
index 623f0f847921137f08f1f953d2179b5df56edd8e..387afa7c9827fdb8490167eba6723cdef0aaaff8 100644 (file)
@@ -24,6 +24,8 @@ import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.jface.resource.JFaceResources;
 import org.eclipse.jface.resource.LocalResourceManager;
 import org.eclipse.jface.resource.ResourceLocator;
+import org.eclipse.jface.window.Window;
+import org.eclipse.nebula.widgets.tablecombo.TableCombo;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
@@ -31,11 +33,15 @@ import org.eclipse.swt.widgets.Button;
 import org.eclipse.swt.widgets.Combo;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Table;
+import org.eclipse.swt.widgets.TableItem;
 import org.eclipse.swt.widgets.Text;
 import org.eclipse.swt.widgets.Widget;
 import org.simantics.Simantics;
+import org.simantics.databoard.Bindings;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.WriteGraph;
@@ -50,6 +56,7 @@ import org.simantics.db.layer0.request.PropertyInfo;
 import org.simantics.db.layer0.request.PropertyInfoRequest;
 import org.simantics.db.layer0.util.Layer0Utils;
 import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.diagram.stubs.G2DResource;
 import org.simantics.district.network.ontology.DistrictNetworkResource;
 import org.simantics.district.region.ontology.DiagramRegionsResource;
 import org.simantics.district.route.ontology.RouteResource;
@@ -69,12 +76,14 @@ import org.simantics.district.selection.ElementSelector.PropertySelector;
 import org.simantics.district.selection.ElementSelector.RegionCondition;
 import org.simantics.district.selection.ElementSelector.RouteCondition;
 import org.simantics.district.selection.ElementSelector.Selector;
+import org.simantics.district.selection.ui.ElementSelectionTools;
 import org.simantics.layer0.Layer0;
 import org.simantics.layer0.utils.direct.GraphUtils;
 import org.simantics.modeling.ModelingResources;
 import org.simantics.structural.stubs.StructuralResource2;
 import org.simantics.utils.datastructures.Arrays;
 import org.simantics.utils.datastructures.Pair;
+import org.simantics.utils.ui.color.Color;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -116,6 +125,9 @@ public class EditSelectorDialog extends Dialog {
        private String name;
        private Text nameField;
 
+       private Color color;
+       private TableCombo colorCombo;
+       
        private Resource diagram;
        private Combo diagramField;
 
@@ -158,7 +170,6 @@ public class EditSelectorDialog extends Dialog {
        
        // Called to read values from controls into conditions
        Updater updater = NULL_UPDATE;
-
        @Inject
        public EditSelectorDialog(Shell shell, ElementSelector elementSelector, Collection<Resource> currentSelection) {
                super(shell);
@@ -200,6 +211,11 @@ public class EditSelectorDialog extends Dialog {
                condition = null;
                
                if (elementSelector != null) {
+                       float[] color = elementSelector.getColor();
+                       if (color != null) {
+                               this.color = ElementSelectionTools.literalToColor(color);
+                       }
+                       
                        Generator generator = elementSelector.getGenerator();
                        if (generator instanceof ModelGenerator) {
                                generatorIndex = 0;
@@ -357,6 +373,13 @@ public class EditSelectorDialog extends Dialog {
                                throw new ValidationException("Please enter a name");
                        }
                        
+                       {
+                               int index = colorCombo.getSelectionIndex();
+                               if (index >= 0) {
+                                       color = new Color(colorCombo.getTable().getItem(index).getBackground().getRGB());
+                               }
+                       }
+                       
                        componentType = componentTypes.get(componentTypeField.getSelectionIndex());
                        
                        selectorIndex = selectorField.getSelectionIndex();
@@ -424,6 +447,11 @@ public class EditSelectorDialog extends Dialog {
                                graph.claimLiteral(selection, L0.HasLabel, L0.String, name);
                                graph.claim(selection, L0.PartOf, lib);
                                
+                               if (color != null) {
+                                       float[] value = ElementSelectionTools.colorToLiteral(color);
+                                       graph.claimLiteral(selection, ES.Selection_HasHighlightColor, G2DResource.getInstance(graph).Color, value, Bindings.FLOAT_ARRAY);
+                               }
+                               
                                // Generator
                                Resource generator = graph.newResource();
                                Resource generatorType;
@@ -516,6 +544,58 @@ public class EditSelectorDialog extends Dialog {
                nameField.setText(name);
                GridDataFactory.swtDefaults().hint(200, SWT.DEFAULT).applyTo(nameField);
                
+               // Highlight style
+               Label label = new Label(content, SWT.NONE);
+               GridDataFactory.swtDefaults().applyTo(label);
+               label.setText("Highlight color");
+               
+               colorCombo = new TableCombo(content, SWT.BORDER | SWT.READ_ONLY);
+               GridDataFactory.swtDefaults().hint(80, SWT.DEFAULT).applyTo(colorCombo);
+               Table colorTable = colorCombo.getTable();
+               colorCombo.getTextControl().setEnabled(false);
+               Display display = getShell().getDisplay();
+               colorTable.setFont(display.getSystemFont());
+               createColorItem(colorTable, "Red", SWT.COLOR_RED);
+               createColorItem(colorTable, "Green", SWT.COLOR_GREEN);
+               createColorItem(colorTable, "Blue", SWT.COLOR_BLUE);
+               createColorItem(colorTable, "Yellow", SWT.COLOR_YELLOW);
+               createColorItem(colorTable, "Cyan", SWT.COLOR_CYAN);
+               createColorItem(colorTable, "Magenta", SWT.COLOR_MAGENTA);
+               TableItem other = new TableItem(colorTable, SWT.NONE);
+               other.setText("Other...");
+
+               if (color != null) {
+                       int i;
+                       for (i = 0; i < 6; i++) {
+                               if (colorTable.getItem(i).getBackground().getRGB().equals(color.getRgb())) {
+                                       colorCombo.select(i);
+                                       break;
+                               }
+                       }
+                       
+                       if (i == 6) {
+                               other.setBackground(resourceManager.createColor(color.getRgb()));
+                               colorCombo.select(6);
+                       }
+               }
+               
+               colorCombo.addSelectionListener(new SelectionAdapter() {
+                       @Override
+                       public void widgetSelected(SelectionEvent e) {
+                               int index = colorCombo.getSelectionIndex();
+                               if (index == 6) {
+                                       org.simantics.utils.ui.color.ColorDialog colorDialog = new org.simantics.utils.ui.workbench.dialogs.ColorDialog(getParentShell());
+                                       int result = colorDialog.open();
+                                       if (result == Window.OK) {
+                                               other.setBackground(resourceManager.createColor(colorDialog.getColor().getRgb()));
+                                               colorCombo.clearSelection();
+                                               colorCombo.select(6);
+                                               colorTable.redraw();
+                                       }
+                               }
+                       }
+               });
+               
                // Selector
                Label selectorLabel = new Label(content, SWT.NONE);
                selectorLabel.setText("Select");
@@ -632,18 +712,25 @@ public class EditSelectorDialog extends Dialog {
                sourceField.select(generatorIndex);
                
                // Condition
-               Label label = new Label(content, SWT.NONE);
+               label = new Label(content, SWT.NONE);
                GridDataFactory.swtDefaults().align(SWT.BEGINNING, SWT.CENTER).applyTo(label);
                label.setText("where");
                
                conditionPanel = new Composite(content, SWT.NONE);
-               GridDataFactory.swtDefaults().span(1, 2).minSize(400, SWT.DEFAULT).grab(true, false).applyTo(conditionPanel);
+               GridDataFactory.swtDefaults().minSize(400, SWT.DEFAULT).grab(true, false).applyTo(conditionPanel);
                GridLayoutFactory.fillDefaults().numColumns(2).applyTo(conditionPanel);
                
                updater = updateConditionPanel();
                
                return content;
        }
+
+       private TableItem createColorItem(Table colorTable, String name, int color) {
+               TableItem item = new TableItem(colorTable, SWT.BORDER);
+               item.setText(name);
+               item.setBackground(colorTable.getDisplay().getSystemColor(color));
+               return item;
+       }
        
        private Updater updateConditionPanel() {
                // Erase contents