]> gerrit.simantics Code Review - simantics/platform.git/blob
8a73bed10bec36ea7acadaa0e1340cb71624cfba
[simantics/platform.git] /
1 package org.simantics.modeling.ui.diagram.renaming;\r
2 \r
3 import org.eclipse.jface.dialogs.Dialog;\r
4 import org.eclipse.jface.layout.GridDataFactory;\r
5 import org.eclipse.jface.layout.GridLayoutFactory;\r
6 import org.eclipse.jface.viewers.ArrayContentProvider;\r
7 import org.eclipse.jface.viewers.ColumnLabelProvider;\r
8 import org.eclipse.jface.viewers.TableViewer;\r
9 import org.eclipse.jface.viewers.TableViewerColumn;\r
10 import org.eclipse.swt.SWT;\r
11 import org.eclipse.swt.events.ModifyEvent;\r
12 import org.eclipse.swt.events.ModifyListener;\r
13 import org.eclipse.swt.events.SelectionAdapter;\r
14 import org.eclipse.swt.events.SelectionEvent;\r
15 import org.eclipse.swt.layout.GridData;\r
16 import org.eclipse.swt.layout.GridLayout;\r
17 import org.eclipse.swt.widgets.Button;\r
18 import org.eclipse.swt.widgets.Composite;\r
19 import org.eclipse.swt.widgets.Control;\r
20 import org.eclipse.swt.widgets.Display;\r
21 import org.eclipse.swt.widgets.Label;\r
22 import org.eclipse.swt.widgets.Shell;\r
23 import org.eclipse.swt.widgets.Text;\r
24 \r
25 public class ComponentsRenamingDialog extends Dialog {\r
26 \r
27     ComponentsRenamingModel model;\r
28 \r
29     // UI\r
30     Composite area;\r
31     Label oldNamePrefixLabel;\r
32     Text oldNamePrefix;\r
33     Text newNamePrefix;\r
34     Label errorLabel;\r
35     Text error;\r
36     TableViewer tableViewer;\r
37 \r
38     public ComponentsRenamingDialog(Shell parentShell, ComponentsRenamingModel model) {\r
39         super(parentShell);\r
40         this.model = model;\r
41         setShellStyle(SWT.RESIZE | SWT.TITLE);\r
42     }\r
43     \r
44     @Override\r
45     protected Control createDialogArea(Composite parent) {\r
46         getShell().setText("Rename diagram contents");\r
47         getShell().setLayout(new GridLayout());\r
48         Composite composite = new Composite(parent, SWT.NONE);\r
49         composite.setLayoutData(new GridData(GridData.FILL_BOTH));\r
50         GridLayoutFactory.fillDefaults().numColumns(1).applyTo(composite);\r
51         \r
52         // Name prefix\r
53         \r
54         {\r
55             area = new Composite(composite, SWT.BORDER);\r
56             GridDataFactory.fillDefaults().grab(true, false).applyTo(area);\r
57             GridLayoutFactory.fillDefaults().numColumns(2).margins(5, 5).applyTo(area);\r
58             \r
59             oldNamePrefixLabel = new Label(area, SWT.NONE);\r
60             oldNamePrefixLabel.setText("Old name prefix:");\r
61             \r
62             oldNamePrefix = new Text(area, SWT.READ_ONLY | SWT.BORDER);\r
63             oldNamePrefix.setEditable(false);\r
64             oldNamePrefix.setText(model.oldNamePrefix);\r
65             GridDataFactory.fillDefaults().grab(true, false).applyTo(oldNamePrefix);\r
66             \r
67             Label newNamePrefixLabel = new Label(area, SWT.NONE);\r
68             newNamePrefixLabel.setText("&New name prefix:");\r
69             \r
70             newNamePrefix = new Text(area, SWT.BORDER);\r
71             newNamePrefix.setText(model.oldNamePrefix);\r
72             newNamePrefix.setSelection(model.oldNamePrefix.length());\r
73             GridDataFactory.fillDefaults().grab(true, false).applyTo(newNamePrefix);\r
74             \r
75             newNamePrefix.addModifyListener(new ModifyListener() {\r
76 \r
77                 @Override\r
78                 public void modifyText(ModifyEvent e) {\r
79                     String text = newNamePrefix.getText();\r
80                     if (model.prefixValidator != null) {\r
81                         String err = model.prefixValidator.apply(text);\r
82                         if (err != null) {\r
83                             if (error == null) {\r
84                                 errorLabel = new Label(area, 0);\r
85                                 errorLabel.moveBelow(newNamePrefix);\r
86                                 GridDataFactory.fillDefaults().grab(false, false).applyTo(errorLabel);\r
87                                 error = new Text(area, SWT.MULTI | SWT.WRAP | SWT.READ_ONLY);\r
88                                 error.moveBelow(errorLabel);\r
89                                 GridDataFactory.fillDefaults().grab(true, false).applyTo(error);\r
90                                 error.setForeground(error.getDisplay().getSystemColor(SWT.COLOR_RED));\r
91                             }\r
92                             error.setText(err);\r
93                             area.getParent().layout();\r
94                             getButton(OK).setEnabled(false);\r
95                             return;\r
96                         }\r
97                         if (error != null) {\r
98                             errorLabel.dispose();\r
99                             errorLabel = null;\r
100                             error.dispose();\r
101                             error = null;\r
102                             area.getParent().layout();\r
103                             getButton(OK).setEnabled(true);\r
104                         }\r
105                     }\r
106 \r
107                     model.newNamePrefix = text;\r
108                     model.computeNewNames();\r
109                     refresh();\r
110                 }\r
111             });\r
112         }\r
113         \r
114         // Reset\r
115         \r
116         final Button resetNames = new Button(composite, SWT.CHECK);\r
117         resetNames.setText("&Reset names");\r
118         resetNames.addSelectionListener(new SelectionAdapter() {\r
119             @Override\r
120             public void widgetSelected(SelectionEvent e) {\r
121                 model.reset = resetNames.getSelection();\r
122                 model.computeNewNames();\r
123                 refresh();\r
124             }\r
125         });\r
126         \r
127         // Name table\r
128         \r
129         tableViewer = new TableViewer(composite);\r
130         tableViewer.getTable().setHeaderVisible(true);\r
131         GridDataFactory.fillDefaults().grab(true, true).applyTo(tableViewer.getTable());\r
132         \r
133         tableViewer.setContentProvider(ArrayContentProvider.getInstance());\r
134         \r
135         TableViewerColumn oldNameColumn = new TableViewerColumn(tableViewer, SWT.NONE);\r
136         oldNameColumn.getColumn().setText("Old name");\r
137         oldNameColumn.getColumn().setWidth(250);\r
138         oldNameColumn.setLabelProvider(new ColumnLabelProvider() {\r
139             @Override\r
140             public String getText(Object element) {\r
141                 NameEntry entry = (NameEntry)element;\r
142                 return entry.oldName;\r
143             }\r
144         });\r
145         \r
146         TableViewerColumn newNameColumn = new TableViewerColumn(tableViewer, SWT.NONE);\r
147         newNameColumn.getColumn().setText("New name");\r
148         newNameColumn.getColumn().setWidth(250);\r
149         newNameColumn.setLabelProvider(new ColumnLabelProvider() {\r
150             @Override\r
151             public String getText(Object element) {\r
152                 NameEntry entry = (NameEntry)element;\r
153                 return entry.newName;\r
154             }\r
155         });\r
156         tableViewer.setInput(model.entries.toArray());\r
157         newNamePrefix.setFocus();\r
158         \r
159         return composite;\r
160     }\r
161     \r
162     public void refresh() {\r
163         tableViewer.refresh();\r
164     }\r
165     \r
166     public static void main(String[] args) {\r
167         final Display display = new Display();\r
168         Shell shell = new Shell(display);\r
169         shell.open();\r
170         \r
171         ComponentsRenamingModel model = new ComponentsRenamingModel();\r
172         model.oldNamePrefix = "FOO";\r
173         model.newNamePrefix = "FOO";\r
174         for(int i=0;i<100;++i)\r
175             model.entries.add(new NameEntry(null, "FOO"+(i*5), "FOO"+(i*5), "PREFIX"));\r
176         \r
177         ComponentsRenamingDialog dialog = new ComponentsRenamingDialog(shell, model);\r
178         dialog.open();\r
179         \r
180         while (!shell.isDisposed()) {\r
181             if (!display.readAndDispatch())\r
182                 display.sleep();\r
183         }\r
184         display.dispose();\r
185     }\r
186 \r
187 }\r