X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2FCreateVersionDialog.java;h=e59bc124676b7ba2b344d1021d0804e9e4f5390c;hb=06ee0c4c71cd9e372969da1570e7fcac2c4397a5;hp=a9029235dcf00cb8472d64407c4fd3e266b06a10;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/CreateVersionDialog.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/CreateVersionDialog.java index a9029235d..e59bc1246 100644 --- a/bundles/org.simantics.modeling/src/org/simantics/modeling/CreateVersionDialog.java +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/CreateVersionDialog.java @@ -1,137 +1,137 @@ -package org.simantics.modeling; - -import org.eclipse.jface.dialogs.Dialog; -import org.eclipse.jface.dialogs.IDialogConstants; -import org.eclipse.jface.layout.GridDataFactory; -import org.eclipse.jface.layout.GridLayoutFactory; -import org.eclipse.swt.SWT; -import org.eclipse.swt.events.ModifyEvent; -import org.eclipse.swt.events.ModifyListener; -import org.eclipse.swt.graphics.Point; -import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Control; -import org.eclipse.swt.widgets.Label; -import org.eclipse.swt.widgets.Shell; -import org.eclipse.swt.widgets.Text; -import org.simantics.db.common.utils.VersionInfo; -import org.simantics.utils.strings.AlphanumComparator; - -public class CreateVersionDialog extends Dialog { - - private final VersionInfo info; - - private Label errors; - private Text versionTxT; - private String result; - - public CreateVersionDialog(Shell parentShell, VersionInfo info) { - super(parentShell); - this.info = info; - } - - public String mutate(String ver) { - try { - int currentInt = Integer.parseInt(ver); - return "" + (currentInt+1); - } catch (NumberFormatException e) { - if(ver.length() == 1) { - char c = ver.charAt(0); - c++; - Character ch = c; - if(Character.isLetter(ch) && Character.isUpperCase(ch)) return ch.toString(); - } - } - return ver + "_1"; - } - - public String getSuggestion() { - - String ver = info.version; - while(validateVersion(ver) != null) ver = mutate(ver); - return ver; - - } - - @Override - protected Control createDialogArea(Composite parent) { - Composite c = (Composite) super.createDialogArea(parent); - - GridLayoutFactory.fillDefaults().margins(8, 8).numColumns(2).applyTo(c); - GridDataFactory gd1 = GridDataFactory.fillDefaults().span(2, 1); - - // 1st line - Label label = new Label(c, 0); - label.setText("Current version identifier is: " + info.version); - gd1.applyTo(label); - - // 2nd line - label = new Label(c, 0); - label.setText("New identifier:"); - GridDataFactory.fillDefaults().applyTo(label); - - versionTxT = new Text(c, SWT.BORDER); - versionTxT.setText(getSuggestion()); - - GridDataFactory.fillDefaults().grab(true, false).applyTo(versionTxT); - - versionTxT.addModifyListener(new ModifyListener() { - - @Override - public void modifyText(ModifyEvent e) { - - String result = validateVersion(versionTxT.getText()); - if(result != null) { - errors.setText(result); - getButton(IDialogConstants.OK_ID).setEnabled(false); - Shell shell = errors.getShell(); - Point ns = shell.computeSize(SWT.DEFAULT, SWT.DEFAULT); - Point cs = shell.getSize(); - shell.setSize(Math.max(cs.x, ns.x), Math.max(cs.y, ns.y)); - } else { - errors.setText(""); - getButton(IDialogConstants.OK_ID).setEnabled(true); - } - - } - - }); - - // Errors - errors= new Label(c, 0); - errors.setText(""); - errors.setForeground(errors.getDisplay().getSystemColor(SWT.COLOR_RED)); - - gd1.applyTo(errors); - - return c; - } - - private String validateVersion(final String version) { - - if(info.containsVersion(version)) return "Version already in use"; - if(AlphanumComparator.COMPARATOR.compare(version, info.version) < 0) return "New version must be lexically greater than current"; - return null; - - } - - @Override - protected void okPressed() { - result = versionTxT.getText(); - super.okPressed(); - } - - public String getResult() { - return result; - } - - @Override - protected int getShellStyle() { - return SWT.RESIZE | SWT.TITLE | SWT.CLOSE | SWT.BORDER; - } - - @Override - protected void configureShell(Shell newShell) { - super.configureShell(newShell); - newShell.setText("Create new version of " + info.baseName); - } -} +package org.simantics.modeling; + +import org.eclipse.jface.dialogs.Dialog; +import org.eclipse.jface.dialogs.IDialogConstants; +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.ModifyEvent; +import org.eclipse.swt.events.ModifyListener; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Text; +import org.simantics.db.common.utils.VersionInfo; +import org.simantics.utils.strings.AlphanumComparator; + +public class CreateVersionDialog extends Dialog { + + private final VersionInfo info; + + private Label errors; + private Text versionTxT; + private String result; + + public CreateVersionDialog(Shell parentShell, VersionInfo info) { + super(parentShell); + this.info = info; + } + + public String mutate(String ver) { + try { + int currentInt = Integer.parseInt(ver); + return "" + (currentInt+1); + } catch (NumberFormatException e) { + if(ver.length() == 1) { + char c = ver.charAt(0); + c++; + Character ch = c; + if(Character.isLetter(ch) && Character.isUpperCase(ch)) return ch.toString(); + } + } + return ver + "_1"; + } + + public String getSuggestion() { + + String ver = info.version; + while(validateVersion(ver) != null) ver = mutate(ver); + return ver; + + } + + @Override + protected Control createDialogArea(Composite parent) { + Composite c = (Composite) super.createDialogArea(parent); + + GridLayoutFactory.fillDefaults().margins(8, 8).numColumns(2).applyTo(c); + GridDataFactory gd1 = GridDataFactory.fillDefaults().span(2, 1); + + // 1st line + Label label = new Label(c, 0); + label.setText("Current version identifier is: " + info.version); + gd1.applyTo(label); + + // 2nd line + label = new Label(c, 0); + label.setText("New identifier:"); + GridDataFactory.fillDefaults().applyTo(label); + + versionTxT = new Text(c, SWT.BORDER); + versionTxT.setText(getSuggestion()); + + GridDataFactory.fillDefaults().grab(true, false).applyTo(versionTxT); + + versionTxT.addModifyListener(new ModifyListener() { + + @Override + public void modifyText(ModifyEvent e) { + + String result = validateVersion(versionTxT.getText()); + if(result != null) { + errors.setText(result); + getButton(IDialogConstants.OK_ID).setEnabled(false); + Shell shell = errors.getShell(); + Point ns = shell.computeSize(SWT.DEFAULT, SWT.DEFAULT); + Point cs = shell.getSize(); + shell.setSize(Math.max(cs.x, ns.x), Math.max(cs.y, ns.y)); + } else { + errors.setText(""); + getButton(IDialogConstants.OK_ID).setEnabled(true); + } + + } + + }); + + // Errors + errors= new Label(c, 0); + errors.setText(""); + errors.setForeground(errors.getDisplay().getSystemColor(SWT.COLOR_RED)); + + gd1.applyTo(errors); + + return c; + } + + private String validateVersion(final String version) { + + if(info.containsVersion(version)) return "Version already in use"; + if(AlphanumComparator.COMPARATOR.compare(version, info.version) < 0) return "New version must be lexically greater than current"; + return null; + + } + + @Override + protected void okPressed() { + result = versionTxT.getText(); + super.okPressed(); + } + + public String getResult() { + return result; + } + + @Override + protected int getShellStyle() { + return SWT.RESIZE | SWT.TITLE | SWT.CLOSE | SWT.BORDER; + } + + @Override + protected void configureShell(Shell newShell) { + super.configureShell(newShell); + newShell.setText("Create new version of " + info.baseName); + } +}