Fix change of type in component property editor when range is present
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / componentTypeEditor / ComponentTypeViewerData.java
1 package org.simantics.modeling.ui.componentTypeEditor;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6 import java.util.concurrent.ScheduledFuture;
7 import java.util.concurrent.TimeUnit;
8 import java.util.regex.Matcher;
9 import java.util.regex.Pattern;
10
11 import org.eclipse.jface.dialogs.IDialogConstants;
12 import org.eclipse.jface.dialogs.IMessageProvider;
13 import org.eclipse.jface.layout.GridDataFactory;
14 import org.eclipse.jface.layout.GridLayoutFactory;
15 import org.eclipse.osgi.util.NLS;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.custom.StyledText;
18 import org.eclipse.swt.custom.TableEditor;
19 import org.eclipse.swt.events.SelectionAdapter;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.graphics.Point;
22 import org.eclipse.swt.graphics.Rectangle;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Combo;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Display;
27 import org.eclipse.swt.widgets.Event;
28 import org.eclipse.swt.widgets.Label;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.swt.widgets.Table;
31 import org.eclipse.swt.widgets.TableItem;
32 import org.eclipse.swt.widgets.Text;
33 import org.eclipse.ui.forms.widgets.Form;
34 import org.eclipse.ui.forms.widgets.FormToolkit;
35 import org.simantics.Simantics;
36 import org.simantics.databoard.type.NumberType;
37 import org.simantics.databoard.units.internal.library.UnitLibrary;
38 import org.simantics.databoard.util.Limit;
39 import org.simantics.databoard.util.Range;
40 import org.simantics.databoard.util.RangeException;
41 import org.simantics.db.RequestProcessor;
42 import org.simantics.db.Resource;
43 import org.simantics.db.WriteGraph;
44 import org.simantics.db.common.NamedResource;
45 import org.simantics.db.common.request.WriteRequest;
46 import org.simantics.db.exception.DatabaseException;
47 import org.simantics.db.function.DbConsumer;
48 import org.simantics.layer0.Layer0;
49 import org.simantics.modeling.userComponent.ComponentTypeCommands;
50 import org.simantics.scl.runtime.function.Function2;
51 import org.simantics.scl.runtime.function.Function4;
52 import org.simantics.utils.threads.ThreadUtils;
53 import org.simantics.utils.ui.ErrorLogger;
54
55 public class ComponentTypeViewerData {
56     /**
57      * Used to validate property names.
58      */
59     public static final Pattern PROPERTY_NAME_PATTERN =
60             Pattern.compile("([a-z]|_[0-9a-zA-Z_])[0-9a-zA-Z_]*"); //$NON-NLS-1$
61
62     public static final String[] PROPERTY_TYPE_SUGGESTIONS = new String[] {
63         "Double", //$NON-NLS-1$
64         "Integer", //$NON-NLS-1$
65         "Float", //$NON-NLS-1$
66         "String", //$NON-NLS-1$
67         "Boolean", //$NON-NLS-1$
68         "Long", //$NON-NLS-1$
69         "[Double]", //$NON-NLS-1$
70         "[Integer]", //$NON-NLS-1$
71         "[Float]", //$NON-NLS-1$
72         "[String]", //$NON-NLS-1$
73         "[Boolean]", //$NON-NLS-1$
74         "[Long]", //$NON-NLS-1$
75         "Vector Double", //$NON-NLS-1$
76         "Vector Integer", //$NON-NLS-1$
77         "Vector Float", //$NON-NLS-1$
78         "Vector String", //$NON-NLS-1$
79         "Vector Boolean", //$NON-NLS-1$
80         "Vector Long" //$NON-NLS-1$
81     };
82
83     public Resource componentType;
84     public FormToolkit tk;
85     public Form form;
86     public UnitLibrary unitLibrary = UnitLibrary.createDefault();
87     public boolean readOnly;
88     public NamedResource[] connectionPoints;
89     public ComponentTypeViewerPropertyInfo[] properties;
90
91     public ComponentTypeViewerData(FormToolkit tk, Resource componentType, Form form) {
92         this.tk = tk;
93         this.componentType = componentType;
94         this.form = form;
95     }
96
97     public void editName(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column,
98             Pattern namePattern) {
99         editName(table, editor, propertyInfo, selectedItem, column, namePattern, null);
100     }
101
102     public void editName(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column,
103             Pattern namePattern, DbConsumer<WriteGraph> extraWriter) {
104         editName(table, editor, propertyInfo, selectedItem, column,
105                 null,
106                 (pInfo, name) -> validatePropertyName(pInfo, name, namePattern),
107                 extraWriter);
108     }
109
110     public void editName(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column,
111             Function2<ComponentTypeViewerPropertyInfo, String, String> nameFilter, Pattern namePattern, DbConsumer<WriteGraph> extraWriter) {
112         editName(table, editor, propertyInfo, selectedItem, column, nameFilter,
113                 (pInfo, name) -> validatePropertyName(pInfo, name, namePattern),
114                 extraWriter);
115     }
116
117     public void editName(Table table, TableEditor editor, ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column,
118             Function2<ComponentTypeViewerPropertyInfo, String, String> nameValidator)
119     {
120         editName(table, editor, propertyInfo, selectedItem, column, nameValidator, null);
121     }
122
123     public void editName(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column,
124             Function2<ComponentTypeViewerPropertyInfo, String, String> nameValidator, DbConsumer<WriteGraph> extraWriter) {
125         editName(table, editor, propertyInfo, selectedItem, column, null, nameValidator, extraWriter);
126     }
127
128     public void editName(
129             Table table,
130             TableEditor editor,
131             final ComponentTypeViewerPropertyInfo propertyInfo,
132             TableItem selectedItem,
133             int column,
134             Function2<ComponentTypeViewerPropertyInfo, String, String> nameFilter,
135             Function2<ComponentTypeViewerPropertyInfo, String, String> nameValidator,
136             DbConsumer<WriteGraph> extraWriter) {
137         int extraStyle = propertyInfo.immutable ? SWT.READ_ONLY : 0;
138         final Text text = new Text(table, SWT.NONE | extraStyle);
139         org.eclipse.swt.widgets.Listener listener = 
140                 new org.eclipse.swt.widgets.Listener() {
141             @Override
142             public void handleEvent(Event e) {
143                 if (e.type == SWT.Dispose) {
144                     form.setMessage(null);
145                     return;
146                 } else if (e.type == SWT.Verify) {
147                     // Filter input if necessary
148                     e.text = nameFilter != null ? nameFilter.apply(propertyInfo, e.text) : e.text;
149                     return;
150                 } else if (e.type == SWT.Modify) {
151                     // validate current name
152                     String error = nameValidator.apply(propertyInfo, text.getText());
153                     if (error != null) {
154                         text.setBackground(text.getDisplay().getSystemColor(SWT.COLOR_RED));
155                         form.setMessage(error, IMessageProvider.ERROR);
156                     } else {
157                         text.setBackground(null);
158                         form.setMessage(null);
159                     }
160                     return;
161                 } else if (e.type == SWT.Traverse) {
162                     if (e.detail == SWT.TRAVERSE_ESCAPE) {
163                         text.dispose();
164                         e.doit = false;
165                         return;
166                     }
167                     if (e.detail == SWT.TRAVERSE_ARROW_NEXT || e.detail == SWT.TRAVERSE_ARROW_PREVIOUS || e.detail == SWT.TRAVERSE_MNEMONIC)
168                         return;
169                     e.doit = false;
170                 }
171                 final String newValue = text.getText();
172                 text.dispose();
173
174                 String error = nameValidator.apply(propertyInfo, newValue);
175                 if (error != null)
176                     return;
177
178                 if (propertyInfo.immutable)
179                     return;
180
181                 Simantics.getSession().async(new WriteRequest() {
182                     @Override
183                     public void perform(WriteGraph graph)
184                             throws DatabaseException {
185                         graph.markUndoPoint();
186                         Layer0 L0 = Layer0.getInstance(graph);
187                         String prevName = graph.getPossibleRelatedValue2(propertyInfo.resource, L0.HasName);
188                         String oldCamelCasedLabel = prevName != null ? ComponentTypeCommands.camelCaseNameToLabel(prevName) : ""; //$NON-NLS-1$
189                         String oldLabel = graph.getPossibleRelatedValue(propertyInfo.resource, L0.HasLabel);
190                         boolean setLabel = oldLabel == null
191                                 || oldLabel.isEmpty()
192                                 || oldCamelCasedLabel.isEmpty()
193                                 || oldCamelCasedLabel.equals(oldLabel);
194
195                         ComponentTypeCommands.rename(graph, propertyInfo.resource, newValue);
196                         if (setLabel)
197                             ComponentTypeCommands.setLabel(graph, propertyInfo.resource, ComponentTypeCommands.camelCaseNameToLabel(newValue));
198
199                         if (extraWriter != null)
200                             extraWriter.accept(graph);
201                     }
202                 });
203             }
204         };
205         if (nameFilter != null)
206             text.addListener(SWT.Verify, listener);
207         text.addListener(SWT.Modify, listener);
208         text.addListener(SWT.Deactivate, listener);
209         text.addListener(SWT.Traverse, listener);
210         text.addListener(SWT.Dispose, listener);
211
212         text.setText(selectedItem.getText(column));
213         text.selectAll();
214         text.setFocus();
215
216         editor.setEditor(text, selectedItem, column);
217     }
218
219     public void editType(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column, String range, final boolean convertDefaultValue) {
220         int extraStyle = propertyInfo.immutable ? SWT.READ_ONLY : 0;
221         final Combo combo = new Combo(table, SWT.NONE | extraStyle);
222         combo.setText(selectedItem.getText(column));
223         for(String suggestion : PROPERTY_TYPE_SUGGESTIONS)
224             combo.add(suggestion);
225         org.eclipse.swt.widgets.Listener listener = 
226                 new org.eclipse.swt.widgets.Listener() {
227             @Override
228             public void handleEvent(Event e) {
229                 if(e.type == SWT.Traverse) {
230                     if (e.detail == SWT.TRAVERSE_ESCAPE) {
231                         combo.dispose();
232                         e.doit = false;
233                         return;
234                     }
235                     if (e.detail == SWT.TRAVERSE_ARROW_NEXT
236                             || e.detail == SWT.TRAVERSE_ARROW_PREVIOUS
237                             || e.detail == SWT.TRAVERSE_MNEMONIC)
238                         return;
239                 }
240                 final String newValue = combo.getText();
241                 if (e.type == SWT.Traverse) {
242                     e.doit = false;
243                 }
244                 combo.dispose();
245
246                 if (propertyInfo.immutable)
247                     return;
248
249                 Simantics.getSession().async(new WriteRequest() {
250                     @Override
251                     public void perform(WriteGraph graph)
252                             throws DatabaseException {
253                         graph.markUndoPoint();
254                         ComponentTypeCommands.editType(graph, componentType, propertyInfo.resource, convertDefaultValue, newValue);
255                         if (range != null) ComponentTypeCommands.setRange(graph, componentType, propertyInfo.resource, range);
256                     }
257                 });
258             }
259         };
260         combo.setFocus();
261         editor.setEditor(combo, selectedItem, column);
262         combo.addListener(SWT.FocusOut, listener);
263         combo.addListener(SWT.Traverse, listener);
264     }
265
266     public void editUnit(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, int column) {
267         // Disallow unit editing for non-numeric configuration properties
268         if (propertyInfo.numberType == null && propertyInfo.sectionSpecificData == null)
269             return;
270
271         int extraStyle = propertyInfo.immutable ? SWT.READ_ONLY : 0;
272         final Combo combo = new Combo(table, SWT.NONE | extraStyle);
273         String initialValue = selectedItem.getText(column);
274         List<String> units = new ArrayList<>( unitLibrary.getUnits() );
275         Collections.sort(units, String.CASE_INSENSITIVE_ORDER);
276         int i = -1;
277         int selected = -1;
278         for (String unit : units) {
279             combo.add(unit);
280             if (unit.equals(initialValue))
281                 combo.select(i);
282         }
283         if (selected == -1)
284             combo.setText(initialValue);
285
286         org.eclipse.swt.widgets.Listener listener = 
287                 new org.eclipse.swt.widgets.Listener() {
288             @Override
289             public void handleEvent(Event e) {
290                 if(e.type == SWT.Traverse) {
291                     if (e.detail == SWT.TRAVERSE_ESCAPE) {
292                         combo.dispose();
293                         e.doit = false;
294                         return;
295                     }
296                     if (e.detail == SWT.TRAVERSE_ARROW_NEXT
297                             || e.detail == SWT.TRAVERSE_ARROW_PREVIOUS
298                             || e.detail == SWT.TRAVERSE_MNEMONIC)
299                         return;
300                 }
301                 final String newValue = combo.getText();
302                 if(e.type == SWT.Traverse) {
303                     e.doit = false;
304                 }
305                 combo.dispose();
306
307                 if (propertyInfo.immutable)
308                     return;
309
310                 Simantics.getSession().async(new WriteRequest() {
311                     @Override
312                     public void perform(WriteGraph graph)
313                             throws DatabaseException {
314                         graph.markUndoPoint();
315                         ComponentTypeCommands.setUnit(graph, componentType, propertyInfo.resource, newValue);
316                     }
317                 });
318             }
319         };
320         combo.setFocus();
321         editor.setEditor(combo, selectedItem, column);
322         combo.addListener(SWT.Deactivate, listener);
323         combo.addListener(SWT.Traverse, listener);
324     }
325
326     public void editValue(Table table, TableEditor editor,
327             final ComponentTypeViewerPropertyInfo propertyInfo,
328             TableItem selectedItem, int column,
329             final StringWriter writer,
330             final Function4<RequestProcessor, Resource, Resource, String, String> validator)
331     {
332         int extraStyle = writer == null ? SWT.READ_ONLY : 0;
333         final Text text = new Text(table, SWT.NONE | extraStyle);
334         text.setText(selectedItem.getText(column));
335         org.eclipse.swt.widgets.Listener listener = 
336                 new org.eclipse.swt.widgets.Listener() {
337             @Override
338             public void handleEvent(Event e) {
339                 if(e.type == SWT.Traverse) {
340                     if (e.detail == SWT.TRAVERSE_ESCAPE) {
341                         text.dispose();
342                         e.doit = false;
343                         return;
344                     }
345                     if (e.detail == SWT.TRAVERSE_ARROW_NEXT
346                             || e.detail == SWT.TRAVERSE_ARROW_PREVIOUS
347                             || e.detail == SWT.TRAVERSE_MNEMONIC)
348                         return;
349                 }
350                 final String newValue = text.getText();
351                 if(e.type == SWT.Traverse) {
352                     e.doit = false;
353                 }
354                 text.dispose();
355
356                 if (validator != null) {
357                     String error = validator.apply(Simantics.getSession(), componentType, propertyInfo.resource, newValue);
358                     if (error != null)
359                         return;
360                 }
361
362                 if (writer != null) {
363                     Simantics.getSession().async(new WriteRequest() {
364                         @Override
365                         public void perform(WriteGraph graph) throws DatabaseException {
366                             writer.perform(graph, newValue);
367                         }
368                     });
369                 }
370             }
371         };
372         text.selectAll();
373         text.setFocus();
374         editor.setEditor(text, selectedItem, column);
375         text.addListener(SWT.FocusOut, listener);
376         text.addListener(SWT.Traverse, listener);
377
378         if (validator != null) {
379             org.eclipse.swt.widgets.Listener validationListener = new org.eclipse.swt.widgets.Listener() {
380                 
381                 private ScheduledFuture<?> future;
382                 
383                 @Override
384                 public void handleEvent(Event e) {
385                     final String newValue = text.getText();
386                     if (future != null && !future.isCancelled())
387                         future.cancel(true);
388                     future = ThreadUtils.getNonBlockingWorkExecutor().schedule(() -> {
389                         String error = validator.apply(Simantics.getSession(), componentType, propertyInfo.resource, newValue);
390                         if (!text.isDisposed()) {
391                             text.getDisplay().asyncExec(() -> {
392                                 if (!text.isDisposed()) {
393                                     if (error != null) {
394                                         text.setBackground(text.getDisplay().getSystemColor(SWT.COLOR_RED));
395                                         text.setToolTipText(error);
396                                         return;
397                                     } else {
398                                         text.setBackground(null);
399                                         text.setToolTipText(null);
400                                     }
401                                 }
402                                 
403                             });
404                         }
405                     }, 500, TimeUnit.MILLISECONDS);
406                 }
407             };
408             text.addListener(SWT.Modify, validationListener);
409         }
410     }
411
412     private Range parseRange(NumberType numberType, String minStr, String maxStr, boolean lowInclusive, boolean highInclusive) throws RangeException {
413         try {
414             String rangeStr = (lowInclusive ? "[" : "(")  + minStr + ".." + maxStr + (highInclusive ? "]" : ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
415             return Range.valueOf(rangeStr);
416         } catch (IllegalArgumentException e) {
417             // Limits are invalid
418             throw new RangeException(e.getMessage(), e);
419         }
420     }
421     
422     private static Combo createRangeInclusionCombo(Composite parent, boolean inclusive) {
423         Combo rng = new Combo(parent, SWT.READ_ONLY);
424         rng.add(Messages.ComponentTypeViewerData_Inclusive);
425         rng.add(Messages.ComponentTypeViewerData_Exclusive);
426         rng.select(inclusive ? 0 : 1);
427         return rng;
428     }
429     
430     protected void editRange(Table table, TableEditor editor, final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem, Rectangle selectedItemBounds, int column) {
431         // Disallow range editing when the property is not numeric
432         if (propertyInfo.numberType == null)
433             return;
434
435         int extraTextStyle = propertyInfo.immutable ? SWT.READ_ONLY : 0;
436
437         // Parse initial range value
438         Range range = null;
439         String rangeStr = selectedItem.getText(column);
440         try {
441             range = Range.valueOf(rangeStr);
442         } catch (RangeException ex) {
443             range = new Range(Limit.nolimit(), Limit.nolimit());
444         }
445
446         final Shell shell = new Shell(table.getShell(), SWT.ON_TOP);
447         GridLayoutFactory.fillDefaults().applyTo(shell);
448
449         Composite composite = new Composite(shell, SWT.NONE);
450         GridDataFactory.fillDefaults().grab(true, true).applyTo(composite);
451         GridLayoutFactory.swtDefaults().numColumns(3).applyTo(composite);
452
453         Label low = new Label(composite, SWT.NONE);
454         low.setText(Messages.ComponentTypeViewerData_MinimumValue);
455         final Text lowText = new Text(composite, SWT.BORDER | extraTextStyle);
456         GridDataFactory.fillDefaults().grab(true, false).hint(100, SWT.DEFAULT).applyTo(lowText);
457         final Combo lowSelector = createRangeInclusionCombo(composite, !range.getLower().isExclusive());
458         Label high = new Label(composite, SWT.NONE);
459         high.setText(Messages.ComponentTypeViewerData_MaximumValue);
460         final Text highText = new Text(composite, SWT.BORDER | extraTextStyle);
461         GridDataFactory.fillDefaults().grab(true, false).hint(100, SWT.DEFAULT).applyTo(highText);
462         final Combo highSelector = createRangeInclusionCombo(composite, !range.getUpper().isExclusive());
463
464         Composite buttonComposite = new Composite(shell, SWT.NONE);
465         GridDataFactory.fillDefaults().grab(true, false).align(SWT.TRAIL, SWT.FILL).applyTo(buttonComposite);
466         GridLayoutFactory.swtDefaults().numColumns(2).equalWidth(true).applyTo(buttonComposite);
467
468         Button ok = new Button(buttonComposite, SWT.NONE);
469         ok.setText(IDialogConstants.OK_LABEL);
470         GridDataFactory.swtDefaults().align(SWT.FILL, SWT.CENTER).applyTo(ok);
471         Button cancel = new Button(buttonComposite, SWT.NONE);
472         cancel.setText(IDialogConstants.CANCEL_LABEL);
473         GridDataFactory.swtDefaults().align(SWT.FILL, SWT.CENTER).applyTo(ok);
474
475         if (range.getLower().getValue() != null)
476             lowText.setText(range.getLower().getValue().toString());
477         if (range.getUpper().getValue() != null)
478             highText.setText(range.getUpper().getValue().toString());
479
480         shell.addListener(SWT.Deactivate, new org.eclipse.swt.widgets.Listener() {
481             @Override
482             public void handleEvent(Event event) {
483                 shell.dispose();
484             }
485         });
486
487         ok.addSelectionListener(new SelectionAdapter() {
488             public void widgetSelected(SelectionEvent e) {
489                 try {
490                     final Range newRange = parseRange(propertyInfo.numberType,
491                             lowText.getText().trim(),
492                             highText.getText().trim(),
493                             lowSelector.getSelectionIndex() == 0 ? true : false,
494                             highSelector.getSelectionIndex() == 0 ? true : false);
495
496                     shell.dispose();
497
498                     if (propertyInfo.immutable)
499                         return;
500
501                     Simantics.getSession().async(new WriteRequest() {
502                         @Override
503                         public void perform(WriteGraph graph)
504                                 throws DatabaseException {
505                             graph.markUndoPoint();
506                             ComponentTypeCommands.setRange(graph, componentType, propertyInfo.resource, newRange == null ? null : newRange.toString());
507                         }
508                     });
509                 } catch (RangeException ex) {
510                     ErrorLogger.defaultLogError(ex);
511                 }
512             }
513         });
514         cancel.addSelectionListener(new SelectionAdapter() {
515             public void widgetSelected(SelectionEvent e) {
516                 shell.dispose();
517             }
518         });
519
520         shell.pack();
521         Point size = shell.getSize();
522
523         Display display = table.getDisplay();
524         Rectangle clientArea = display.getClientArea();
525         Point bt = table.toDisplay(selectedItemBounds.x, selectedItemBounds.y);
526         Rectangle b = selectedItemBounds;
527         b.x = bt.x;
528         b.y = bt.y;
529         b.width = size.x;
530         b.height = size.y;
531         if ((b.x + b.width) > clientArea.width)
532             b.x -= b.x + b.width - clientArea.width;
533         if (b.height > clientArea.height)
534             b.height = clientArea.height;
535         if ((b.y + b.height) > clientArea.height)
536             b.y -= b.y + b.height - clientArea.height;
537
538         shell.setBounds(selectedItemBounds);
539         shell.open();
540     }
541
542     public void editMultilineText(Table table, TableEditor editor,
543             final ComponentTypeViewerPropertyInfo propertyInfo, TableItem selectedItem,
544             Rectangle selectedItemBounds, int column, final StringWriter writer)
545     {
546         final Shell shell = new Shell(table.getShell(), SWT.ON_TOP);
547         GridLayoutFactory.fillDefaults().spacing(0, 0).applyTo(shell);
548         final StyledText text = new StyledText(shell, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | (propertyInfo.immutable ? SWT.READ_ONLY : 0));
549         GridDataFactory.fillDefaults().grab(true, true).applyTo(text);
550         text.setText(selectedItem.getText(column));
551         org.eclipse.swt.widgets.Listener listener = 
552                 new org.eclipse.swt.widgets.Listener() {
553             @Override
554             public void handleEvent(Event e) {
555                 final String newValue = text.getText();
556
557                 if (e.type == SWT.Traverse) {
558                     if (e.detail == SWT.TRAVERSE_ESCAPE) {
559                         shell.dispose();
560                         e.doit = false;
561                         return;
562                     }
563                     if (e.detail == SWT.TRAVERSE_ARROW_NEXT
564                             || e.detail == SWT.TRAVERSE_ARROW_PREVIOUS
565                             || e.detail == SWT.TRAVERSE_MNEMONIC)
566                         return;
567                     if ((e.stateMask & SWT.CTRL) == 0)
568                         return;
569                     e.doit = false;
570                 }
571
572                 shell.dispose();
573
574                 if (propertyInfo.immutable)
575                     return;
576
577                 if (writer != null) {
578                     Simantics.getSession().async(new WriteRequest() {
579                         @Override
580                         public void perform(WriteGraph graph) throws DatabaseException {
581                             writer.perform(graph, newValue);
582                         }
583                     });
584                 }
585             }
586         };
587
588         String helpText = propertyInfo.immutable ? Messages.ComponentTypeViewerData_ESCToClose : Messages.ComponentTypeViewerData_CtrlEnterApplyChanges;
589         Label help = tk.createLabel(shell, helpText, SWT.BORDER | SWT.FLAT);
590         GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.CENTER).applyTo(help);
591         help.setForeground( tk.getColors().createColor( "fg", tk.getColors().getSystemColor(SWT.COLOR_LIST_SELECTION) ) ); //$NON-NLS-1$
592
593         Display display = table.getDisplay();
594         Rectangle clientArea = display.getClientArea();
595         Point bt = table.toDisplay(selectedItemBounds.x, selectedItemBounds.y);
596         Rectangle b = selectedItemBounds;
597         b.x = bt.x;
598         b.y = bt.y;
599         b.height = 200;
600         if ((b.x + b.width) > clientArea.width)
601             b.x -= b.x + b.width - clientArea.width;
602         if (b.height > clientArea.height)
603             b.height = clientArea.height;
604         if ((b.y + b.height) > clientArea.height)
605             b.y -= b.y + b.height - clientArea.height;
606
607         shell.setBounds(selectedItemBounds);
608         shell.open();
609
610         text.selectAll();
611         text.setFocus();
612
613         text.addListener(SWT.Traverse, listener);
614         shell.addListener(SWT.Deactivate, listener);
615     }
616
617     private String validatePropertyName(ComponentTypeViewerPropertyInfo propertyInfo, String propertyName, Pattern namePattern) {
618         if (propertyName.equals(propertyInfo.name))
619             return null;
620         for (ComponentTypeViewerPropertyInfo info : properties) {
621             if (propertyName.equals(info.name))
622                 return NLS.bind(Messages.ComponentTypeViewerData_PropertyNameInUse, propertyName); 
623         }
624         for (NamedResource cp : connectionPoints) {
625             if (propertyName.equals(cp.getName()))
626                 return NLS.bind(Messages.ComponentTypeViewerData_NameInUse, propertyName ); 
627         }
628         Matcher m = namePattern.matcher(propertyName);
629         if (!m.matches())
630             return NLS.bind(Messages.ComponentTypeViewerData_ContainsInvalidCharacters, propertyName, namePattern.pattern());
631         return null;
632     }
633
634 }