]> gerrit.simantics Code Review - simantics/platform.git/blob
7e97ccc5629ca7619ee6ac34001b2de81c191ea5
[simantics/platform.git] /
1 package org.simantics.modeling.ui.diagram.renaming;
2
3 import java.util.HashSet;
4 import java.util.Set;
5
6 import org.eclipse.jface.dialogs.Dialog;
7 import org.eclipse.jface.layout.GridDataFactory;
8 import org.eclipse.jface.layout.GridLayoutFactory;
9 import org.eclipse.jface.viewers.ArrayContentProvider;
10 import org.eclipse.jface.viewers.CheckStateChangedEvent;
11 import org.eclipse.jface.viewers.CheckboxTableViewer;
12 import org.eclipse.jface.viewers.ColumnLabelProvider;
13 import org.eclipse.jface.viewers.ICheckStateListener;
14 import org.eclipse.jface.viewers.ICheckStateProvider;
15 import org.eclipse.jface.viewers.StructuredSelection;
16 import org.eclipse.jface.viewers.TableViewerColumn;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.ModifyEvent;
19 import org.eclipse.swt.events.ModifyListener;
20 import org.eclipse.swt.events.SelectionAdapter;
21 import org.eclipse.swt.events.SelectionEvent;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.layout.RowLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Display;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.swt.widgets.Text;
32 import org.simantics.utils.ui.ISelectionUtils;
33
34 public class ComponentsRenamingDialog extends Dialog {
35
36     ComponentsRenamingModel model;
37
38     // UI
39     Composite area;
40     Label oldNamePrefixLabel;
41     Text oldNamePrefix;
42     Text newNamePrefix;
43     Label errorLabel;
44     Text error;
45     CheckboxTableViewer tableViewer;
46
47     public ComponentsRenamingDialog(Shell parentShell, ComponentsRenamingModel model) {
48         super(parentShell);
49         this.model = model;
50         setShellStyle(SWT.RESIZE | SWT.TITLE);
51     }
52     
53     @Override
54     protected Control createDialogArea(Composite parent) {
55         getShell().setText("Rename diagram contents");
56         getShell().setLayout(new GridLayout());
57         Composite composite = new Composite(parent, SWT.NONE);
58         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
59         GridLayoutFactory.fillDefaults().numColumns(1).applyTo(composite);
60         
61         // Name prefix
62         
63         {
64             area = new Composite(composite, SWT.BORDER);
65             GridDataFactory.fillDefaults().grab(true, false).applyTo(area);
66             GridLayoutFactory.fillDefaults().numColumns(2).margins(5, 5).applyTo(area);
67             
68             oldNamePrefixLabel = new Label(area, SWT.NONE);
69             oldNamePrefixLabel.setText("Old name prefix:");
70             
71             oldNamePrefix = new Text(area, SWT.READ_ONLY | SWT.BORDER);
72             oldNamePrefix.setEditable(false);
73             oldNamePrefix.setText(model.oldNamePrefix);
74             GridDataFactory.fillDefaults().grab(true, false).applyTo(oldNamePrefix);
75             
76             Label newNamePrefixLabel = new Label(area, SWT.NONE);
77             newNamePrefixLabel.setText("&New name prefix:");
78             
79             newNamePrefix = new Text(area, SWT.BORDER);
80             newNamePrefix.setText(model.oldNamePrefix);
81             newNamePrefix.setSelection(model.oldNamePrefix.length());
82             GridDataFactory.fillDefaults().grab(true, false).applyTo(newNamePrefix);
83             
84             newNamePrefix.addModifyListener(new ModifyListener() {
85
86                 @Override
87                 public void modifyText(ModifyEvent e) {
88                     String text = newNamePrefix.getText();
89                     if (model.prefixValidator != null) {
90                         String err = model.prefixValidator.apply(text);
91                         if (err != null) {
92                             if (error == null) {
93                                 errorLabel = new Label(area, 0);
94                                 errorLabel.moveBelow(newNamePrefix);
95                                 GridDataFactory.fillDefaults().grab(false, false).applyTo(errorLabel);
96                                 error = new Text(area, SWT.MULTI | SWT.WRAP | SWT.READ_ONLY);
97                                 error.moveBelow(errorLabel);
98                                 GridDataFactory.fillDefaults().grab(true, false).applyTo(error);
99                                 error.setForeground(error.getDisplay().getSystemColor(SWT.COLOR_RED));
100                             }
101                             error.setText(err);
102                             area.getParent().layout();
103                             getButton(OK).setEnabled(false);
104                             return;
105                         }
106                         if (error != null) {
107                             errorLabel.dispose();
108                             errorLabel = null;
109                             error.dispose();
110                             error = null;
111                             area.getParent().layout();
112                             getButton(OK).setEnabled(true);
113                         }
114                     }
115
116                     model.newNamePrefix = text;
117                     model.computeNewNames();
118                     refresh();
119                 }
120             });
121         }
122         
123         // Reset
124         
125         final Button resetNames = new Button(composite, SWT.CHECK);
126         resetNames.setText("&Reset names");
127         resetNames.addSelectionListener(new SelectionAdapter() {
128             @Override
129             public void widgetSelected(SelectionEvent e) {
130                 model.reset = resetNames.getSelection();
131                 model.computeNewNames();
132                 refresh();
133             }
134         });
135         
136         // Name table
137         
138         tableViewer = CheckboxTableViewer.newCheckList(composite,
139                 SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
140
141         tableViewer.getTable().setHeaderVisible(true);
142         GridDataFactory.fillDefaults().grab(true, true).applyTo(tableViewer.getTable());
143         
144         tableViewer.setContentProvider(ArrayContentProvider.getInstance());
145         tableViewer.setCheckStateProvider(new ICheckStateProvider() {
146             @Override
147             public boolean isGrayed(Object element) {
148                 return false;
149             }
150             @Override
151             public boolean isChecked(Object element) {
152                 return model.selectedEntries.contains(element);
153             }
154         });
155         
156         TableViewerColumn oldNameColumn = new TableViewerColumn(tableViewer, SWT.NONE);
157         oldNameColumn.getColumn().setText("Old name");
158         oldNameColumn.getColumn().setWidth(250);
159         oldNameColumn.setLabelProvider(new ColumnLabelProvider() {
160             @Override
161             public String getText(Object element) {
162                 NameEntry entry = (NameEntry)element;
163                 return entry.oldName;
164             }
165         });
166         
167         TableViewerColumn newNameColumn = new TableViewerColumn(tableViewer, SWT.NONE);
168         newNameColumn.getColumn().setText("New name");
169         newNameColumn.getColumn().setWidth(250);
170         newNameColumn.setLabelProvider(new ColumnLabelProvider() {
171             @Override
172             public String getText(Object element) {
173                 NameEntry entry = (NameEntry)element;
174                 return entry.newName;
175             }
176         });
177         tableViewer.addCheckStateListener(new ICheckStateListener() {
178             void addOrRemoveSelection(NameEntry entry, boolean add) {
179                 if (add)
180                     model.selectedEntries.add(entry);
181                 else
182                     model.selectedEntries.remove(entry);
183             }
184             @Override
185             public void checkStateChanged(CheckStateChangedEvent event) {
186                 final boolean checked = event.getChecked();
187                 NameEntry checkedNode = (NameEntry) event.getElement();
188
189                 Set<NameEntry> entries = new HashSet<NameEntry>();
190                 Set<NameEntry> selection = ISelectionUtils.filterSetSelection(tableViewer.getSelection(), NameEntry.class);
191                 if (selection.contains(checkedNode))
192                     entries.addAll(selection);
193                 else
194                     tableViewer.setSelection(StructuredSelection.EMPTY);
195                 entries.add(checkedNode);
196
197                 for (NameEntry entry : entries) {
198                     addOrRemoveSelection(entry, checked);
199                 }
200
201                 tableViewer.refresh();
202             }
203         });
204         tableViewer.setInput(model.entries.toArray());
205         newNamePrefix.setFocus();
206
207         // Select All/None button bar
208         Composite bar = new Composite(composite, SWT.NONE);
209         GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(bar);
210         bar.setLayout(new RowLayout());
211         Button selectAll = new Button(bar, SWT.PUSH);
212         selectAll.setText("Select &All");
213         selectAll.addSelectionListener(new SelectionAdapter() {
214             @Override
215             public void widgetSelected(SelectionEvent e) {
216                 model.selectedEntries.addAll(model.entries);
217                 tableViewer.setAllChecked(true);
218             }
219         });
220         Button clearSelection = new Button(bar, SWT.PUSH);
221         clearSelection.setText("&Clear Selection");
222         clearSelection.addSelectionListener(new SelectionAdapter() {
223             @Override
224             public void widgetSelected(SelectionEvent e) {
225                 model.selectedEntries.clear();
226                 tableViewer.setAllChecked(false);
227             }
228         });
229
230         return composite;
231     }
232     
233     public void refresh() {
234         tableViewer.refresh();
235     }
236     
237     public static void main(String[] args) {
238         final Display display = new Display();
239         Shell shell = new Shell(display);
240         shell.open();
241         
242         ComponentsRenamingModel model = new ComponentsRenamingModel();
243         model.oldNamePrefix = "FOO";
244         model.newNamePrefix = "FOO";
245         for(int i=0;i<100;++i)
246             model.entries.add(new NameEntry(null, "FOO"+(i*5), "FOO"+(i*5), "PREFIX"));
247         
248         ComponentsRenamingDialog dialog = new ComponentsRenamingDialog(shell, model);
249         dialog.open();
250         
251         while (!shell.isDisposed()) {
252             if (!display.readAndDispatch())
253                 display.sleep();
254         }
255         display.dispose();
256     }
257
258 }