1 11 package org.eclipse.pde.internal.ui.editor.plugin; 12 import org.eclipse.core.runtime.CoreException; 13 import org.eclipse.jface.dialogs.IMessageProvider; 14 import org.eclipse.pde.core.IBaseModel; 15 import org.eclipse.pde.core.IModelChangeProvider; 16 import org.eclipse.pde.core.IModelChangedEvent; 17 import org.eclipse.pde.core.plugin.IPluginBase; 18 import org.eclipse.pde.core.plugin.IPluginModelBase; 19 import org.eclipse.pde.core.plugin.PluginRegistry; 20 import org.eclipse.pde.internal.core.ibundle.IBundle; 21 import org.eclipse.pde.internal.core.ibundle.IBundleModel; 22 import org.eclipse.pde.internal.ui.PDEPlugin; 23 import org.eclipse.pde.internal.ui.PDEUIMessages; 24 import org.eclipse.pde.internal.ui.editor.FormEntryAdapter; 25 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory; 26 import org.eclipse.pde.internal.ui.editor.PDEFormPage; 27 import org.eclipse.pde.internal.ui.editor.PDESection; 28 import org.eclipse.pde.internal.ui.editor.context.InputContextManager; 29 import org.eclipse.pde.internal.ui.editor.validation.ControlValidationUtility; 30 import org.eclipse.pde.internal.ui.editor.validation.TextValidator; 31 import org.eclipse.pde.internal.ui.parts.FormEntry; 32 import org.eclipse.swt.dnd.Clipboard; 33 import org.eclipse.swt.widgets.Composite; 34 import org.eclipse.swt.widgets.Display; 35 import org.eclipse.swt.widgets.Text; 36 import org.eclipse.ui.IActionBars; 37 import org.eclipse.ui.forms.widgets.FormToolkit; 38 import org.eclipse.ui.forms.widgets.Section; 39 import org.eclipse.ui.forms.widgets.TableWrapData; 40 41 public abstract class GeneralInfoSection extends PDESection { 42 private static String PLATFORM_FILTER = "Eclipse-PlatformFilter"; 44 private FormEntry fIdEntry; 45 private FormEntry fVersionEntry; 46 private FormEntry fNameEntry; 47 private FormEntry fProviderEntry; 48 private FormEntry fPlatformFilterEntry; 49 50 private TextValidator fIdEntryValidator; 51 52 private TextValidator fVersionEntryValidator; 53 54 private TextValidator fNameEntryValidator; 55 56 private TextValidator fProviderEntryValidator; 57 58 private TextValidator fPlatformEntryValidator; 59 60 private IPluginModelBase fModel; 61 62 63 public GeneralInfoSection(PDEFormPage page, Composite parent) { 64 super(page, parent, Section.DESCRIPTION); 65 createClient(getSection(), page.getEditor().getToolkit()); 66 } 67 73 protected void createClient(Section section, FormToolkit toolkit) { 74 section.setText(PDEUIMessages.ManifestEditor_PluginSpecSection_title); 75 section.setLayout(FormLayoutFactory.createClearTableWrapLayout(false, 1)); 76 TableWrapData data = new TableWrapData(TableWrapData.FILL_GRAB); 77 section.setLayoutData(data); 78 79 section.setDescription(getSectionDescription()); 80 Composite client = toolkit.createComposite(section); 81 client.setLayout(FormLayoutFactory.createSectionClientTableWrapLayout(false, 3)); 82 section.setClient(client); 83 84 IActionBars actionBars = getPage().getPDEEditor().getEditorSite().getActionBars(); 85 createIDEntry(client, toolkit, actionBars); 86 createVersionEntry(client, toolkit, actionBars); 87 createNameEntry(client, toolkit, actionBars); 88 createProviderEntry(client, toolkit, actionBars); 89 if (isBundle() && ((ManifestEditor)getPage().getEditor()).isEquinox()) 90 createPlatformFilterEntry(client, toolkit, actionBars); 91 createSpecificControls(client, toolkit, actionBars); 92 toolkit.paintBordersFor(client); 93 94 addListeners(); 95 } 96 97 protected abstract String getSectionDescription(); 98 99 protected abstract void createSpecificControls(Composite parent, FormToolkit toolkit, IActionBars actionBars); 100 101 protected IPluginBase getPluginBase() { 102 IBaseModel model = getPage().getPDEEditor().getAggregateModel(); 103 return ((IPluginModelBase) model).getPluginBase(); 104 } 105 106 113 protected IPluginModelBase getModelBase() { 114 if (fModel == null) { 116 fModel = PluginRegistry.findModel(getProject()); 117 } 118 return fModel; 119 } 120 121 protected boolean isBundle() { 122 return getBundleContext() != null; 123 } 124 125 private BundleInputContext getBundleContext() { 126 InputContextManager manager = getPage().getPDEEditor().getContextManager(); 127 return (BundleInputContext) manager.findContext(BundleInputContext.CONTEXT_ID); 128 } 129 130 protected IBundle getBundle() { 131 BundleInputContext context = getBundleContext(); 132 if (context != null) { 133 IBundleModel model = (IBundleModel)context.getModel(); 134 return model.getBundle(); 135 } 136 return null; 137 } 138 139 private void createIDEntry(Composite client, FormToolkit toolkit, IActionBars actionBars) { 140 fIdEntry = new FormEntry(client, toolkit, PDEUIMessages.GeneralInfoSection_id, null, false); 141 fIdEntry.setFormEntryListener(new FormEntryAdapter(this, actionBars) { 142 public void textValueChanged(FormEntry entry) { 143 try { 144 getPluginBase().setId(entry.getValue()); 145 } catch (CoreException e) { 146 PDEPlugin.logException(e); 147 } 148 } 149 }); 150 fIdEntry.setEditable(isEditable()); 151 fIdEntryValidator = new TextValidator(getManagedForm(), 153 fIdEntry.getText(), getProject(), true) { 154 protected boolean validateControl() { 155 return validateIdEntry(); 156 } 157 }; 158 } 159 160 163 private boolean validateIdEntry() { 164 return ControlValidationUtility.validateRequiredField( 166 fIdEntry.getText().getText(), fIdEntryValidator, IMessageProvider.ERROR); 167 } 168 169 private void createVersionEntry(Composite client, FormToolkit toolkit, IActionBars actionBars) { 170 fVersionEntry = new FormEntry(client, toolkit, PDEUIMessages.GeneralInfoSection_version, null, false); 171 fVersionEntry.setFormEntryListener(new FormEntryAdapter(this,actionBars) { 172 public void textValueChanged(FormEntry entry) { 173 try { 174 getPluginBase().setVersion(entry.getValue()); 175 } catch (CoreException e) { 176 PDEPlugin.logException(e); 177 } 178 } 179 }); 180 fVersionEntry.setEditable(isEditable()); 181 fVersionEntryValidator = new TextValidator(getManagedForm(), 183 fVersionEntry.getText(), getProject(), true) { 184 protected boolean validateControl() { 185 return validateVersionEntry(); 186 } 187 }; 188 } 189 190 193 private boolean validateVersionEntry() { 194 if (ControlValidationUtility.validateRequiredField( 196 fVersionEntry.getText().getText(), 197 fVersionEntryValidator, IMessageProvider.ERROR) == false) { 198 return false; 199 } 200 return ControlValidationUtility.validateVersionField( 202 fVersionEntry.getText().getText(), fVersionEntryValidator); 203 } 204 205 private void createNameEntry(Composite client, FormToolkit toolkit,IActionBars actionBars) { 206 fNameEntry = new FormEntry(client, toolkit, PDEUIMessages.GeneralInfoSection_name, null, false); 207 fNameEntry.setFormEntryListener(new FormEntryAdapter(this, actionBars) { 208 public void textValueChanged(FormEntry entry) { 209 try { 210 getPluginBase().setName(entry.getValue()); 211 } catch (CoreException e) { 212 PDEPlugin.logException(e); 213 } 214 } 215 }); 216 fNameEntry.setEditable(isEditable()); 217 fNameEntryValidator = new TextValidator(getManagedForm(), 219 fNameEntry.getText(), getProject(), true) { 220 protected boolean validateControl() { 221 return validateNameEntry(); 222 } 223 }; 224 } 225 226 229 private boolean validateNameEntry() { 230 if (ControlValidationUtility.validateRequiredField( 232 fNameEntry.getText().getText(), fNameEntryValidator, 233 IMessageProvider.ERROR) == false) { 234 return false; 235 } 236 return ControlValidationUtility.validateTranslatableField( 238 fNameEntry.getText().getText(), fNameEntryValidator, 239 getModelBase(), getProject()); 240 } 241 242 private void createProviderEntry(Composite client, FormToolkit toolkit, IActionBars actionBars) { 243 fProviderEntry = new FormEntry(client, toolkit, PDEUIMessages.GeneralInfoSection_provider, null, false); 244 fProviderEntry.setFormEntryListener(new FormEntryAdapter(this,actionBars) { 245 public void textValueChanged(FormEntry entry) { 246 try { 247 getPluginBase().setProviderName(entry.getValue()); 248 } catch (CoreException e) { 249 PDEPlugin.logException(e); 250 } 251 } 252 }); 253 fProviderEntry.setEditable(isEditable()); 254 fProviderEntryValidator = new TextValidator(getManagedForm(), 256 fProviderEntry.getText(), getProject(), true) { 257 protected boolean validateControl() { 258 return validateProviderEntry(); 259 } 260 }; 261 } 262 263 266 private boolean validateProviderEntry() { 267 if (fProviderEntry.getText().getText().length() == 0) { 269 return true; 270 } 271 return ControlValidationUtility.validateTranslatableField( 273 fProviderEntry.getText().getText(), fProviderEntryValidator, 274 getModelBase(), getProject()); 275 } 276 277 private void createPlatformFilterEntry(Composite client, FormToolkit toolkit, IActionBars actionBars) { 278 fPlatformFilterEntry = new FormEntry(client, toolkit, PDEUIMessages.GeneralInfoSection_platformFilter, null, false); 279 fPlatformFilterEntry.setFormEntryListener(new FormEntryAdapter(this,actionBars) { 280 public void textValueChanged(FormEntry entry) { 281 getBundle().setHeader(PLATFORM_FILTER, fPlatformFilterEntry.getValue()); 282 } 283 }); 284 fPlatformFilterEntry.setEditable(isEditable()); 285 fPlatformEntryValidator = new TextValidator(getManagedForm(), 287 fPlatformFilterEntry.getText(), getProject(), true) { 288 protected boolean validateControl() { 289 return validatePlatformEntry(); 290 } 291 }; 292 } 293 294 297 private boolean validatePlatformEntry() { 298 if (fPlatformFilterEntry.getText().getText().length() == 0) { 300 return true; 301 } 302 return ControlValidationUtility.validatePlatformFilterField( 304 fPlatformFilterEntry.getText().getText(), 305 fPlatformEntryValidator); 306 } 307 308 public void commit(boolean onSave) { 309 fIdEntry.commit(); 310 fVersionEntry.commit(); 311 fNameEntry.commit(); 312 fProviderEntry.commit(); 313 if (fPlatformFilterEntry != null) 314 fPlatformFilterEntry.commit(); 315 super.commit(onSave); 316 } 317 318 323 public void modelChanged(IModelChangedEvent e) { 324 if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 325 markStale(); 326 return; 327 } 328 refresh(); 329 if (e.getChangeType() == IModelChangedEvent.CHANGE) { 330 Object obj = e.getChangedObjects()[0]; 331 if (obj instanceof IPluginBase) { 332 String property = e.getChangedProperty(); 333 if (property != null && property.equals(getPage().getPDEEditor().getTitleProperty())) 334 getPage().getPDEEditor().updateTitle(); 335 } 336 } 337 } 338 339 public void refresh() { 340 IPluginModelBase model = (IPluginModelBase) getPage().getPDEEditor() 341 .getContextManager().getAggregateModel(); 342 IPluginBase pluginBase = model.getPluginBase(); 343 fIdEntry.setValue(pluginBase.getId(), true); 344 fNameEntry.setValue(pluginBase.getName(), true); 345 fVersionEntry.setValue(pluginBase.getVersion(), true); 346 fProviderEntry.setValue(pluginBase.getProviderName(), true); 347 if (fPlatformFilterEntry != null) { 348 IBundle bundle = getBundle(); 349 if (bundle != null) 350 fPlatformFilterEntry.setValue(bundle.getHeader(PLATFORM_FILTER), true); 351 } 352 getPage().getPDEEditor().updateTitle(); 353 super.refresh(); 354 } 355 356 public void cancelEdit() { 357 fIdEntry.cancelEdit(); 358 fNameEntry.cancelEdit(); 359 fVersionEntry.cancelEdit(); 360 fProviderEntry.cancelEdit(); 361 if (fPlatformFilterEntry != null) 362 fPlatformFilterEntry.cancelEdit(); 363 super.cancelEdit(); 364 } 365 366 public void dispose() { 367 removeListeners(); 368 super.dispose(); 369 } 370 371 protected void removeListeners() { 372 IBaseModel model = getPage().getModel(); 373 if (model instanceof IModelChangeProvider) 374 ((IModelChangeProvider) model).removeModelChangedListener(this); 375 } 376 377 protected void addListeners() { 378 IBaseModel model = getPage().getModel(); 379 if (model instanceof IModelChangeProvider) 380 ((IModelChangeProvider) model).addModelChangedListener(this); 381 } 382 383 public boolean canPaste(Clipboard clipboard) { 384 Display d = getSection().getDisplay(); 385 return (d.getFocusControl() instanceof Text); 386 } 387 388 } 389 | Popular Tags |