1 11 package org.eclipse.pde.internal.ui.editor.target; 12 13 import java.io.BufferedInputStream ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.io.PrintWriter ; 17 import java.io.StringWriter ; 18 import java.util.ArrayList ; 19 20 import org.eclipse.core.resources.IFile; 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.jface.text.IDocument; 23 import org.eclipse.pde.core.IBaseModel; 24 import org.eclipse.pde.core.IEditable; 25 import org.eclipse.pde.core.IModelChangedEvent; 26 import org.eclipse.pde.internal.core.itarget.ITargetModel; 27 import org.eclipse.pde.internal.core.target.TargetModel; 28 import org.eclipse.pde.internal.core.target.WorkspaceTargetModel; 29 import org.eclipse.pde.internal.ui.PDEPlugin; 30 import org.eclipse.pde.internal.ui.editor.PDEFormEditor; 31 import org.eclipse.pde.internal.ui.editor.context.UTF8InputContext; 32 import org.eclipse.ui.IEditorInput; 33 import org.eclipse.ui.IFileEditorInput; 34 import org.eclipse.ui.IStorageEditorInput; 35 36 37 public class TargetInputContext extends UTF8InputContext { 38 39 public static final String CONTEXT_ID = "target-context"; 41 public TargetInputContext(PDEFormEditor editor, IEditorInput input,boolean primary) { 42 super(editor, input, primary); 43 create(); 44 } 45 46 49 public String getId() { 50 return CONTEXT_ID; 51 } 52 53 56 protected IBaseModel createModel(IEditorInput input) throws CoreException { 57 ITargetModel model = null; 58 if (input instanceof IStorageEditorInput) { 59 try { 60 if (input instanceof IFileEditorInput) { 61 IFile file = ((IFileEditorInput) input).getFile(); 62 model = new WorkspaceTargetModel(file, true); 63 model.load(); 64 } else if (input instanceof IStorageEditorInput) { 65 InputStream is = new BufferedInputStream (((IStorageEditorInput) input).getStorage().getContents()); 66 model = new TargetModel(); 67 model.load(is, false); 68 } 69 } catch (CoreException e) { 70 PDEPlugin.logException(e); 71 return null; 72 } 73 } 74 return model; 75 } 76 77 80 protected void addTextEditOperation(ArrayList ops, IModelChangedEvent event) { 81 } 82 83 86 protected void flushModel(IDocument doc) { 87 if (!(getModel() instanceof IEditable)) 88 return; 89 IEditable editableModel = (IEditable) getModel(); 90 if (editableModel.isDirty() == false) 91 return; 92 try { 93 StringWriter swriter = new StringWriter (); 94 PrintWriter writer = new PrintWriter (swriter); 95 editableModel.save(writer); 96 writer.flush(); 97 swriter.close(); 98 doc.set(swriter.toString()); 99 } catch (IOException e) { 100 PDEPlugin.logException(e); 101 } 102 } 103 104 protected String getPartitionName() { 105 return "___target_partition"; } 107 108 } 109 | Popular Tags |