1 11 12 package org.eclipse.pde.internal.ui.wizards.plugin; 13 14 import java.util.StringTokenizer ; 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 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 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 errorMessage = null; 89 90 errorMessage = validateId(); 92 if (errorMessage != null) { 93 return errorMessage; 94 } 95 96 errorMessage = validateVersion(fVersionText); 98 if (errorMessage != null) { 99 return errorMessage; 100 } 101 102 errorMessage = validateName(); 104 if (errorMessage != null) { 105 return errorMessage; 106 } 107 108 return null; 109 } 110 111 115 protected String 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 129 private String validateName() { 130 if (fNameText.getText().trim().length() == 0) { 131 return PDEUIMessages.ContentPage_noname; 132 } 133 return null; 134 } 135 136 private String validateId() { 137 String 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 version) { 148 return VersionUtil.validateVersion(version).getSeverity() == IStatus.OK; 149 } 150 151 public IWizardPage getNextPage() { 152 updateData(); 153 return super.getNextPage(); 154 } 155 156 161 public void setVisible(boolean visible) { 162 if (visible) { 163 fLibraryLabel.setEnabled(!fData.isSimple()); 165 fLibraryText.setEnabled(!fData.isSimple()); 166 167 String id = computeId(); 168 if ((fChangedGroups & PROPERTIES_GROUP) == 0) { 170 int oldfChanged = fChangedGroups; 171 fIdText.setText(id); 172 fVersionText.setText("1.0.0"); 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 computeId() { 187 return IdUtil.getValidId(fProjectProvider.getProjectName()); 188 } 189 190 private void presetLibraryField(String id){ 191 double version = Double.parseDouble(fData.getTargetVersion()); 192 if (version >= 3.1) { 193 fLibraryText.setText(""); return; 195 } 196 197 StringTokenizer tok = new StringTokenizer (id, "."); while (tok.hasMoreTokens()) { 199 String token = tok.nextToken(); 200 if (!tok.hasMoreTokens()) 201 fLibraryText.setText(token + ".jar"); } 203 } 204 205 protected abstract String 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 library = fLibraryText.getText().trim(); 214 if (library.length() > 0) { 215 if (!library.endsWith(".jar") &&!library.endsWith("/") && !library.equals(".")) library += "/"; 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 getId() { 229 return fIdText.getText().trim(); 230 } 231 } 232 | Popular Tags |