1 11 package org.eclipse.pde.internal.ui.parts; 12 13 import org.eclipse.core.runtime.IStatus; 14 import org.eclipse.core.runtime.Status; 15 import org.eclipse.osgi.service.resolver.VersionRange; 16 import org.eclipse.pde.internal.core.PDECoreMessages; 17 import org.eclipse.pde.internal.core.util.VersionUtil; 18 import org.eclipse.pde.internal.ui.PDEUIMessages; 19 import org.eclipse.pde.internal.ui.util.PDELabelUtility; 20 import org.eclipse.swt.SWT; 21 import org.eclipse.swt.events.ModifyListener; 22 import org.eclipse.swt.layout.GridData; 23 import org.eclipse.swt.layout.GridLayout; 24 import org.eclipse.swt.widgets.Combo; 25 import org.eclipse.swt.widgets.Composite; 26 import org.eclipse.swt.widgets.Group; 27 import org.eclipse.swt.widgets.Label; 28 import org.eclipse.swt.widgets.Text; 29 import org.osgi.framework.Version; 30 31 public class PluginVersionPart { 32 33 private Text fMinVersionText; 34 private Text fMaxVersionText; 35 private Combo fMinVersionBound; 36 private Combo fMaxVersionBound; 37 38 private VersionRange fVersionRange; 39 private boolean fIsRanged; 40 private boolean fRangeAllowed; 41 42 public PluginVersionPart(boolean rangeAllowed) { 43 fRangeAllowed = rangeAllowed; 44 } 45 46 public void setVersion(String version) { 47 try { 48 if (version != null && !version.equals("")) { fVersionRange = new VersionRange(version); 50 Version max = fVersionRange.getMaximum(); 51 if (max.getMajor() != Integer.MAX_VALUE && 52 fVersionRange.getMinimum().compareTo(fVersionRange.getMaximum()) < 0) 53 fIsRanged = true; 54 } 55 } catch (IllegalArgumentException e) { 56 fVersionRange = new VersionRange("[1.0.0,1.0.0]"); } 59 } 60 61 public void createVersionFields(Composite comp, boolean createGroup, boolean editable) { 62 if (fRangeAllowed) 63 createRangeField(comp, createGroup, editable); 64 else 65 createSingleField(comp, createGroup, editable); 66 preloadFields(); 67 } 68 69 private void createRangeField(Composite parent, boolean createGroup, boolean editable) { 70 if (createGroup) { 71 parent = new Group(parent, SWT.NONE); 72 ((Group)parent).setText(getGroupText()); 73 parent.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 74 parent.setLayout(new GridLayout(3, false)); 75 } 76 String [] comboItems = new String [] {PDEUIMessages.DependencyPropertiesDialog_comboInclusive, PDEUIMessages.DependencyPropertiesDialog_comboExclusive}; 77 Label minlabel = new Label(parent, SWT.NONE); 78 minlabel.setText(PDEUIMessages.DependencyPropertiesDialog_minimumVersion); 79 fMinVersionText = new Text(parent, SWT.SINGLE|SWT.BORDER); 80 fMinVersionText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 81 fMinVersionText.setEnabled(editable); 82 83 fMinVersionBound = new Combo(parent, SWT.SINGLE|SWT.BORDER|SWT.READ_ONLY ); 84 fMinVersionBound.setEnabled(editable); 85 fMinVersionBound.setItems(comboItems); 86 87 Label maxlabel = new Label(parent, SWT.NONE); 88 maxlabel.setText(PDEUIMessages.DependencyPropertiesDialog_maximumVersion); 89 fMaxVersionText = new Text(parent, SWT.SINGLE|SWT.BORDER); 90 fMaxVersionText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 91 fMaxVersionText.setEnabled(editable); 92 93 fMaxVersionBound = new Combo(parent, SWT.SINGLE|SWT.BORDER|SWT.READ_ONLY); 94 fMaxVersionBound.setEnabled(editable); 95 fMaxVersionBound.setItems(comboItems); 96 } 97 98 private void createSingleField(Composite parent, boolean createGroup, boolean editable) { 99 if (createGroup) { 100 parent = new Group(parent, SWT.NONE); 101 ((Group)parent).setText(getGroupText()); 102 parent.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING|GridData.FILL_HORIZONTAL)); 103 parent.setLayout(new GridLayout(2, false)); 104 } 105 Label label = new Label(parent, SWT.NONE); 106 label.setText(PDEUIMessages.DependencyPropertiesDialog_version); 107 108 fMinVersionText = new Text(parent, SWT.SINGLE|SWT.BORDER); 109 fMinVersionText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 110 fMinVersionText.setEnabled(editable); 111 } 112 113 public void preloadFields() { 114 if (fRangeAllowed) { 115 fMinVersionText.setText((fVersionRange != null) ? fVersionRange.getMinimum().toString() : ""); fMaxVersionText.setText((fVersionRange != null && fVersionRange.getMaximum().getMajor() != Integer.MAX_VALUE) ? fVersionRange.getMaximum().toString() : ""); 118 if (fVersionRange != null) 119 fMinVersionBound.select((fVersionRange.getIncludeMinimum()) ? 0 : 1); 120 else 121 fMinVersionBound.select(0); 122 123 if (fVersionRange != null && getMaxVersion().length() > 0) 124 fMaxVersionBound.select((fVersionRange.getIncludeMaximum()) ? 0 : 1); 125 else 126 fMaxVersionBound.select(1); 127 } 128 fMinVersionText.setText((fVersionRange != null) ? fVersionRange.getMinimum().toString() : ""); } 130 131 private IStatus validateVersion(String text, Text textWidget, boolean shortErrorMessage) { 132 if (text.length() == 0) return Status.OK_STATUS; 133 if (VersionUtil.validateVersion(text).getSeverity() != IStatus.OK) { 134 String errorMessage = null; 135 if (shortErrorMessage) { 136 errorMessage = PDEUIMessages.DependencyPropertiesDialog_invalidFormat; 138 } else { 139 errorMessage = PDECoreMessages.BundleErrorReporter_InvalidFormatInBundleVersion; 141 } 142 return new Status(IStatus.ERROR, "org.eclipse.pde.ui", IStatus.ERROR, 144 PDELabelUtility.qualifyMessage(PDELabelUtility.getFieldLabel(textWidget), 145 errorMessage), 146 null); 147 } 148 149 return Status.OK_STATUS; 150 } 151 152 private IStatus validateVersionRange(boolean shortErrorMessage) { 153 if ((!fRangeAllowed && getMinVersion().length() == 0) 154 || (fRangeAllowed && (getMinVersion().length() == 0 || getMaxVersion().length() == 0))) { 155 fIsRanged = false; 156 return Status.OK_STATUS; 157 } 158 159 String errorMessage = null; 160 if (shortErrorMessage) { 161 errorMessage = PDEUIMessages.DependencyPropertiesDialog_invalidFormat; 163 } else { 164 errorMessage = PDECoreMessages.BundleErrorReporter_InvalidFormatInBundleVersion; 166 } 167 168 Version v1; 169 Version v2; 170 try { 171 v1 = new Version(getMinVersion()); 172 } catch (IllegalArgumentException e) { 173 return new Status( 174 IStatus.ERROR, 175 "org.eclipse.pde.ui", IStatus.ERROR, 177 PDELabelUtility.qualifyMessage(PDELabelUtility.getFieldLabel(fMinVersionText), 178 errorMessage), 179 null); 180 } 181 if (!fRangeAllowed) return Status.OK_STATUS; 183 184 try { 185 v2 = new Version(getMaxVersion()); 186 } catch (IllegalArgumentException e) { 187 return new Status( 188 IStatus.ERROR, 189 "org.eclipse.pde.ui", IStatus.ERROR, 191 PDELabelUtility.qualifyMessage(PDELabelUtility.getFieldLabel(fMaxVersionText), 192 errorMessage), 193 null); } 195 if (v1.compareTo(v2) == 0 || v1.compareTo(v2) < 0) { 196 fIsRanged = true; 197 return Status.OK_STATUS; 198 } 199 return new Status(IStatus.ERROR, 200 "org.eclipse.pde.ui", IStatus.ERROR, 202 PDEUIMessages.DependencyPropertiesDialog_versionRangeError, 203 null); 204 } 205 206 213 public IStatus validateFullVersionRangeText(boolean shortErrorMessage) { 214 IStatus status = validateVersion(getMinVersion(), fMinVersionText, shortErrorMessage); 215 if (status.isOK()) status = validateVersion(getMaxVersion(), fMaxVersionText, shortErrorMessage); 216 if (status.isOK()) status = validateVersionRange(shortErrorMessage); 217 return status; 218 } 219 220 private String getMinVersion() { 221 return fMinVersionText.getText().trim(); 222 } 223 private String getMaxVersion() { 224 if (fMaxVersionText != null) 225 return fMaxVersionText.getText().trim(); 226 return ""; } 228 private boolean getMinInclusive() { 229 if (fMinVersionBound != null) 230 return fMinVersionBound.getSelectionIndex() == 0; 231 return false; 232 } 233 private boolean getMaxInclusive() { 234 if (fMaxVersionBound != null) 235 return fMaxVersionBound.getSelectionIndex() == 0; 236 return true; 237 } 238 239 private String extractSingleVersionFromText() { 240 if (!fRangeAllowed) return getMinVersion(); 241 if (getMinVersion().length() == 0) 242 return getMaxVersion(); 243 return getMinVersion(); 244 } 245 246 public String getVersion() { 247 String version; 248 if (fIsRanged) { 249 String minV = getMinVersion(); 252 String maxV = getMaxVersion(); 253 boolean minI = getMinInclusive(); 254 boolean maxI = getMaxInclusive(); 255 if (minV.equals(maxV)) 256 minI = maxI = true; 257 version = new VersionRange(new Version(minV), minI, new Version(maxV), maxI).toString(); 258 } else { 259 String singleversion = extractSingleVersionFromText(); 260 if (singleversion == null || singleversion.length() == 0) 261 version = ""; else 263 version = new Version(singleversion).toString(); 264 } 265 return version; 266 } 267 268 public void addListeners(ModifyListener minListener, ModifyListener maxListener) { 269 if (fMinVersionText != null && minListener != null) 270 fMinVersionText.addModifyListener(minListener); 271 if (fRangeAllowed && fMaxVersionText != null && maxListener != null) 272 fMaxVersionText.addModifyListener(maxListener); 273 } 274 275 protected String getGroupText() { 276 return PDEUIMessages.DependencyPropertiesDialog_groupText; 277 } 278 } 279 | Popular Tags |