1 11 package org.eclipse.pde.internal.ui.editor.product; 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.iproduct.IProductModel; 27 import org.eclipse.pde.internal.core.product.ProductModel; 28 import org.eclipse.pde.internal.core.product.WorkspaceProductModel; 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 ProductInputContext extends UTF8InputContext { 38 39 public static final String CONTEXT_ID = "product-context"; 41 public ProductInputContext(PDEFormEditor editor, IEditorInput input, 42 boolean primary) { 43 super(editor, input, primary); 44 create(); 45 } 46 47 50 public String getId() { 51 return CONTEXT_ID; 52 } 53 54 57 protected IBaseModel createModel(IEditorInput input) throws CoreException { 58 IProductModel model = null; 59 if (input instanceof IStorageEditorInput) { 60 try { 61 if (input instanceof IFileEditorInput) { 62 IFile file = ((IFileEditorInput) input).getFile(); 63 model = new WorkspaceProductModel(file, true); 64 model.load(); 65 } else if (input instanceof IStorageEditorInput) { 66 InputStream is = new BufferedInputStream (((IStorageEditorInput) input).getStorage() 67 .getContents()); 68 model = new ProductModel(); 69 model.load(is, false); 70 } 71 } catch (CoreException e) { 72 PDEPlugin.logException(e); 73 return null; 74 } 75 } 76 return model; 77 } 78 79 82 protected void addTextEditOperation(ArrayList ops, IModelChangedEvent event) { 83 } 84 85 88 protected void flushModel(IDocument doc) { 89 if (!(getModel() instanceof IEditable)) 90 return; 91 IEditable editableModel = (IEditable) getModel(); 92 if (editableModel.isDirty() == false) 93 return; 94 try { 95 StringWriter swriter = new StringWriter (); 96 PrintWriter writer = new PrintWriter (swriter); 97 editableModel.save(writer); 98 writer.flush(); 99 swriter.close(); 100 doc.set(swriter.toString()); 101 } catch (IOException e) { 102 PDEPlugin.logException(e); 103 } 104 } 105 106 protected String getPartitionName() { 107 return "___prod_partition"; } 109 110 } 111 | Popular Tags |