1 11 12 package org.eclipse.pde.internal.ui.editor.cheatsheet.simple; 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.simple.SimpleCSModel; 28 import org.eclipse.pde.internal.core.cheatsheet.simple.SimpleCSWorkspaceModel; 29 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModel; 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 SimpleCSInputContext extends UTF8InputContext { 42 43 public static final String CONTEXT_ID = "simplecs-context"; 45 50 public SimpleCSInputContext(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 ISimpleCSModel model = null; 68 if (input instanceof IStorageEditorInput) { 69 try { 70 if (input instanceof IFileEditorInput) { 71 IFile file = ((IFileEditorInput) input).getFile(); 72 model = new SimpleCSWorkspaceModel(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 SimpleCSModel(); 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 "___simplecs_partition"; } 102 103 106 protected void flushModel(IDocument doc) { 107 if (!(getModel() instanceof IEditable)) 108 return; 109 IEditable editableModel = (IEditable) getModel(); 110 if (editableModel.isDirty() == false) 111 return; 112 try { 113 StringWriter swriter = new StringWriter (); 114 PrintWriter writer = new PrintWriter (swriter); 115 editableModel.save(writer); 116 writer.flush(); 117 swriter.close(); 118 doc.set(swriter.toString()); 119 } catch (IOException e) { 120 PDEPlugin.logException(e); 121 } 122 } 123 } 124 | Popular Tags |