1 11 package org.eclipse.pde.internal.ui.editor.plugin; 12 import org.eclipse.core.resources.IContainer; 13 import org.eclipse.core.resources.IFile; 14 import org.eclipse.core.resources.IProject; 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IStatus; 18 import org.eclipse.core.runtime.Path; 19 import org.eclipse.core.runtime.Status; 20 import org.eclipse.jface.viewers.ISelection; 21 import org.eclipse.jface.viewers.IStructuredSelection; 22 import org.eclipse.jface.viewers.Viewer; 23 import org.eclipse.jface.viewers.ViewerFilter; 24 import org.eclipse.jface.window.Window; 25 import org.eclipse.jface.wizard.WizardDialog; 26 import org.eclipse.pde.core.IBaseModel; 27 import org.eclipse.pde.core.IModelChangedEvent; 28 import org.eclipse.pde.core.plugin.IPluginBase; 29 import org.eclipse.pde.core.plugin.IPluginExtensionPoint; 30 import org.eclipse.pde.core.plugin.IPluginModelBase; 31 import org.eclipse.pde.internal.ui.IPDEUIConstants; 32 import org.eclipse.pde.internal.ui.PDEPlugin; 33 import org.eclipse.pde.internal.ui.PDEPluginImages; 34 import org.eclipse.pde.internal.ui.PDEUIMessages; 35 import org.eclipse.pde.internal.ui.editor.FormEntryAdapter; 36 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory; 37 import org.eclipse.pde.internal.ui.editor.PDEDetails; 38 import org.eclipse.pde.internal.ui.editor.PDEFormPage; 39 import org.eclipse.pde.internal.ui.editor.actions.OpenSchemaAction; 40 import org.eclipse.pde.internal.ui.parts.FormEntry; 41 import org.eclipse.pde.internal.ui.search.FindReferencesAction; 42 import org.eclipse.pde.internal.ui.search.ShowDescriptionAction; 43 import org.eclipse.pde.internal.ui.util.SWTUtil; 44 import org.eclipse.pde.internal.ui.wizards.extension.NewSchemaFileWizard; 45 import org.eclipse.swt.SWT; 46 import org.eclipse.swt.custom.BusyIndicator; 47 import org.eclipse.swt.layout.GridData; 48 import org.eclipse.swt.widgets.Composite; 49 import org.eclipse.swt.widgets.Display; 50 import org.eclipse.ui.IWorkbenchWindow; 51 import org.eclipse.ui.PartInitException; 52 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; 53 import org.eclipse.ui.dialogs.ISelectionStatusValidator; 54 import org.eclipse.ui.forms.IFormPart; 55 import org.eclipse.ui.forms.events.HyperlinkAdapter; 56 import org.eclipse.ui.forms.events.HyperlinkEvent; 57 import org.eclipse.ui.forms.widgets.ExpandableComposite; 58 import org.eclipse.ui.forms.widgets.FormText; 59 import org.eclipse.ui.forms.widgets.FormToolkit; 60 import org.eclipse.ui.forms.widgets.Section; 61 import org.eclipse.ui.model.WorkbenchContentProvider; 62 import org.eclipse.ui.model.WorkbenchLabelProvider; 63 import org.eclipse.ui.part.FileEditorInput; 64 import org.eclipse.ui.views.navigator.ResourceComparator; 65 66 public class ExtensionPointDetails extends PDEDetails { 67 private IPluginExtensionPoint fInput; 68 private FormEntry fIdEntry; 69 private FormEntry fNameEntry; 70 private FormEntry fSchemaEntry; 71 private FormText fRichText; 72 private String fRichTextData; 73 74 private static final String SCHEMA_RTEXT_DATA = PDEUIMessages.ExtensionPointDetails_schemaLinks; 75 private static final String NO_SCHEMA_RTEXT_DATA = PDEUIMessages.ExtensionPointDetails_noSchemaLinks; 76 public ExtensionPointDetails() { 77 } 78 public String getContextId() { 79 return PluginInputContext.CONTEXT_ID; 80 } 81 public void fireSaveNeeded() { 82 markDirty(); 83 getPage().getPDEEditor().fireSaveNeeded(getContextId(), false); 84 } 85 public PDEFormPage getPage() { 86 return (PDEFormPage)getManagedForm().getContainer(); 87 } 88 public boolean isEditable() { 89 return getPage().getPDEEditor().getAggregateModel().isEditable(); 90 } 91 96 public void createContents(Composite parent) { 97 parent.setLayout(FormLayoutFactory.createDetailsGridLayout(false, 1)); 98 FormToolkit toolkit = getManagedForm().getToolkit(); 99 Section section = toolkit.createSection(parent, Section.DESCRIPTION|ExpandableComposite.TITLE_BAR); 100 section.clientVerticalSpacing = FormLayoutFactory.SECTION_HEADER_VERTICAL_SPACING; 101 section.setText(PDEUIMessages.ExtensionPointDetails_title); 102 section.setDescription(PDEUIMessages.ExtensionPointDetails_desc); 103 section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1)); 104 section.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING)); 105 106 Composite client = toolkit.createComposite(section); 107 client.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 3)); 108 client.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 109 110 fIdEntry = new FormEntry(client, toolkit, PDEUIMessages.ExtensionPointDetails_id, null, false); 111 fIdEntry.setFormEntryListener(new FormEntryAdapter(this) { 112 public void textValueChanged(FormEntry entry) { 113 if (fInput != null) { 114 try { 115 fInput.setId(fIdEntry.getValue()); 116 } catch (CoreException e) { 117 PDEPlugin.logException(e); 118 } 119 } 120 } 121 }); 122 fNameEntry = new FormEntry(client, toolkit, PDEUIMessages.ExtensionPointDetails_name, null, false); 123 fNameEntry.setFormEntryListener(new FormEntryAdapter(this) { 124 public void textValueChanged(FormEntry entry) { 125 if (fInput != null) 126 try { 127 fInput.setName(fNameEntry.getValue()); 128 } catch (CoreException e) { 129 PDEPlugin.logException(e); 130 } 131 } 132 }); 133 boolean editable = getPage().getModel().isEditable(); 134 fSchemaEntry = new FormEntry(client, toolkit, PDEUIMessages.ExtensionPointDetails_schema, PDEUIMessages.ExtensionPointDetails_browse, editable); fSchemaEntry.setFormEntryListener(new FormEntryAdapter(this) { 136 public void textValueChanged(FormEntry entry) { 137 if (fInput != null) { 138 try { 139 fInput.setSchema(fSchemaEntry.getValue()); 140 } catch (CoreException e) { 141 PDEPlugin.logException(e); 142 } 143 updateRichText(); 144 } 145 } 146 147 public void linkActivated(HyperlinkEvent e) { 148 IProject project = getPage().getPDEEditor().getCommonProject(); 149 if (fSchemaEntry.getValue() == null || fSchemaEntry.getValue().length() ==0){ 150 generateSchema(); 151 return; 152 } 153 IFile file = project.getFile(fSchemaEntry.getValue()); 154 if (file.exists()) 155 openSchemaFile(file); 156 else 157 generateSchema(); 158 } 159 160 public void browseButtonSelected(FormEntry entry) { 161 final IProject project = getPage().getPDEEditor().getCommonProject(); 162 ElementTreeSelectionDialog dialog = 163 new ElementTreeSelectionDialog( 164 PDEPlugin.getActiveWorkbenchShell(), 165 new WorkbenchLabelProvider(), 166 new WorkbenchContentProvider()); 167 dialog.setTitle(PDEUIMessages.ManifestEditor_ExtensionPointDetails_schemaLocation_title); 168 dialog.setMessage(PDEUIMessages.ManifestEditor_ExtensionPointDetails_schemaLocation_desc); 169 dialog.setDoubleClickSelects(false); 170 dialog.setAllowMultiple(false); 171 dialog.addFilter(new ViewerFilter(){ 172 public boolean select(Viewer viewer, Object parent, 173 Object element) { 174 if (element instanceof IFile){ 175 String ext = ((IFile)element).getFullPath().getFileExtension(); 176 return "exsd".equals(ext) || "mxsd".equals(ext); } else if (element instanceof IContainer){ try { 179 IResource[] resources = ((IContainer)element).members(); 180 for (int i = 0; i < resources.length; i++){ 181 if (select(viewer, parent, resources[i])) 182 return true; 183 } 184 } catch (CoreException e) { 185 PDEPlugin.logException(e); 186 } 187 } 188 return false; 189 } 190 }); 191 dialog.setValidator(new ISelectionStatusValidator() { 192 public IStatus validate(Object [] selection) { 193 IPluginModelBase model = (IPluginModelBase) getPage() 194 .getPDEEditor().getAggregateModel(); 195 String pluginName = model.getPluginBase().getId(); 196 197 if (selection == null || selection.length != 1 198 || !(selection[0] instanceof IFile)) 199 return new Status( 200 IStatus.ERROR, 201 pluginName, 202 IStatus.ERROR, 203 PDEUIMessages.ManifestEditor_ExtensionPointDetails_validate_errorStatus, 204 null); 205 IFile file = (IFile) selection[0]; 206 String ext = file.getFullPath().getFileExtension(); 207 if ("exsd".equals(ext) || "mxsd".equals(ext)) return new Status(IStatus.OK, pluginName, 209 IStatus.OK, "", null); return new Status( 211 IStatus.ERROR, 212 pluginName, 213 IStatus.ERROR, 214 PDEUIMessages.ManifestEditor_ExtensionPointDetails_validate_errorStatus, 215 null); 216 } 217 }); 218 dialog.setDoubleClickSelects(true); 219 dialog.setStatusLineAboveButtons(true); 220 dialog.setInput(project); 221 dialog.setComparator(new ResourceComparator(ResourceComparator.NAME)); 222 String filePath = fSchemaEntry.getValue(); 223 if (filePath!=null && filePath.length()!=0 && project.exists(new Path(filePath))) 224 dialog.setInitialSelection(project.getFile(new Path(filePath))); 225 else 226 dialog.setInitialSelection(null); 227 if (dialog.open() == Window.OK) { 228 Object [] elements = dialog.getResult(); 229 if (elements.length >0){ 230 IResource elem = (IResource) elements[0]; 231 fSchemaEntry.setValue(elem.getProjectRelativePath().toString()); 232 } 233 } 234 } 235 }); 236 createSpacer(toolkit, client, 2); 237 238 Composite container = toolkit.createComposite(parent, SWT.NONE); 239 container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 1)); 240 container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING)); 241 242 fRichText = toolkit.createFormText(container, true); 243 fRichText.setImage("open", PDEPlugin.getDefault().getLabelProvider().get( PDEPluginImages.DESC_SCHEMA_OBJ)); 245 fRichText.setImage("desc", PDEPlugin.getDefault().getLabelProvider().get( PDEPluginImages.DESC_DOC_SECTION_OBJ)); 247 fRichText.setImage("search", PDEPlugin.getDefault().getLabelProvider().get( PDEPluginImages.DESC_PSEARCH_OBJ)); 249 fRichText.addHyperlinkListener(new HyperlinkAdapter() { 250 public void linkActivated(HyperlinkEvent e) { 251 IBaseModel model = getPage().getPDEEditor().getAggregateModel(); 252 String pointID = null; 253 IPluginBase base = ((IPluginModelBase)model).getPluginBase(); 254 String pluginID = base.getId(); 255 if (Double.parseDouble(base.getSchemaVersion()) >= 3.2) { 256 if (fInput.getId().indexOf('.') != -1) 257 pointID = fInput.getId(); 258 } 259 if (pointID == null) 260 pointID = pluginID + "." + fInput.getId(); if (e.getHref().equals("search")) { new FindReferencesAction(fInput, pluginID).run(); 263 } else if (e.getHref().equals("open")) { OpenSchemaAction action = new OpenSchemaAction(); 265 action.setInput(pointID); 266 action.setEnabled(true); 267 action.run(); 268 } else { 269 new ShowDescriptionAction(pointID).run(); 270 } 271 } 272 }); 273 274 fIdEntry.setEditable(isEditable()); 275 fNameEntry.setEditable(isEditable()); 276 fSchemaEntry.setEditable(isEditable()); 277 toolkit.paintBordersFor(client); 278 section.setClient(client); 279 IPluginModelBase model = (IPluginModelBase)getPage().getModel(); 280 model.addModelChangedListener(this); 281 markDetailsPart(section); 282 } 283 284 public void dispose() { 285 IPluginModelBase model = (IPluginModelBase)getPage().getModel(); 286 if (model!=null) 287 model.removeModelChangedListener(this); 288 super.dispose(); 289 } 290 public void modelChanged(IModelChangedEvent e) { 291 if (e.getChangeType()==IModelChangedEvent.CHANGE) { 292 Object obj = e.getChangedObjects()[0]; 293 if (obj.equals(fInput)) 294 refresh(); 295 } 296 } 297 private void update() { 298 fIdEntry.setValue( 299 fInput != null && fInput.getId() != null ? fInput.getId() : "", true); 301 fNameEntry.setValue(fInput != null && fInput.getName() != null ? fInput 302 .getName() : "", true); fSchemaEntry.setValue(fInput != null && fInput.getSchema() != null ? fInput 304 .getSchema() : "", true); updateRichText(); 306 } 307 public void cancelEdit() { 308 fIdEntry.cancelEdit(); 309 fNameEntry.cancelEdit(); 310 fSchemaEntry.cancelEdit(); 311 updateRichText(); 312 super.cancelEdit(); 313 } 314 private void updateRichText() { 315 boolean hasSchema = fSchemaEntry.getValue().length() > 0; 316 if (hasSchema && fRichTextData == SCHEMA_RTEXT_DATA) 317 return; 318 if (!hasSchema && fRichTextData == NO_SCHEMA_RTEXT_DATA) 319 return; 320 fRichTextData = hasSchema ? SCHEMA_RTEXT_DATA : NO_SCHEMA_RTEXT_DATA; 321 fRichText.setText(fRichTextData, true, false); 322 getManagedForm().getForm().reflow(true); 323 } 324 private void openSchemaFile(final IFile file) { 325 final IWorkbenchWindow ww = PDEPlugin.getActiveWorkbenchWindow(); 326 327 Display d = ww.getShell().getDisplay(); 328 d.asyncExec(new Runnable () { 329 public void run() { 330 try { 331 String editorId = IPDEUIConstants.SCHEMA_EDITOR_ID; 332 ww.getActivePage().openEditor( 333 new FileEditorInput(file), 334 editorId); 335 } catch (PartInitException e) { 336 PDEPlugin.logException(e); 337 } 338 } 339 }); 340 } 341 342 private void generateSchema() { 343 final IProject project = getPage().getPDEEditor().getCommonProject(); 344 BusyIndicator 345 .showWhile(getPage().getPartControl().getDisplay(), new Runnable () { 346 public void run() { 347 NewSchemaFileWizard wizard = 348 new NewSchemaFileWizard(project, fInput, true); 349 WizardDialog dialog = 350 new WizardDialog( 351 PDEPlugin.getActiveWorkbenchShell(), 352 wizard); 353 dialog.create(); 354 SWTUtil.setDialogSize(dialog, 400, 450); 355 if(dialog.open() == Window.OK) 356 update(); 357 } 358 }); 359 } 360 365 public void selectionChanged(IFormPart masterPart, ISelection selection) { 366 IStructuredSelection ssel = (IStructuredSelection) selection; 367 if (ssel.size() == 1) { 368 fInput = (IPluginExtensionPoint) ssel.getFirstElement(); 369 } else 370 fInput = null; 371 update(); 372 } 373 378 public void commit(boolean onSave) { 379 fIdEntry.commit(); 380 fNameEntry.commit(); 381 fSchemaEntry.commit(); 382 super.commit(onSave); 383 } 384 389 public void setFocus() { 390 fIdEntry.getText().setFocus(); 391 } 392 397 public void refresh() { 398 update(); 399 super.refresh(); 400 } 401 402 } 403 | Popular Tags |