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