001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.feature;
011:
012: import java.io.BufferedInputStream;
013: import java.io.ByteArrayInputStream;
014: import java.io.IOException;
015: import java.io.InputStream;
016: import java.io.PrintWriter;
017: import java.io.StringWriter;
018: import java.io.UnsupportedEncodingException;
019: import java.util.ArrayList;
020:
021: import org.eclipse.core.resources.IFile;
022: import org.eclipse.core.resources.IStorage;
023: import org.eclipse.core.runtime.CoreException;
024: import org.eclipse.core.runtime.IPath;
025: import org.eclipse.jface.text.IDocument;
026: import org.eclipse.pde.core.IBaseModel;
027: import org.eclipse.pde.core.IEditable;
028: import org.eclipse.pde.core.IModelChangedEvent;
029: import org.eclipse.pde.internal.core.feature.ExternalFeatureModel;
030: import org.eclipse.pde.internal.core.feature.WorkspaceFeatureModel;
031: import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
032: import org.eclipse.pde.internal.ui.PDEPlugin;
033: import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
034: import org.eclipse.pde.internal.ui.editor.context.XMLInputContext;
035: import org.eclipse.ui.IEditorInput;
036: import org.eclipse.ui.IFileEditorInput;
037: import org.eclipse.ui.IStorageEditorInput;
038:
039: /**
040: *
041: */
042: public class FeatureInputContext extends XMLInputContext {
043: public static final String CONTEXT_ID = "feature-context"; //$NON-NLS-1$
044:
045: /**
046: * @param editor
047: * @param input
048: * @param primary
049: */
050: public FeatureInputContext(PDEFormEditor editor,
051: IEditorInput input, boolean primary) {
052: super (editor, input, primary);
053: create();
054: }
055:
056: /* (non-Javadoc)
057: * @see org.eclipse.pde.internal.ui.neweditor.context.InputContext#getId()
058: */
059: public String getId() {
060: return CONTEXT_ID;
061: }
062:
063: /* (non-Javadoc)
064: * @see org.eclipse.pde.internal.ui.neweditor.context.InputContext#createModel(org.eclipse.ui.IEditorInput)
065: */
066: protected IBaseModel createModel(IEditorInput input)
067: throws CoreException {
068: if (input instanceof IFileEditorInput)
069: return createResourceModel((IFileEditorInput) input);
070: if (input instanceof IStorageEditorInput)
071: return createStorageModel((IStorageEditorInput) input);
072: return null;
073: }
074:
075: private IBaseModel createResourceModel(IFileEditorInput input)
076: throws CoreException {
077: IFile file = input.getFile();
078: WorkspaceFeatureModel model = new WorkspaceFeatureModel(file);
079: model.load();
080: return model;
081: }
082:
083: private IBaseModel createStorageModel(IStorageEditorInput input)
084: throws CoreException {
085: InputStream stream = null;
086: IStorage storage = input.getStorage();
087: try {
088: stream = new BufferedInputStream(storage.getContents());
089: } catch (CoreException e) {
090: PDEPlugin.logException(e);
091: return null;
092: }
093: ExternalFeatureModel model = new ExternalFeatureModel();
094: IPath path = input.getStorage().getFullPath();
095: model
096: .setInstallLocation(path == null ? "" : path.removeLastSegments(1).toOSString()); //$NON-NLS-1$
097: try {
098: model.load(stream, false);
099: } catch (CoreException e) {
100: // Errors in the file
101: return null;
102: } finally {
103: try {
104: stream.close();
105: } catch (IOException e) {
106: }
107: }
108: return model;
109: }
110:
111: /* (non-Javadoc)
112: * @see org.eclipse.pde.internal.ui.neweditor.context.InputContext#addTextEditOperation(java.util.ArrayList, org.eclipse.pde.core.IModelChangedEvent)
113: */
114: protected void addTextEditOperation(ArrayList ops,
115: IModelChangedEvent event) {
116: }
117:
118: protected void flushModel(IDocument doc) {
119: // if model is dirty, flush its content into
120: // the document so that the source editor will
121: // pick up the changes.
122: if (!(getModel() instanceof IEditable))
123: return;
124: IEditable editableModel = (IEditable) getModel();
125: if (editableModel.isDirty() == false)
126: return;
127: try {
128: StringWriter swriter = new StringWriter();
129: PrintWriter writer = new PrintWriter(swriter);
130: editableModel.save(writer);
131: writer.flush();
132: swriter.close();
133: doc.set(swriter.toString());
134: } catch (IOException e) {
135: PDEPlugin.logException(e);
136: }
137: }
138:
139: protected boolean synchronizeModel(IDocument doc) {
140: IFeatureModel model = (IFeatureModel) getModel();
141:
142: boolean cleanModel = true;
143: String text = doc.get();
144: try {
145: InputStream stream = new ByteArrayInputStream(text
146: .getBytes("UTF8")); //$NON-NLS-1$
147: try {
148: model.reload(stream, false);
149: } catch (CoreException e) {
150: cleanModel = false;
151: }
152: try {
153: stream.close();
154: } catch (IOException e) {
155: }
156: } catch (UnsupportedEncodingException e) {
157: PDEPlugin.logException(e);
158: }
159: return cleanModel;
160: }
161:
162: /* (non-Javadoc)
163: * @see org.eclipse.pde.internal.ui.neweditor.context.XMLInputContext#reorderInsertEdits(java.util.ArrayList)
164: */
165: protected void reorderInsertEdits(ArrayList ops) {
166: }
167:
168: protected String getPartitionName() {
169: return "___feature_partition"; //$NON-NLS-1$
170: }
171: }
|