KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > plugin > PluginInputContext


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.plugin;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.pde.core.IBaseModel;
22 import org.eclipse.pde.internal.core.text.AbstractEditingModel;
23 import org.eclipse.pde.internal.core.text.IDocumentNode;
24 import org.eclipse.pde.internal.core.text.plugin.FragmentModel;
25 import org.eclipse.pde.internal.core.text.plugin.PluginBaseNode;
26 import org.eclipse.pde.internal.core.text.plugin.PluginModel;
27 import org.eclipse.pde.internal.core.text.plugin.PluginModelBase;
28 import org.eclipse.pde.internal.ui.editor.JarEntryEditorInput;
29 import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
30 import org.eclipse.pde.internal.ui.editor.SystemFileEditorInput;
31 import org.eclipse.pde.internal.ui.editor.context.XMLInputContext;
32 import org.eclipse.text.edits.InsertEdit;
33 import org.eclipse.text.edits.TextEdit;
34 import org.eclipse.ui.IEditorInput;
35 import org.eclipse.ui.IFileEditorInput;
36 import org.eclipse.ui.IStorageEditorInput;
37
38 public class PluginInputContext extends XMLInputContext {
39     public static final String JavaDoc CONTEXT_ID = "plugin-context"; //$NON-NLS-1$
40
private boolean fIsFragment;
41
42     public PluginInputContext(PDEFormEditor editor, IEditorInput input, boolean primary, boolean isFragment) {
43         super(editor, input, primary);
44         fIsFragment = isFragment;
45         create();
46     }
47     /* (non-Javadoc)
48      * @see org.eclipse.pde.internal.ui.editor.InputContext#createModel(org.eclipse.ui.IEditorInput)
49      */

50     protected IBaseModel createModel(IEditorInput input) throws CoreException {
51         PluginModelBase model = null;
52         if (input instanceof IStorageEditorInput) {
53             boolean isReconciling = input instanceof IFileEditorInput;
54             IDocument document = getDocumentProvider().getDocument(input);
55             if (fIsFragment) {
56                 model = new FragmentModel(document, isReconciling);
57             } else {
58                 model = new PluginModel(document, isReconciling);
59             }
60             if (input instanceof IFileEditorInput) {
61                 IFile file = ((IFileEditorInput)input).getFile();
62                 model.setUnderlyingResource(file);
63                 model.setCharset(file.getCharset());
64             } else if (input instanceof SystemFileEditorInput){
65                 File JavaDoc file = (File JavaDoc)((SystemFileEditorInput)input).getAdapter(File JavaDoc.class);
66                 model.setInstallLocation(file.getParent());
67                 model.setCharset(getDefaultCharset());
68             } else if (input instanceof JarEntryEditorInput){
69                 File JavaDoc file = (File JavaDoc)((JarEntryEditorInput)input).getAdapter(File JavaDoc.class);
70                 model.setInstallLocation(file.toString());
71                 model.setCharset(getDefaultCharset());
72             } else {
73                 model.setCharset(getDefaultCharset());
74             }
75             model.load();
76         }
77         return model;
78     }
79     
80     /* (non-Javadoc)
81      * @see org.eclipse.pde.internal.ui.editor.InputContext#getId()
82      */

83     public String JavaDoc getId() {
84         return CONTEXT_ID;
85     }
86     public boolean isFragment() {
87         return fIsFragment;
88     }
89     
90     protected void reorderInsertEdits(ArrayList JavaDoc ops) {
91         HashMap JavaDoc map = getOperationTable();
92         Iterator JavaDoc iter = map.keySet().iterator();
93         TextEdit runtimeInsert = null;
94         TextEdit requiresInsert = null;
95         ArrayList JavaDoc extensionPointInserts = new ArrayList JavaDoc();
96         ArrayList JavaDoc extensionInserts = new ArrayList JavaDoc();
97         
98         while (iter.hasNext()) {
99             Object JavaDoc object = iter.next();
100             if (object instanceof IDocumentNode) {
101                 IDocumentNode node = (IDocumentNode)object;
102                 if (node.getParentNode() instanceof PluginBaseNode) {
103                     TextEdit edit = (TextEdit)map.get(node);
104                     if (edit instanceof InsertEdit) {
105                         if (node.getXMLTagName().equals("runtime")) { //$NON-NLS-1$
106
runtimeInsert = edit;
107                         } else if (node.getXMLTagName().equals("requires")) { //$NON-NLS-1$
108
requiresInsert = edit;
109                         } else if (node.getXMLTagName().equals("extension")) { //$NON-NLS-1$
110
extensionInserts.add(edit);
111                         } else if (node.getXMLTagName().equals("extension-point")) { //$NON-NLS-1$
112
extensionPointInserts.add(edit);
113                         }
114                     }
115                 }
116             }
117         }
118         
119         for (int i = 0; i < ops.size(); i++) {
120             TextEdit edit = (TextEdit)ops.get(i);
121             if (edit instanceof InsertEdit) {
122                 if (extensionPointInserts.contains(edit)) {
123                     ops.remove(edit);
124                     ops.add(0, edit);
125                 }
126             }
127         }
128         
129         if (requiresInsert != null) {
130             ops.remove(requiresInsert);
131             ops.add(0, requiresInsert);
132         }
133         
134         if (runtimeInsert != null) {
135             ops.remove(runtimeInsert);
136             ops.add(0, runtimeInsert);
137         }
138     }
139     
140     public void doRevert() {
141         fEditOperations.clear();
142         fOperationTable.clear();
143         fMoveOperations.clear();
144         AbstractEditingModel model = (AbstractEditingModel)getModel();
145         model.reconciled(model.getDocument());
146     }
147     
148     protected String JavaDoc getPartitionName() {
149         return "___plugin_partition"; //$NON-NLS-1$
150
}
151 }
152
Popular Tags