1 11 package org.eclipse.pde.internal.ui.editor.plugin; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 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 CONTEXT_ID = "bundle-context"; 48 private HashMap fOperationTable = new HashMap (); 49 53 public BundleInputContext(PDEFormEditor editor, IEditorInput input, boolean primary) { 54 super(editor, input, primary); 55 create(); 56 } 57 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 file = (File )((SystemFileEditorInput)input).getAdapter(File .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 file = (File )((JarEntryEditorInput)input).getAdapter(File .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 90 public String getId() { 91 return CONTEXT_ID; 92 } 93 96 protected void addTextEditOperation(ArrayList ops, IModelChangedEvent event) { 97 Object [] objects = event.getChangedObjects(); 98 if (objects != null) { 99 for (int i = 0; i < objects.length; i++) { 100 Object 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 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 127 protected TextEdit[] getMoveOperations() { 128 return new TextEdit[0]; 129 } 130 131 private void insertKey(IDocumentKey key, ArrayList 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 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 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 getPartitionName() { 162 return "___bundle_partition"; } 164 165 protected IDocumentSetupParticipant getDocumentSetupParticipant() { 166 return new ManifestDocumentSetupParticipant(); 167 } 168 } 169 | Popular Tags |