1 11 package org.eclipse.pde.internal.ui.editor.plugin; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 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 CONTEXT_ID = "plugin-context"; 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 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 file = (File )((SystemFileEditorInput)input).getAdapter(File .class); 66 model.setInstallLocation(file.getParent()); 67 model.setCharset(getDefaultCharset()); 68 } else if (input instanceof JarEntryEditorInput){ 69 File file = (File )((JarEntryEditorInput)input).getAdapter(File .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 83 public String getId() { 84 return CONTEXT_ID; 85 } 86 public boolean isFragment() { 87 return fIsFragment; 88 } 89 90 protected void reorderInsertEdits(ArrayList ops) { 91 HashMap map = getOperationTable(); 92 Iterator iter = map.keySet().iterator(); 93 TextEdit runtimeInsert = null; 94 TextEdit requiresInsert = null; 95 ArrayList extensionPointInserts = new ArrayList (); 96 ArrayList extensionInserts = new ArrayList (); 97 98 while (iter.hasNext()) { 99 Object 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")) { runtimeInsert = edit; 107 } else if (node.getXMLTagName().equals("requires")) { requiresInsert = edit; 109 } else if (node.getXMLTagName().equals("extension")) { extensionInserts.add(edit); 111 } else if (node.getXMLTagName().equals("extension-point")) { 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 getPartitionName() { 149 return "___plugin_partition"; } 151 } 152 | Popular Tags |