KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > nls > ModelChange


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.nls;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Properties JavaDoc;
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 JavaDoc DEFAULT_LOCALIZATION_PREFIX = "plugin"; //$NON-NLS-1$
29
public static final String JavaDoc LOCALIZATION_FILE_SUFFIX = ".properties"; //$NON-NLS-1$
30

31     private ModelChangeFile fXMLCoupling;
32     private ModelChangeFile fMFCoupling;
33     
34     private IPluginModelBase fParent;
35     private boolean fPreSelected;
36     
37     private String JavaDoc fBundleLocalization;
38     private Properties JavaDoc 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 JavaDoc ext = file.getFileExtension();
61         if (ext.equalsIgnoreCase("xml")) //$NON-NLS-1$
62
addXMLChange(file, change);
63         else if (ext.equalsIgnoreCase("MF")) //$NON-NLS-1$
64
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 JavaDoc getProperties() {
104         if (fProperties == null || fReloadProperties) {
105             try {
106                 fProperties = new Properties JavaDoc();
107                 IFile propertiesFile = getPropertiesFile();
108                 if (propertiesFile != null && propertiesFile.exists()) {
109                     InputStream JavaDoc stream = propertiesFile.getContents();
110                     fProperties.load(stream);
111                     stream.close();
112                 }
113             } catch (CoreException e) {
114             } catch (IOException JavaDoc e) {
115             }
116             fReloadProperties = false;
117         }
118         return fProperties;
119     }
120     
121     public ArrayList JavaDoc 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 JavaDoc bundleLocalization) {
156         if (bundleLocalization == null || bundleLocalization.endsWith(LOCALIZATION_FILE_SUFFIX))
157             throw new IllegalArgumentException JavaDoc();
158         if (bundleLocalization.equals(fBundleLocalization))
159             return;
160         fBundleLocalization = bundleLocalization;
161         fReloadProperties = true;
162     }
163     public String JavaDoc getBundleLocalization() {
164         return fBundleLocalization;
165     }
166     
167     public boolean localizationSet() {
168         String JavaDoc localization = PDEManager.getBundleLocalization(fParent);
169         return localization != null && localization.length() > 0;
170     }
171 }
172
Popular Tags