1 package org.simantics.modeling.ui.diagram.renaming;
3 import java.util.HashSet;
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;
34 public class ComponentsRenamingDialog extends Dialog {
36 ComponentsRenamingModel model;
40 Label oldNamePrefixLabel;
45 CheckboxTableViewer tableViewer;
47 public ComponentsRenamingDialog(Shell parentShell, ComponentsRenamingModel model) {
50 setShellStyle(SWT.RESIZE | SWT.TITLE);
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);
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);
68 oldNamePrefixLabel = new Label(area, SWT.NONE);
69 oldNamePrefixLabel.setText("Old name prefix:");
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);
76 Label newNamePrefixLabel = new Label(area, SWT.NONE);
77 newNamePrefixLabel.setText("&New name prefix:");
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);
84 newNamePrefix.addModifyListener(new ModifyListener() {
87 public void modifyText(ModifyEvent e) {
88 String text = newNamePrefix.getText();
89 if (model.prefixValidator != null) {
90 String err = model.prefixValidator.apply(text);
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));
102 area.getParent().layout();
103 getButton(OK).setEnabled(false);
107 errorLabel.dispose();
111 area.getParent().layout();
112 getButton(OK).setEnabled(true);
116 model.newNamePrefix = text;
117 model.computeNewNames();
125 final Button resetNames = new Button(composite, SWT.CHECK);
126 resetNames.setText("&Reset names");
127 resetNames.addSelectionListener(new SelectionAdapter() {
129 public void widgetSelected(SelectionEvent e) {
130 model.reset = resetNames.getSelection();
131 model.computeNewNames();
138 tableViewer = CheckboxTableViewer.newCheckList(composite,
139 SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
141 tableViewer.getTable().setHeaderVisible(true);
142 GridDataFactory.fillDefaults().grab(true, true).applyTo(tableViewer.getTable());
144 tableViewer.setContentProvider(ArrayContentProvider.getInstance());
145 tableViewer.setCheckStateProvider(new ICheckStateProvider() {
147 public boolean isGrayed(Object element) {
151 public boolean isChecked(Object element) {
152 return model.selectedEntries.contains(element);
156 TableViewerColumn oldNameColumn = new TableViewerColumn(tableViewer, SWT.NONE);
157 oldNameColumn.getColumn().setText("Old name");
158 oldNameColumn.getColumn().setWidth(250);
159 oldNameColumn.setLabelProvider(new ColumnLabelProvider() {
161 public String getText(Object element) {
162 NameEntry entry = (NameEntry)element;
163 return entry.oldName;
167 TableViewerColumn newNameColumn = new TableViewerColumn(tableViewer, SWT.NONE);
168 newNameColumn.getColumn().setText("New name");
169 newNameColumn.getColumn().setWidth(250);
170 newNameColumn.setLabelProvider(new ColumnLabelProvider() {
172 public String getText(Object element) {
173 NameEntry entry = (NameEntry)element;
174 return entry.newName;
177 tableViewer.addCheckStateListener(new ICheckStateListener() {
178 void addOrRemoveSelection(NameEntry entry, boolean add) {
180 model.selectedEntries.add(entry);
182 model.selectedEntries.remove(entry);
185 public void checkStateChanged(CheckStateChangedEvent event) {
186 final boolean checked = event.getChecked();
187 NameEntry checkedNode = (NameEntry) event.getElement();
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);
194 tableViewer.setSelection(StructuredSelection.EMPTY);
195 entries.add(checkedNode);
197 for (NameEntry entry : entries) {
198 addOrRemoveSelection(entry, checked);
201 tableViewer.refresh();
204 tableViewer.setInput(model.entries.toArray());
205 newNamePrefix.setFocus();
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() {
215 public void widgetSelected(SelectionEvent e) {
216 model.selectedEntries.addAll(model.entries);
217 tableViewer.setAllChecked(true);
220 Button clearSelection = new Button(bar, SWT.PUSH);
221 clearSelection.setText("&Clear Selection");
222 clearSelection.addSelectionListener(new SelectionAdapter() {
224 public void widgetSelected(SelectionEvent e) {
225 model.selectedEntries.clear();
226 tableViewer.setAllChecked(false);
233 public void refresh() {
234 tableViewer.refresh();
237 public static void main(String[] args) {
238 final Display display = new Display();
239 Shell shell = new Shell(display);
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"));
248 ComponentsRenamingDialog dialog = new ComponentsRenamingDialog(shell, model);
251 while (!shell.isDisposed()) {
252 if (!display.readAndDispatch())