View Javadoc
1   /*
2    *  Copyright 2006 the GLXDesktop Project Team, all rights reserved.
3    *  
4    *  This file is part of GLXDesktop Project.
5    *  GLXDesktop Project is free software; you can redistribute it and/or modify
6    *  it under the terms of the GNU General Public License as published by
7    *  the Free Software Foundation; either version 2 of the License.
8    *  3ddesktop-configurator is distributed in the hope that it will be useful,
9    *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   *  GNU General Public License for more details.
12   *  You should have received a copy of the GNU General Public License
13   *  along with GLXDesktop Project; if not, write to the Free Software
14   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
15   */
16  package net.sf.glxdesktop.gui.glade;
17  
18  import java.io.FileInputStream;
19  import java.io.FileNotFoundException;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.util.ResourceBundle;
23  
24  import net.sf.glxdesktop.gui.Messages;
25  
26  import org.gnu.glade.GladeXMLException;
27  import org.gnu.glade.LibGlade;
28  
29  public class LoadGlade {
30  
31  	private LibGlade glade;
32  
33  	public LoadGlade(String templatePath, Object owner)
34  			throws LoadGladeException {
35  		this(templatePath, owner, null);
36  	}
37  
38  	public LoadGlade(String templatePath, Object owner, String root)
39  			throws LoadGladeException {
40  		try {
41  			InputStream inputStream = getGladeTemplateAsStream(templatePath);
42  			glade = new LibGlade(inputStream, owner, root);
43  		} catch (GladeXMLException e) {
44  			throw new LoadGladeException(templatePath + " " + Messages
45  					.getString("LoadGlade.glade_xml_exception"),e); //$NON-NLS-1$
46  		} catch (IOException e) {
47  			throw new LoadGladeException(templatePath + " " + Messages
48  					.getString("LoadGlade.glade_IO_exception"), e); //$NON-NLS-1$
49  		} catch (Exception e) {
50  			// TODO add custom exceptions.
51  			e.printStackTrace();
52  		}	
53  	}
54  
55  	public LibGlade getGlade() {
56  		return glade;
57  	}
58  
59  	public void i18GladeTemplate(ResourceBundle bundle, String[] labels) {
60  		I18GladeTemplate template = new I18GladeTemplate(glade, labels, bundle);
61  		template.localizeTemplate();
62  	}
63  
64  	private InputStream getGladeTemplateAsStream(String file)
65  			throws LoadGladeException {
66  		InputStream inputStream;
67  		try {
68  			inputStream = new FileInputStream(file);
69  			return inputStream;
70  		} catch (FileNotFoundException e) {
71  			inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream(file);
72  			if (inputStream == null) {
73  				throw new LoadGladeException(
74  						Messages.getString("LoadGlade.glade_template") + file + Messages.getString("LoadGlade.glade_template_not_found"), e); //$NON-NLS-1$ //$NON-NLS-2$
75  			}
76  			return inputStream;
77  		}
78  	}
79  }