KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > build > BuildInputContext


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.build;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.pde.core.IBaseModel;
21 import org.eclipse.pde.core.IModelChangedEvent;
22 import org.eclipse.pde.internal.core.text.AbstractEditingModel;
23 import org.eclipse.pde.internal.core.text.IDocumentKey;
24 import org.eclipse.pde.internal.core.text.build.BuildModel;
25 import org.eclipse.pde.internal.core.util.PropertiesUtil;
26 import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
27 import org.eclipse.pde.internal.ui.editor.SystemFileEditorInput;
28 import org.eclipse.pde.internal.ui.editor.context.InputContext;
29 import org.eclipse.text.edits.DeleteEdit;
30 import org.eclipse.text.edits.InsertEdit;
31 import org.eclipse.text.edits.ReplaceEdit;
32 import org.eclipse.text.edits.TextEdit;
33 import org.eclipse.ui.IEditorInput;
34 import org.eclipse.ui.IFileEditorInput;
35 import org.eclipse.ui.IStorageEditorInput;
36
37 public class BuildInputContext extends InputContext {
38     public static final String JavaDoc CONTEXT_ID = "build-context"; //$NON-NLS-1$
39

40     private HashMap JavaDoc fOperationTable = new HashMap JavaDoc();
41
42     public BuildInputContext(PDEFormEditor editor, IEditorInput input, boolean primary) {
43         super(editor, input, primary);
44         create();
45     }
46     
47     /* (non-Javadoc)
48      * @see org.eclipse.pde.internal.ui.neweditor.context.InputContext#getCharSet()
49      */

50     protected String JavaDoc getDefaultCharset() {
51         return "ISO-8859-1"; //$NON-NLS-1$
52
}
53
54     protected IBaseModel createModel(IEditorInput input) throws CoreException {
55         BuildModel model = null;
56         if (input instanceof IStorageEditorInput) {
57             boolean isReconciling = input instanceof IFileEditorInput;
58             IDocument document = getDocumentProvider().getDocument(input);
59             model = new BuildModel(document, isReconciling);
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 {
69                 model.setCharset(getDefaultCharset());
70             }
71             model.load();
72         }
73         return model;
74     }
75
76     /* (non-Javadoc)
77      * @see org.eclipse.pde.internal.ui.neweditor.InputContext#getId()
78      */

79     public String JavaDoc getId() {
80         return CONTEXT_ID;
81     }
82
83     /* (non-Javadoc)
84      * @see org.eclipse.pde.internal.ui.neweditor.context.InputContext#addTextEditOperation(java.util.ArrayList, org.eclipse.pde.core.IModelChangedEvent)
85      */

86     protected void addTextEditOperation(ArrayList JavaDoc ops, IModelChangedEvent event) {
87         Object JavaDoc[] objects = event.getChangedObjects();
88         for (int i = 0; i < objects.length; i++) {
89             Object JavaDoc object = objects[i];
90             IDocumentKey key = (IDocumentKey)object;
91             TextEdit op = (TextEdit)fOperationTable.get(key);
92             if (op != null) {
93                 fOperationTable.remove(key);
94                 ops.remove(op);
95             }
96             switch (event.getChangeType()) {
97                 case IModelChangedEvent.REMOVE :
98                     deleteKey(key, ops);
99                     break;
100                 case IModelChangedEvent.INSERT :
101                     insertKey(key, ops);
102                     break;
103                 case IModelChangedEvent.CHANGE :
104                     modifyKey(key, ops);
105                 default:
106                     break;
107             }
108         }
109     }
110     
111     private void insertKey(IDocumentKey key, ArrayList JavaDoc ops) {
112         IDocument doc = getDocumentProvider().getDocument(getInput());
113         InsertEdit op = new InsertEdit(PropertiesUtil.getInsertOffset(doc), key.write());
114         fOperationTable.put(key, op);
115         ops.add(op);
116     }
117     
118     private void deleteKey(IDocumentKey key, ArrayList JavaDoc ops) {
119         if (key.getOffset() >= 0) {
120             TextEdit op = new DeleteEdit(key.getOffset(), key.getLength());
121             fOperationTable.put(key, op);
122             ops.add(op);
123         }
124     }
125     
126     private void modifyKey(IDocumentKey key, ArrayList JavaDoc ops) {
127         if (key.getOffset() == -1) {
128             insertKey(key, ops);
129         } else {
130             TextEdit op = new ReplaceEdit(key.getOffset(), key.getLength(), key.write());
131             fOperationTable.put(key, op);
132             ops.add(op);
133         }
134     }
135     public void doRevert() {
136         fEditOperations.clear();
137         fOperationTable.clear();
138         AbstractEditingModel model = (AbstractEditingModel)getModel();
139         model.reconciled(model.getDocument());
140     }
141
142     protected String JavaDoc getPartitionName() {
143         return "___build_partition"; //$NON-NLS-1$
144
}
145 }
146
Popular Tags