001: /*******************************************************************************
002: * Copyright (c) 2000, 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.build;
011:
012: import java.io.File;
013: import java.util.ArrayList;
014: import java.util.HashMap;
015:
016: import org.eclipse.core.resources.IFile;
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.jface.text.IDocument;
019: import org.eclipse.pde.core.IBaseModel;
020: import org.eclipse.pde.core.IModelChangedEvent;
021: import org.eclipse.pde.internal.core.text.AbstractEditingModel;
022: import org.eclipse.pde.internal.core.text.IDocumentKey;
023: import org.eclipse.pde.internal.core.text.build.BuildModel;
024: import org.eclipse.pde.internal.core.util.PropertiesUtil;
025: import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
026: import org.eclipse.pde.internal.ui.editor.SystemFileEditorInput;
027: import org.eclipse.pde.internal.ui.editor.context.InputContext;
028: import org.eclipse.text.edits.DeleteEdit;
029: import org.eclipse.text.edits.InsertEdit;
030: import org.eclipse.text.edits.ReplaceEdit;
031: import org.eclipse.text.edits.TextEdit;
032: import org.eclipse.ui.IEditorInput;
033: import org.eclipse.ui.IFileEditorInput;
034: import org.eclipse.ui.IStorageEditorInput;
035:
036: public class BuildInputContext extends InputContext {
037: public static final String CONTEXT_ID = "build-context"; //$NON-NLS-1$
038:
039: private HashMap fOperationTable = new HashMap();
040:
041: public BuildInputContext(PDEFormEditor editor, IEditorInput input,
042: boolean primary) {
043: super (editor, input, primary);
044: create();
045: }
046:
047: /* (non-Javadoc)
048: * @see org.eclipse.pde.internal.ui.neweditor.context.InputContext#getCharSet()
049: */
050: protected String getDefaultCharset() {
051: return "ISO-8859-1"; //$NON-NLS-1$
052: }
053:
054: protected IBaseModel createModel(IEditorInput input)
055: throws CoreException {
056: BuildModel model = null;
057: if (input instanceof IStorageEditorInput) {
058: boolean isReconciling = input instanceof IFileEditorInput;
059: IDocument document = getDocumentProvider().getDocument(
060: input);
061: model = new BuildModel(document, isReconciling);
062: if (input instanceof IFileEditorInput) {
063: IFile file = ((IFileEditorInput) input).getFile();
064: model.setUnderlyingResource(file);
065: model.setCharset(file.getCharset());
066: } else if (input instanceof SystemFileEditorInput) {
067: File file = (File) ((SystemFileEditorInput) input)
068: .getAdapter(File.class);
069: model.setInstallLocation(file.getParent());
070: model.setCharset(getDefaultCharset());
071: } else {
072: model.setCharset(getDefaultCharset());
073: }
074: model.load();
075: }
076: return model;
077: }
078:
079: /* (non-Javadoc)
080: * @see org.eclipse.pde.internal.ui.neweditor.InputContext#getId()
081: */
082: public String getId() {
083: return CONTEXT_ID;
084: }
085:
086: /* (non-Javadoc)
087: * @see org.eclipse.pde.internal.ui.neweditor.context.InputContext#addTextEditOperation(java.util.ArrayList, org.eclipse.pde.core.IModelChangedEvent)
088: */
089: protected void addTextEditOperation(ArrayList ops,
090: IModelChangedEvent event) {
091: Object[] objects = event.getChangedObjects();
092: for (int i = 0; i < objects.length; i++) {
093: Object object = objects[i];
094: IDocumentKey key = (IDocumentKey) object;
095: TextEdit op = (TextEdit) fOperationTable.get(key);
096: if (op != null) {
097: fOperationTable.remove(key);
098: ops.remove(op);
099: }
100: switch (event.getChangeType()) {
101: case IModelChangedEvent.REMOVE:
102: deleteKey(key, ops);
103: break;
104: case IModelChangedEvent.INSERT:
105: insertKey(key, ops);
106: break;
107: case IModelChangedEvent.CHANGE:
108: modifyKey(key, ops);
109: default:
110: break;
111: }
112: }
113: }
114:
115: private void insertKey(IDocumentKey key, ArrayList ops) {
116: IDocument doc = getDocumentProvider().getDocument(getInput());
117: InsertEdit op = new InsertEdit(PropertiesUtil
118: .getInsertOffset(doc), key.write());
119: fOperationTable.put(key, op);
120: ops.add(op);
121: }
122:
123: private void deleteKey(IDocumentKey key, ArrayList ops) {
124: if (key.getOffset() >= 0) {
125: TextEdit op = new DeleteEdit(key.getOffset(), key
126: .getLength());
127: fOperationTable.put(key, op);
128: ops.add(op);
129: }
130: }
131:
132: private void modifyKey(IDocumentKey key, ArrayList ops) {
133: if (key.getOffset() == -1) {
134: insertKey(key, ops);
135: } else {
136: TextEdit op = new ReplaceEdit(key.getOffset(), key
137: .getLength(), key.write());
138: fOperationTable.put(key, op);
139: ops.add(op);
140: }
141: }
142:
143: public void doRevert() {
144: fEditOperations.clear();
145: fOperationTable.clear();
146: AbstractEditingModel model = (AbstractEditingModel) getModel();
147: model.reconciled(model.getDocument());
148: }
149:
150: protected String getPartitionName() {
151: return "___build_partition"; //$NON-NLS-1$
152: }
153: }
|