1 11 package org.eclipse.pde.internal.ui.editor.feature; 12 13 import java.io.BufferedInputStream ; 14 import java.io.ByteArrayInputStream ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.io.PrintWriter ; 18 import java.io.StringWriter ; 19 import java.io.UnsupportedEncodingException ; 20 import java.util.ArrayList ; 21 22 import org.eclipse.core.resources.IFile; 23 import org.eclipse.core.resources.IStorage; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.IPath; 26 import org.eclipse.jface.text.IDocument; 27 import org.eclipse.pde.core.IBaseModel; 28 import org.eclipse.pde.core.IEditable; 29 import org.eclipse.pde.core.IModelChangedEvent; 30 import org.eclipse.pde.internal.core.feature.ExternalFeatureModel; 31 import org.eclipse.pde.internal.core.feature.WorkspaceFeatureModel; 32 import org.eclipse.pde.internal.core.ifeature.IFeatureModel; 33 import org.eclipse.pde.internal.ui.PDEPlugin; 34 import org.eclipse.pde.internal.ui.editor.PDEFormEditor; 35 import org.eclipse.pde.internal.ui.editor.context.XMLInputContext; 36 import org.eclipse.ui.IEditorInput; 37 import org.eclipse.ui.IFileEditorInput; 38 import org.eclipse.ui.IStorageEditorInput; 39 40 43 public class FeatureInputContext extends XMLInputContext { 44 public static final String CONTEXT_ID="feature-context"; 50 public FeatureInputContext(PDEFormEditor editor, IEditorInput input, 51 boolean primary) { 52 super(editor, input, primary); 53 create(); 54 } 55 58 public String getId() { 59 return CONTEXT_ID; 60 } 61 64 protected IBaseModel createModel(IEditorInput input) throws CoreException { 65 if (input instanceof IFileEditorInput) 66 return createResourceModel((IFileEditorInput) input); 67 if (input instanceof IStorageEditorInput) 68 return createStorageModel((IStorageEditorInput)input); 69 return null; 70 } 71 72 private IBaseModel createResourceModel(IFileEditorInput input) 73 throws CoreException { 74 IFile file = input.getFile(); 75 WorkspaceFeatureModel model = new WorkspaceFeatureModel(file); 76 model.load(); 77 return model; 78 } 79 80 private IBaseModel createStorageModel(IStorageEditorInput input) throws CoreException { 81 InputStream stream = null; 82 IStorage storage = input.getStorage(); 83 try { 84 stream = new BufferedInputStream (storage.getContents()); 85 } catch (CoreException e) { 86 PDEPlugin.logException(e); 87 return null; 88 } 89 ExternalFeatureModel model = new ExternalFeatureModel(); 90 IPath path = input.getStorage().getFullPath(); 91 model.setInstallLocation(path == null ? "" : path.removeLastSegments(1).toOSString()); try { 93 model.load(stream, false); 94 } catch (CoreException e) { 95 return null; 97 } 98 finally { 99 try { 100 stream.close(); 101 } 102 catch (IOException e) { 103 } 104 } 105 return model; 106 } 107 110 protected void addTextEditOperation(ArrayList ops, IModelChangedEvent event) { 111 } 112 113 protected void flushModel(IDocument doc) { 114 if (!(getModel() instanceof IEditable)) 118 return; 119 IEditable editableModel = (IEditable) getModel(); 120 if (editableModel.isDirty() == false) 121 return; 122 try { 123 StringWriter swriter = new StringWriter (); 124 PrintWriter writer = new PrintWriter (swriter); 125 editableModel.save(writer); 126 writer.flush(); 127 swriter.close(); 128 doc.set(swriter.toString()); 129 } catch (IOException e) { 130 PDEPlugin.logException(e); 131 } 132 } 133 134 protected boolean synchronizeModel(IDocument doc) { 135 IFeatureModel model = (IFeatureModel) getModel(); 136 137 boolean cleanModel = true; 138 String text = doc.get(); 139 try { 140 InputStream stream = 141 new ByteArrayInputStream (text.getBytes("UTF8")); try { 143 model.reload(stream, false); 144 } catch (CoreException e) { 145 cleanModel = false; 146 } 147 try { 148 stream.close(); 149 } catch (IOException e) { 150 } 151 } catch (UnsupportedEncodingException e) { 152 PDEPlugin.logException(e); 153 } 154 return cleanModel; 155 } 156 159 protected void reorderInsertEdits(ArrayList ops) { 160 } 161 protected String getPartitionName() { 162 return "___feature_partition"; } 164 } 165 | Popular Tags |