1 package org.simantics.modeling.ui.diagram.renaming;
\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
25 public class ComponentsRenamingDialog extends Dialog {
\r
27 ComponentsRenamingModel model;
\r
31 Label oldNamePrefixLabel;
\r
36 TableViewer tableViewer;
\r
38 public ComponentsRenamingDialog(Shell parentShell, ComponentsRenamingModel model) {
\r
41 setShellStyle(SWT.RESIZE | SWT.TITLE);
\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
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
59 oldNamePrefixLabel = new Label(area, SWT.NONE);
\r
60 oldNamePrefixLabel.setText("Old name prefix:");
\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
67 Label newNamePrefixLabel = new Label(area, SWT.NONE);
\r
68 newNamePrefixLabel.setText("&New name prefix:");
\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
75 newNamePrefix.addModifyListener(new ModifyListener() {
\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
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
93 area.getParent().layout();
\r
94 getButton(OK).setEnabled(false);
\r
97 if (error != null) {
\r
98 errorLabel.dispose();
\r
102 area.getParent().layout();
\r
103 getButton(OK).setEnabled(true);
\r
107 model.newNamePrefix = text;
\r
108 model.computeNewNames();
\r
116 final Button resetNames = new Button(composite, SWT.CHECK);
\r
117 resetNames.setText("&Reset names");
\r
118 resetNames.addSelectionListener(new SelectionAdapter() {
\r
120 public void widgetSelected(SelectionEvent e) {
\r
121 model.reset = resetNames.getSelection();
\r
122 model.computeNewNames();
\r
129 tableViewer = new TableViewer(composite);
\r
130 tableViewer.getTable().setHeaderVisible(true);
\r
131 GridDataFactory.fillDefaults().grab(true, true).applyTo(tableViewer.getTable());
\r
133 tableViewer.setContentProvider(ArrayContentProvider.getInstance());
\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
140 public String getText(Object element) {
\r
141 NameEntry entry = (NameEntry)element;
\r
142 return entry.oldName;
\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
151 public String getText(Object element) {
\r
152 NameEntry entry = (NameEntry)element;
\r
153 return entry.newName;
\r
156 tableViewer.setInput(model.entries.toArray());
\r
157 newNamePrefix.setFocus();
\r
162 public void refresh() {
\r
163 tableViewer.refresh();
\r
166 public static void main(String[] args) {
\r
167 final Display display = new Display();
\r
168 Shell shell = new Shell(display);
\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
177 ComponentsRenamingDialog dialog = new ComponentsRenamingDialog(shell, model);
\r
180 while (!shell.isDisposed()) {
\r
181 if (!display.readAndDispatch())
\r