001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.parts;
011:
012: import org.eclipse.core.runtime.IStatus;
013: import org.eclipse.core.runtime.Status;
014: import org.eclipse.osgi.service.resolver.VersionRange;
015: import org.eclipse.pde.internal.core.PDECoreMessages;
016: import org.eclipse.pde.internal.core.util.VersionUtil;
017: import org.eclipse.pde.internal.ui.PDEUIMessages;
018: import org.eclipse.pde.internal.ui.util.PDELabelUtility;
019: import org.eclipse.swt.SWT;
020: import org.eclipse.swt.events.ModifyListener;
021: import org.eclipse.swt.layout.GridData;
022: import org.eclipse.swt.layout.GridLayout;
023: import org.eclipse.swt.widgets.Combo;
024: import org.eclipse.swt.widgets.Composite;
025: import org.eclipse.swt.widgets.Group;
026: import org.eclipse.swt.widgets.Label;
027: import org.eclipse.swt.widgets.Text;
028: import org.osgi.framework.Version;
029:
030: public class PluginVersionPart {
031:
032: private Text fMinVersionText;
033: private Text fMaxVersionText;
034: private Combo fMinVersionBound;
035: private Combo fMaxVersionBound;
036:
037: private VersionRange fVersionRange;
038: private boolean fIsRanged;
039: private boolean fRangeAllowed;
040:
041: public PluginVersionPart(boolean rangeAllowed) {
042: fRangeAllowed = rangeAllowed;
043: }
044:
045: public void setVersion(String version) {
046: try {
047: if (version != null && !version.equals("")) { //$NON-NLS-1$
048: fVersionRange = new VersionRange(version);
049: Version max = fVersionRange.getMaximum();
050: if (max.getMajor() != Integer.MAX_VALUE
051: && fVersionRange.getMinimum().compareTo(
052: fVersionRange.getMaximum()) < 0)
053: fIsRanged = true;
054: }
055: } catch (IllegalArgumentException e) {
056: // illegal version string passed
057: fVersionRange = new VersionRange("[1.0.0,1.0.0]"); //$NON-NLS-1$
058: }
059: }
060:
061: public void createVersionFields(Composite comp,
062: boolean createGroup, boolean editable) {
063: if (fRangeAllowed)
064: createRangeField(comp, createGroup, editable);
065: else
066: createSingleField(comp, createGroup, editable);
067: preloadFields();
068: }
069:
070: private void createRangeField(Composite parent,
071: boolean createGroup, boolean editable) {
072: if (createGroup) {
073: parent = new Group(parent, SWT.NONE);
074: ((Group) parent).setText(getGroupText());
075: parent
076: .setLayoutData(new GridData(
077: GridData.FILL_HORIZONTAL));
078: parent.setLayout(new GridLayout(3, false));
079: }
080: String[] comboItems = new String[] {
081: PDEUIMessages.DependencyPropertiesDialog_comboInclusive,
082: PDEUIMessages.DependencyPropertiesDialog_comboExclusive };
083: Label minlabel = new Label(parent, SWT.NONE);
084: minlabel
085: .setText(PDEUIMessages.DependencyPropertiesDialog_minimumVersion);
086: fMinVersionText = new Text(parent, SWT.SINGLE | SWT.BORDER);
087: fMinVersionText.setLayoutData(new GridData(
088: GridData.FILL_HORIZONTAL));
089: fMinVersionText.setEnabled(editable);
090:
091: fMinVersionBound = new Combo(parent, SWT.SINGLE | SWT.BORDER
092: | SWT.READ_ONLY);
093: fMinVersionBound.setEnabled(editable);
094: fMinVersionBound.setItems(comboItems);
095:
096: Label maxlabel = new Label(parent, SWT.NONE);
097: maxlabel
098: .setText(PDEUIMessages.DependencyPropertiesDialog_maximumVersion);
099: fMaxVersionText = new Text(parent, SWT.SINGLE | SWT.BORDER);
100: fMaxVersionText.setLayoutData(new GridData(
101: GridData.FILL_HORIZONTAL));
102: fMaxVersionText.setEnabled(editable);
103:
104: fMaxVersionBound = new Combo(parent, SWT.SINGLE | SWT.BORDER
105: | SWT.READ_ONLY);
106: fMaxVersionBound.setEnabled(editable);
107: fMaxVersionBound.setItems(comboItems);
108: }
109:
110: private void createSingleField(Composite parent,
111: boolean createGroup, boolean editable) {
112: if (createGroup) {
113: parent = new Group(parent, SWT.NONE);
114: ((Group) parent).setText(getGroupText());
115: parent.setLayoutData(new GridData(
116: GridData.VERTICAL_ALIGN_BEGINNING
117: | GridData.FILL_HORIZONTAL));
118: parent.setLayout(new GridLayout(2, false));
119: }
120: Label label = new Label(parent, SWT.NONE);
121: label.setText(PDEUIMessages.DependencyPropertiesDialog_version);
122:
123: fMinVersionText = new Text(parent, SWT.SINGLE | SWT.BORDER);
124: fMinVersionText.setLayoutData(new GridData(
125: GridData.FILL_HORIZONTAL));
126: fMinVersionText.setEnabled(editable);
127: }
128:
129: public void preloadFields() {
130: if (fRangeAllowed) {
131: fMinVersionText
132: .setText((fVersionRange != null) ? fVersionRange
133: .getMinimum().toString() : ""); //$NON-NLS-1$
134: fMaxVersionText
135: .setText((fVersionRange != null && fVersionRange
136: .getMaximum().getMajor() != Integer.MAX_VALUE) ? fVersionRange
137: .getMaximum().toString()
138: : ""); //$NON-NLS-1$
139:
140: if (fVersionRange != null)
141: fMinVersionBound.select((fVersionRange
142: .getIncludeMinimum()) ? 0 : 1);
143: else
144: fMinVersionBound.select(0);
145:
146: if (fVersionRange != null && getMaxVersion().length() > 0)
147: fMaxVersionBound.select((fVersionRange
148: .getIncludeMaximum()) ? 0 : 1);
149: else
150: fMaxVersionBound.select(1);
151: }
152: fMinVersionText.setText((fVersionRange != null) ? fVersionRange
153: .getMinimum().toString() : ""); //$NON-NLS-1$
154: }
155:
156: private IStatus validateVersion(String text, Text textWidget,
157: boolean shortErrorMessage) {
158: if (text.length() == 0)
159: return Status.OK_STATUS;
160: if (VersionUtil.validateVersion(text).getSeverity() != IStatus.OK) {
161: String errorMessage = null;
162: if (shortErrorMessage) {
163: // For dialogs
164: errorMessage = PDEUIMessages.DependencyPropertiesDialog_invalidFormat;
165: } else {
166: // For everything else: Field assist, wizards
167: errorMessage = PDECoreMessages.BundleErrorReporter_InvalidFormatInBundleVersion;
168: }
169: return new Status(IStatus.ERROR, "org.eclipse.pde.ui", //$NON-NLS-1$
170: IStatus.ERROR, PDELabelUtility.qualifyMessage(
171: PDELabelUtility.getFieldLabel(textWidget),
172: errorMessage), null);
173: }
174:
175: return Status.OK_STATUS;
176: }
177:
178: private IStatus validateVersionRange(boolean shortErrorMessage) {
179: if ((!fRangeAllowed && getMinVersion().length() == 0)
180: || (fRangeAllowed && (getMinVersion().length() == 0 || getMaxVersion()
181: .length() == 0))) {
182: fIsRanged = false;
183: return Status.OK_STATUS;
184: }
185:
186: String errorMessage = null;
187: if (shortErrorMessage) {
188: // For dialogs
189: errorMessage = PDEUIMessages.DependencyPropertiesDialog_invalidFormat;
190: } else {
191: // For everything else: Field assist, wizards
192: errorMessage = PDECoreMessages.BundleErrorReporter_InvalidFormatInBundleVersion;
193: }
194:
195: Version v1;
196: Version v2;
197: try {
198: v1 = new Version(getMinVersion());
199: } catch (IllegalArgumentException e) {
200: return new Status(IStatus.ERROR, "org.eclipse.pde.ui", //$NON-NLS-1$
201: IStatus.ERROR, PDELabelUtility.qualifyMessage(
202: PDELabelUtility
203: .getFieldLabel(fMinVersionText),
204: errorMessage), null);
205: }
206: if (!fRangeAllowed) // version created fine
207: return Status.OK_STATUS;
208:
209: try {
210: v2 = new Version(getMaxVersion());
211: } catch (IllegalArgumentException e) {
212: return new Status(IStatus.ERROR, "org.eclipse.pde.ui", //$NON-NLS-1$
213: IStatus.ERROR, PDELabelUtility.qualifyMessage(
214: PDELabelUtility
215: .getFieldLabel(fMaxVersionText),
216: errorMessage), null); //$NON-NLS-1$;
217: }
218: if (v1.compareTo(v2) == 0 || v1.compareTo(v2) < 0) {
219: fIsRanged = true;
220: return Status.OK_STATUS;
221: }
222: return new Status(
223: IStatus.ERROR,
224: "org.eclipse.pde.ui", //$NON-NLS-1$
225: IStatus.ERROR,
226: PDEUIMessages.DependencyPropertiesDialog_versionRangeError,
227: null);
228: }
229:
230: /**
231: * Short error messages are required for dialog status lines. Long error
232: * messages are truncated and are not decorated with a status image.
233: * @param shortErrorMessage if <code>true</code>, a brief error message
234: * will be used.
235: * @return
236: */
237: public IStatus validateFullVersionRangeText(
238: boolean shortErrorMessage) {
239: IStatus status = validateVersion(getMinVersion(),
240: fMinVersionText, shortErrorMessage);
241: if (status.isOK())
242: status = validateVersion(getMaxVersion(), fMaxVersionText,
243: shortErrorMessage);
244: if (status.isOK())
245: status = validateVersionRange(shortErrorMessage);
246: return status;
247: }
248:
249: private String getMinVersion() {
250: return fMinVersionText.getText().trim();
251: }
252:
253: private String getMaxVersion() {
254: if (fMaxVersionText != null)
255: return fMaxVersionText.getText().trim();
256: return ""; //$NON-NLS-1$
257: }
258:
259: private boolean getMinInclusive() {
260: if (fMinVersionBound != null)
261: return fMinVersionBound.getSelectionIndex() == 0;
262: return false;
263: }
264:
265: private boolean getMaxInclusive() {
266: if (fMaxVersionBound != null)
267: return fMaxVersionBound.getSelectionIndex() == 0;
268: return true;
269: }
270:
271: private String extractSingleVersionFromText() {
272: if (!fRangeAllowed)
273: return getMinVersion();
274: if (getMinVersion().length() == 0)
275: return getMaxVersion();
276: return getMinVersion();
277: }
278:
279: public String getVersion() {
280: String version;
281: if (fIsRanged) {
282: // if versions are equal they must be inclusive for a range to be valid
283: // blindly set for the user
284: String minV = getMinVersion();
285: String maxV = getMaxVersion();
286: boolean minI = getMinInclusive();
287: boolean maxI = getMaxInclusive();
288: if (minV.equals(maxV))
289: minI = maxI = true;
290: version = new VersionRange(new Version(minV), minI,
291: new Version(maxV), maxI).toString();
292: } else {
293: String singleversion = extractSingleVersionFromText();
294: if (singleversion == null || singleversion.length() == 0)
295: version = ""; //$NON-NLS-1$
296: else
297: version = new Version(singleversion).toString();
298: }
299: return version;
300: }
301:
302: public void addListeners(ModifyListener minListener,
303: ModifyListener maxListener) {
304: if (fMinVersionText != null && minListener != null)
305: fMinVersionText.addModifyListener(minListener);
306: if (fRangeAllowed && fMaxVersionText != null
307: && maxListener != null)
308: fMaxVersionText.addModifyListener(maxListener);
309: }
310:
311: protected String getGroupText() {
312: return PDEUIMessages.DependencyPropertiesDialog_groupText;
313: }
314: }
|