xref: /AOO41X/main/javainstaller2/src/JavaSetup/org/openoffice/setup/ResourceManager.java (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package org.openoffice.setup;
29 
30 import org.openoffice.setup.SetupData.SetupDataProvider;
31 import java.io.File;
32 import java.net.MalformedURLException;
33 import java.net.URL;
34 import java.util.Enumeration;
35 import java.util.HashMap;
36 import java.util.Locale;
37 import java.util.MissingResourceException;
38 import java.util.PropertyResourceBundle;
39 import java.util.ResourceBundle;
40 import javax.swing.ImageIcon;
41 
42 public class ResourceManager {
43 
44     static PropertyResourceBundle stringResourceBundle;
45     static PropertyResourceBundle fileNameResourceBundle;
46     static HashMap setupFiles = new HashMap();  // required, because it is not possible to set values in fileNameResourceBundle
47 
48     private ResourceManager() {
49     }
50 
51     static public void checkFileExistence(File htmlDirectory) {
52 
53         for (Enumeration e = fileNameResourceBundle.getKeys(); e.hasMoreElements(); ) {
54             String key = (String) e.nextElement();
55             String fileName = (String)(fileNameResourceBundle.getObject(key));
56 
57             if ( ! fileName.endsWith("html") ) {
58                 // no check of existence for non-html files
59                 setupFiles.put(key, fileName);
60                 // System.err.println("Using file: " + fileName);
61             }
62 
63             if ( fileName.endsWith("html") ) {
64                 boolean fileExists = true;
65 
66                 File file = new File(htmlDirectory, fileName);
67                 File newFile = null;
68 
69                 if ( file.exists() ) {
70                     setupFiles.put(key, fileName);
71                     // System.err.println("Using file: " + fileName);
72                 } else {
73                     fileExists = false;
74                     // try to use english version
75                     int pos1 = fileName.lastIndexOf("_");
76 
77                     if ( pos1 > 0 ) {
78                         int pos2 = fileName.lastIndexOf(".");
79                         String newFileName = fileName.substring(0, pos1) + fileName.substring(pos2, fileName.length());
80                         newFile = new File(htmlDirectory, newFileName);
81                         if ( newFile.exists() ) {
82                             fileExists = true;
83                             setupFiles.put(key, newFileName);
84                             // System.err.println("Using file: " + fileName);
85                         } else {
86                             // Introducing fallback to a very special simple html page
87                             String simplePage = "Excuse.html";
88                             File simpleFile = new File(htmlDirectory, simplePage);
89                             if ( simpleFile.exists() ) {
90                                 fileExists = true;
91                                 setupFiles.put(key, simplePage);
92                                 // System.err.println("Using file: " + fileName);
93                             }
94                         }
95                     }
96                 }
97 
98                 if ( ! fileExists ) {
99                     if ( newFile != null ) {
100                         System.err.println("ERROR: Neither file \"" + file.getPath() +
101                                            "\" nor file \"" + newFile.getPath() + "\" do exist!");
102                     } else {
103                         System.err.println("ERROR: File \"" + file.getPath() + "\" does not exist!");
104                     }
105                     System.exit(1);
106                 }
107             }
108         }
109     }
110 
111     static public String getString(String key) {
112         String value = (String)(stringResourceBundle.getObject(key));
113         if (value != null && (value.indexOf('$') >= 0)) {
114             value = SetupDataProvider.replaceMacros(value);
115         }
116         return value;
117     }
118 
119     static public String getFileName(String key) {
120         String value = (String)setupFiles.get(key);
121         // String value = (String)(fileNameResourceBundle.getObject(key));
122         return value;
123     }
124 
125     static public ImageIcon getIcon(String key) {
126 
127         String name = getFileName(key);
128 
129         try {
130             Class c = Class.forName("org.openoffice.setup.ResourceManager");
131             URL url = c.getResource(name);
132             if (url != null) {
133                 return new ImageIcon(url);
134             } else {
135                 System.err.println("Error: file not found: " + name);
136             }
137         } catch (ClassNotFoundException e) {
138             System.err.println(e);
139         }
140 
141         return new ImageIcon();
142     }
143 
144     static public ImageIcon getIconFromPath(File file) {
145 
146         try {
147             URL url = file.toURL();
148             if (url != null) {
149                 return new ImageIcon(url);
150             } else {
151                 System.err.println("Error: file not found: " + file.getPath());
152             }
153         } catch (MalformedURLException e) {
154             System.err.println(e);
155         }
156 
157         return new ImageIcon();
158     }
159 
160     static {
161         Locale locale = Locale.getDefault();
162         System.err.println("System locale: " + locale );
163         try {
164             stringResourceBundle = (PropertyResourceBundle) ResourceBundle.getBundle("org.openoffice.setup.setupstrings", locale);
165             fileNameResourceBundle = (PropertyResourceBundle) ResourceBundle.getBundle("org.openoffice.setup.setupfiles", locale);
166         } catch (MissingResourceException ex) {
167             ex.printStackTrace();
168             System.exit(1);
169         }
170     }
171 }
172