KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > schema > SchemaIncludesSection


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.editor.schema;
12
13 import org.eclipse.core.resources.IFile;
14 import org.eclipse.core.resources.IProject;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.Path;
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.jface.viewers.ArrayContentProvider;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.viewers.TableViewer;
23 import org.eclipse.jface.viewers.Viewer;
24 import org.eclipse.jface.viewers.ViewerFilter;
25 import org.eclipse.jface.window.Window;
26 import org.eclipse.osgi.util.NLS;
27 import org.eclipse.pde.core.IModelChangedEvent;
28 import org.eclipse.pde.core.plugin.IPluginModelBase;
29 import org.eclipse.pde.core.plugin.PluginRegistry;
30 import org.eclipse.pde.internal.core.ischema.ISchema;
31 import org.eclipse.pde.internal.core.ischema.ISchemaInclude;
32 import org.eclipse.pde.internal.core.natures.PDE;
33 import org.eclipse.pde.internal.core.schema.Schema;
34 import org.eclipse.pde.internal.core.schema.SchemaInclude;
35 import org.eclipse.pde.internal.ui.PDEPlugin;
36 import org.eclipse.pde.internal.ui.PDEUIMessages;
37 import org.eclipse.pde.internal.ui.editor.TableSection;
38 import org.eclipse.pde.internal.ui.parts.TablePart;
39 import org.eclipse.pde.internal.ui.util.FileExtensionFilter;
40 import org.eclipse.pde.internal.ui.util.FileValidator;
41 import org.eclipse.swt.SWT;
42 import org.eclipse.swt.layout.GridData;
43 import org.eclipse.swt.widgets.Composite;
44 import org.eclipse.ui.IEditorInput;
45 import org.eclipse.ui.IFileEditorInput;
46 import org.eclipse.ui.actions.ActionFactory;
47 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
48 import org.eclipse.ui.forms.widgets.FormToolkit;
49 import org.eclipse.ui.forms.widgets.Section;
50 import org.eclipse.ui.model.WorkbenchContentProvider;
51 import org.eclipse.ui.model.WorkbenchLabelProvider;
52
53 public class SchemaIncludesSection extends TableSection {
54
55     private TableViewer fViewer;
56     
57     class PDEProjectFilter extends ViewerFilter {
58         public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
59             if (element instanceof IProject) {
60                 try {
61                     return ((IProject)element).hasNature(PDE.PLUGIN_NATURE);
62                 } catch (CoreException e) {
63                 }
64             } else if (element instanceof IFile) {
65                 return isUnlistedInclude((IFile)element);
66             }
67             return true;
68         }
69     }
70     
71     public SchemaIncludesSection(SchemaOverviewPage page, Composite parent) {
72         super(page, parent, Section.DESCRIPTION, new String JavaDoc[] { PDEUIMessages.SchemaIncludesSection_addButton, PDEUIMessages.SchemaIncludesSection_removeButton });
73         getSection().setText(PDEUIMessages.SchemaIncludesSection_title);
74         getSection().setDescription(PDEUIMessages.SchemaIncludesSection_description);
75     }
76
77     public void createClient(Section section, FormToolkit toolkit) {
78         Composite container = createClientContainer(section, 2, toolkit);
79         createViewerPartControl(container, SWT.MULTI, 2, toolkit);
80         TablePart tablePart = getTablePart();
81         fViewer = tablePart.getTableViewer();
82         fViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider());
83         PDEPlugin.getDefault().getLabelProvider().connect(this);
84         fViewer.setContentProvider(new ArrayContentProvider());
85
86         getSchema().addModelChangedListener(this);
87         toolkit.paintBordersFor(container);
88         section.setClient(container);
89         section.setLayoutData(new GridData(GridData.FILL_BOTH));
90         
91         initialize();
92     }
93
94     protected void buttonSelected(int index) {
95         if (index == 0)
96             handleNewInclude();
97         else
98             handleRemoveInclude();
99     }
100     
101     protected void selectionChanged(IStructuredSelection selection) {
102         getPage().getManagedForm().fireSelectionChanged(this, selection);
103         getPage().getPDEEditor().setSelection(selection);
104         if (!getSchema().isEditable())
105             return;
106         Object JavaDoc object = ((IStructuredSelection) fViewer.getSelection()).getFirstElement();
107         getTablePart().setButtonEnabled(1, object instanceof ISchemaInclude);
108     }
109     
110     public void dispose() {
111         ISchema schema = getSchema();
112         if (schema != null)
113             schema.removeModelChangedListener(this);
114         PDEPlugin.getDefault().getLabelProvider().disconnect(this);
115         super.dispose();
116     }
117
118     public void modelChanged(IModelChangedEvent e) {
119         int changeType = e.getChangeType();
120         if (changeType == IModelChangedEvent.WORLD_CHANGED) {
121             markStale();
122             return;
123         }
124         Object JavaDoc[] objects = e.getChangedObjects();
125         for (int i = 0; i < objects.length; i++) {
126             if (objects[i] instanceof ISchemaInclude) {
127                 if (changeType == IModelChangedEvent.INSERT) {
128                     fViewer.add(objects[i]);
129                 } else if (changeType == IModelChangedEvent.REMOVE) {
130                     fViewer.remove(objects[i]);
131                 }
132             }
133         }
134     }
135     
136     public boolean doGlobalAction(String JavaDoc actionId) {
137         if (actionId.equals(ActionFactory.DELETE.getId())) {
138             handleRemoveInclude();
139             return true;
140         }
141         return false;
142     }
143     
144     private ISchema getSchema() {
145         return (ISchema) getPage().getModel();
146     }
147
148     protected void handleRemoveInclude() {
149         Object JavaDoc[] selected = new Object JavaDoc[0];
150         ISelection selection = fViewer.getSelection();
151         if (selection.isEmpty())
152             return;
153         if (selection instanceof IStructuredSelection) {
154             selected = ((IStructuredSelection) selection).toArray();
155             Schema schema = (Schema) getSchema();
156             for (int i = 0; i < selected.length; i++) {
157                 schema.removeInclude((ISchemaInclude) selected[i]);
158             }
159         }
160     }
161
162     protected void handleNewInclude() {
163         ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
164                                 getPage().getSite().getShell(),
165                                 new WorkbenchLabelProvider(),
166                                 new WorkbenchContentProvider());
167         dialog.setValidator(new FileValidator());
168         dialog.setAllowMultiple(false);
169         dialog.setTitle(PDEUIMessages.ProductExportWizardPage_fileSelection);
170         dialog.setMessage(PDEUIMessages.SchemaIncludesSection_dialogMessage);
171         dialog.addFilter(new FileExtensionFilter("exsd")); //$NON-NLS-1$
172
dialog.addFilter(new PDEProjectFilter());
173         dialog.setInput(PDEPlugin.getWorkspace().getRoot());
174
175         if (dialog.open() == Window.OK) {
176             Object JavaDoc result = dialog.getFirstResult();
177             if (!(result instanceof IFile))
178                 return;
179             IFile newInclude = (IFile) result;
180
181             String JavaDoc location = getIncludeLocation(newInclude);
182             ISchemaInclude include = new SchemaInclude(getSchema(), location, false);
183             ISchema schema = getSchema();
184             if (schema instanceof Schema)
185                 ((Schema) schema).addInclude(include);
186         }
187     }
188     
189     private void initialize() {
190         refresh();
191     }
192     
193     private String JavaDoc getIncludeLocation(IFile file) {
194         IEditorInput input = getPage().getEditorInput();
195         if (!(input instanceof IFileEditorInput))
196             return null;
197         IPath schemaPath = ((IFileEditorInput)input).getFile().getFullPath();
198         IPath currPath = file.getFullPath();
199         int matchinSegments = schemaPath.matchingFirstSegments(currPath);
200         if (matchinSegments > 0) {
201             schemaPath = schemaPath.removeFirstSegments(matchinSegments);
202             currPath = currPath.removeFirstSegments(matchinSegments);
203             if (schemaPath.segmentCount() == 1)
204                 return currPath.toString();
205             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
206             while (schemaPath.segmentCount() > 1) {
207                 sb.append("../"); //$NON-NLS-1$
208
schemaPath = schemaPath.removeFirstSegments(1);
209             }
210             String JavaDoc location = sb.toString() + currPath.toString();
211             return location.trim().length() > 0 ? location : null;
212         }
213         IPluginModelBase model = PluginRegistry.findModel(file.getProject());
214         String JavaDoc id = model.getPluginBase().getId();
215         if (id != null)
216             return "schema://" + id + "/" + file.getProjectRelativePath().toString(); //$NON-NLS-1$ //$NON-NLS-2$
217
return null;
218     }
219     
220     private boolean isUnlistedInclude(IFile file) {
221         String JavaDoc location = getIncludeLocation(file);
222         if (location == null)
223             return false;
224         boolean unlisted = true;
225         ISchemaInclude[] includes = getSchema().getIncludes();
226         for (int i = 0; i < includes.length; i++) {
227             if (includes[i].getLocation().equals(location)) {
228                 unlisted = false;
229                 break;
230             }
231         }
232         return unlisted;
233     }
234
235     protected void handleDoubleClick(IStructuredSelection selection) {
236         Object JavaDoc object = selection.getFirstElement();
237         if (object instanceof ISchemaInclude) {
238             IEditorInput edinput = getPage().getEditorInput();
239             if (!(edinput instanceof IFileEditorInput))
240                 return;
241             String JavaDoc path = ((ISchemaInclude)object).getLocation();
242             IPath includePath = new Path(((ISchemaInclude)object).getLocation());
243             boolean result = false;
244             if (path.startsWith("schema:")) { //$NON-NLS-1$
245
result = SchemaEditor.openSchema(includePath);
246             } else {
247                 IFile currSchemaFile = ((IFileEditorInput)edinput).getFile();
248                 IProject project = currSchemaFile.getProject();
249                 IPath currSchemaPath = currSchemaFile.getProjectRelativePath();
250                 IFile file = project.getFile(currSchemaPath.removeLastSegments(1).append(includePath));
251                 result = SchemaEditor.openSchema(file);
252             }
253             if (!result)
254                 MessageDialog.openWarning(
255                         getPage().getSite().getShell(),
256                         PDEUIMessages.SchemaIncludesSection_missingWarningTitle,
257                         NLS.bind(PDEUIMessages.SchemaIncludesSection_missingWarningMessage, includePath.toString()));
258         }
259     }
260     
261     /* (non-Javadoc)
262      * @see org.eclipse.ui.forms.AbstractFormPart#refresh()
263      */

264     public void refresh() {
265         getTablePart().setButtonEnabled(0, getSchema().isEditable());
266         getTablePart().setButtonEnabled(1, false);
267         fViewer.setInput(getSchema().getIncludes());
268         super.refresh();
269     }
270 }
271
Popular Tags