1 11 12 package org.eclipse.pde.internal.ui.editor.cheatsheet.comp; 13 14 import java.io.BufferedInputStream ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.io.PrintWriter ; 18 import java.io.StringWriter ; 19 import java.util.ArrayList ; 20 21 import org.eclipse.core.resources.IFile; 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.jface.text.IDocument; 24 import org.eclipse.pde.core.IBaseModel; 25 import org.eclipse.pde.core.IEditable; 26 import org.eclipse.pde.core.IModelChangedEvent; 27 import org.eclipse.pde.internal.core.cheatsheet.comp.CompCSModel; 28 import org.eclipse.pde.internal.core.cheatsheet.comp.CompCSWorkspaceModel; 29 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSModel; 30 import org.eclipse.pde.internal.ui.PDEPlugin; 31 import org.eclipse.pde.internal.ui.editor.PDEFormEditor; 32 import org.eclipse.pde.internal.ui.editor.context.UTF8InputContext; 33 import org.eclipse.ui.IEditorInput; 34 import org.eclipse.ui.IFileEditorInput; 35 import org.eclipse.ui.IStorageEditorInput; 36 37 41 public class CompCSInputContext extends UTF8InputContext { 42 43 public static final String CONTEXT_ID = "compcs-context"; 45 50 public CompCSInputContext(PDEFormEditor editor, IEditorInput input, 51 boolean primary) { 52 super(editor, input, primary); 53 create(); 54 } 55 56 59 protected void addTextEditOperation(ArrayList ops, IModelChangedEvent event) { 60 } 62 63 66 protected IBaseModel createModel(IEditorInput input) throws CoreException { 67 ICompCSModel model = null; 68 if (input instanceof IStorageEditorInput) { 69 try { 70 if (input instanceof IFileEditorInput) { 71 IFile file = ((IFileEditorInput) input).getFile(); 72 model = new CompCSWorkspaceModel(file, true); 73 model.load(); 74 } else if (input instanceof IStorageEditorInput) { 75 InputStream is = new BufferedInputStream ( 76 ((IStorageEditorInput) input).getStorage() 77 .getContents()); 78 model = new CompCSModel(); 79 model.load(is, false); 80 } 81 } catch (CoreException e) { 82 PDEPlugin.logException(e); 83 return null; 84 } 85 } 86 return model; 87 } 88 89 92 public String getId() { 93 return CONTEXT_ID; 94 } 95 96 99 protected String getPartitionName() { 100 return "___compcs_partition"; } 102 103 106 protected void flushModel(IDocument doc) { 107 if ((getModel() instanceof IEditable) == false) { 108 return; 109 } 110 IEditable editableModel = (IEditable)getModel(); 111 if (editableModel.isDirty() == false) { 113 return; 114 } 115 try { 116 StringWriter swriter = new StringWriter (); 117 PrintWriter writer = new PrintWriter (swriter); 118 editableModel.save(writer); 119 writer.flush(); 120 swriter.close(); 121 doc.set(swriter.toString()); 122 } catch (IOException e) { 123 PDEPlugin.logException(e); 124 } 125 } 126 127 } 128 | Popular Tags |