001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.nls;
011:
012: import java.io.IOException;
013: import java.io.InputStream;
014: import java.util.ArrayList;
015: import java.util.Properties;
016:
017: import org.eclipse.core.resources.IFile;
018: import org.eclipse.core.resources.IProject;
019: import org.eclipse.core.runtime.CoreException;
020: import org.eclipse.pde.core.IModel;
021: import org.eclipse.pde.core.plugin.IPluginModelBase;
022: import org.eclipse.pde.internal.core.PDEManager;
023:
024: public class ModelChange {
025:
026: private static final String DEFAULT_LOCALIZATION_PREFIX = "plugin"; //$NON-NLS-1$
027: public static final String LOCALIZATION_FILE_SUFFIX = ".properties"; //$NON-NLS-1$
028:
029: private ModelChangeFile fXMLCoupling;
030: private ModelChangeFile fMFCoupling;
031:
032: private IPluginModelBase fParent;
033: private boolean fPreSelected;
034:
035: private String fBundleLocalization;
036: private Properties fProperties;
037: private boolean fReloadProperties = true;
038:
039: protected static boolean modelLoaded(IModel model) {
040: try {
041: model.load();
042: } catch (CoreException e) {
043: }
044: return (model.isLoaded());
045: }
046:
047: public ModelChange(IPluginModelBase parent, boolean preSelected) {
048: fParent = parent;
049: fPreSelected = preSelected;
050: fBundleLocalization = PDEManager.getBundleLocalization(fParent);
051: if (fBundleLocalization == null)
052: fBundleLocalization = DEFAULT_LOCALIZATION_PREFIX;
053: }
054:
055: public void addChange(IFile file, ModelChangeElement change) {
056: if (change == null || file == null)
057: return;
058: String ext = file.getFileExtension();
059: if (ext.equalsIgnoreCase("xml")) //$NON-NLS-1$
060: addXMLChange(file, change);
061: else if (ext.equalsIgnoreCase("MF")) //$NON-NLS-1$
062: addMFChange(file, change);
063: else
064: return;
065: }
066:
067: private void addXMLChange(IFile file, ModelChangeElement change) {
068: if (fXMLCoupling == null) {
069: fXMLCoupling = new ModelChangeFile(file, this );
070: }
071: if (!fXMLCoupling.getFile().equals(file)) {
072: return;
073: }
074: fXMLCoupling.add(change);
075: }
076:
077: private void addMFChange(IFile file, ModelChangeElement change) {
078: if (fMFCoupling == null) {
079: fMFCoupling = new ModelChangeFile(file, this );
080: }
081: fMFCoupling.add(change);
082: }
083:
084: public IFile[] getChangeFiles() {
085: IFile xmlFile = fXMLCoupling != null ? fXMLCoupling.getFile()
086: : null;
087: IFile mfFile = fMFCoupling != null ? fMFCoupling.getFile()
088: : null;
089: if (xmlFile != null && mfFile != null)
090: return new IFile[] { xmlFile, mfFile };
091: if (xmlFile != null)
092: return new IFile[] { xmlFile };
093: if (mfFile != null)
094: return new IFile[] { mfFile };
095: return new IFile[0];
096: }
097:
098: public IFile getPropertiesFile() {
099: IProject project = fParent.getUnderlyingResource().getProject();
100: return project.getFile(getBundleLocalization()
101: + LOCALIZATION_FILE_SUFFIX);
102: }
103:
104: public Properties getProperties() {
105: if (fProperties == null || fReloadProperties) {
106: try {
107: fProperties = new Properties();
108: IFile propertiesFile = getPropertiesFile();
109: if (propertiesFile != null && propertiesFile.exists()) {
110: InputStream stream = propertiesFile.getContents();
111: fProperties.load(stream);
112: stream.close();
113: }
114: } catch (CoreException e) {
115: } catch (IOException e) {
116: }
117: fReloadProperties = false;
118: }
119: return fProperties;
120: }
121:
122: public ArrayList getChangesInFile(IFile file) {
123: if (fXMLCoupling != null && file == fXMLCoupling.getFile())
124: return fXMLCoupling.getChanges();
125: if (fMFCoupling != null && file == fMFCoupling.getFile())
126: return fMFCoupling.getChanges();
127: return null;
128: }
129:
130: public int getNumberOfChangesInFile(IFile file) {
131: if (fXMLCoupling != null && file == fXMLCoupling.getFile())
132: return fXMLCoupling.getNumChanges();
133: if (fMFCoupling != null && file == fMFCoupling.getFile())
134: return fMFCoupling.getNumChanges();
135: return 0;
136: }
137:
138: public boolean wasPreSelected() {
139: return fPreSelected;
140: }
141:
142: public IPluginModelBase getParentModel() {
143: return fParent;
144: }
145:
146: public ModelChangeFile[] getModelChangeFiles() {
147: if (fXMLCoupling != null && fMFCoupling != null)
148: return new ModelChangeFile[] { fXMLCoupling, fMFCoupling };
149: if (fXMLCoupling != null)
150: return new ModelChangeFile[] { fXMLCoupling };
151: if (fMFCoupling != null)
152: return new ModelChangeFile[] { fMFCoupling };
153: return new ModelChangeFile[0];
154: }
155:
156: public void setBundleLocalization(String bundleLocalization) {
157: if (bundleLocalization == null
158: || bundleLocalization
159: .endsWith(LOCALIZATION_FILE_SUFFIX))
160: throw new IllegalArgumentException();
161: if (bundleLocalization.equals(fBundleLocalization))
162: return;
163: fBundleLocalization = bundleLocalization;
164: fReloadProperties = true;
165: }
166:
167: public String getBundleLocalization() {
168: return fBundleLocalization;
169: }
170:
171: public boolean localizationSet() {
172: String localization = PDEManager.getBundleLocalization(fParent);
173: return localization != null && localization.length() > 0;
174: }
175: }
|