1 package org.simantics.modeling.ui.componentTypeEditor;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashMap;
7 import java.util.HashSet;
12 import org.eclipse.jface.dialogs.IDialogSettings;
13 import org.eclipse.jface.layout.GridDataFactory;
14 import org.eclipse.jface.layout.GridLayoutFactory;
15 import org.eclipse.jface.layout.TableColumnLayout;
16 import org.eclipse.jface.resource.ImageDescriptor;
17 import org.eclipse.jface.viewers.ColumnWeightData;
18 import org.eclipse.jface.window.Window;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.custom.TableEditor;
21 import org.eclipse.swt.events.DisposeEvent;
22 import org.eclipse.swt.events.DisposeListener;
23 import org.eclipse.swt.events.MouseAdapter;
24 import org.eclipse.swt.events.MouseEvent;
25 import org.eclipse.swt.events.SelectionAdapter;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.graphics.Color;
28 import org.eclipse.swt.graphics.Rectangle;
29 import org.eclipse.swt.layout.FillLayout;
30 import org.eclipse.swt.widgets.Button;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Shell;
34 import org.eclipse.swt.widgets.Table;
35 import org.eclipse.swt.widgets.TableColumn;
36 import org.eclipse.swt.widgets.TableItem;
37 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.ui.forms.widgets.Form;
39 import org.eclipse.ui.forms.widgets.FormToolkit;
40 import org.eclipse.ui.forms.widgets.Section;
41 import org.simantics.Simantics;
42 import org.simantics.databoard.Bindings;
43 import org.simantics.db.ReadGraph;
44 import org.simantics.db.Resource;
45 import org.simantics.db.WriteGraph;
46 import org.simantics.db.common.CommentMetadata;
47 import org.simantics.db.common.request.ObjectsWithType;
48 import org.simantics.db.common.request.PossibleIndexRoot;
49 import org.simantics.db.common.request.UniqueRead;
50 import org.simantics.db.common.request.WriteRequest;
51 import org.simantics.db.common.utils.NameUtils;
52 import org.simantics.db.exception.DatabaseException;
53 import org.simantics.db.layer0.adapter.CopyHandler2;
54 import org.simantics.db.layer0.adapter.Instances;
55 import org.simantics.db.layer0.util.Layer0Utils;
56 import org.simantics.db.layer0.variable.Variable;
57 import org.simantics.db.layer0.variable.Variables;
58 import org.simantics.layer0.Layer0;
59 import org.simantics.modeling.ui.Activator;
60 import org.simantics.modeling.ui.componentTypeEditor.LiftPropertiesDialog.LiftedProperty;
61 import org.simantics.modeling.userComponent.ComponentTypeCommands;
62 import org.simantics.selectionview.SelectionViewResources;
63 import org.simantics.structural.stubs.StructuralResource2;
64 import org.simantics.utils.datastructures.Pair;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
68 public class ConfigurationPropertiesSection implements ComponentTypeViewerSection {
70 private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationPropertiesSection.class);
72 private static final String[] COLUMN_NAMES =
73 new String[] {"Name", "Type", "Default Value", "Unit", "Range", "Label", "Description"};
74 private static final int[] COLUMN_LENGTHS =
75 new int[] { 120, 100, 100, 50, 100, 100, 100 };
76 private static final int[] COLUMN_WEIGHTS =
77 new int[] { 0, 0, 0, 0, 0, 50, 100 };
80 * Configuration property table column indexes that are to be considered
81 * immutable when the property relation is immutable. Note that relation
82 * immutability does not make the asserted default value immutable.
84 private static final int[] IMMUTABLE_COLUMNS_WITH_IMMUTABLE_RELATION =
87 ComponentTypeViewerData data;
89 TableColumn[] columns;
92 Button removeProperty;
93 Button liftProperties;
95 boolean hasTypes = false;
100 public ConfigurationPropertiesSection(ComponentTypeViewerData data) {
102 FormToolkit tk = data.tk;
103 Form form = data.form;
105 section = tk.createSection(form.getBody(), Section.TITLE_BAR | Section.EXPANDED);
106 section.setLayout(new FillLayout());
107 section.setText("Configuration properties");
109 Composite sectionBody = tk.createComposite(section);
110 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(sectionBody);
111 section.setClient(sectionBody);
113 Composite tableComposite = tk.createComposite(sectionBody);
114 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(tableComposite);
115 TableColumnLayout tcl = new TableColumnLayout();
116 tableComposite.setLayout(tcl);
118 table = tk.createTable(tableComposite, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER);
119 table.setLinesVisible(true);
120 table.setHeaderVisible(true);
122 columns = new TableColumn[COLUMN_NAMES.length];
123 for(int i=0;i<COLUMN_NAMES.length;++i) {
124 TableColumn column = new TableColumn(table, SWT.NONE);
126 tcl.setColumnData(column, new ColumnWeightData(COLUMN_WEIGHTS[i], COLUMN_LENGTHS[i], true));
127 column.setText(COLUMN_NAMES[i]);
131 editor = new TableEditor(table);
132 editor.grabHorizontal = true;
133 editor.grabVertical = true;
134 editor.horizontalAlignment = SWT.LEFT;
135 table.addMouseListener(new MouseAdapter() {
137 public void mouseDown(MouseEvent e) {
138 // Clean up any previous editor control
139 Control oldEditor = editor.getEditor();
140 if (oldEditor != null) oldEditor.dispose();
146 Rectangle tableBounds = table.getClientArea();
147 int rx = e.x - tableBounds.x;
148 int ry = e.y - tableBounds.y;
151 TableItem selectedItem = null;
152 int selectedColumn = -1;
153 Rectangle selectedItemBounds = null;
154 for(TableItem item : table.getItems()) {
155 for(int column = 0;column < COLUMN_NAMES.length;++column) {
156 Rectangle bounds = item.getBounds(column);
157 if(bounds.contains(rx, ry)) {
158 selectedItemBounds = bounds;
160 selectedColumn = column;
165 if(selectedItem == null) {
170 final int column = selectedColumn;
171 final ComponentTypeViewerPropertyInfo propertyInfo = (ComponentTypeViewerPropertyInfo)selectedItem.getData();
172 final Resource resource = propertyInfo.resource;
175 data.editName(table, editor, propertyInfo, selectedItem, column, ComponentTypeViewerData.PROPERTY_NAME_PATTERN);
179 data.editType(table, editor, propertyInfo, selectedItem, column, true);
183 data.editValue(table, editor, propertyInfo, selectedItem, column, data.readOnly ? null : new StringWriter() {
185 public void perform(WriteGraph graph, String newValue) throws DatabaseException {
186 graph.markUndoPoint();
187 ComponentTypeCommands.setDefaultValue(graph, data.componentType, propertyInfo.resource, newValue);
193 data.editUnit(table, editor, propertyInfo, selectedItem, column);
197 data.editRange(table, editor, propertyInfo, selectedItem, selectedItemBounds, column);
201 data.editValue(table, editor, propertyInfo, selectedItem, column, propertyInfo.immutable ? null : new StringWriter() {
203 public void perform(WriteGraph graph, String newValue) throws DatabaseException {
204 graph.markUndoPoint();
205 String value = newValue.isEmpty() ? null : newValue;
206 ComponentTypeCommands.setLabel(graph, resource, value);
212 data.editMultilineText(table, editor, propertyInfo, selectedItem, selectedItemBounds, column, new StringWriter() {
214 public void perform(WriteGraph graph, String newValue) throws DatabaseException {
215 graph.markUndoPoint();
216 String value = newValue.isEmpty() ? null : newValue;
217 ComponentTypeCommands.setDescription(graph, resource, value);
227 Composite buttons = tk.createComposite(sectionBody);
228 GridDataFactory.fillDefaults().applyTo(buttons);
229 GridLayoutFactory.fillDefaults().applyTo(buttons);
231 newProperty = tk.createButton(buttons, "New property", SWT.PUSH);
232 GridDataFactory.fillDefaults().applyTo(newProperty);
233 removeProperty = tk.createButton(buttons, "Remove property", SWT.PUSH);
234 GridDataFactory.fillDefaults().applyTo(removeProperty);
236 liftProperties = tk.createButton(buttons, "Lift Properties", SWT.PUSH);
237 GridDataFactory.fillDefaults().applyTo(liftProperties);
239 hasTypes = !getTypes().isEmpty();
242 setTypes = tk.createButton(buttons, "Assign Types", SWT.PUSH);
243 GridDataFactory.fillDefaults().applyTo(setTypes);
248 table.addSelectionListener(new SelectionAdapter() {
250 public void widgetSelected(SelectionEvent e) {
251 TableItem[] sel = table.getSelection();
252 for (TableItem item : sel) {
253 ComponentTypeViewerPropertyInfo pi = (ComponentTypeViewerPropertyInfo) item.getData();
255 removeProperty.setEnabled(false);
259 removeProperty.setEnabled(true);
263 newProperty.addSelectionListener(new SelectionAdapter() {
265 public void widgetSelected(SelectionEvent e) {
266 if(editor.getEditor() != null)
267 editor.getEditor().dispose();
268 Simantics.getSession().async(new WriteRequest() {
270 public void perform(WriteGraph graph)
271 throws DatabaseException {
272 ComponentTypeCommands.createPropertyWithDefaults(graph, data.componentType);
278 removeProperty.addSelectionListener(new SelectionAdapter() {
280 public void widgetSelected(SelectionEvent e) {
281 if(editor.getEditor() != null)
282 editor.getEditor().dispose();
283 final List<Resource> propertiesToBeRemoved =
285 for(TableItem item : table.getSelection()) {
286 ComponentTypeViewerPropertyInfo info = (ComponentTypeViewerPropertyInfo) item.getData();
288 propertiesToBeRemoved.add(info.resource);
290 System.out.println("remove " + propertiesToBeRemoved.size() + " resources");
291 if(!propertiesToBeRemoved.isEmpty())
292 Simantics.getSession().async(new WriteRequest() {
294 public void perform(WriteGraph graph)
295 throws DatabaseException {
296 graph.markUndoPoint();
297 for(Resource property : propertiesToBeRemoved)
298 ComponentTypeCommands.removeProperty(graph, data.componentType, property);
304 liftProperties.addSelectionListener(new SelectionAdapter() {
306 public void widgetSelected(SelectionEvent e) {
308 if(editor.getEditor() != null)
309 editor.getEditor().dispose();
313 Map<LiftedProperty, Pair<String, ImageDescriptor>> map = Simantics.sync(new UniqueRead<Map<LiftedProperty,Pair<String,ImageDescriptor>>>() {
316 public Map<LiftedProperty, Pair<String, ImageDescriptor>> perform(ReadGraph graph) throws DatabaseException {
318 Map<LiftedProperty, Pair<String,ImageDescriptor>> map = new HashMap<>();
320 Layer0 L0 = Layer0.getInstance(graph);
321 StructuralResource2 STR = StructuralResource2.getInstance(graph);
322 SelectionViewResources SEL = SelectionViewResources.getInstance(graph);
324 Resource composite = graph.getPossibleObject(data.componentType, STR.IsDefinedBy);
325 if(composite == null) return map;
328 Set<String> existing = new HashSet<>();
329 for(Resource predicate : graph.getObjects(data.componentType, L0.DomainOf)) {
330 if(graph.isSubrelationOf(predicate, L0.HasProperty)) {
331 existing.add(NameUtils.getSafeName(graph, predicate));
335 for(Resource component : graph.syncRequest(new ObjectsWithType(composite, L0.ConsistsOf, STR.Component))) {
337 Resource type = graph.getPossibleType(component, STR.Component);
338 if(type == null) continue;
340 String componentName = NameUtils.getSafeName(graph, component);
342 for(Resource predicate : graph.getPredicates(component)) {
343 if(graph.isSubrelationOf(predicate, L0.HasProperty)) {
345 // Do not list properties shown under other properties
346 if(graph.hasStatement(predicate, SEL.IsShownUnder)) continue;
348 // Do not list properties that are not visible in selection view
349 if(!graph.hasStatement(predicate, SEL.HasStandardPropertyInfo)) continue;
351 // Some properties are explicitly marked as non-liftable
352 Boolean canBeLifted = graph.getPossibleRelatedValue(predicate, SEL.canBeLifted, Bindings.BOOLEAN);
353 if(canBeLifted != null && !canBeLifted) continue;
355 String predicateName = NameUtils.getSafeName(graph, predicate);
356 if(existing.contains(predicateName)) continue;
358 String name = componentName + " " + predicateName;
359 map.put(new LiftedProperty(component, type, predicate), new Pair<String, ImageDescriptor>(name, null));
372 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
373 LiftPropertiesDialog dialog = new LiftPropertiesDialog(shell, map, "Select properties to lift") {
375 protected IDialogSettings getBaseDialogSettings() {
376 return Activator.getDefault().getDialogSettings();
379 if (dialog.open() == Window.OK) {
380 final Collection<LiftedProperty> _result = dialog.getResultT();
381 final boolean mapProperties = dialog.getMapProperties();
382 if (!_result.isEmpty()) {
383 Simantics.getSession().async(new WriteRequest() {
384 public Resource findAssertion(ReadGraph graph, Resource sourceType, Resource predicate) throws DatabaseException {
385 Collection<Resource> ass = graph.getAssertedObjects(sourceType, predicate);
386 if(ass.size() == 1) return ass.iterator().next();
390 public void processSubs(ReadGraph graph, Resource predicate, Resource component, Resource componentType, List<LiftedProperty> result, List<Resource> properties, List<Resource> assertions) throws DatabaseException {
391 SelectionViewResources SEL = SelectionViewResources.getInstance(graph);
392 for(Resource sub : graph.getObjects(predicate, SEL.UnderOf)) {
393 Resource ass = findAssertion(graph, componentType, sub);
394 if(ass == null) continue;
395 result.add(new LiftedProperty(component, componentType, sub));
398 processSubs(graph, sub, component, componentType, result, properties, assertions);
403 public void perform(WriteGraph graph) throws DatabaseException {
405 Layer0 L0 = Layer0.getInstance(graph);
406 graph.markUndoPoint();
407 List<Resource> properties = new ArrayList<>();
408 List<Resource> assertions = new ArrayList<>();
410 List<LiftedProperty> result = new ArrayList<>();
411 for(LiftedProperty p : _result) {
412 Resource ass = findAssertion(graph, p.getComponentType(), p.getPredicate());
413 if(ass == null) continue;
415 properties.add(p.getPredicate());
417 processSubs(graph, p.getPredicate(), p.getComponent(), p.getComponentType(), result, properties, assertions);
420 CopyHandler2 ch = Layer0Utils.getPossibleCopyHandler(graph, properties);
421 Collection<Resource> copies = Layer0Utils.copyTo(graph, data.componentType, null, ch, null);
423 for(Resource copy : copies) {
424 Resource ass = assertions.get(index);
425 LiftedProperty p = result.get(index);
426 Collection<Resource> copyAss = Layer0Utils.copyTo(graph, null, ass);
427 if(copyAss.size() == 1) {
428 graph.deny(copy, L0.HasDomain);
429 graph.claim(data.componentType, L0.DomainOf, copy);
430 Layer0Utils.assert_(graph, data.componentType, copy, copyAss.iterator().next());
431 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
432 graph.addMetadata(cm.add("Lifted property " + NameUtils.getSafeName(graph, copy) + " into "+ NameUtils.getSafeName(graph, data.componentType)));
435 Variable v = Variables.getVariable(graph, p.getComponent());
436 Variable property = v.getProperty(graph, p.getPredicate());
437 Variable displayValue = property.getProperty(graph, Variables.DISPLAY_VALUE);
438 displayValue.setValue(graph, "=" + NameUtils.getSafeName(graph, p.getPredicate()), Bindings.STRING);
448 } catch (DatabaseException e1) {
450 LOGGER.error("Lifting properties failed", e1);
460 setTypes.addSelectionListener(new SelectionAdapter() {
462 public void widgetSelected(SelectionEvent e) {
463 if(editor.getEditor() != null)
464 editor.getEditor().dispose();
465 final List<Resource> propertiesToSet =
467 for(TableItem item : table.getSelection())
468 propertiesToSet.add(((ComponentTypeViewerPropertyInfo)item.getData()).resource);
470 if(propertiesToSet.size() != 1) return;
472 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
473 SetTypesDialog page = new SetTypesDialog(shell, getTypes(), "Select user types for property");
474 if (page.open() == Window.OK) {
475 final Object[] result = page.getResult();
476 if (result != null && result.length > 0) {
477 Simantics.getSession().async(new WriteRequest() {
479 public void perform(WriteGraph graph)
480 throws DatabaseException {
481 for(Object type : result) {
482 Layer0 L0 = Layer0.getInstance(graph);
483 graph.claim(propertiesToSet.get(0), L0.InstanceOf, null, (Resource)type);
495 table.addDisposeListener(new DisposeListener() {
497 public void widgetDisposed(DisposeEvent e) {
503 public void update(ComponentTypePropertiesResult result) {
504 if (table.isDisposed())
507 // Save old selection
508 Set<ComponentTypeViewerPropertyInfo> selected = new HashSet<>();
509 List<TableItem> selectedItems = new ArrayList<>(selected.size());
510 for (int i : table.getSelectionIndices()) {
511 TableItem item = table.getItem(i);
512 selected.add((ComponentTypeViewerPropertyInfo) item.getData());
515 int topIndex = table.getTopIndex();
519 if(editor.getEditor() != null)
520 editor.getEditor().dispose();
522 for(ComponentTypeViewerPropertyInfo info : result.getProperties()) {
523 boolean immutable = result.isImmutable() || info.immutable;
524 Color fg = immutable ? table.getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY) : null;
525 if(info.sectionSpecificData != null)
528 TableItem item = new TableItem(table, SWT.NONE);
530 item.setText(0, info.name);
531 item.setText(1, info.type);
532 item.setText(2, info.defaultValue);
533 item.setText(3, info.unitString());
534 item.setText(4, info.rangeString());
535 item.setText(5, info.label);
536 item.setText(6, info.description);
538 for (int columnIndex : IMMUTABLE_COLUMNS_WITH_IMMUTABLE_RELATION)
539 item.setForeground(columnIndex, fg);
543 if (selected.contains(info))
544 selectedItems.add(item);
547 // Restore old selection
548 table.setTopIndex(topIndex);
549 table.setSelection(selectedItems.toArray(new TableItem[selectedItems.size()]));
553 private Map<Resource, Pair<String, ImageDescriptor>> getTypes() {
555 return Simantics.getSession().syncRequest(new UniqueRead<Map<Resource, Pair<String, ImageDescriptor>>>() {
557 public Map<Resource, Pair<String, ImageDescriptor>> perform(ReadGraph graph)
558 throws DatabaseException {
559 StructuralResource2 STR = StructuralResource2.getInstance(graph);
560 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(data.componentType));
561 Instances query = graph.adapt(STR.UserDefinedProperty, Instances.class);
562 Collection<Resource> types = query.find(graph, indexRoot);
563 Map<Resource, Pair<String, ImageDescriptor>> result = new HashMap<>();
564 for(Resource type : types) {
565 String name = NameUtils.getSafeLabel(graph, type);
566 result.put(type, new Pair<String, ImageDescriptor>(name, null));
571 } catch (DatabaseException e) {
572 LOGGER.error("Finding UserDefinedProperties failed.", e);
573 return Collections.emptyMap();
578 public void setReadOnly(boolean readOnly) {
579 boolean e = !readOnly;
580 newProperty.setEnabled(e);
581 removeProperty.setEnabled(e);
582 liftProperties.setEnabled(e);
586 public Section getSection() {
591 public double getPriority() {
596 public Object getSectionSpecificData(ReadGraph graph,
597 ComponentTypeViewerPropertyInfo info) throws DatabaseException {
602 public double getDataPriority() {