1 11 package org.eclipse.pde.internal.ui.nls; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.util.ArrayList ; 16 import java.util.Properties ; 17 18 import org.eclipse.core.resources.IFile; 19 import org.eclipse.core.resources.IProject; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.pde.core.IModel; 22 import org.eclipse.pde.core.plugin.IPluginModelBase; 23 import org.eclipse.pde.internal.core.PDEManager; 24 25 26 public class ModelChange { 27 28 private static final String DEFAULT_LOCALIZATION_PREFIX = "plugin"; public static final String LOCALIZATION_FILE_SUFFIX = ".properties"; 31 private ModelChangeFile fXMLCoupling; 32 private ModelChangeFile fMFCoupling; 33 34 private IPluginModelBase fParent; 35 private boolean fPreSelected; 36 37 private String fBundleLocalization; 38 private Properties fProperties; 39 private boolean fReloadProperties = true; 40 41 protected static boolean modelLoaded(IModel model) { 42 try { 43 model.load(); 44 } catch (CoreException e) { 45 } 46 return (model.isLoaded()); 47 } 48 49 public ModelChange(IPluginModelBase parent, boolean preSelected) { 50 fParent = parent; 51 fPreSelected = preSelected; 52 fBundleLocalization = PDEManager.getBundleLocalization(fParent); 53 if (fBundleLocalization == null) 54 fBundleLocalization = DEFAULT_LOCALIZATION_PREFIX; 55 } 56 57 public void addChange(IFile file, ModelChangeElement change) { 58 if (change == null || file == null) 59 return; 60 String ext = file.getFileExtension(); 61 if (ext.equalsIgnoreCase("xml")) addXMLChange(file, change); 63 else if (ext.equalsIgnoreCase("MF")) addMFChange(file, change); 65 else 66 return; 67 } 68 69 private void addXMLChange(IFile file, ModelChangeElement change) { 70 if (fXMLCoupling == null) { 71 fXMLCoupling = new ModelChangeFile(file, this); 72 } 73 if (!fXMLCoupling.getFile().equals(file)) { 74 return; 75 } 76 fXMLCoupling.add(change); 77 } 78 79 private void addMFChange(IFile file, ModelChangeElement change) { 80 if (fMFCoupling == null) { 81 fMFCoupling = new ModelChangeFile(file, this); 82 } 83 fMFCoupling.add(change); 84 } 85 86 public IFile[] getChangeFiles() { 87 IFile xmlFile = fXMLCoupling != null ? fXMLCoupling.getFile() : null; 88 IFile mfFile = fMFCoupling != null ? fMFCoupling.getFile() : null; 89 if (xmlFile != null && mfFile != null) 90 return new IFile[] {xmlFile, mfFile}; 91 if (xmlFile != null) 92 return new IFile[] {xmlFile}; 93 if (mfFile != null) 94 return new IFile[] {mfFile}; 95 return new IFile[0]; 96 } 97 98 public IFile getPropertiesFile() { 99 IProject project = fParent.getUnderlyingResource().getProject(); 100 return project.getFile(getBundleLocalization() + LOCALIZATION_FILE_SUFFIX); 101 } 102 103 public Properties getProperties() { 104 if (fProperties == null || fReloadProperties) { 105 try { 106 fProperties = new Properties (); 107 IFile propertiesFile = getPropertiesFile(); 108 if (propertiesFile != null && propertiesFile.exists()) { 109 InputStream stream = propertiesFile.getContents(); 110 fProperties.load(stream); 111 stream.close(); 112 } 113 } catch (CoreException e) { 114 } catch (IOException e) { 115 } 116 fReloadProperties = false; 117 } 118 return fProperties; 119 } 120 121 public ArrayList getChangesInFile(IFile file) { 122 if (fXMLCoupling != null && file == fXMLCoupling.getFile()) 123 return fXMLCoupling.getChanges(); 124 if (fMFCoupling != null && file == fMFCoupling.getFile()) 125 return fMFCoupling.getChanges(); 126 return null; 127 } 128 129 public int getNumberOfChangesInFile(IFile file) { 130 if (fXMLCoupling != null && file == fXMLCoupling.getFile()) 131 return fXMLCoupling.getNumChanges(); 132 if (fMFCoupling != null && file == fMFCoupling.getFile()) 133 return fMFCoupling.getNumChanges(); 134 return 0; 135 } 136 137 public boolean wasPreSelected() { 138 return fPreSelected; 139 } 140 141 public IPluginModelBase getParentModel() { 142 return fParent; 143 } 144 145 public ModelChangeFile[] getModelChangeFiles() { 146 if (fXMLCoupling != null && fMFCoupling != null) 147 return new ModelChangeFile[] {fXMLCoupling, fMFCoupling}; 148 if (fXMLCoupling != null) 149 return new ModelChangeFile[] {fXMLCoupling}; 150 if (fMFCoupling != null) 151 return new ModelChangeFile[] {fMFCoupling}; 152 return new ModelChangeFile[0]; 153 } 154 155 public void setBundleLocalization(String bundleLocalization) { 156 if (bundleLocalization == null || bundleLocalization.endsWith(LOCALIZATION_FILE_SUFFIX)) 157 throw new IllegalArgumentException (); 158 if (bundleLocalization.equals(fBundleLocalization)) 159 return; 160 fBundleLocalization = bundleLocalization; 161 fReloadProperties = true; 162 } 163 public String getBundleLocalization() { 164 return fBundleLocalization; 165 } 166 167 public boolean localizationSet() { 168 String localization = PDEManager.getBundleLocalization(fParent); 169 return localization != null && localization.length() > 0; 170 } 171 } 172 | Popular Tags |