1 11 package org.eclipse.pde.internal.ui.editor.site; 12 import java.io.BufferedInputStream ; 13 import java.io.ByteArrayInputStream ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.io.PrintWriter ; 17 import java.io.StringWriter ; 18 import java.io.UnsupportedEncodingException ; 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.isite.ISiteModel; 28 import org.eclipse.pde.internal.core.site.ExternalSiteModel; 29 import org.eclipse.pde.internal.core.site.WorkspaceSiteModel; 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.XMLInputContext; 33 import org.eclipse.ui.IEditorInput; 34 import org.eclipse.ui.IFileEditorInput; 35 import org.eclipse.ui.IStorageEditorInput; 36 37 public class SiteInputContext extends XMLInputContext { 38 public static final String CONTEXT_ID = "site-context"; private boolean storageModel=false; 40 44 public SiteInputContext(PDEFormEditor editor, IEditorInput input, 45 boolean primary) { 46 super(editor, input, primary); 47 create(); 48 } 49 50 protected IBaseModel createModel(IEditorInput input) { 51 IBaseModel model = null; 52 if (input instanceof IStorageEditorInput) { 53 InputStream is = null; 54 try { 55 if (input instanceof IFileEditorInput) { 56 IFile file = ((IFileEditorInput) input).getFile(); 57 is = new BufferedInputStream (file.getContents()); 58 model = createWorkspaceModel(file, is, true); 59 } else if (input instanceof IStorageEditorInput) { 60 is = new BufferedInputStream (((IStorageEditorInput) input).getStorage() 61 .getContents()); 62 model = createStorageModel(is); 63 } 64 } catch (CoreException e) { 65 PDEPlugin.logException(e); 66 return null; 67 } 68 } 69 return model; 70 } 71 72 private IBaseModel createWorkspaceModel(IFile file, InputStream stream, 73 boolean editable) { 74 WorkspaceSiteModel model = new WorkspaceSiteModel(file); 75 try { 76 model.setEditable(editable); 77 model.load(stream, false); 78 } catch (CoreException e) { 79 } 80 try { 81 stream.close(); 82 } catch (IOException e) { 83 PDEPlugin.logException(e); 84 } 85 return model; 86 } 87 88 private IBaseModel createStorageModel(InputStream stream) { 89 ExternalSiteModel model = new ExternalSiteModel(); 90 try { 91 model.load(stream, true); 92 } catch (CoreException e) { 93 } finally { 94 try { 95 stream.close(); 96 } catch (IOException e1) { 97 } 98 } 99 return model; 100 } 101 102 public void dispose() { 103 ISiteModel model = (ISiteModel) getModel(); 104 if (storageModel) { 105 model.dispose(); 106 } 107 super.dispose(); 108 } 109 protected void flushModel(IDocument doc) { 110 if (!(getModel() instanceof IEditable)) 114 return; 115 IEditable editableModel = (IEditable) getModel(); 116 if (editableModel.isEditable()==false) return; 117 if (editableModel.isDirty() == false) return; 118 try { 119 StringWriter swriter = new StringWriter (); 120 PrintWriter writer = new PrintWriter (swriter); 121 editableModel.save(writer); 122 writer.flush(); 123 swriter.close(); 124 doc.set(swriter.toString()); 125 } catch (IOException e) { 126 PDEPlugin.logException(e); 127 } 128 } 129 130 protected boolean synchronizeModel(IDocument doc) { 131 ISiteModel model = (ISiteModel) getModel(); 132 boolean cleanModel = true; 133 String text = doc.get(); 134 try { 135 InputStream stream = 136 new ByteArrayInputStream (text.getBytes("UTF8")); try { 138 model.reload(stream, false); 139 } catch (CoreException e) { 140 cleanModel = false; 141 } 142 try { 143 stream.close(); 144 } catch (IOException e) { 145 } 146 } catch (UnsupportedEncodingException e) { 147 PDEPlugin.logException(e); 148 } 149 return cleanModel; 150 } 151 156 public String getId() { 157 return CONTEXT_ID; 158 } 159 165 protected void addTextEditOperation(ArrayList ops, IModelChangedEvent event) { 166 } 167 168 171 protected void reorderInsertEdits(ArrayList ops) { 172 } 173 174 protected String getPartitionName() { 175 return "___site_partition"; } 177 } 178 | Popular Tags |