1 package org.simantics.modeling;
3 import org.eclipse.jface.dialogs.Dialog;
4 import org.eclipse.jface.dialogs.IDialogConstants;
5 import org.eclipse.jface.layout.GridDataFactory;
6 import org.eclipse.jface.layout.GridLayoutFactory;
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.events.ModifyEvent;
9 import org.eclipse.swt.events.ModifyListener;
10 import org.eclipse.swt.graphics.Point;
11 import org.eclipse.swt.widgets.Composite;
12 import org.eclipse.swt.widgets.Control;
13 import org.eclipse.swt.widgets.Label;
14 import org.eclipse.swt.widgets.Shell;
15 import org.eclipse.swt.widgets.Text;
16 import org.simantics.db.common.utils.VersionInfo;
17 import org.simantics.utils.strings.AlphanumComparator;
19 public class CreateVersionDialog extends Dialog {
21 private final VersionInfo info;
24 private Text versionTxT;
25 private String result;
27 public CreateVersionDialog(Shell parentShell, VersionInfo info) {
32 public String mutate(String ver) {
34 int currentInt = Integer.parseInt(ver);
35 return "" + (currentInt+1);
36 } catch (NumberFormatException e) {
37 if(ver.length() == 1) {
38 char c = ver.charAt(0);
41 if(Character.isLetter(ch) && Character.isUpperCase(ch)) return ch.toString();
47 public String getSuggestion() {
49 String ver = info.version;
50 while(validateVersion(ver) != null) ver = mutate(ver);
56 protected Control createDialogArea(Composite parent) {
57 Composite c = (Composite) super.createDialogArea(parent);
59 GridLayoutFactory.fillDefaults().margins(8, 8).numColumns(2).applyTo(c);
60 GridDataFactory gd1 = GridDataFactory.fillDefaults().span(2, 1);
63 Label label = new Label(c, 0);
64 label.setText("Current version identifier is: " + info.version);
68 label = new Label(c, 0);
69 label.setText("New identifier:");
70 GridDataFactory.fillDefaults().applyTo(label);
72 versionTxT = new Text(c, SWT.BORDER);
73 versionTxT.setText(getSuggestion());
75 GridDataFactory.fillDefaults().grab(true, false).applyTo(versionTxT);
77 versionTxT.addModifyListener(new ModifyListener() {
80 public void modifyText(ModifyEvent e) {
82 String result = validateVersion(versionTxT.getText());
84 errors.setText(result);
85 getButton(IDialogConstants.OK_ID).setEnabled(false);
86 Shell shell = errors.getShell();
87 Point ns = shell.computeSize(SWT.DEFAULT, SWT.DEFAULT);
88 Point cs = shell.getSize();
89 shell.setSize(Math.max(cs.x, ns.x), Math.max(cs.y, ns.y));
92 getButton(IDialogConstants.OK_ID).setEnabled(true);
100 errors= new Label(c, 0);
102 errors.setForeground(errors.getDisplay().getSystemColor(SWT.COLOR_RED));
109 private String validateVersion(final String version) {
111 if(info.containsVersion(version)) return "Version already in use";
112 if(AlphanumComparator.COMPARATOR.compare(version, info.version) < 0) return "New version must be lexically greater than current";
118 protected void okPressed() {
119 result = versionTxT.getText();
123 public String getResult() {
128 protected int getShellStyle() {
129 return SWT.RESIZE | SWT.TITLE | SWT.CLOSE | SWT.BORDER;
133 protected void configureShell(Shell newShell) {
134 super.configureShell(newShell);
135 newShell.setText("Create new version of " + info.baseName);