KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > plugin > BundleInputContext


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.plugin;
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.filebuffers.IDocumentSetupParticipant;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.pde.core.IBaseModel;
24 import org.eclipse.pde.core.IModelChangedEvent;
25 import org.eclipse.pde.internal.core.text.AbstractEditingModel;
26 import org.eclipse.pde.internal.core.text.IDocumentKey;
27 import org.eclipse.pde.internal.core.text.bundle.BundleModel;
28 import org.eclipse.pde.internal.core.text.bundle.ManifestHeader;
29 import org.eclipse.pde.internal.core.text.bundle.PDEManifestElement;
30 import org.eclipse.pde.internal.core.text.bundle.PackageFriend;
31 import org.eclipse.pde.internal.core.util.PropertiesUtil;
32 import org.eclipse.pde.internal.ui.editor.JarEntryEditorInput;
33 import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
34 import org.eclipse.pde.internal.ui.editor.SystemFileEditorInput;
35 import org.eclipse.pde.internal.ui.editor.context.ManifestDocumentSetupParticipant;
36 import org.eclipse.pde.internal.ui.editor.context.UTF8InputContext;
37 import org.eclipse.text.edits.DeleteEdit;
38 import org.eclipse.text.edits.InsertEdit;
39 import org.eclipse.text.edits.ReplaceEdit;
40 import org.eclipse.text.edits.TextEdit;
41 import org.eclipse.ui.IEditorInput;
42 import org.eclipse.ui.IFileEditorInput;
43 import org.eclipse.ui.IStorageEditorInput;
44
45 public class BundleInputContext extends UTF8InputContext {
46     public static final String JavaDoc CONTEXT_ID = "bundle-context"; //$NON-NLS-1$
47

48     private HashMap JavaDoc fOperationTable = new HashMap JavaDoc();
49     /**
50      * @param editor
51      * @param input
52      */

53     public BundleInputContext(PDEFormEditor editor, IEditorInput input, boolean primary) {
54         super(editor, input, primary);
55         create();
56     }
57     /* (non-Javadoc)
58      * @see org.eclipse.pde.internal.ui.neweditor.InputContext#createModel(org.eclipse.ui.IEditorInput)
59      */

60     protected IBaseModel createModel(IEditorInput input) throws CoreException {
61         BundleModel model = null;
62         if (input instanceof IStorageEditorInput) {
63             boolean isReconciling = input instanceof IFileEditorInput;
64             IDocument document = getDocumentProvider().getDocument(input);
65             model = new BundleModel(document, isReconciling);
66             if (input instanceof IFileEditorInput) {
67                 IFile file = ((IFileEditorInput)input).getFile();
68                 model.setUnderlyingResource(file);
69                 model.setCharset(file.getCharset());
70             } else if (input instanceof SystemFileEditorInput){
71                 File JavaDoc file = (File JavaDoc)((SystemFileEditorInput)input).getAdapter(File JavaDoc.class);
72                 IPath path = new Path(file.getAbsolutePath()).removeLastSegments(2);
73                 model.setInstallLocation(path.addTrailingSeparator().toString());
74                 model.setCharset(getDefaultCharset());
75             } else if (input instanceof JarEntryEditorInput){
76                 File JavaDoc file = (File JavaDoc)((JarEntryEditorInput)input).getAdapter(File JavaDoc.class);
77                 model.setInstallLocation(file.toString());
78                 model.setCharset(getDefaultCharset());
79             } else {
80                 model.setCharset(getDefaultCharset());
81             }
82             model.load();
83         }
84         return model;
85     }
86
87     /* (non-Javadoc)
88      * @see org.eclipse.pde.internal.ui.neweditor.InputContext#getId()
89      */

90     public String JavaDoc getId() {
91         return CONTEXT_ID;
92     }
93     /* (non-Javadoc)
94      * @see org.eclipse.pde.internal.ui.neweditor.context.InputContext#addTextEditOperation(java.util.ArrayList, org.eclipse.pde.core.IModelChangedEvent)
95      */

96     protected void addTextEditOperation(ArrayList JavaDoc ops, IModelChangedEvent event) {
97         Object JavaDoc[] objects = event.getChangedObjects();
98         if (objects != null) {
99             for (int i = 0; i < objects.length; i++) {
100                 Object JavaDoc object = objects[i];
101                 if (object instanceof PDEManifestElement)
102                     object = ((PDEManifestElement)object).getHeader();
103                 else if (object instanceof PackageFriend)
104                     object = ((PackageFriend)object).getHeader();
105                 
106                 if (object instanceof ManifestHeader) {
107                     ManifestHeader header = (ManifestHeader)object;
108                     TextEdit op = (TextEdit)fOperationTable.get(header);
109                     if (op != null) {
110                         fOperationTable.remove(header);
111                         ops.remove(op);
112                     }
113                     String JavaDoc value = header.getValue();
114                     if (value == null || value.trim().length() == 0) {
115                         deleteKey(header, ops);
116                     } else {
117                         modifyKey(header, ops);
118                     }
119                 }
120             }
121         }
122     }
123     
124     /* (non-Javadoc)
125      * @see org.eclipse.pde.internal.ui.neweditor.context.InputContext#getMoveOperations()
126      */

127     protected TextEdit[] getMoveOperations() {
128         return new TextEdit[0];
129     }
130     
131     private void insertKey(IDocumentKey key, ArrayList JavaDoc ops) {
132         IDocument doc = getDocumentProvider().getDocument(getInput());
133         InsertEdit op = new InsertEdit(PropertiesUtil.getInsertOffset(doc), key.write());
134         fOperationTable.put(key, op);
135         ops.add(op);
136     }
137     
138     private void deleteKey(IDocumentKey key, ArrayList JavaDoc ops) {
139         if (key.getOffset() > 0) {
140             TextEdit op = new DeleteEdit(key.getOffset(), key.getLength());
141             fOperationTable.put(key, op);
142             ops.add(op);
143         }
144     }
145     
146     private void modifyKey(IDocumentKey key, ArrayList JavaDoc ops) {
147         if (key.getOffset() == -1) {
148             insertKey(key, ops);
149         } else {
150             TextEdit op = new ReplaceEdit(key.getOffset(), key.getLength(), key.write());
151             fOperationTable.put(key, op);
152             ops.add(op);
153         }
154     }
155     public void doRevert() {
156         fEditOperations.clear();
157         fOperationTable.clear();
158         AbstractEditingModel model = (AbstractEditingModel)getModel();
159         model.reconciled(model.getDocument());
160     }
161     protected String JavaDoc getPartitionName() {
162         return "___bundle_partition"; //$NON-NLS-1$
163
}
164     
165     protected IDocumentSetupParticipant getDocumentSetupParticipant() {
166         return new ManifestDocumentSetupParticipant();
167     }
168 }
169
Popular Tags