KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > product > ProductInputContext


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.product;
12
13 import java.io.BufferedInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.PrintWriter JavaDoc;
17 import java.io.StringWriter JavaDoc;
18 import java.util.ArrayList JavaDoc;
19
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.pde.core.IBaseModel;
24 import org.eclipse.pde.core.IEditable;
25 import org.eclipse.pde.core.IModelChangedEvent;
26 import org.eclipse.pde.internal.core.iproduct.IProductModel;
27 import org.eclipse.pde.internal.core.product.ProductModel;
28 import org.eclipse.pde.internal.core.product.WorkspaceProductModel;
29 import org.eclipse.pde.internal.ui.PDEPlugin;
30 import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
31 import org.eclipse.pde.internal.ui.editor.context.UTF8InputContext;
32 import org.eclipse.ui.IEditorInput;
33 import org.eclipse.ui.IFileEditorInput;
34 import org.eclipse.ui.IStorageEditorInput;
35
36
37 public class ProductInputContext extends UTF8InputContext {
38     
39     public static final String JavaDoc CONTEXT_ID = "product-context"; //$NON-NLS-1$
40

41     public ProductInputContext(PDEFormEditor editor, IEditorInput input,
42             boolean primary) {
43         super(editor, input, primary);
44         create();
45     }
46
47     /* (non-Javadoc)
48      * @see org.eclipse.pde.internal.ui.editor.context.InputContext#getId()
49      */

50     public String JavaDoc getId() {
51         return CONTEXT_ID;
52     }
53
54     /* (non-Javadoc)
55      * @see org.eclipse.pde.internal.ui.editor.context.InputContext#createModel(org.eclipse.ui.IEditorInput)
56      */

57     protected IBaseModel createModel(IEditorInput input) throws CoreException {
58         IProductModel model = null;
59         if (input instanceof IStorageEditorInput) {
60             try {
61                 if (input instanceof IFileEditorInput) {
62                     IFile file = ((IFileEditorInput) input).getFile();
63                     model = new WorkspaceProductModel(file, true);
64                     model.load();
65                 } else if (input instanceof IStorageEditorInput) {
66                     InputStream JavaDoc is = new BufferedInputStream JavaDoc(((IStorageEditorInput) input).getStorage()
67                             .getContents());
68                     model = new ProductModel();
69                     model.load(is, false);
70                 }
71             } catch (CoreException e) {
72                 PDEPlugin.logException(e);
73                 return null;
74             }
75         }
76         return model;
77     }
78
79     /* (non-Javadoc)
80      * @see org.eclipse.pde.internal.ui.editor.context.InputContext#addTextEditOperation(java.util.ArrayList, org.eclipse.pde.core.IModelChangedEvent)
81      */

82     protected void addTextEditOperation(ArrayList JavaDoc ops, IModelChangedEvent event) {
83     }
84     
85     /* (non-Javadoc)
86      * @see org.eclipse.pde.internal.ui.editor.context.InputContext#flushModel(org.eclipse.jface.text.IDocument)
87      */

88     protected void flushModel(IDocument doc) {
89         if (!(getModel() instanceof IEditable))
90             return;
91         IEditable editableModel = (IEditable) getModel();
92         if (editableModel.isDirty() == false)
93             return;
94         try {
95             StringWriter JavaDoc swriter = new StringWriter JavaDoc();
96             PrintWriter JavaDoc writer = new PrintWriter JavaDoc(swriter);
97             editableModel.save(writer);
98             writer.flush();
99             swriter.close();
100             doc.set(swriter.toString());
101         } catch (IOException JavaDoc e) {
102             PDEPlugin.logException(e);
103         }
104     }
105
106     protected String JavaDoc getPartitionName() {
107         return "___prod_partition"; //$NON-NLS-1$
108
}
109
110 }
111
Popular Tags