1 package org.simantics.modeling.ui.componentTypeEditor;
3 import java.util.ArrayList;
4 import java.util.Collections;
6 import java.util.regex.Matcher;
7 import java.util.regex.Pattern;
9 import org.eclipse.jface.dialogs.IMessageProvider;
10 import org.eclipse.jface.layout.GridDataFactory;
11 import org.eclipse.jface.layout.GridLayoutFactory;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.custom.StyledText;
14 import org.eclipse.swt.custom.TableEditor;
15 import org.eclipse.swt.events.SelectionAdapter;
16 import org.eclipse.swt.events.SelectionEvent;
17 import org.eclipse.swt.graphics.Point;
18 import org.eclipse.swt.graphics.Rectangle;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.Combo;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Display;
23 import org.eclipse.swt.widgets.Event;
24 import org.eclipse.swt.widgets.Label;
25 import org.eclipse.swt.widgets.Shell;
26 import org.eclipse.swt.widgets.Table;
27 import org.eclipse.swt.widgets.TableItem;
28 import org.eclipse.swt.widgets.Text;
29 import org.eclipse.ui.forms.widgets.Form;
30 import org.eclipse.ui.forms.widgets.FormToolkit;
31 import org.simantics.Simantics;
32 import org.simantics.databoard.type.NumberType;
33 import org.simantics.databoard.units.internal.library.UnitLibrary;
34 import org.simantics.databoard.util.Limit;
35 import org.simantics.databoard.util.Range;
36 import org.simantics.databoard.util.RangeException;
37 import org.simantics.db.RequestProcessor;
38 import org.simantics.db.Resource;
39 import org.simantics.db.WriteGraph;
40 import org.simantics.db.common.NamedResource;
41 import org.simantics.db.common.request.WriteRequest;
42 import org.simantics.db.exception.DatabaseException;
43 import org.simantics.layer0.Layer0;
44 import org.simantics.modeling.userComponent.ComponentTypeCommands;
45 import org.simantics.scl.runtime.function.Function2;
46 import org.simantics.scl.runtime.function.Function4;
47 import org.simantics.utils.ui.ErrorLogger;
49 public class ComponentTypeViewerData {
51 * Used to validate property names.
53 public static final Pattern PROPERTY_NAME_PATTERN =
54 Pattern.compile("([a-z]|_[0-9a-zA-Z_])[0-9a-zA-Z_]*");
56 public static final String[] PROPERTY_TYPE_SUGGESTIONS = new String[] {
77 public Resource componentType;
78 public FormToolkit tk;
80 public UnitLibrary unitLibrary = UnitLibrary.createDefault();
81 public boolean readOnly;
82 public NamedResource[] connectionPoints;
83 public ComponentTypeViewerPropertyInfo[] properties;
85 public ComponentTypeViewerData(FormToolkit tk, Resource componentType, Form form) {
87 this.componentType = componentType;
91 public void editName(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column,
92 Pattern namePattern) {
93 editName(table, editor, propertyInfo, selectedItem, column,
94 (pInfo, name) -> validatePropertyName(pInfo, name, namePattern));
97 public void editName(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column,
98 Function2<ComponentTypeViewerPropertyInfo, String, String> nameValidator) {
99 int extraStyle = propertyInfo.immutable ? SWT.READ_ONLY : 0;
100 final Text text = new Text(table, SWT.NONE | extraStyle);
101 org.eclipse.swt.widgets.Listener listener =
102 new org.eclipse.swt.widgets.Listener() {
104 public void handleEvent(Event e) {
105 if (e.type == SWT.Dispose) {
106 form.setMessage(null);
110 if (e.type == SWT.Modify) {
111 // validate current name
112 String error = nameValidator.apply(propertyInfo, text.getText());
114 text.setBackground(text.getDisplay().getSystemColor(SWT.COLOR_RED));
115 form.setMessage(error, IMessageProvider.ERROR);
117 text.setBackground(null);
118 form.setMessage(null);
123 if (e.type == SWT.Traverse) {
124 if (e.detail == SWT.TRAVERSE_ESCAPE) {
129 if (e.detail == SWT.TRAVERSE_ARROW_NEXT || e.detail == SWT.TRAVERSE_ARROW_PREVIOUS || e.detail == SWT.TRAVERSE_MNEMONIC)
133 final String newValue = text.getText();
136 String error = nameValidator.apply(propertyInfo, newValue);
140 if (propertyInfo.immutable)
143 Simantics.getSession().async(new WriteRequest() {
145 public void perform(WriteGraph graph)
146 throws DatabaseException {
147 graph.markUndoPoint();
148 Layer0 L0 = Layer0.getInstance(graph);
149 String prevName = graph.getPossibleRelatedValue2(propertyInfo.resource, L0.HasName);
150 String oldCamelCasedLabel = prevName != null ? ComponentTypeCommands.camelCaseNameToLabel(prevName) : "";
151 String oldLabel = graph.getPossibleRelatedValue(propertyInfo.resource, L0.HasLabel);
152 boolean setLabel = oldLabel == null
153 || oldLabel.isEmpty()
154 || oldCamelCasedLabel.isEmpty()
155 || oldCamelCasedLabel.equals(oldLabel);
157 ComponentTypeCommands.rename(graph, propertyInfo.resource, newValue);
159 ComponentTypeCommands.setLabel(graph, propertyInfo.resource, ComponentTypeCommands.camelCaseNameToLabel(newValue));
164 text.addListener(SWT.Modify, listener);
165 text.addListener(SWT.Deactivate, listener);
166 text.addListener(SWT.Traverse, listener);
167 text.addListener(SWT.Dispose, listener);
169 text.setText(selectedItem.getText(column));
173 editor.setEditor(text, selectedItem, column);
176 public void editType(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column, final boolean convertDefaultValue) {
177 int extraStyle = propertyInfo.immutable ? SWT.READ_ONLY : 0;
178 final Combo combo = new Combo(table, SWT.NONE | extraStyle);
179 combo.setText(selectedItem.getText(column));
180 for(String suggestion : PROPERTY_TYPE_SUGGESTIONS)
181 combo.add(suggestion);
182 org.eclipse.swt.widgets.Listener listener =
183 new org.eclipse.swt.widgets.Listener() {
185 public void handleEvent(Event e) {
186 if(e.type == SWT.Traverse) {
187 if (e.detail == SWT.TRAVERSE_ESCAPE) {
192 if (e.detail == SWT.TRAVERSE_ARROW_NEXT
193 || e.detail == SWT.TRAVERSE_ARROW_PREVIOUS
194 || e.detail == SWT.TRAVERSE_MNEMONIC)
197 final String newValue = combo.getText();
198 if (e.type == SWT.Traverse) {
203 if (propertyInfo.immutable)
206 Simantics.getSession().async(new WriteRequest() {
208 public void perform(WriteGraph graph)
209 throws DatabaseException {
210 graph.markUndoPoint();
211 ComponentTypeCommands.editType(graph, componentType, propertyInfo.resource, convertDefaultValue, newValue);
217 editor.setEditor(combo, selectedItem, column);
218 combo.addListener(SWT.FocusOut, listener);
219 combo.addListener(SWT.Traverse, listener);
222 protected void editUnit(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column) {
223 // Disallow unit editing for non-numeric configuration properties
224 if (propertyInfo.numberType == null && propertyInfo.sectionSpecificData == null)
227 int extraStyle = propertyInfo.immutable ? SWT.READ_ONLY : 0;
228 final Combo combo = new Combo(table, SWT.NONE | extraStyle);
229 String initialValue = selectedItem.getText(column);
230 List<String> units = new ArrayList<>( unitLibrary.getUnits() );
231 Collections.sort(units, String.CASE_INSENSITIVE_ORDER);
234 for (String unit : units) {
236 if (unit.equals(initialValue))
240 combo.setText(initialValue);
242 org.eclipse.swt.widgets.Listener listener =
243 new org.eclipse.swt.widgets.Listener() {
245 public void handleEvent(Event e) {
246 if(e.type == SWT.Traverse) {
247 if (e.detail == SWT.TRAVERSE_ESCAPE) {
252 if (e.detail == SWT.TRAVERSE_ARROW_NEXT
253 || e.detail == SWT.TRAVERSE_ARROW_PREVIOUS
254 || e.detail == SWT.TRAVERSE_MNEMONIC)
257 final String newValue = combo.getText();
258 if(e.type == SWT.Traverse) {
263 if (propertyInfo.immutable)
266 Simantics.getSession().async(new WriteRequest() {
268 public void perform(WriteGraph graph)
269 throws DatabaseException {
270 graph.markUndoPoint();
271 ComponentTypeCommands.setUnit(graph, componentType, propertyInfo.resource, newValue);
277 editor.setEditor(combo, selectedItem, column);
278 combo.addListener(SWT.Deactivate, listener);
279 combo.addListener(SWT.Traverse, listener);
282 public void editValue(Table table, TableEditor editor,
283 final ComponentTypeViewerPropertyInfo propertyInfo,
284 TableItem selectedItem, int column,
285 final StringWriter writer,
286 final Function4<RequestProcessor, Resource, Resource, String, String> validator)
288 int extraStyle = writer == null ? SWT.READ_ONLY : 0;
289 final Text text = new Text(table, SWT.NONE | extraStyle);
290 text.setText(selectedItem.getText(column));
291 org.eclipse.swt.widgets.Listener listener =
292 new org.eclipse.swt.widgets.Listener() {
294 public void handleEvent(Event e) {
295 if(e.type == SWT.Traverse) {
296 if (e.detail == SWT.TRAVERSE_ESCAPE) {
301 if (e.detail == SWT.TRAVERSE_ARROW_NEXT
302 || e.detail == SWT.TRAVERSE_ARROW_PREVIOUS
303 || e.detail == SWT.TRAVERSE_MNEMONIC)
306 final String newValue = text.getText();
307 if(e.type == SWT.Traverse) {
312 String error = validator.apply(Simantics.getSession(), componentType, propertyInfo.resource, newValue);
316 if (writer != null) {
317 Simantics.getSession().async(new WriteRequest() {
319 public void perform(WriteGraph graph) throws DatabaseException {
320 writer.perform(graph, newValue);
328 editor.setEditor(text, selectedItem, column);
329 text.addListener(SWT.FocusOut, listener);
330 text.addListener(SWT.Traverse, listener);
332 if (validator != null) {
333 org.eclipse.swt.widgets.Listener validationListener = new org.eclipse.swt.widgets.Listener() {
335 public void handleEvent(Event e) {
336 final String newValue = text.getText();
337 String error = validator.apply(Simantics.getSession(), componentType, propertyInfo.resource, newValue);
339 text.setBackground(text.getDisplay().getSystemColor(SWT.COLOR_RED));
340 text.setToolTipText(error);
343 text.setBackground(null);
344 text.setToolTipText(null);
348 text.addListener(SWT.Modify, validationListener);
352 private Range parseRange(NumberType numberType, String minStr, String maxStr, boolean lowInclusive, boolean highInclusive) throws RangeException {
354 String rangeStr = (lowInclusive ? "[" : "(") + minStr + ".." + maxStr + (highInclusive ? "]" : ")");
355 return Range.valueOf(rangeStr);
356 } catch (IllegalArgumentException e) {
357 // Limits are invalid
358 throw new RangeException(e.getMessage(), e);
362 private static Combo createRangeInclusionCombo(Composite parent, boolean inclusive) {
363 Combo rng = new Combo(parent, SWT.READ_ONLY);
364 rng.add("Inclusive");
365 rng.add("Exclusive");
366 rng.select(inclusive ? 0 : 1);
370 protected void editRange(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, Rectangle selectedItemBounds, int column) {
371 // Disallow range editing when the property is not numeric
372 if (propertyInfo.numberType == null)
375 int extraTextStyle = propertyInfo.immutable ? SWT.READ_ONLY : 0;
377 // Parse initial range value
379 String rangeStr = selectedItem.getText(column);
381 range = Range.valueOf(rangeStr);
382 } catch (RangeException ex) {
383 range = new Range(Limit.nolimit(), Limit.nolimit());
386 final Shell shell = new Shell(table.getShell(), SWT.ON_TOP);
387 GridLayoutFactory.fillDefaults().applyTo(shell);
389 Composite composite = new Composite(shell, SWT.NONE);
390 GridDataFactory.fillDefaults().grab(true, true).applyTo(composite);
391 GridLayoutFactory.swtDefaults().numColumns(3).applyTo(composite);
393 Label low = new Label(composite, SWT.NONE);
394 low.setText("Minimum Value:");
395 final Text lowText = new Text(composite, SWT.BORDER | extraTextStyle);
396 GridDataFactory.fillDefaults().grab(true, false).hint(100, SWT.DEFAULT).applyTo(lowText);
397 final Combo lowSelector = createRangeInclusionCombo(composite, !range.getLower().isExclusive());
398 Label high = new Label(composite, SWT.NONE);
399 high.setText("Maximum Value:");
400 final Text highText = new Text(composite, SWT.BORDER | extraTextStyle);
401 GridDataFactory.fillDefaults().grab(true, false).hint(100, SWT.DEFAULT).applyTo(highText);
402 final Combo highSelector = createRangeInclusionCombo(composite, !range.getUpper().isExclusive());
404 Composite buttonComposite = new Composite(shell, SWT.NONE);
405 GridDataFactory.fillDefaults().grab(true, false).align(SWT.TRAIL, SWT.FILL).applyTo(buttonComposite);
406 GridLayoutFactory.swtDefaults().numColumns(2).equalWidth(true).applyTo(buttonComposite);
408 Button ok = new Button(buttonComposite, SWT.NONE);
410 GridDataFactory.swtDefaults().align(SWT.FILL, SWT.CENTER).applyTo(ok);
411 Button cancel = new Button(buttonComposite, SWT.NONE);
412 cancel.setText("&Cancel");
413 GridDataFactory.swtDefaults().align(SWT.FILL, SWT.CENTER).applyTo(ok);
415 if (range.getLower().getValue() != null)
416 lowText.setText(range.getLower().getValue().toString());
417 if (range.getUpper().getValue() != null)
418 highText.setText(range.getUpper().getValue().toString());
420 shell.addListener(SWT.Deactivate, new org.eclipse.swt.widgets.Listener() {
422 public void handleEvent(Event event) {
427 ok.addSelectionListener(new SelectionAdapter() {
428 public void widgetSelected(SelectionEvent e) {
430 final Range newRange = parseRange(propertyInfo.numberType,
431 lowText.getText().trim(),
432 highText.getText().trim(),
433 lowSelector.getSelectionIndex() == 0 ? true : false,
434 highSelector.getSelectionIndex() == 0 ? true : false);
438 if (propertyInfo.immutable)
441 Simantics.getSession().async(new WriteRequest() {
443 public void perform(WriteGraph graph)
444 throws DatabaseException {
445 graph.markUndoPoint();
446 ComponentTypeCommands.setRange(graph, componentType, propertyInfo.resource, newRange == null ? null : newRange.toString());
449 } catch (RangeException ex) {
450 ErrorLogger.defaultLogError(ex);
454 cancel.addSelectionListener(new SelectionAdapter() {
455 public void widgetSelected(SelectionEvent e) {
461 Point size = shell.getSize();
463 Display display = table.getDisplay();
464 Rectangle clientArea = display.getClientArea();
465 Point bt = table.toDisplay(selectedItemBounds.x, selectedItemBounds.y);
466 Rectangle b = selectedItemBounds;
471 if ((b.x + b.width) > clientArea.width)
472 b.x -= b.x + b.width - clientArea.width;
473 if (b.height > clientArea.height)
474 b.height = clientArea.height;
475 if ((b.y + b.height) > clientArea.height)
476 b.y -= b.y + b.height - clientArea.height;
478 shell.setBounds(selectedItemBounds);
482 protected void editMultilineText(Table table, TableEditor editor,
483 final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem,
484 Rectangle selectedItemBounds, int column, final StringWriter writer)
486 final Shell shell = new Shell(table.getShell(), SWT.ON_TOP);
487 GridLayoutFactory.fillDefaults().spacing(0, 0).applyTo(shell);
488 final StyledText text = new StyledText(shell, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | (propertyInfo.immutable ? SWT.READ_ONLY : 0));
489 GridDataFactory.fillDefaults().grab(true, true).applyTo(text);
490 text.setText(selectedItem.getText(column));
491 org.eclipse.swt.widgets.Listener listener =
492 new org.eclipse.swt.widgets.Listener() {
494 public void handleEvent(Event e) {
495 final String newValue = text.getText();
497 if (e.type == SWT.Traverse) {
498 if (e.detail == SWT.TRAVERSE_ESCAPE) {
503 if (e.detail == SWT.TRAVERSE_ARROW_NEXT
504 || e.detail == SWT.TRAVERSE_ARROW_PREVIOUS
505 || e.detail == SWT.TRAVERSE_MNEMONIC)
507 if ((e.stateMask & SWT.CTRL) == 0)
514 if (propertyInfo.immutable)
517 if (writer != null) {
518 Simantics.getSession().async(new WriteRequest() {
520 public void perform(WriteGraph graph) throws DatabaseException {
521 writer.perform(graph, newValue);
528 String helpText = propertyInfo.immutable ? "ESC to close." : "Ctrl+Enter to apply changes, ESC to cancel.";
529 Label help = tk.createLabel(shell, helpText, SWT.BORDER | SWT.FLAT);
530 GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).applyTo(help);
531 help.setForeground( tk.getColors().createColor( "fg", tk.getColors().getSystemColor(SWT.COLOR_LIST_SELECTION) ) );
533 Display display = table.getDisplay();
534 Rectangle clientArea = display.getClientArea();
535 Point bt = table.toDisplay(selectedItemBounds.x, selectedItemBounds.y);
536 Rectangle b = selectedItemBounds;
540 if ((b.x + b.width) > clientArea.width)
541 b.x -= b.x + b.width - clientArea.width;
542 if (b.height > clientArea.height)
543 b.height = clientArea.height;
544 if ((b.y + b.height) > clientArea.height)
545 b.y -= b.y + b.height - clientArea.height;
547 shell.setBounds(selectedItemBounds);
553 text.addListener(SWT.Traverse, listener);
554 shell.addListener(SWT.Deactivate, listener);
557 private String validatePropertyName(ComponentTypeViewerPropertyInfo propertyInfo, String propertyName, Pattern namePattern) {
558 if (propertyName.equals(propertyInfo.name))
560 for (ComponentTypeViewerPropertyInfo info : properties) {
561 if (propertyName.equals(info.name))
562 return "Property name '" + propertyName + "' is already in use.";
564 for (NamedResource cp : connectionPoints) {
565 if (propertyName.equals(cp.getName()))
566 return "Name '" + propertyName + "' is already used for a terminal.";
568 Matcher m = namePattern.matcher(propertyName);
570 return "Property name '" + propertyName + "' contains invalid characters, does not match pattern "
571 + namePattern.pattern() + ".";