001: /*******************************************************************************
002: * Copyright (c) 2000, 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.wizards.plugin;
011:
012: import java.util.StringTokenizer;
013:
014: import org.eclipse.core.runtime.IStatus;
015: import org.eclipse.jface.wizard.IWizardPage;
016: import org.eclipse.jface.wizard.WizardPage;
017: import org.eclipse.pde.internal.core.PDECoreMessages;
018: import org.eclipse.pde.internal.core.util.IdUtil;
019: import org.eclipse.pde.internal.core.util.VersionUtil;
020: import org.eclipse.pde.internal.ui.PDEUIMessages;
021: import org.eclipse.pde.internal.ui.util.PDELabelUtility;
022: import org.eclipse.pde.internal.ui.wizards.IProjectProvider;
023: import org.eclipse.pde.ui.IFieldData;
024: import org.eclipse.swt.SWT;
025: import org.eclipse.swt.events.ModifyEvent;
026: import org.eclipse.swt.events.ModifyListener;
027: import org.eclipse.swt.layout.GridData;
028: import org.eclipse.swt.widgets.Composite;
029: import org.eclipse.swt.widgets.Label;
030: import org.eclipse.swt.widgets.Text;
031:
032: public abstract class ContentPage extends WizardPage {
033:
034: protected boolean fInitialized = false;
035: protected Text fIdText;
036: protected Text fVersionText;
037: protected Text fNameText;
038: protected Text fProviderText;
039: protected Label fLibraryLabel;
040: protected Text fLibraryText;
041:
042: protected NewProjectCreationPage fMainPage;
043: protected AbstractFieldData fData;
044: protected IProjectProvider fProjectProvider;
045:
046: protected final static int PROPERTIES_GROUP = 1;
047:
048: protected int fChangedGroups = 0;
049:
050: protected ModifyListener propertiesListener = new ModifyListener() {
051: public void modifyText(ModifyEvent e) {
052: if (fInitialized)
053: fChangedGroups |= PROPERTIES_GROUP;
054: validatePage();
055: }
056: };
057:
058: public ContentPage(String pageName, IProjectProvider provider,
059: NewProjectCreationPage page, AbstractFieldData data) {
060: super (pageName);
061: fMainPage = page;
062: fProjectProvider = provider;
063: fData = data;
064: }
065:
066: protected Text createText(Composite parent, ModifyListener listener) {
067: Text text = new Text(parent, SWT.BORDER | SWT.SINGLE);
068: text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
069: text.addModifyListener(listener);
070: return text;
071: }
072:
073: protected abstract void validatePage();
074:
075: protected String validateProperties() {
076:
077: if (!fInitialized) {
078: if (!fIdText.getText().trim().equals(
079: fProjectProvider.getProjectName())) {
080: setMessage(
081: PDEUIMessages.ContentPage_illegalCharactersInID,
082: INFORMATION);
083: } else {
084: setMessage(null);
085: }
086: return null;
087: }
088:
089: setMessage(null);
090: String errorMessage = null;
091:
092: // Validate ID
093: errorMessage = validateId();
094: if (errorMessage != null) {
095: return errorMessage;
096: }
097:
098: // Validate Version
099: errorMessage = validateVersion(fVersionText);
100: if (errorMessage != null) {
101: return errorMessage;
102: }
103:
104: // Validate Name
105: errorMessage = validateName();
106: if (errorMessage != null) {
107: return errorMessage;
108: }
109:
110: return null;
111: }
112:
113: /**
114: * @param text
115: * @return
116: */
117: protected String validateVersion(Text text) {
118: if (text.getText().trim().length() == 0) {
119: return PDELabelUtility
120: .qualifyMessage(
121: PDELabelUtility.getFieldLabel(text),
122: PDEUIMessages.ControlValidationUtility_errorMsgValueMustBeSpecified);
123: } else if (!isVersionValid(text.getText().trim())) {
124: return PDELabelUtility
125: .qualifyMessage(
126: PDELabelUtility.getFieldLabel(text),
127: PDECoreMessages.BundleErrorReporter_InvalidFormatInBundleVersion);
128: }
129: return null;
130: }
131:
132: /**
133: * @return
134: */
135: private String validateName() {
136: if (fNameText.getText().trim().length() == 0) {
137: return PDEUIMessages.ContentPage_noname;
138: }
139: return null;
140: }
141:
142: private String validateId() {
143: String id = fIdText.getText().trim();
144: if (id.length() == 0)
145: return PDEUIMessages.ContentPage_noid;
146:
147: if (!IdUtil.isValidCompositeID(id)) {
148: return PDEUIMessages.ContentPage_invalidId;
149: }
150: return null;
151: }
152:
153: protected boolean isVersionValid(String version) {
154: return VersionUtil.validateVersion(version).getSeverity() == IStatus.OK;
155: }
156:
157: public IWizardPage getNextPage() {
158: updateData();
159: return super .getNextPage();
160: }
161:
162: /*
163: * (non-Javadoc)
164: *
165: * @see org.eclipse.jface.dialogs.IDialogPage#setVisible(boolean)
166: */
167: public void setVisible(boolean visible) {
168: if (visible) {
169: // update the library field/label enabled state
170: fLibraryLabel.setEnabled(!fData.isSimple());
171: fLibraryText.setEnabled(!fData.isSimple());
172:
173: String id = computeId();
174: // properties group
175: if ((fChangedGroups & PROPERTIES_GROUP) == 0) {
176: int oldfChanged = fChangedGroups;
177: fIdText.setText(id);
178: fVersionText.setText("1.0.0"); //$NON-NLS-1$
179: fNameText.setText(IdUtil.getValidName(id,
180: getNameFieldQualifier()));
181: fProviderText.setText(IdUtil.getValidProvider(id));
182: presetLibraryField(id);
183: fChangedGroups = oldfChanged;
184: }
185: if (fInitialized)
186: validatePage();
187: else
188: fInitialized = true;
189: }
190: super .setVisible(visible);
191: }
192:
193: protected String computeId() {
194: return IdUtil.getValidId(fProjectProvider.getProjectName());
195: }
196:
197: private void presetLibraryField(String id) {
198: double version = Double.parseDouble(fData.getTargetVersion());
199: if (version >= 3.1) {
200: fLibraryText.setText(""); //$NON-NLS-1$
201: return;
202: }
203:
204: StringTokenizer tok = new StringTokenizer(id, "."); //$NON-NLS-1$
205: while (tok.hasMoreTokens()) {
206: String token = tok.nextToken();
207: if (!tok.hasMoreTokens())
208: fLibraryText.setText(token + ".jar"); //$NON-NLS-1$
209: }
210: }
211:
212: protected abstract String getNameFieldQualifier();
213:
214: public void updateData() {
215: fData.setId(fIdText.getText().trim());
216: fData.setVersion(fVersionText.getText().trim());
217: fData.setName(fNameText.getText().trim());
218: fData.setProvider(fProviderText.getText().trim());
219: if (!fData.isSimple()) {
220: String library = fLibraryText.getText().trim();
221: if (library.length() > 0) {
222: if (!library.endsWith(".jar") && !library.endsWith("/") && !library.equals(".")) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
223: library += "/"; //$NON-NLS-1$
224: fData.setLibraryName(library);
225: } else {
226: fData.setLibraryName(null);
227: }
228: }
229: }
230:
231: public IFieldData getData() {
232: return fData;
233: }
234:
235: public String getId() {
236: return fIdText.getText().trim();
237: }
238: }
|