KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > extension > BaseExtensionPointMainPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.wizards.extension;
12 import java.io.ByteArrayInputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.PrintWriter JavaDoc;
16 import java.io.StringWriter JavaDoc;
17 import java.io.UnsupportedEncodingException JavaDoc;
18
19 import org.eclipse.core.resources.IContainer;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IFolder;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.IWorkspace;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.Path;
28 import org.eclipse.jface.dialogs.Dialog;
29 import org.eclipse.jface.operation.IRunnableWithProgress;
30 import org.eclipse.jface.viewers.Viewer;
31 import org.eclipse.jface.viewers.ViewerFilter;
32 import org.eclipse.jface.window.Window;
33 import org.eclipse.jface.wizard.WizardPage;
34 import org.eclipse.osgi.util.NLS;
35 import org.eclipse.pde.core.plugin.IPluginModelBase;
36 import org.eclipse.pde.core.plugin.PluginRegistry;
37 import org.eclipse.pde.internal.core.PDECore;
38 import org.eclipse.pde.internal.core.ischema.IDocumentSection;
39 import org.eclipse.pde.internal.core.ischema.ISchemaAttribute;
40 import org.eclipse.pde.internal.core.schema.DocumentSection;
41 import org.eclipse.pde.internal.core.schema.EditableSchema;
42 import org.eclipse.pde.internal.core.schema.SchemaAttribute;
43 import org.eclipse.pde.internal.core.schema.SchemaComplexType;
44 import org.eclipse.pde.internal.core.schema.SchemaElement;
45 import org.eclipse.pde.internal.core.schema.SchemaRootElement;
46 import org.eclipse.pde.internal.core.schema.SchemaSimpleType;
47 import org.eclipse.pde.internal.core.util.CoreUtility;
48 import org.eclipse.pde.internal.core.util.IdUtil;
49 import org.eclipse.pde.internal.ui.IHelpContextIds;
50 import org.eclipse.pde.internal.ui.IPDEUIConstants;
51 import org.eclipse.pde.internal.ui.PDEPlugin;
52 import org.eclipse.pde.internal.ui.PDEUIMessages;
53 import org.eclipse.pde.internal.ui.util.SWTUtil;
54 import org.eclipse.pde.internal.ui.wizards.PluginSelectionDialog;
55 import org.eclipse.swt.SWT;
56 import org.eclipse.swt.events.ModifyEvent;
57 import org.eclipse.swt.events.ModifyListener;
58 import org.eclipse.swt.events.SelectionAdapter;
59 import org.eclipse.swt.events.SelectionEvent;
60 import org.eclipse.swt.layout.GridData;
61 import org.eclipse.swt.layout.GridLayout;
62 import org.eclipse.swt.widgets.Button;
63 import org.eclipse.swt.widgets.Composite;
64 import org.eclipse.swt.widgets.Display;
65 import org.eclipse.swt.widgets.Label;
66 import org.eclipse.swt.widgets.Text;
67 import org.eclipse.ui.IWorkbenchWindow;
68 import org.eclipse.ui.PartInitException;
69 import org.eclipse.ui.PlatformUI;
70 import org.eclipse.ui.actions.WorkspaceModifyOperation;
71 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
72 import org.eclipse.ui.ide.IDE;
73 import org.eclipse.ui.model.WorkbenchContentProvider;
74 import org.eclipse.ui.model.WorkbenchLabelProvider;
75 import org.eclipse.ui.part.FileEditorInput;
76 import org.eclipse.ui.views.navigator.ResourceComparator;
77 public abstract class BaseExtensionPointMainPage extends WizardPage {
78     public static final String JavaDoc SETTINGS_PLUGIN_ID = "BaseExtensionPoint.settings.pluginId"; //$NON-NLS-1$
79
public static final String JavaDoc SCHEMA_DIR = "schema"; //$NON-NLS-1$
80

81     protected IContainer fContainer;
82     protected Text fIdText;
83     protected Text fPluginIdText;
84     protected Text fNameText;
85     protected Text fSchemaText;
86     protected Text fSchemaLocationText;
87     protected Button fOpenSchemaButton;
88     protected Button fSharedSchemaButton;
89     protected Button fPluginBrowseButton;
90     protected Button fFindLocationButton;
91
92     public BaseExtensionPointMainPage(IContainer container) {
93         super("newExtensionPoint"); //$NON-NLS-1$
94
fContainer = container;
95     }
96     public void createControl(Composite parent) {
97         Composite container = new Composite(parent, SWT.NONE);
98         GridLayout layout = new GridLayout();
99         layout.numColumns = 3;
100         layout.verticalSpacing = 9;
101         layout.makeColumnsEqualWidth = false;
102         container.setLayout(layout);
103         Label label;
104         GridData gd;
105         if (isPluginIdNeeded()) {
106             label = new Label(container, SWT.NONE);
107             label.setText(PDEUIMessages.BaseExtensionPoint_pluginId);
108             fPluginIdText = new Text(container, SWT.SINGLE | SWT.BORDER);
109             gd = new GridData(GridData.FILL_HORIZONTAL);
110             gd.horizontalSpan=1;
111             gd.widthHint = 275;
112             fPluginIdText.setLayoutData(gd);
113             fPluginIdText.addModifyListener(new ModifyListener() {
114                 public void modifyText(ModifyEvent e) {
115                     validatePage();
116                 }
117             });
118             fPluginBrowseButton = new Button(container, SWT.PUSH);
119             gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
120             gd.horizontalSpan =1;
121             gd.widthHint = 50;
122             fPluginBrowseButton.setLayoutData(gd);
123             fPluginBrowseButton.setText(PDEUIMessages.BaseExtensionPointMainPage_pluginBrowse);
124             fPluginBrowseButton.setToolTipText(PDEUIMessages.BaseExtensionPointMainPage_pluginId_tooltip);
125             fPluginBrowseButton.addSelectionListener(new SelectionAdapter(){
126                 public void widgetSelected(SelectionEvent e) {
127                     handlePluginBrowse();
128                 }
129             });
130             SWTUtil.setButtonDimensionHint(fPluginBrowseButton);
131         }
132         label = new Label(container, SWT.NONE);
133         label.setText(PDEUIMessages.BaseExtensionPoint_id);
134         fIdText = new Text(container, SWT.SINGLE | SWT.BORDER);
135         gd = new GridData(GridData.FILL_HORIZONTAL);
136         gd.horizontalSpan = 2;
137         fIdText.setLayoutData(gd);
138         fIdText.addModifyListener(new ModifyListener() {
139             public void modifyText(ModifyEvent e) {
140                 // setting the text will trigger validation
141
// do not implicitly validate here
142
fSchemaText.setText(getSchemaLocation() + (getSchemaLocation().length() > 0 ? "/" : "") + fIdText.getText() + ".exsd"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
143
}
144         });
145         label = new Label(container, SWT.NONE);
146         label.setText(PDEUIMessages.BaseExtensionPoint_name);
147         fNameText = new Text(container, SWT.SINGLE | SWT.BORDER);
148         gd = new GridData(GridData.FILL_HORIZONTAL);
149         gd.horizontalSpan = 2;
150         fNameText.setLayoutData(gd);
151         fNameText.addModifyListener(new ModifyListener() {
152             public void modifyText(ModifyEvent e) {
153                 validatePage();
154             }
155         });
156         if (isPluginIdNeeded() && !isPluginIdFinal()){
157             label = new Label(container, SWT.NONE);
158             label.setText(PDEUIMessages.BaseExtensionPoint_schemaLocation);
159             fSchemaLocationText = new Text(container, SWT.SINGLE | SWT.BORDER);
160             gd = new GridData(GridData.FILL_HORIZONTAL);
161             gd.widthHint = 150;
162             gd.grabExcessHorizontalSpace = true;
163             fSchemaLocationText.setLayoutData(gd);
164             fSchemaLocationText.addModifyListener(new ModifyListener(){
165                 public void modifyText(ModifyEvent e){
166                     validatePage();
167                 }
168             });
169             fFindLocationButton = new Button(container, SWT.PUSH);
170             gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
171             gd.widthHint = 50;
172             fFindLocationButton.setLayoutData(gd);
173             fFindLocationButton.setText(PDEUIMessages.BaseExtensionPointMainPage_findBrowse);
174             fFindLocationButton.setToolTipText(PDEUIMessages.BaseExtensionPointMainPage_schemaLocation_tooltip);
175             fFindLocationButton.addSelectionListener(new SelectionAdapter(){
176                 public void widgetSelected(SelectionEvent e) {
177                     handleSchemaLocation();
178                 }
179             });
180             SWTUtil.setButtonDimensionHint(fFindLocationButton);
181         }
182         label = new Label(container, SWT.NONE);
183         label.setText(PDEUIMessages.BaseExtensionPoint_schema);
184         fSchemaText = new Text(container, SWT.SINGLE | SWT.BORDER);
185         gd = new GridData(GridData.FILL_HORIZONTAL);
186         gd.horizontalSpan = 2;
187         fSchemaText.setLayoutData(gd);
188         fSchemaText.addModifyListener(new ModifyListener(){
189             public void modifyText(ModifyEvent e){
190                 validatePage();
191             }
192         });
193         if (isSharedSchemaSwitchNeeded()) {
194             fSharedSchemaButton = new Button(container, SWT.CHECK);
195             fSharedSchemaButton.setText(PDEUIMessages.BaseExtensionPoint_shared);
196             gd = new GridData(GridData.FILL_HORIZONTAL);
197             gd.horizontalSpan = 2;
198             fSharedSchemaButton.setLayoutData(gd);
199         }
200         fOpenSchemaButton = new Button(container, SWT.CHECK);
201         fOpenSchemaButton.setText(PDEUIMessages.BaseExtensionPoint_edit);
202         fOpenSchemaButton.setSelection(true);
203         gd = new GridData(GridData.FILL_HORIZONTAL);
204         gd.horizontalSpan = 2;
205         fOpenSchemaButton.setLayoutData(gd);
206         if (isPluginIdNeeded())
207             fPluginIdText.setFocus();
208         else
209             fIdText.setFocus();
210         setControl(container);
211         initializeValues();
212         validatePage();
213         // do not start with an error message, convert to regular message
214
String JavaDoc error = getErrorMessage();
215         if (error != null) {
216             setMessage(error);
217             setErrorMessage(null);
218         }
219         Dialog.applyDialogFont(container);
220         PlatformUI.getWorkbench().getHelpSystem().setHelp(container, IHelpContextIds.NEW_SCHEMA);
221     }
222     private InputStream JavaDoc createSchemaStream(String JavaDoc pluginId, String JavaDoc pointId,
223             String JavaDoc name, boolean shared) {
224         if (name.length() == 0)
225             name = pointId;
226         EditableSchema schema = new EditableSchema(pluginId, pointId, name, false);
227         schema.setDescription(PDEUIMessages.BaseExtensionPoint_sections_overview);
228         DocumentSection section;
229         section = new DocumentSection(schema, IDocumentSection.SINCE, PDEUIMessages.BaseExtensionPointMainPage_since);
230         section.setDescription(PDEUIMessages.BaseExtensionPoint_sections_since);
231         schema.addDocumentSection(section);
232         SchemaElement element;
233         if (!shared) {
234             element = new SchemaRootElement(schema, "extension"); //$NON-NLS-1$
235
SchemaComplexType complexType = new SchemaComplexType(schema);
236             element.setType(complexType);
237             SchemaAttribute attribute = new SchemaAttribute(element, "point"); //$NON-NLS-1$
238
attribute.setType(new SchemaSimpleType(schema, "string")); //$NON-NLS-1$
239
attribute.setUse(ISchemaAttribute.REQUIRED);
240             complexType.addAttribute(attribute);
241             attribute = new SchemaAttribute(element, "id"); //$NON-NLS-1$
242
attribute.setType(new SchemaSimpleType(schema, "string")); //$NON-NLS-1$
243
complexType.addAttribute(attribute);
244             attribute = new SchemaAttribute(element, "name"); //$NON-NLS-1$
245
attribute.setType(new SchemaSimpleType(schema, "string")); //$NON-NLS-1$
246
attribute.setTranslatableProperty(true);
247             complexType.addAttribute(attribute);
248             schema.addElement(element);
249         }
250         section = new DocumentSection(schema, IDocumentSection.EXAMPLES,
251         "Examples"); //$NON-NLS-1$
252
section.setDescription(PDEUIMessages.BaseExtensionPoint_sections_usage);
253         schema.addDocumentSection(section);
254         section = new DocumentSection(schema, IDocumentSection.API_INFO,
255         "API Information"); //$NON-NLS-1$
256
section.setDescription(PDEUIMessages.BaseExtensionPoint_sections_api);
257         schema.addDocumentSection(section);
258         section = new DocumentSection(schema, IDocumentSection.IMPLEMENTATION,
259         "Supplied Implementation"); //$NON-NLS-1$
260
section.setDescription(PDEUIMessages.BaseExtensionPoint_sections_supplied);
261         schema.addDocumentSection(section);
262         section = new DocumentSection(schema, IDocumentSection.COPYRIGHT,
263         "Copyright"); //$NON-NLS-1$
264
section.setDescription(PDEUIMessages.BaseExtensionPoint_sections_copyright);
265         schema.addDocumentSection(section);
266         StringWriter JavaDoc swriter = new StringWriter JavaDoc();
267         try {
268             PrintWriter JavaDoc writer = new PrintWriter JavaDoc(swriter, true);
269             schema.save(writer);
270             swriter.close();
271         } catch (IOException JavaDoc e) {
272             PDEPlugin.logException(e);
273         }
274         try {
275             return new ByteArrayInputStream JavaDoc(swriter.toString().getBytes("UTF8")); //$NON-NLS-1$
276
} catch (UnsupportedEncodingException JavaDoc e) {
277             return new ByteArrayInputStream JavaDoc(new byte[0]);
278         }
279     }
280     private IFile generateSchemaFile(String JavaDoc pluginId, String JavaDoc id, String JavaDoc name,
281             boolean shared, String JavaDoc schema, IProgressMonitor monitor)
282     throws CoreException {
283         IFile schemaFile = null;
284
285         IWorkspace workspace = fContainer.getWorkspace();
286         IPath schemaPath = new Path(schema).removeLastSegments(1);
287         IPath newSchemaPath = fContainer.getProjectRelativePath().append(schemaPath);
288         monitor.subTask(PDEUIMessages.BaseExtensionPoint_generating);
289         if (newSchemaPath.isEmpty() == false) {
290             IFolder folder = fContainer.getProject().getFolder(newSchemaPath);
291             CoreUtility.createFolder(folder);
292         }
293         InputStream JavaDoc source = createSchemaStream(pluginId, id, name, shared);
294         IPath filePath = fContainer.getFullPath().append(schema);
295         schemaFile = workspace.getRoot().getFile(filePath);
296         if (!schemaFile.exists()) {
297             // create for the first time
298
schemaFile.create(source, true, monitor);
299         } else {
300             schemaFile.setContents(source, true, false, monitor);
301         }
302         IDE.setDefaultEditor(schemaFile, IPDEUIConstants.SCHEMA_EDITOR_ID);
303         return schemaFile;
304     }
305     public IRunnableWithProgress getOperation() {
306         final boolean openFile = fOpenSchemaButton.getSelection();
307         final String JavaDoc id = fIdText.getText();
308         final String JavaDoc name = fNameText.getText();
309         final String JavaDoc schema = fSchemaText.getText();
310         final boolean shared = fSharedSchemaButton != null ? fSharedSchemaButton
311                 .getSelection() : false;
312                 IRunnableWithProgress operation = new WorkspaceModifyOperation() {
313                     public void execute(final IProgressMonitor monitor) {
314                         try {
315                             Display.getDefault().asyncExec(new Runnable JavaDoc() {
316
317                                 public void run() {
318                                     String JavaDoc schemaName = schema;
319                                     if (!schema.endsWith(".exsd")) //$NON-NLS-1$
320
schemaName = schema + ".exsd"; //$NON-NLS-1$
321

322                                     IFile file = fContainer.getFile(new Path(schema));
323                                     // do not overwrite if schema already exists
324
if (!file.exists())
325                                         try {
326                                             file = generateSchemaFile(getPluginId(), id, name,
327                                                     shared, schemaName, monitor);
328                                         } catch (CoreException e) {
329                                             PDEPlugin.logException(e);
330                                         }
331
332                                         if (file != null && openFile){
333                                             fSchemaText.setText(file.getProjectRelativePath().toString());
334                                             openSchemaFile(file);
335                                         }
336                                 }
337
338                             });
339
340                         } finally {
341                             monitor.done();
342                         }
343                     }
344                 };
345                 return operation;
346     }
347     public String JavaDoc getSchemaLocation() {
348         if (fSchemaText!=null){
349             String JavaDoc schema = fSchemaText.getText();
350             if (schema.length() == 0) {
351                 if (fSchemaLocationText != null
352                         && SCHEMA_DIR.equals(new Path(fSchemaLocationText
353                                 .getText()).lastSegment())) {
354                     return ""; //$NON-NLS-1$
355
}
356                 return SCHEMA_DIR;
357             }
358
359             int loc = schema.lastIndexOf("/"); //$NON-NLS-1$
360
if (loc!=-1)
361                 return schema.substring(0,loc);
362         }
363         return ""; //$NON-NLS-1$
364
}
365     public String JavaDoc getPluginId() {
366         if (fPluginIdText != null) {
367             return fPluginIdText.getText();
368         }
369         return ""; //$NON-NLS-1$
370
}
371
372     protected boolean isPluginIdNeeded() {
373         return false;
374     }
375     protected boolean isPluginIdFinal(){
376         return false;
377     }
378     protected boolean isSharedSchemaSwitchNeeded() {
379         return false;
380     }
381     private void openSchemaFile(final IFile file) {
382         final IWorkbenchWindow ww = PDEPlugin.getActiveWorkbenchWindow();
383         Display d = ww.getShell().getDisplay();
384         d.asyncExec(new Runnable JavaDoc() {
385             public void run() {
386                 try {
387                     String JavaDoc editorId = IPDEUIConstants.SCHEMA_EDITOR_ID;
388                     ww.getActivePage().openEditor(new FileEditorInput(file),
389                             editorId);
390                 } catch (PartInitException e) {
391                     PDEPlugin.logException(e);
392                 }
393             }
394         });
395     }
396
397     private void validatePage() {
398         // clear opening message
399
setMessage(null);
400         String JavaDoc message = validateFieldContents();
401         setErrorMessage(message);
402         setPageComplete(message == null);
403     }
404
405     protected abstract String JavaDoc validateFieldContents();
406
407     protected abstract void initializeValues();
408
409     protected String JavaDoc validateExtensionPointID() {
410
411         // Verify not zero length
412
String JavaDoc id = fIdText.getText();
413         if (id.length() == 0)
414             return PDEUIMessages.BaseExtensionPointMainPage_missingExtensionPointID;
415
416         // For 3.2 or greater plug-ins verify that it is a valid composite ID
417
// and that it has a valid namespace
418
// For 3.1 and lower plug-ins verify that it is a valid simple ID
419
String JavaDoc pluginID = getPluginId();
420         IPluginModelBase model = PluginRegistry.findModel(pluginID);
421         // Verify that the plugin was found
422
if (model == null) {
423             return NLS.bind(PDEUIMessages.BaseExtensionPointMainPage_errorMsgPluginNotFound, pluginID);
424         }
425         
426         String JavaDoc schemaVersion = model.getPluginBase().getSchemaVersion();
427         if (schemaVersion == null || Float.parseFloat(schemaVersion) >= 3.2) {
428             if (!IdUtil.isValidCompositeID(id))
429                 return PDEUIMessages.BaseExtensionPointMainPage_invalidCompositeID;
430
431         } else if (!IdUtil.isValidSimpleID(id))
432             return PDEUIMessages.BaseExtensionPointMainPage_invalidSimpleID;
433
434         return null;
435     }
436
437     protected String JavaDoc validateExtensionPointName() {
438         // Verify not zero length
439
if ( fNameText.getText().length() == 0 )
440             return PDEUIMessages.BaseExtensionPointMainPage_missingExtensionPointName;
441
442         return null;
443     }
444
445     protected String JavaDoc validateExtensionPointSchema() {
446         // Verify not zero length
447
if ( fSchemaText.getText().length() == 0 )
448             return PDEUIMessages.BaseExtensionPointMainPage_missingExtensionPointSchema;
449
450         return null;
451     }
452
453     private void handlePluginBrowse(){
454         PluginSelectionDialog dialog = new PluginSelectionDialog(getShell(), PluginRegistry.getWorkspaceModels(), false);
455         dialog.create();
456         if (dialog.open() == Window.OK){
457             IPluginModelBase workspaceModelBase = (IPluginModelBase)dialog.getFirstResult();
458             fPluginIdText.setText(workspaceModelBase.getPluginBase().getId());
459         }
460     }
461     private void handleSchemaLocation(){
462         ElementTreeSelectionDialog dialog =
463             new ElementTreeSelectionDialog(
464                     getShell(),
465                     new WorkbenchLabelProvider(),
466                     new WorkbenchContentProvider());
467         dialog.setTitle(PDEUIMessages.BaseExtensionPointMainPage_schemaLocation_title);
468         dialog.setMessage(PDEUIMessages.BaseExtensionPointMainPage_schemaLocation_desc);
469         dialog.setDoubleClickSelects(false);
470         dialog.setAllowMultiple(false);
471         dialog.addFilter(new ViewerFilter(){
472             public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
473                 if (element instanceof IFile)
474                     return false;
475                 return true;
476             }
477         });
478
479         dialog.setInput(PDEPlugin.getWorkspace().getRoot());
480         dialog.setComparator(new ResourceComparator(ResourceComparator.NAME));
481         dialog.setInitialSelection(fContainer);
482         if (dialog.open() == Window.OK) {
483             Object JavaDoc[] elements = dialog.getResult();
484             if (elements.length >0){
485                 IResource elem = (IResource) elements[0];
486                 String JavaDoc newPath = getWorkspaceRelativePath(elem.getLocation().toString());
487                 fSchemaLocationText.setText(newPath + "/"); //$NON-NLS-1$
488
}
489         }
490     }
491     private String JavaDoc getWorkspaceRelativePath(String JavaDoc path){
492         String JavaDoc workspacePath = PDECore.getWorkspace().getRoot().getLocation().toString();
493         if (path.startsWith(workspacePath))
494             path = path.replaceFirst(workspacePath, ""); //$NON-NLS-1$
495
return path;
496     }
497     public String JavaDoc getInvalidIdMessage() {
498         // No validation done (other than making sure id is not blank)
499
return null;
500     }
501
502 }
503
Popular Tags