]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/CreateVersionDialog.java
Multiple readers and variable optimization
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / CreateVersionDialog.java
1 package org.simantics.modeling;
2
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;
18
19 public class CreateVersionDialog extends Dialog {
20
21         private final VersionInfo info;
22
23         private Label errors;
24         private Text versionTxT;
25         private String result;
26         
27         public CreateVersionDialog(Shell parentShell, VersionInfo info) {
28                 super(parentShell);
29                 this.info = info;
30         }
31         
32         public String mutate(String ver) {
33         try {
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);
39                         c++;
40                         Character ch = c; 
41                         if(Character.isLetter(ch) && Character.isUpperCase(ch)) return ch.toString();
42                 }
43         }
44         return ver + "_1";
45         }
46         
47         public String getSuggestion() {
48                 
49                 String ver = info.version;
50                 while(validateVersion(ver) != null) ver = mutate(ver);
51                 return ver;
52                 
53         }
54
55         @Override
56         protected Control createDialogArea(Composite parent) {
57                 Composite c = (Composite) super.createDialogArea(parent);
58                 
59         GridLayoutFactory.fillDefaults().margins(8, 8).numColumns(2).applyTo(c);
60         GridDataFactory gd1 = GridDataFactory.fillDefaults().span(2, 1);
61
62         // 1st line
63         Label label = new Label(c, 0);
64         label.setText("Current version identifier is: " + info.version);
65         gd1.applyTo(label);
66         
67         // 2nd line
68         label = new Label(c, 0);
69         label.setText("New identifier:");
70         GridDataFactory.fillDefaults().applyTo(label);
71
72         versionTxT = new Text(c, SWT.BORDER);
73         versionTxT.setText(getSuggestion());
74         
75         GridDataFactory.fillDefaults().grab(true, false).applyTo(versionTxT);
76         
77         versionTxT.addModifyListener(new ModifyListener() {
78
79                         @Override
80                         public void modifyText(ModifyEvent e) {
81
82                                 String result = validateVersion(versionTxT.getText()); 
83                                 if(result != null) {
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));
90                                 } else {
91                                         errors.setText("");
92                                         getButton(IDialogConstants.OK_ID).setEnabled(true);
93                                 }
94                                 
95                         }
96                 
97         });
98         
99         // Errors
100         errors= new Label(c, 0);
101         errors.setText("");
102         errors.setForeground(errors.getDisplay().getSystemColor(SWT.COLOR_RED));
103         
104         gd1.applyTo(errors);
105         
106                 return c;
107         }
108         
109         private String validateVersion(final String version) {
110                 
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";
113                 return null;
114                 
115         }
116         
117         @Override
118         protected void okPressed() {
119                 result = versionTxT.getText();
120         super.okPressed();
121         }
122         
123         public String getResult() {
124                 return result;
125         }
126
127         @Override
128     protected int getShellStyle() {
129         return SWT.RESIZE | SWT.TITLE | SWT.CLOSE | SWT.BORDER;
130     }
131
132     @Override
133     protected void configureShell(Shell newShell) {
134         super.configureShell(newShell);
135         newShell.setText("Create new version of " + info.baseName);
136     }
137 }