001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.schema;
011:
012: import org.eclipse.core.resources.IFile;
013: import org.eclipse.core.resources.IProject;
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.core.runtime.IPath;
016: import org.eclipse.core.runtime.Path;
017: import org.eclipse.jface.dialogs.MessageDialog;
018: import org.eclipse.jface.viewers.ArrayContentProvider;
019: import org.eclipse.jface.viewers.ISelection;
020: import org.eclipse.jface.viewers.IStructuredSelection;
021: import org.eclipse.jface.viewers.TableViewer;
022: import org.eclipse.jface.viewers.Viewer;
023: import org.eclipse.jface.viewers.ViewerFilter;
024: import org.eclipse.jface.window.Window;
025: import org.eclipse.osgi.util.NLS;
026: import org.eclipse.pde.core.IModelChangedEvent;
027: import org.eclipse.pde.core.plugin.IPluginModelBase;
028: import org.eclipse.pde.core.plugin.PluginRegistry;
029: import org.eclipse.pde.internal.core.ischema.ISchema;
030: import org.eclipse.pde.internal.core.ischema.ISchemaInclude;
031: import org.eclipse.pde.internal.core.natures.PDE;
032: import org.eclipse.pde.internal.core.schema.Schema;
033: import org.eclipse.pde.internal.core.schema.SchemaInclude;
034: import org.eclipse.pde.internal.ui.PDEPlugin;
035: import org.eclipse.pde.internal.ui.PDEUIMessages;
036: import org.eclipse.pde.internal.ui.editor.TableSection;
037: import org.eclipse.pde.internal.ui.parts.TablePart;
038: import org.eclipse.pde.internal.ui.util.FileExtensionFilter;
039: import org.eclipse.pde.internal.ui.util.FileValidator;
040: import org.eclipse.swt.SWT;
041: import org.eclipse.swt.layout.GridData;
042: import org.eclipse.swt.widgets.Composite;
043: import org.eclipse.ui.IEditorInput;
044: import org.eclipse.ui.IFileEditorInput;
045: import org.eclipse.ui.actions.ActionFactory;
046: import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
047: import org.eclipse.ui.forms.widgets.FormToolkit;
048: import org.eclipse.ui.forms.widgets.Section;
049: import org.eclipse.ui.model.WorkbenchContentProvider;
050: import org.eclipse.ui.model.WorkbenchLabelProvider;
051:
052: public class SchemaIncludesSection extends TableSection {
053:
054: private TableViewer fViewer;
055:
056: class PDEProjectFilter extends ViewerFilter {
057: public boolean select(Viewer viewer, Object parentElement,
058: Object element) {
059: if (element instanceof IProject) {
060: try {
061: return ((IProject) element)
062: .hasNature(PDE.PLUGIN_NATURE);
063: } catch (CoreException e) {
064: }
065: } else if (element instanceof IFile) {
066: return isUnlistedInclude((IFile) element);
067: }
068: return true;
069: }
070: }
071:
072: public SchemaIncludesSection(SchemaOverviewPage page,
073: Composite parent) {
074: super (page, parent, Section.DESCRIPTION, new String[] {
075: PDEUIMessages.SchemaIncludesSection_addButton,
076: PDEUIMessages.SchemaIncludesSection_removeButton });
077: getSection().setText(PDEUIMessages.SchemaIncludesSection_title);
078: getSection().setDescription(
079: PDEUIMessages.SchemaIncludesSection_description);
080: }
081:
082: public void createClient(Section section, FormToolkit toolkit) {
083: Composite container = createClientContainer(section, 2, toolkit);
084: createViewerPartControl(container, SWT.MULTI, 2, toolkit);
085: TablePart tablePart = getTablePart();
086: fViewer = tablePart.getTableViewer();
087: fViewer.setLabelProvider(PDEPlugin.getDefault()
088: .getLabelProvider());
089: PDEPlugin.getDefault().getLabelProvider().connect(this );
090: fViewer.setContentProvider(new ArrayContentProvider());
091:
092: getSchema().addModelChangedListener(this );
093: toolkit.paintBordersFor(container);
094: section.setClient(container);
095: section.setLayoutData(new GridData(GridData.FILL_BOTH));
096:
097: initialize();
098: }
099:
100: protected void buttonSelected(int index) {
101: if (index == 0)
102: handleNewInclude();
103: else
104: handleRemoveInclude();
105: }
106:
107: protected void selectionChanged(IStructuredSelection selection) {
108: getPage().getManagedForm()
109: .fireSelectionChanged(this , selection);
110: getPage().getPDEEditor().setSelection(selection);
111: if (!getSchema().isEditable())
112: return;
113: Object object = ((IStructuredSelection) fViewer.getSelection())
114: .getFirstElement();
115: getTablePart().setButtonEnabled(1,
116: object instanceof ISchemaInclude);
117: }
118:
119: public void dispose() {
120: ISchema schema = getSchema();
121: if (schema != null)
122: schema.removeModelChangedListener(this );
123: PDEPlugin.getDefault().getLabelProvider().disconnect(this );
124: super .dispose();
125: }
126:
127: public void modelChanged(IModelChangedEvent e) {
128: int changeType = e.getChangeType();
129: if (changeType == IModelChangedEvent.WORLD_CHANGED) {
130: markStale();
131: return;
132: }
133: Object[] objects = e.getChangedObjects();
134: for (int i = 0; i < objects.length; i++) {
135: if (objects[i] instanceof ISchemaInclude) {
136: if (changeType == IModelChangedEvent.INSERT) {
137: fViewer.add(objects[i]);
138: } else if (changeType == IModelChangedEvent.REMOVE) {
139: fViewer.remove(objects[i]);
140: }
141: }
142: }
143: }
144:
145: public boolean doGlobalAction(String actionId) {
146: if (actionId.equals(ActionFactory.DELETE.getId())) {
147: handleRemoveInclude();
148: return true;
149: }
150: return false;
151: }
152:
153: private ISchema getSchema() {
154: return (ISchema) getPage().getModel();
155: }
156:
157: protected void handleRemoveInclude() {
158: Object[] selected = new Object[0];
159: ISelection selection = fViewer.getSelection();
160: if (selection.isEmpty())
161: return;
162: if (selection instanceof IStructuredSelection) {
163: selected = ((IStructuredSelection) selection).toArray();
164: Schema schema = (Schema) getSchema();
165: for (int i = 0; i < selected.length; i++) {
166: schema.removeInclude((ISchemaInclude) selected[i]);
167: }
168: }
169: }
170:
171: protected void handleNewInclude() {
172: ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
173: getPage().getSite().getShell(),
174: new WorkbenchLabelProvider(),
175: new WorkbenchContentProvider());
176: dialog.setValidator(new FileValidator());
177: dialog.setAllowMultiple(false);
178: dialog
179: .setTitle(PDEUIMessages.ProductExportWizardPage_fileSelection);
180: dialog
181: .setMessage(PDEUIMessages.SchemaIncludesSection_dialogMessage);
182: dialog.addFilter(new FileExtensionFilter("exsd")); //$NON-NLS-1$
183: dialog.addFilter(new PDEProjectFilter());
184: dialog.setInput(PDEPlugin.getWorkspace().getRoot());
185:
186: if (dialog.open() == Window.OK) {
187: Object result = dialog.getFirstResult();
188: if (!(result instanceof IFile))
189: return;
190: IFile newInclude = (IFile) result;
191:
192: String location = getIncludeLocation(newInclude);
193: ISchemaInclude include = new SchemaInclude(getSchema(),
194: location, false);
195: ISchema schema = getSchema();
196: if (schema instanceof Schema)
197: ((Schema) schema).addInclude(include);
198: }
199: }
200:
201: private void initialize() {
202: refresh();
203: }
204:
205: private String getIncludeLocation(IFile file) {
206: IEditorInput input = getPage().getEditorInput();
207: if (!(input instanceof IFileEditorInput))
208: return null;
209: IPath schemaPath = ((IFileEditorInput) input).getFile()
210: .getFullPath();
211: IPath currPath = file.getFullPath();
212: int matchinSegments = schemaPath
213: .matchingFirstSegments(currPath);
214: if (matchinSegments > 0) {
215: schemaPath = schemaPath
216: .removeFirstSegments(matchinSegments);
217: currPath = currPath.removeFirstSegments(matchinSegments);
218: if (schemaPath.segmentCount() == 1)
219: return currPath.toString();
220: StringBuffer sb = new StringBuffer();
221: while (schemaPath.segmentCount() > 1) {
222: sb.append("../"); //$NON-NLS-1$
223: schemaPath = schemaPath.removeFirstSegments(1);
224: }
225: String location = sb.toString() + currPath.toString();
226: return location.trim().length() > 0 ? location : null;
227: }
228: IPluginModelBase model = PluginRegistry.findModel(file
229: .getProject());
230: String id = model.getPluginBase().getId();
231: if (id != null)
232: return "schema://" + id + "/" + file.getProjectRelativePath().toString(); //$NON-NLS-1$ //$NON-NLS-2$
233: return null;
234: }
235:
236: private boolean isUnlistedInclude(IFile file) {
237: String location = getIncludeLocation(file);
238: if (location == null)
239: return false;
240: boolean unlisted = true;
241: ISchemaInclude[] includes = getSchema().getIncludes();
242: for (int i = 0; i < includes.length; i++) {
243: if (includes[i].getLocation().equals(location)) {
244: unlisted = false;
245: break;
246: }
247: }
248: return unlisted;
249: }
250:
251: protected void handleDoubleClick(IStructuredSelection selection) {
252: Object object = selection.getFirstElement();
253: if (object instanceof ISchemaInclude) {
254: IEditorInput edinput = getPage().getEditorInput();
255: if (!(edinput instanceof IFileEditorInput))
256: return;
257: String path = ((ISchemaInclude) object).getLocation();
258: IPath includePath = new Path(((ISchemaInclude) object)
259: .getLocation());
260: boolean result = false;
261: if (path.startsWith("schema:")) { //$NON-NLS-1$
262: result = SchemaEditor.openSchema(includePath);
263: } else {
264: IFile currSchemaFile = ((IFileEditorInput) edinput)
265: .getFile();
266: IProject project = currSchemaFile.getProject();
267: IPath currSchemaPath = currSchemaFile
268: .getProjectRelativePath();
269: IFile file = project.getFile(currSchemaPath
270: .removeLastSegments(1).append(includePath));
271: result = SchemaEditor.openSchema(file);
272: }
273: if (!result)
274: MessageDialog
275: .openWarning(
276: getPage().getSite().getShell(),
277: PDEUIMessages.SchemaIncludesSection_missingWarningTitle,
278: NLS
279: .bind(
280: PDEUIMessages.SchemaIncludesSection_missingWarningMessage,
281: includePath.toString()));
282: }
283: }
284:
285: /* (non-Javadoc)
286: * @see org.eclipse.ui.forms.AbstractFormPart#refresh()
287: */
288: public void refresh() {
289: getTablePart().setButtonEnabled(0, getSchema().isEditable());
290: getTablePart().setButtonEnabled(1, false);
291: fViewer.setInput(getSchema().getIncludes());
292: super.refresh();
293: }
294: }
|