1 11 package org.eclipse.pde.internal.ui.wizards.product; 12 13 import java.util.Set ; 14 15 import org.eclipse.core.resources.IProject; 16 import org.eclipse.core.runtime.Platform; 17 import org.eclipse.jface.dialogs.Dialog; 18 import org.eclipse.jface.window.Window; 19 import org.eclipse.jface.wizard.WizardPage; 20 import org.eclipse.pde.core.plugin.IPluginExtensionPoint; 21 import org.eclipse.pde.core.plugin.IPluginModelBase; 22 import org.eclipse.pde.core.plugin.PluginRegistry; 23 import org.eclipse.pde.core.plugin.TargetPlatform; 24 import org.eclipse.pde.internal.core.PDEStateHelper; 25 import org.eclipse.pde.internal.core.TargetPlatformHelper; 26 import org.eclipse.pde.internal.core.iproduct.IProduct; 27 import org.eclipse.pde.internal.ui.IHelpContextIds; 28 import org.eclipse.pde.internal.ui.PDEUIMessages; 29 import org.eclipse.pde.internal.ui.search.ShowDescriptionAction; 30 import org.eclipse.pde.internal.ui.util.SWTUtil; 31 import org.eclipse.pde.internal.ui.wizards.PluginSelectionDialog; 32 import org.eclipse.swt.SWT; 33 import org.eclipse.swt.events.ModifyEvent; 34 import org.eclipse.swt.events.ModifyListener; 35 import org.eclipse.swt.events.SelectionAdapter; 36 import org.eclipse.swt.events.SelectionEvent; 37 import org.eclipse.swt.layout.GridData; 38 import org.eclipse.swt.layout.GridLayout; 39 import org.eclipse.swt.widgets.Button; 40 import org.eclipse.swt.widgets.Combo; 41 import org.eclipse.swt.widgets.Composite; 42 import org.eclipse.swt.widgets.Group; 43 import org.eclipse.swt.widgets.Label; 44 import org.eclipse.swt.widgets.Text; 45 import org.eclipse.ui.PlatformUI; 46 import org.eclipse.ui.forms.events.HyperlinkEvent; 47 import org.eclipse.ui.forms.events.IHyperlinkListener; 48 import org.eclipse.ui.forms.widgets.FormText; 49 import org.eclipse.ui.forms.widgets.FormToolkit; 50 51 public class ProductDefinitonWizardPage extends WizardPage implements IHyperlinkListener { 52 53 private Text fProductName; 54 private Text fPluginText; 55 private Text fProductText; 56 private Set fProductSet; 57 private Combo fApplicationCombo; 58 private IProduct fProduct; 59 60 private ModifyListener fListener = new ModifyListener() { 61 public void modifyText(ModifyEvent e) { 62 validatePage(); 63 } 64 }; 65 66 public ProductDefinitonWizardPage(String pageName, IProduct product) { 67 super(pageName); 68 fProduct = product; 69 setTitle(PDEUIMessages.ProductDefinitonWizardPage_title); 70 if (productNameDefined()) 71 setDescription(PDEUIMessages.ProductDefinitonWizardPage_desc); 72 else 73 setDescription(PDEUIMessages.ProductDefinitonWizardPage_descNoName); 74 } 75 76 public void createControl(Composite parent) { 77 Composite comp = new Composite(parent, SWT.NONE); 78 GridLayout layout = new GridLayout(); 79 layout.verticalSpacing = 20; 80 comp.setLayout(layout); 81 82 FormToolkit toolkit = new FormToolkit(parent.getDisplay()); 83 createProductGroup(toolkit, comp); 84 createApplicationGroup(toolkit, comp); 85 toolkit.dispose(); 86 setControl(comp); 87 setPageComplete(getPluginId() != null && productNameDefined()); 88 Dialog.applyDialogFont(comp); 89 PlatformUI.getWorkbench().getHelpSystem().setHelp(comp, IHelpContextIds.PRODUCT_DEFINITIONS_WIZARD); 90 } 91 92 private void createFormText(FormToolkit toolkit, Composite parent, String content, int span) { 93 FormText text = toolkit.createFormText(parent, false); 94 text.setText(content, true, false); 95 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 96 gd.horizontalSpan = span; 97 gd.widthHint = 400; 98 text.setLayoutData(gd); 99 text.setBackground(null); 100 text.addHyperlinkListener(this); 101 } 102 103 private void createProductGroup(FormToolkit toolkit, Composite comp) { 104 Group group = new Group(comp, SWT.NONE); 105 group.setText(PDEUIMessages.ProductDefinitonWizardPage_productGroup); 106 group.setLayout(new GridLayout(3, false)); 107 group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 108 109 createFormText(toolkit, group, PDEUIMessages.ProductDefinitonWizardPage_productDefinition, 3); 110 111 Label label; 112 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 113 gd.horizontalSpan = 2; 114 115 if (!productNameDefined()) { label = new Label(group, SWT.NONE); 117 label.setText(PDEUIMessages.ProductDefinitonWizardPage_productName); 118 119 fProductName = new Text(group, SWT.SINGLE|SWT.BORDER); 120 fProductName.setLayoutData(gd); 121 fProductName.addModifyListener(fListener); 122 } 123 124 label = new Label(group, SWT.NONE); 125 label.setText(PDEUIMessages.ProductDefinitonWizardPage_plugin); 126 127 fPluginText = new Text(group, SWT.SINGLE|SWT.BORDER); 128 fPluginText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 129 130 Button button = new Button(group, SWT.PUSH); 131 button.setText(PDEUIMessages.ProductDefinitonWizardPage_browse); 132 SWTUtil.setButtonDimensionHint(button); 133 button.addSelectionListener(new SelectionAdapter() { 134 public void widgetSelected(SelectionEvent e) { 135 handleBrowse(); 136 } 137 }); 138 139 label = new Label(group, SWT.NONE); 140 label.setText(PDEUIMessages.ProductDefinitonWizardPage_productId); 141 142 fProductText = new Text(group, SWT.SINGLE|SWT.BORDER); 143 fProductText.setLayoutData(gd); 144 145 String pluginId = getPluginId(); 146 if (pluginId != null) { 147 fPluginText.setText(pluginId); 148 String productId = "product"; String numString = ""; int idNum = 1; 151 while (getProductNameSet().contains(pluginId + "." + productId + numString)) { numString = Integer.toString(idNum++); 153 } 154 fProductText.setText(productId + numString); 155 } 156 fPluginText.addModifyListener(fListener); 157 fProductText.addModifyListener(fListener); 158 159 } 160 161 private void createApplicationGroup(FormToolkit toolkit, Composite comp) { 162 Group group = new Group(comp, SWT.NONE); 163 group.setText(PDEUIMessages.ProductDefinitonWizardPage_applicationGroup); 164 group.setLayout(new GridLayout(2, false)); 165 group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 166 167 createFormText(toolkit, group, PDEUIMessages.ProductDefinitonWizardPage_applicationDefinition, 2); 168 169 Label label = new Label(group, SWT.NONE); 170 label.setText(PDEUIMessages.ProductDefinitonWizardPage_application); 171 172 fApplicationCombo = new Combo(group, SWT.SINGLE|SWT.READ_ONLY); 173 fApplicationCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 174 fApplicationCombo.setItems(TargetPlatform.getApplications()); 175 if (fApplicationCombo.getItemCount() > 0) 176 fApplicationCombo.setText(fApplicationCombo.getItem(0)); 177 } 178 179 public void setVisible(boolean visible) { 180 if (visible) { 181 if (fProductName != null) 182 fProductName.setFocus(); 183 else 184 fPluginText.setFocus(); 185 } 186 super.setVisible(visible); 187 } 188 189 private void validatePage() { 190 String error = null; 191 String productName = getProductName(); 192 if (productName != null && productName.length() == 0) { 193 error = PDEUIMessages.ProductDefinitonWizardPage_noProductName; 194 } 195 validateIdAndProduct(error); 196 } 197 198 private void validateIdAndProduct(String error) { 199 if (error == null) { 200 String pluginId = getDefiningPlugin(); 201 IPluginModelBase model = PluginRegistry.findModel(pluginId); 202 if (pluginId.length() == 0) { 203 error = PDEUIMessages.ProductDefinitonWizardPage_noPluginId; 204 } else if (model == null){ 205 error = PDEUIMessages.ProductDefinitonWizardPage_noPlugin; 206 } else if (model.getUnderlyingResource() == null) { 207 error = PDEUIMessages.ProductDefinitonWizardPage_notInWorkspace; 208 } 209 if (error == null) 210 error = validateId(); 211 if (error == null && getProductNameSet().contains(pluginId + "." + fProductText.getText().trim())) { error = PDEUIMessages.ProductDefinitonWizardPage_productExists; 213 } 214 } 215 setErrorMessage(error); 216 setPageComplete(error == null); 217 } 218 219 private String validateId() { 220 String id = fProductText.getText().trim(); 221 if (id.length() == 0) 222 return PDEUIMessages.ProductDefinitonWizardPage_noProductID; 223 224 for (int i = 0; i<id.length(); i++){ 225 if (!id.substring(i,i+1).matches("[a-zA-Z0-9_]")) return PDEUIMessages.ProductDefinitonWizardPage_invalidId; 227 } 228 return null; 229 } 230 231 public void linkEntered(HyperlinkEvent e) { 232 } 233 234 public void linkExited(HyperlinkEvent e) { 235 } 236 237 public void linkActivated(HyperlinkEvent e) { 238 String extPoint = Platform.PI_RUNTIME + "." + e.getHref().toString(); IPluginExtensionPoint point = PDEStateHelper.findExtensionPoint(extPoint); 240 if (point != null) 241 new ShowDescriptionAction(point, true).run(); 242 } 243 244 private void handleBrowse() { 245 PluginSelectionDialog dialog = new PluginSelectionDialog(getShell(), PluginRegistry.getWorkspaceModels(), false); 246 if (dialog.open() == Window.OK) { 247 IPluginModelBase model = (IPluginModelBase)dialog.getFirstResult(); 248 fPluginText.setText(model.getPluginBase().getId()); 249 } 250 } 251 252 private Set getProductNameSet() { 253 if (fProductSet == null) 254 fProductSet = TargetPlatformHelper.getProductNameSet(); 255 return fProductSet; 256 } 257 258 public String getDefiningPlugin() { 259 return fPluginText.getText().trim(); 260 } 261 262 public String getProductId() { 263 return fProductText.getText().trim(); 264 } 265 266 public String getApplication() { 267 return fApplicationCombo.getText(); 268 } 269 270 public String getProductName() { 271 return (fProductName == null) ? null : fProductName.getText().trim(); 272 } 273 274 private boolean productNameDefined() { 275 return (fProduct.getName() != null && !fProduct.getName().equals("")); } 277 278 private String getPluginId() { 279 IProject project = fProduct.getModel().getUnderlyingResource().getProject(); 280 IPluginModelBase model = PluginRegistry.findModel(project); 281 return (model == null) ? null : model.getPluginBase().getId(); 282 } 283 } 284 | Popular Tags |