1 11 package org.eclipse.pde.internal.ui.editor.build; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 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 CONTEXT_ID = "build-context"; 40 private HashMap fOperationTable = new HashMap (); 41 42 public BuildInputContext(PDEFormEditor editor, IEditorInput input, boolean primary) { 43 super(editor, input, primary); 44 create(); 45 } 46 47 50 protected String getDefaultCharset() { 51 return "ISO-8859-1"; } 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 file = (File )((SystemFileEditorInput)input).getAdapter(File .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 79 public String getId() { 80 return CONTEXT_ID; 81 } 82 83 86 protected void addTextEditOperation(ArrayList ops, IModelChangedEvent event) { 87 Object [] objects = event.getChangedObjects(); 88 for (int i = 0; i < objects.length; i++) { 89 Object 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 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 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 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 getPartitionName() { 143 return "___build_partition"; } 145 } 146 | Popular Tags |