001: /*******************************************************************************
002: * Copyright (c) 2003, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.plugin;
011:
012: import java.io.File;
013: import java.util.ArrayList;
014: import java.util.HashMap;
015: import java.util.Iterator;
016:
017: import org.eclipse.core.resources.IFile;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.jface.text.IDocument;
020: import org.eclipse.pde.core.IBaseModel;
021: import org.eclipse.pde.internal.core.text.AbstractEditingModel;
022: import org.eclipse.pde.internal.core.text.IDocumentElementNode;
023: import org.eclipse.pde.internal.core.text.plugin.FragmentModel;
024: import org.eclipse.pde.internal.core.text.plugin.PluginBaseNode;
025: import org.eclipse.pde.internal.core.text.plugin.PluginModel;
026: import org.eclipse.pde.internal.core.text.plugin.PluginModelBase;
027: import org.eclipse.pde.internal.ui.editor.JarEntryEditorInput;
028: import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
029: import org.eclipse.pde.internal.ui.editor.SystemFileEditorInput;
030: import org.eclipse.pde.internal.ui.editor.context.XMLInputContext;
031: import org.eclipse.text.edits.InsertEdit;
032: import org.eclipse.text.edits.TextEdit;
033: import org.eclipse.ui.IEditorInput;
034: import org.eclipse.ui.IFileEditorInput;
035: import org.eclipse.ui.IStorageEditorInput;
036:
037: public class PluginInputContext extends XMLInputContext {
038: public static final String CONTEXT_ID = "plugin-context"; //$NON-NLS-1$
039: private boolean fIsFragment;
040:
041: public PluginInputContext(PDEFormEditor editor, IEditorInput input,
042: boolean primary, boolean isFragment) {
043: super (editor, input, primary);
044: fIsFragment = isFragment;
045: create();
046: }
047:
048: /* (non-Javadoc)
049: * @see org.eclipse.pde.internal.ui.editor.InputContext#createModel(org.eclipse.ui.IEditorInput)
050: */
051: protected IBaseModel createModel(IEditorInput input)
052: throws CoreException {
053: PluginModelBase model = null;
054: if (input instanceof IStorageEditorInput) {
055: boolean isReconciling = input instanceof IFileEditorInput;
056: IDocument document = getDocumentProvider().getDocument(
057: input);
058: if (fIsFragment) {
059: model = new FragmentModel(document, isReconciling);
060: } else {
061: model = new PluginModel(document, isReconciling);
062: }
063: if (input instanceof IFileEditorInput) {
064: IFile file = ((IFileEditorInput) input).getFile();
065: model.setUnderlyingResource(file);
066: model.setCharset(file.getCharset());
067: } else if (input instanceof SystemFileEditorInput) {
068: File file = (File) ((SystemFileEditorInput) input)
069: .getAdapter(File.class);
070: model.setInstallLocation(file.getParent());
071: model.setCharset(getDefaultCharset());
072: } else if (input instanceof JarEntryEditorInput) {
073: File file = (File) ((JarEntryEditorInput) input)
074: .getAdapter(File.class);
075: model.setInstallLocation(file.toString());
076: model.setCharset(getDefaultCharset());
077: } else {
078: model.setCharset(getDefaultCharset());
079: }
080: model.load();
081: }
082: return model;
083: }
084:
085: /* (non-Javadoc)
086: * @see org.eclipse.pde.internal.ui.editor.InputContext#getId()
087: */
088: public String getId() {
089: return CONTEXT_ID;
090: }
091:
092: public boolean isFragment() {
093: return fIsFragment;
094: }
095:
096: protected void reorderInsertEdits(ArrayList ops) {
097: HashMap map = getOperationTable();
098: Iterator iter = map.keySet().iterator();
099: TextEdit runtimeInsert = null;
100: TextEdit requiresInsert = null;
101: ArrayList extensionPointInserts = new ArrayList();
102: ArrayList extensionInserts = new ArrayList();
103:
104: while (iter.hasNext()) {
105: Object object = iter.next();
106: if (object instanceof IDocumentElementNode) {
107: IDocumentElementNode node = (IDocumentElementNode) object;
108: if (node.getParentNode() instanceof PluginBaseNode) {
109: TextEdit edit = (TextEdit) map.get(node);
110: if (edit instanceof InsertEdit) {
111: if (node.getXMLTagName().equals("runtime")) { //$NON-NLS-1$
112: runtimeInsert = edit;
113: } else if (node.getXMLTagName().equals(
114: "requires")) { //$NON-NLS-1$
115: requiresInsert = edit;
116: } else if (node.getXMLTagName().equals(
117: "extension")) { //$NON-NLS-1$
118: extensionInserts.add(edit);
119: } else if (node.getXMLTagName().equals(
120: "extension-point")) { //$NON-NLS-1$
121: extensionPointInserts.add(edit);
122: }
123: }
124: }
125: }
126: }
127:
128: for (int i = 0; i < ops.size(); i++) {
129: TextEdit edit = (TextEdit) ops.get(i);
130: if (edit instanceof InsertEdit) {
131: if (extensionPointInserts.contains(edit)) {
132: ops.remove(edit);
133: ops.add(0, edit);
134: }
135: }
136: }
137:
138: if (requiresInsert != null) {
139: ops.remove(requiresInsert);
140: ops.add(0, requiresInsert);
141: }
142:
143: if (runtimeInsert != null) {
144: ops.remove(runtimeInsert);
145: ops.add(0, runtimeInsert);
146: }
147: }
148:
149: public void doRevert() {
150: fEditOperations.clear();
151: fOperationTable.clear();
152: fMoveOperations.clear();
153: AbstractEditingModel model = (AbstractEditingModel) getModel();
154: model.reconciled(model.getDocument());
155: }
156:
157: protected String getPartitionName() {
158: return "___plugin_partition"; //$NON-NLS-1$
159: }
160: }
|