KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > cheatsheet > simple > SimpleCSInputContext


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.pde.internal.ui.editor.cheatsheet.simple;
13
14 import java.io.BufferedInputStream 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.util.ArrayList JavaDoc;
20
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.pde.core.IBaseModel;
25 import org.eclipse.pde.core.IEditable;
26 import org.eclipse.pde.core.IModelChangedEvent;
27 import org.eclipse.pde.internal.core.cheatsheet.simple.SimpleCSModel;
28 import org.eclipse.pde.internal.core.cheatsheet.simple.SimpleCSWorkspaceModel;
29 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModel;
30 import org.eclipse.pde.internal.ui.PDEPlugin;
31 import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
32 import org.eclipse.pde.internal.ui.editor.context.UTF8InputContext;
33 import org.eclipse.ui.IEditorInput;
34 import org.eclipse.ui.IFileEditorInput;
35 import org.eclipse.ui.IStorageEditorInput;
36
37 /**
38  * SimpleCSInputContext
39  *
40  */

41 public class SimpleCSInputContext extends UTF8InputContext {
42
43     public static final String JavaDoc CONTEXT_ID = "simplecs-context"; //$NON-NLS-1$
44

45     /**
46      * @param editor
47      * @param input
48      * @param primary
49      */

50     public SimpleCSInputContext(PDEFormEditor editor, IEditorInput input,
51             boolean primary) {
52         super(editor, input, primary);
53         create();
54     }
55
56     /* (non-Javadoc)
57      * @see org.eclipse.pde.internal.ui.editor.context.InputContext#addTextEditOperation(java.util.ArrayList, org.eclipse.pde.core.IModelChangedEvent)
58      */

59     protected void addTextEditOperation(ArrayList JavaDoc ops, IModelChangedEvent event) {
60         // NO-OP
61
}
62
63     /* (non-Javadoc)
64      * @see org.eclipse.pde.internal.ui.editor.context.InputContext#createModel(org.eclipse.ui.IEditorInput)
65      */

66     protected IBaseModel createModel(IEditorInput input) throws CoreException {
67         ISimpleCSModel model = null;
68         if (input instanceof IStorageEditorInput) {
69             try {
70                 if (input instanceof IFileEditorInput) {
71                     IFile file = ((IFileEditorInput) input).getFile();
72                     model = new SimpleCSWorkspaceModel(file, true);
73                     model.load();
74                 } else if (input instanceof IStorageEditorInput) {
75                     InputStream JavaDoc is = new BufferedInputStream JavaDoc(
76                             ((IStorageEditorInput) input).getStorage()
77                                     .getContents());
78                     model = new SimpleCSModel();
79                     model.load(is, false);
80                 }
81             } catch (CoreException e) {
82                 PDEPlugin.logException(e);
83                 return null;
84             }
85         }
86         return model;
87     }
88
89     /* (non-Javadoc)
90      * @see org.eclipse.pde.internal.ui.editor.context.InputContext#getId()
91      */

92     public String JavaDoc getId() {
93         return CONTEXT_ID;
94     }
95
96     /* (non-Javadoc)
97      * @see org.eclipse.pde.internal.ui.editor.context.InputContext#getPartitionName()
98      */

99     protected String JavaDoc getPartitionName() {
100         return "___simplecs_partition"; //$NON-NLS-1$
101
}
102
103     /* (non-Javadoc)
104      * @see org.eclipse.pde.internal.ui.editor.context.InputContext#flushModel(org.eclipse.jface.text.IDocument)
105      */

106     protected void flushModel(IDocument doc) {
107         if (!(getModel() instanceof IEditable))
108             return;
109         IEditable editableModel = (IEditable) getModel();
110         if (editableModel.isDirty() == false)
111             return;
112         try {
113             StringWriter JavaDoc swriter = new StringWriter JavaDoc();
114             PrintWriter JavaDoc writer = new PrintWriter JavaDoc(swriter);
115             editableModel.save(writer);
116             writer.flush();
117             swriter.close();
118             doc.set(swriter.toString());
119         } catch (IOException JavaDoc e) {
120             PDEPlugin.logException(e);
121         }
122     }
123 }
124
Popular Tags