001: /*******************************************************************************
002: * Copyright (c) 2005, 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.target;
011:
012: import java.io.BufferedInputStream;
013: import java.io.IOException;
014: import java.io.InputStream;
015: import java.io.PrintWriter;
016: import java.io.StringWriter;
017: import java.util.ArrayList;
018:
019: import org.eclipse.core.resources.IFile;
020: import org.eclipse.core.runtime.CoreException;
021: import org.eclipse.jface.text.IDocument;
022: import org.eclipse.pde.core.IBaseModel;
023: import org.eclipse.pde.core.IEditable;
024: import org.eclipse.pde.core.IModelChangedEvent;
025: import org.eclipse.pde.internal.core.itarget.ITargetModel;
026: import org.eclipse.pde.internal.core.target.TargetModel;
027: import org.eclipse.pde.internal.core.target.WorkspaceTargetModel;
028: import org.eclipse.pde.internal.ui.PDEPlugin;
029: import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
030: import org.eclipse.pde.internal.ui.editor.context.UTF8InputContext;
031: import org.eclipse.ui.IEditorInput;
032: import org.eclipse.ui.IFileEditorInput;
033: import org.eclipse.ui.IStorageEditorInput;
034:
035: public class TargetInputContext extends UTF8InputContext {
036:
037: public static final String CONTEXT_ID = "target-context"; //$NON-NLS-1$
038:
039: public TargetInputContext(PDEFormEditor editor, IEditorInput input,
040: boolean primary) {
041: super (editor, input, primary);
042: create();
043: }
044:
045: /* (non-Javadoc)
046: * @see org.eclipse.pde.internal.ui.editor.context.InputContext#getId()
047: */
048: public String getId() {
049: return CONTEXT_ID;
050: }
051:
052: /* (non-Javadoc)
053: * @see org.eclipse.pde.internal.ui.editor.context.InputContext#createModel(org.eclipse.ui.IEditorInput)
054: */
055: protected IBaseModel createModel(IEditorInput input)
056: throws CoreException {
057: ITargetModel model = null;
058: if (input instanceof IStorageEditorInput) {
059: try {
060: if (input instanceof IFileEditorInput) {
061: IFile file = ((IFileEditorInput) input).getFile();
062: model = new WorkspaceTargetModel(file, true);
063: model.load();
064: } else if (input instanceof IStorageEditorInput) {
065: InputStream is = new BufferedInputStream(
066: ((IStorageEditorInput) input).getStorage()
067: .getContents());
068: model = new TargetModel();
069: model.load(is, false);
070: }
071: } catch (CoreException e) {
072: PDEPlugin.logException(e);
073: return null;
074: }
075: }
076: return model;
077: }
078:
079: /* (non-Javadoc)
080: * @see org.eclipse.pde.internal.ui.editor.context.InputContext#addTextEditOperation(java.util.ArrayList, org.eclipse.pde.core.IModelChangedEvent)
081: */
082: protected void addTextEditOperation(ArrayList ops,
083: IModelChangedEvent event) {
084: }
085:
086: /* (non-Javadoc)
087: * @see org.eclipse.pde.internal.ui.editor.context.InputContext#flushModel(org.eclipse.jface.text.IDocument)
088: */
089: protected void flushModel(IDocument doc) {
090: if (!(getModel() instanceof IEditable))
091: return;
092: IEditable editableModel = (IEditable) getModel();
093: if (editableModel.isDirty() == false)
094: return;
095: try {
096: StringWriter swriter = new StringWriter();
097: PrintWriter writer = new PrintWriter(swriter);
098: editableModel.save(writer);
099: writer.flush();
100: swriter.close();
101: doc.set(swriter.toString());
102: } catch (IOException e) {
103: PDEPlugin.logException(e);
104: }
105: }
106:
107: protected String getPartitionName() {
108: return "___target_partition"; //$NON-NLS-1$
109: }
110:
111: }
|