KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > plugin > ContentPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 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
12 package org.eclipse.pde.internal.ui.wizards.plugin;
13
14 import java.util.StringTokenizer JavaDoc;
15
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.jface.wizard.IWizardPage;
18 import org.eclipse.jface.wizard.WizardPage;
19 import org.eclipse.pde.internal.core.PDECoreMessages;
20 import org.eclipse.pde.internal.core.util.IdUtil;
21 import org.eclipse.pde.internal.core.util.VersionUtil;
22 import org.eclipse.pde.internal.ui.PDEUIMessages;
23 import org.eclipse.pde.internal.ui.util.PDELabelUtility;
24 import org.eclipse.pde.internal.ui.wizards.IProjectProvider;
25 import org.eclipse.pde.ui.IFieldData;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.ModifyEvent;
28 import org.eclipse.swt.events.ModifyListener;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.swt.widgets.Text;
33
34 public abstract class ContentPage extends WizardPage {
35
36     protected boolean fInitialized = false;
37     protected Text fIdText;
38     protected Text fVersionText;
39     protected Text fNameText;
40     protected Text fProviderText;
41     protected Label fLibraryLabel;
42     protected Text fLibraryText;
43
44     protected NewProjectCreationPage fMainPage;
45     protected AbstractFieldData fData;
46     protected IProjectProvider fProjectProvider;
47
48     protected final static int PROPERTIES_GROUP = 1;
49     
50     protected int fChangedGroups = 0;
51     
52     protected ModifyListener propertiesListener = new ModifyListener() {
53         public void modifyText(ModifyEvent e) {
54             if (fInitialized)
55                 fChangedGroups |= PROPERTIES_GROUP;
56             validatePage();
57         }
58     };
59     public ContentPage(String JavaDoc pageName, IProjectProvider provider,
60             NewProjectCreationPage page, AbstractFieldData data) {
61         super(pageName);
62         fMainPage = page;
63         fProjectProvider = provider;
64         fData = data;
65     }
66
67     protected Text createText(Composite parent, ModifyListener listener) {
68         Text text = new Text(parent, SWT.BORDER | SWT.SINGLE);
69         text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
70         text.addModifyListener(listener);
71         return text;
72     }
73     
74     protected abstract void validatePage();
75
76     protected String JavaDoc validateProperties() {
77         
78         if (!fInitialized) {
79             if (!fIdText.getText().trim().equals(fProjectProvider.getProjectName())) {
80                 setMessage(PDEUIMessages.ContentPage_illegalCharactersInID, INFORMATION);
81             } else {
82                 setMessage(null);
83             }
84             return null;
85         }
86         
87         setMessage(null);
88         String JavaDoc errorMessage = null;
89         
90         // Validate ID
91
errorMessage = validateId();
92         if (errorMessage != null) {
93             return errorMessage;
94         }
95         
96         // Validate Version
97
errorMessage = validateVersion(fVersionText);
98         if (errorMessage != null) {
99             return errorMessage;
100         }
101         
102         // Validate Name
103
errorMessage = validateName();
104         if (errorMessage != null) {
105             return errorMessage;
106         }
107         
108         return null;
109     }
110
111     /**
112      * @param text
113      * @return
114      */

115     protected String JavaDoc validateVersion(Text text) {
116         if (text.getText().trim().length() == 0) {
117             return PDELabelUtility.qualifyMessage(PDELabelUtility.getFieldLabel(text),
118                 PDEUIMessages.ControlValidationUtility_errorMsgValueMustBeSpecified);
119         } else if (!isVersionValid(text.getText().trim())) {
120             return PDELabelUtility.qualifyMessage(PDELabelUtility.getFieldLabel(text),
121                 PDECoreMessages.BundleErrorReporter_InvalidFormatInBundleVersion);
122         }
123         return null;
124     }
125
126     /**
127      * @return
128      */

129     private String JavaDoc validateName() {
130         if (fNameText.getText().trim().length() == 0) {
131             return PDEUIMessages.ContentPage_noname;
132         }
133         return null;
134     }
135     
136     private String JavaDoc validateId() {
137         String JavaDoc id = fIdText.getText().trim();
138         if (id.length() == 0)
139             return PDEUIMessages.ContentPage_noid;
140
141         if (!IdUtil.isValidCompositeID(id)) {
142             return PDEUIMessages.ContentPage_invalidId;
143         }
144         return null;
145     }
146
147     protected boolean isVersionValid(String JavaDoc version) {
148         return VersionUtil.validateVersion(version).getSeverity() == IStatus.OK;
149     }
150     
151     public IWizardPage getNextPage() {
152         updateData();
153         return super.getNextPage();
154     }
155
156     /*
157      * (non-Javadoc)
158      *
159      * @see org.eclipse.jface.dialogs.IDialogPage#setVisible(boolean)
160      */

161     public void setVisible(boolean visible) {
162         if (visible) {
163             // update the library field/label enabled state
164
fLibraryLabel.setEnabled(!fData.isSimple());
165             fLibraryText.setEnabled(!fData.isSimple());
166
167             String JavaDoc id = computeId();
168             // properties group
169
if ((fChangedGroups & PROPERTIES_GROUP) == 0) {
170                 int oldfChanged = fChangedGroups;
171                 fIdText.setText(id);
172                 fVersionText.setText("1.0.0"); //$NON-NLS-1$
173
fNameText.setText(IdUtil.getValidName(id, getNameFieldQualifier()));
174                 fProviderText.setText(IdUtil.getValidProvider(id));
175                 presetLibraryField(id);
176                 fChangedGroups = oldfChanged;
177             }
178             if (fInitialized)
179                 validatePage();
180             else
181                 fInitialized = true;
182         }
183         super.setVisible(visible);
184     }
185     
186     protected String JavaDoc computeId() {
187         return IdUtil.getValidId(fProjectProvider.getProjectName());
188     }
189
190     private void presetLibraryField(String JavaDoc id){
191         double version = Double.parseDouble(fData.getTargetVersion());
192         if (version >= 3.1) {
193             fLibraryText.setText(""); //$NON-NLS-1$
194
return;
195         }
196         
197         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(id, "."); //$NON-NLS-1$
198
while (tok.hasMoreTokens()) {
199             String JavaDoc token = tok.nextToken();
200             if (!tok.hasMoreTokens())
201                 fLibraryText.setText(token + ".jar"); //$NON-NLS-1$
202
}
203     }
204     
205     protected abstract String JavaDoc getNameFieldQualifier();
206
207     public void updateData() {
208         fData.setId(fIdText.getText().trim());
209         fData.setVersion(fVersionText.getText().trim());
210         fData.setName(fNameText.getText().trim());
211         fData.setProvider(fProviderText.getText().trim());
212         if (!fData.isSimple()) {
213             String JavaDoc library = fLibraryText.getText().trim();
214             if (library.length() > 0) {
215                 if (!library.endsWith(".jar") &&!library.endsWith("/") && !library.equals(".")) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
216
library += "/"; //$NON-NLS-1$
217
fData.setLibraryName(library);
218             } else {
219                 fData.setLibraryName(null);
220             }
221         }
222     }
223
224     public IFieldData getData() {
225         return fData;
226     }
227
228     public String JavaDoc getId() {
229         return fIdText.getText().trim();
230     }
231 }
232
Popular Tags