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:
016: import org.eclipse.core.filebuffers.IDocumentSetupParticipant;
017: import org.eclipse.core.resources.IFile;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IPath;
020: import org.eclipse.core.runtime.Path;
021: import org.eclipse.jface.text.IDocument;
022: import org.eclipse.pde.core.IBaseModel;
023: import org.eclipse.pde.core.IModelChangedEvent;
024: import org.eclipse.pde.internal.core.text.AbstractEditingModel;
025: import org.eclipse.pde.internal.core.text.IDocumentKey;
026: import org.eclipse.pde.internal.core.text.bundle.BundleModel;
027: import org.eclipse.pde.internal.core.text.bundle.ManifestHeader;
028: import org.eclipse.pde.internal.core.text.bundle.PDEManifestElement;
029: import org.eclipse.pde.internal.core.text.bundle.PackageFriend;
030: import org.eclipse.pde.internal.core.util.PropertiesUtil;
031: import org.eclipse.pde.internal.ui.editor.JarEntryEditorInput;
032: import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
033: import org.eclipse.pde.internal.ui.editor.SystemFileEditorInput;
034: import org.eclipse.pde.internal.ui.editor.context.ManifestDocumentSetupParticipant;
035: import org.eclipse.pde.internal.ui.editor.context.UTF8InputContext;
036: import org.eclipse.text.edits.DeleteEdit;
037: import org.eclipse.text.edits.InsertEdit;
038: import org.eclipse.text.edits.ReplaceEdit;
039: import org.eclipse.text.edits.TextEdit;
040: import org.eclipse.ui.IEditorInput;
041: import org.eclipse.ui.IFileEditorInput;
042: import org.eclipse.ui.IStorageEditorInput;
043:
044: public class BundleInputContext extends UTF8InputContext {
045: public static final String CONTEXT_ID = "bundle-context"; //$NON-NLS-1$
046:
047: private HashMap fOperationTable = new HashMap();
048:
049: /**
050: * @param editor
051: * @param input
052: */
053: public BundleInputContext(PDEFormEditor editor, IEditorInput input,
054: boolean primary) {
055: super (editor, input, primary);
056: create();
057: }
058:
059: /* (non-Javadoc)
060: * @see org.eclipse.pde.internal.ui.neweditor.InputContext#createModel(org.eclipse.ui.IEditorInput)
061: */
062: protected IBaseModel createModel(IEditorInput input)
063: throws CoreException {
064: BundleModel model = null;
065: if (input instanceof IStorageEditorInput) {
066: boolean isReconciling = input instanceof IFileEditorInput;
067: IDocument document = getDocumentProvider().getDocument(
068: input);
069: model = new BundleModel(document, isReconciling);
070: if (input instanceof IFileEditorInput) {
071: IFile file = ((IFileEditorInput) input).getFile();
072: model.setUnderlyingResource(file);
073: model.setCharset(file.getCharset());
074: } else if (input instanceof SystemFileEditorInput) {
075: File file = (File) ((SystemFileEditorInput) input)
076: .getAdapter(File.class);
077: IPath path = new Path(file.getAbsolutePath())
078: .removeLastSegments(2);
079: model.setInstallLocation(path.addTrailingSeparator()
080: .toString());
081: model.setCharset(getDefaultCharset());
082: } else if (input instanceof JarEntryEditorInput) {
083: File file = (File) ((JarEntryEditorInput) input)
084: .getAdapter(File.class);
085: model.setInstallLocation(file.toString());
086: model.setCharset(getDefaultCharset());
087: } else {
088: model.setCharset(getDefaultCharset());
089: }
090: model.load();
091: }
092: return model;
093: }
094:
095: /* (non-Javadoc)
096: * @see org.eclipse.pde.internal.ui.neweditor.InputContext#getId()
097: */
098: public String getId() {
099: return CONTEXT_ID;
100: }
101:
102: /* (non-Javadoc)
103: * @see org.eclipse.pde.internal.ui.neweditor.context.InputContext#addTextEditOperation(java.util.ArrayList, org.eclipse.pde.core.IModelChangedEvent)
104: */
105: protected void addTextEditOperation(ArrayList ops,
106: IModelChangedEvent event) {
107: Object[] objects = event.getChangedObjects();
108: if (objects != null) {
109: for (int i = 0; i < objects.length; i++) {
110: Object object = objects[i];
111: if (object instanceof PDEManifestElement)
112: object = ((PDEManifestElement) object).getHeader();
113: else if (object instanceof PackageFriend)
114: object = ((PackageFriend) object).getHeader();
115:
116: if (object instanceof ManifestHeader) {
117: ManifestHeader header = (ManifestHeader) object;
118: TextEdit op = (TextEdit) fOperationTable
119: .get(header);
120: if (op != null) {
121: fOperationTable.remove(header);
122: ops.remove(op);
123: }
124: String value = header.getValue();
125: if (value == null || value.trim().length() == 0) {
126: deleteKey(header, ops);
127: } else {
128: modifyKey(header, ops);
129: }
130: }
131: }
132: }
133: }
134:
135: /* (non-Javadoc)
136: * @see org.eclipse.pde.internal.ui.neweditor.context.InputContext#getMoveOperations()
137: */
138: protected TextEdit[] getMoveOperations() {
139: return new TextEdit[0];
140: }
141:
142: private void insertKey(IDocumentKey key, ArrayList ops) {
143: IDocument doc = getDocumentProvider().getDocument(getInput());
144: InsertEdit op = new InsertEdit(PropertiesUtil
145: .getInsertOffset(doc), key.write());
146: fOperationTable.put(key, op);
147: ops.add(op);
148: }
149:
150: private void deleteKey(IDocumentKey key, ArrayList ops) {
151: if (key.getOffset() > 0) {
152: TextEdit op = new DeleteEdit(key.getOffset(), key
153: .getLength());
154: fOperationTable.put(key, op);
155: ops.add(op);
156: }
157: }
158:
159: private void modifyKey(IDocumentKey key, ArrayList ops) {
160: if (key.getOffset() == -1) {
161: insertKey(key, ops);
162: } else {
163: TextEdit op = new ReplaceEdit(key.getOffset(), key
164: .getLength(), key.write());
165: fOperationTable.put(key, op);
166: ops.add(op);
167: }
168: }
169:
170: public void doRevert() {
171: fEditOperations.clear();
172: fOperationTable.clear();
173: AbstractEditingModel model = (AbstractEditingModel) getModel();
174: model.reconciled(model.getDocument());
175: }
176:
177: protected String getPartitionName() {
178: return "___bundle_partition"; //$NON-NLS-1$
179: }
180:
181: protected IDocumentSetupParticipant getDocumentSetupParticipant() {
182: return new ManifestDocumentSetupParticipant();
183: }
184: }
|