001: /*******************************************************************************
002: * Copyright (c) 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.compare;
011:
012: import org.eclipse.compare.*;
013: import org.eclipse.compare.structuremergeviewer.*;
014: import org.eclipse.core.resources.ResourcesPlugin;
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IProgressMonitor;
017: import org.eclipse.jface.resource.*;
018: import org.eclipse.jface.text.IDocument;
019: import org.eclipse.pde.core.plugin.*;
020: import org.eclipse.pde.internal.core.text.IDocumentElementNode;
021: import org.eclipse.pde.internal.core.text.plugin.*;
022: import org.eclipse.pde.internal.ui.PDELabelProvider;
023: import org.eclipse.pde.internal.ui.PDEPluginImages;
024: import org.eclipse.pde.internal.ui.PDEUIMessages;
025: import org.eclipse.swt.graphics.Image;
026:
027: public class PluginStructureCreator extends StructureCreator {
028:
029: public static final int ROOT = 0;
030: public static final int LIBRARY = 1;
031: public static final int IMPORT = 2;
032: public static final int EXTENSION_POINT = 3;
033: public static final int EXTENSION = 4;
034:
035: static class PluginNode extends DocumentRangeNode implements
036: ITypedElement {
037:
038: private final Image image;
039:
040: public PluginNode(DocumentRangeNode parent, int type,
041: String id, Image image, IDocument doc, int start,
042: int length) {
043: super (parent, type, id, doc, start, length);
044: this .image = image;
045: if (parent != null) {
046: parent.addChild(PluginNode.this );
047: }
048: }
049:
050: public String getName() {
051: return this .getId();
052: }
053:
054: public String getType() {
055: return "PLUGIN2"; //$NON-NLS-1$
056: }
057:
058: public Image getImage() {
059: return image;
060: }
061: }
062:
063: public PluginStructureCreator() {
064: // Nothing to do
065: }
066:
067: protected IStructureComparator createStructureComparator(
068: Object input, IDocument document,
069: ISharedDocumentAdapter adapter, IProgressMonitor monitor)
070: throws CoreException {
071: final boolean isEditable;
072: if (input instanceof IEditableContent)
073: isEditable = ((IEditableContent) input).isEditable();
074: else
075: isEditable = false;
076:
077: // Create a label provider to provide the text of the elements
078: final PDELabelProvider labelProvider = new PDELabelProvider();
079: // Create a resource manager to manage the images.
080: // We can't use the label provider because an image could be disposed that is still in use.
081: // By using a resource manager, we ensure that the image is not disposed until no resource
082: // managers reference it.
083: final ResourceManager resources = new LocalResourceManager(
084: JFaceResources.getResources());
085: DocumentRangeNode rootNode = new StructureRootNode(document,
086: input, this , adapter) {
087: public boolean isEditable() {
088: return isEditable;
089: }
090:
091: public void dispose() {
092: // Dispose the label provider and the local resource manager
093: labelProvider.dispose();
094: resources.dispose();
095: super .dispose();
096: }
097: };
098: try {
099: parsePlugin(input, rootNode, document, labelProvider,
100: resources, monitor);
101: } catch (CoreException ex) {
102: if (adapter != null)
103: adapter.disconnect(input);
104: throw ex;
105: }
106:
107: return rootNode;
108: }
109:
110: public String getContents(Object node, boolean ignoreWhitespace) {
111: if (node instanceof IStreamContentAccessor) {
112: IStreamContentAccessor sca = (IStreamContentAccessor) node;
113: try {
114: return ManifestStructureCreator.readString(sca);
115: } catch (CoreException ex) {
116: }
117: }
118: return null;
119: }
120:
121: public String getName() {
122: return PDEUIMessages.PluginStructureCreator_name;
123: }
124:
125: private void parsePlugin(Object input, DocumentRangeNode rootNode,
126: IDocument document, PDELabelProvider labelProvider,
127: ResourceManager resources, IProgressMonitor monitor)
128: throws CoreException {
129: boolean isFragment = isFragment(input);
130: PluginModelBase model = createModel(input, document, isFragment);
131: try {
132: String id = isFragment ? "fragment" : "plugin"; //$NON-NLS-1$ //$NON-NLS-2$
133: ImageDescriptor icon = isFragment ? PDEPluginImages.DESC_FRAGMENT_MF_OBJ
134: : PDEPluginImages.DESC_PLUGIN_MF_OBJ;
135: PluginNode parent = new PluginNode(rootNode, ROOT, id,
136: resources.createImage(icon), document, 0, document
137: .getLength());
138: createChildren(parent, model, labelProvider, resources);
139: } finally {
140: model.dispose();
141: }
142: }
143:
144: private boolean isFragment(Object input) {
145: if (input instanceof ITypedElement
146: && ((ITypedElement) input).getName().equals(
147: "fragment.xml")) //$NON-NLS-1$
148: return true;
149: return false;
150: }
151:
152: private PluginModelBase createModel(Object input,
153: IDocument document, boolean isFragment)
154: throws CoreException {
155: PluginModelBase model = null;
156: if (isFragment) {
157: model = new FragmentModel(document, false /* isReconciling */);
158: } else {
159: model = new PluginModel(document, false /* isReconciling */);
160: }
161: model.setCharset(getCharset(input));
162: model.load();
163: return model;
164: }
165:
166: private String getCharset(Object input) throws CoreException {
167: if (input instanceof IEncodedStreamContentAccessor)
168: return ((IEncodedStreamContentAccessor) input).getCharset();
169: return ResourcesPlugin.getEncoding();
170: }
171:
172: private void createChildren(DocumentRangeNode rootNode,
173: PluginModelBase model, PDELabelProvider labelProvider,
174: ResourceManager resources) {
175: createLibraries(rootNode, model, labelProvider, resources);
176: createImports(rootNode, model, labelProvider, labelProvider,
177: resources);
178: createExtensionPoints(rootNode, model, labelProvider, resources);
179: createExtensions(rootNode, model, labelProvider, resources);
180: }
181:
182: private void createLibraries(DocumentRangeNode parent,
183: PluginModelBase model, PDELabelProvider labelProvider,
184: ResourceManager resources) {
185: IPluginLibrary[] libraries = model.getPluginBase()
186: .getLibraries();
187: int type = LIBRARY;
188: for (int i = 0; i < libraries.length; i++) {
189: IPluginLibrary pluginLibrary = libraries[i];
190: createNode(parent, type, pluginLibrary, labelProvider,
191: resources);
192: }
193: }
194:
195: private void createImports(DocumentRangeNode parent,
196: PluginModelBase model, PDELabelProvider labelProvider,
197: PDELabelProvider labelProvider2, ResourceManager resources) {
198: IPluginImport[] imports = model.getPluginBase().getImports();
199: int type = IMPORT;
200: for (int i = 0; i < imports.length; i++) {
201: IPluginImport pluginImport = imports[i];
202: createNode(parent, type, pluginImport, labelProvider,
203: resources);
204: }
205: }
206:
207: private void createExtensionPoints(DocumentRangeNode parent,
208: PluginModelBase model, PDELabelProvider labelProvider,
209: ResourceManager resources) {
210: IPluginExtensionPoint[] extensionPoints = model.getPluginBase()
211: .getExtensionPoints();
212: int type = EXTENSION_POINT;
213: for (int i = 0; i < extensionPoints.length; i++) {
214: IPluginExtensionPoint extensionPoint = extensionPoints[i];
215: createNode(parent, type, extensionPoint, labelProvider,
216: resources);
217: }
218: }
219:
220: private void createExtensions(DocumentRangeNode parent,
221: PluginModelBase model, PDELabelProvider labelProvider,
222: ResourceManager resources) {
223: IPluginExtension[] extensions = model.getPluginBase()
224: .getExtensions();
225: int type = EXTENSION;
226: for (int i = 0; i < extensions.length; i++) {
227: IPluginExtension extension = extensions[i];
228: createNode(parent, type, extension, labelProvider,
229: resources);
230: }
231: }
232:
233: private void createNode(DocumentRangeNode parent, int type,
234: Object element, PDELabelProvider labelProvider,
235: ResourceManager resources) {
236: if (element instanceof IDocumentElementNode) {
237: IDocumentElementNode node = (IDocumentElementNode) element;
238: ImageDescriptor imageDescriptor = getImageDescriptor(element);
239: Image image = null;
240: if (imageDescriptor != null) {
241: image = resources.createImage(imageDescriptor);
242: }
243: new PluginNode(parent, type,
244: labelProvider.getText(element), image, parent
245: .getDocument(), node.getOffset(), node
246: .getLength());
247: }
248: }
249:
250: private ImageDescriptor getImageDescriptor(Object element) {
251: if (element instanceof IPluginImport) {
252: return PDEPluginImages.DESC_REQ_PLUGIN_OBJ;
253: }
254: if (element instanceof IPluginLibrary) {
255: return PDEPluginImages.DESC_JAVA_LIB_OBJ;
256: }
257: if (element instanceof IPluginExtension) {
258: return PDEPluginImages.DESC_EXTENSION_OBJ;
259: }
260: if (element instanceof IPluginExtensionPoint) {
261: return PDEPluginImages.DESC_EXT_POINT_OBJ;
262: }
263: return null;
264: }
265: }
|