KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > parts > PluginVersionPart


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 JavaDoc version) {
47         try {
48             if (version != null && !version.equals("")) { //$NON-NLS-1$
49
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 JavaDoc e) {
56             // illegal version string passed
57
fVersionRange = new VersionRange("[1.0.0,1.0.0]"); //$NON-NLS-1$
58
}
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 JavaDoc[] comboItems = new String JavaDoc[] {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() : ""); //$NON-NLS-1$
116
fMaxVersionText.setText((fVersionRange != null && fVersionRange.getMaximum().getMajor() != Integer.MAX_VALUE) ? fVersionRange.getMaximum().toString() : ""); //$NON-NLS-1$
117

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() : ""); //$NON-NLS-1$
129
}
130     
131     private IStatus validateVersion(String JavaDoc text, Text textWidget, boolean shortErrorMessage) {
132         if (text.length() == 0) return Status.OK_STATUS;
133         if (VersionUtil.validateVersion(text).getSeverity() != IStatus.OK) {
134             String JavaDoc errorMessage = null;
135             if (shortErrorMessage) {
136                 // For dialogs
137
errorMessage = PDEUIMessages.DependencyPropertiesDialog_invalidFormat;
138             } else {
139                 // For everything else: Field assist, wizards
140
errorMessage = PDECoreMessages.BundleErrorReporter_InvalidFormatInBundleVersion;
141             }
142             return new Status(IStatus.ERROR, "org.eclipse.pde.ui", //$NON-NLS-1$
143
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 JavaDoc errorMessage = null;
160         if (shortErrorMessage) {
161             // For dialogs
162
errorMessage = PDEUIMessages.DependencyPropertiesDialog_invalidFormat;
163         } else {
164             // For everything else: Field assist, wizards
165
errorMessage = PDECoreMessages.BundleErrorReporter_InvalidFormatInBundleVersion;
166         }
167         
168         Version v1;
169         Version v2;
170         try {
171             v1 = new Version(getMinVersion());
172         } catch (IllegalArgumentException JavaDoc e) {
173             return new Status(
174                     IStatus.ERROR,
175                     "org.eclipse.pde.ui", //$NON-NLS-1$
176
IStatus.ERROR,
177                     PDELabelUtility.qualifyMessage(PDELabelUtility.getFieldLabel(fMinVersionText),
178                             errorMessage),
179                     null);
180         }
181         if (!fRangeAllowed) // version created fine
182
return Status.OK_STATUS;
183
184         try {
185             v2 = new Version(getMaxVersion());
186         } catch (IllegalArgumentException JavaDoc e) {
187             return new Status(
188                     IStatus.ERROR,
189                     "org.eclipse.pde.ui", //$NON-NLS-1$
190
IStatus.ERROR,
191                     PDELabelUtility.qualifyMessage(PDELabelUtility.getFieldLabel(fMaxVersionText),
192                             errorMessage),
193                     null); //$NON-NLS-1$;
194
}
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", //$NON-NLS-1$
201
IStatus.ERROR,
202                 PDEUIMessages.DependencyPropertiesDialog_versionRangeError,
203                 null);
204     }
205     
206     /**
207      * Short error messages are required for dialog status lines. Long error
208      * messages are truncated and are not decorated with a status image.
209      * @param shortErrorMessage if <code>true</code>, a brief error message
210      * will be used.
211      * @return
212      */

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 JavaDoc getMinVersion() {
221         return fMinVersionText.getText().trim();
222     }
223     private String JavaDoc getMaxVersion() {
224         if (fMaxVersionText != null)
225             return fMaxVersionText.getText().trim();
226         return ""; //$NON-NLS-1$
227
}
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 JavaDoc extractSingleVersionFromText() {
240         if (!fRangeAllowed) return getMinVersion();
241         if (getMinVersion().length() == 0)
242             return getMaxVersion();
243         return getMinVersion();
244     }
245     
246     public String JavaDoc getVersion() {
247         String JavaDoc version;
248         if (fIsRanged) {
249             // if versions are equal they must be inclusive for a range to be valid
250
// blindly set for the user
251
String JavaDoc minV = getMinVersion();
252             String JavaDoc 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 JavaDoc singleversion = extractSingleVersionFromText();
260             if (singleversion == null || singleversion.length() == 0)
261                 version = ""; //$NON-NLS-1$
262
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 JavaDoc getGroupText() {
276         return PDEUIMessages.DependencyPropertiesDialog_groupText;
277     }
278 }
279
Popular Tags