KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > feature > FeatureInputContext


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor.feature;
12
13 import java.io.BufferedInputStream JavaDoc;
14 import java.io.ByteArrayInputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.io.PrintWriter JavaDoc;
18 import java.io.StringWriter JavaDoc;
19 import java.io.UnsupportedEncodingException JavaDoc;
20 import java.util.ArrayList JavaDoc;
21
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IStorage;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.jface.text.IDocument;
27 import org.eclipse.pde.core.IBaseModel;
28 import org.eclipse.pde.core.IEditable;
29 import org.eclipse.pde.core.IModelChangedEvent;
30 import org.eclipse.pde.internal.core.feature.ExternalFeatureModel;
31 import org.eclipse.pde.internal.core.feature.WorkspaceFeatureModel;
32 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
33 import org.eclipse.pde.internal.ui.PDEPlugin;
34 import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
35 import org.eclipse.pde.internal.ui.editor.context.XMLInputContext;
36 import org.eclipse.ui.IEditorInput;
37 import org.eclipse.ui.IFileEditorInput;
38 import org.eclipse.ui.IStorageEditorInput;
39
40 /**
41  *
42  */

43 public class FeatureInputContext extends XMLInputContext {
44     public static final String JavaDoc CONTEXT_ID="feature-context"; //$NON-NLS-1$
45
/**
46      * @param editor
47      * @param input
48      * @param primary
49      */

50     public FeatureInputContext(PDEFormEditor editor, IEditorInput input,
51             boolean primary) {
52         super(editor, input, primary);
53         create();
54     }
55     /* (non-Javadoc)
56      * @see org.eclipse.pde.internal.ui.neweditor.context.InputContext#getId()
57      */

58     public String JavaDoc getId() {
59         return CONTEXT_ID;
60     }
61     /* (non-Javadoc)
62      * @see org.eclipse.pde.internal.ui.neweditor.context.InputContext#createModel(org.eclipse.ui.IEditorInput)
63      */

64     protected IBaseModel createModel(IEditorInput input) throws CoreException {
65         if (input instanceof IFileEditorInput)
66             return createResourceModel((IFileEditorInput) input);
67         if (input instanceof IStorageEditorInput)
68             return createStorageModel((IStorageEditorInput)input);
69         return null;
70     }
71     
72     private IBaseModel createResourceModel(IFileEditorInput input)
73                 throws CoreException {
74         IFile file = input.getFile();
75         WorkspaceFeatureModel model = new WorkspaceFeatureModel(file);
76         model.load();
77         return model;
78     }
79     
80     private IBaseModel createStorageModel(IStorageEditorInput input) throws CoreException {
81         InputStream JavaDoc stream = null;
82         IStorage storage = input.getStorage();
83         try {
84             stream = new BufferedInputStream JavaDoc(storage.getContents());
85         } catch (CoreException e) {
86             PDEPlugin.logException(e);
87             return null;
88         }
89         ExternalFeatureModel model = new ExternalFeatureModel();
90         IPath path = input.getStorage().getFullPath();
91         model.setInstallLocation(path == null ? "" : path.removeLastSegments(1).toOSString()); //$NON-NLS-1$
92
try {
93             model.load(stream, false);
94         } catch (CoreException e) {
95             // Errors in the file
96
return null;
97         }
98         finally {
99             try {
100                 stream.close();
101             }
102             catch (IOException JavaDoc e) {
103             }
104         }
105         return model;
106     }
107     /* (non-Javadoc)
108      * @see org.eclipse.pde.internal.ui.neweditor.context.InputContext#addTextEditOperation(java.util.ArrayList, org.eclipse.pde.core.IModelChangedEvent)
109      */

110     protected void addTextEditOperation(ArrayList JavaDoc ops, IModelChangedEvent event) {
111     }
112     
113     protected void flushModel(IDocument doc) {
114         // if model is dirty, flush its content into
115
// the document so that the source editor will
116
// pick up the changes.
117
if (!(getModel() instanceof IEditable))
118             return;
119         IEditable editableModel = (IEditable) getModel();
120         if (editableModel.isDirty() == false)
121             return;
122         try {
123             StringWriter JavaDoc swriter = new StringWriter JavaDoc();
124             PrintWriter JavaDoc writer = new PrintWriter JavaDoc(swriter);
125             editableModel.save(writer);
126             writer.flush();
127             swriter.close();
128             doc.set(swriter.toString());
129         } catch (IOException JavaDoc e) {
130             PDEPlugin.logException(e);
131         }
132     }
133     
134     protected boolean synchronizeModel(IDocument doc) {
135         IFeatureModel model = (IFeatureModel) getModel();
136
137         boolean cleanModel = true;
138         String JavaDoc text = doc.get();
139         try {
140             InputStream JavaDoc stream =
141                 new ByteArrayInputStream JavaDoc(text.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 JavaDoc e) {
150             }
151         } catch (UnsupportedEncodingException JavaDoc e) {
152             PDEPlugin.logException(e);
153         }
154         return cleanModel;
155     }
156     /* (non-Javadoc)
157      * @see org.eclipse.pde.internal.ui.neweditor.context.XMLInputContext#reorderInsertEdits(java.util.ArrayList)
158      */

159     protected void reorderInsertEdits(ArrayList JavaDoc ops) {
160     }
161     protected String JavaDoc getPartitionName() {
162         return "___feature_partition"; //$NON-NLS-1$
163
}
164 }
165
Popular Tags