001: //==============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program is free software; you can redistribute it and/or modify
007: //=== it under the terms of the GNU General Public License as published by
008: //=== the Free Software Foundation; either version 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.fao.gast.gui;
025:
026: import java.io.File;
027: import java.util.ArrayList;
028: import java.util.HashMap;
029: import java.util.List;
030: import javax.swing.Icon;
031: import javax.swing.ImageIcon;
032: import javax.swing.JButton;
033: import jeeves.utils.Xml;
034: import org.fao.gast.gui.panels.FormPanel;
035: import org.jdom.Element;
036:
037: //==============================================================================
038:
039: public class GuiBuilder {
040: //---------------------------------------------------------------------------
041: //---
042: //--- Constructor
043: //---
044: //---------------------------------------------------------------------------
045:
046: public GuiBuilder(String appPath, ViewPanel view, WorkPanel work) {
047: this .appPath = appPath;
048:
049: viewPanel = view;
050: workPanel = work;
051: }
052:
053: //---------------------------------------------------------------------------
054: //---
055: //--- API methods
056: //---
057: //---------------------------------------------------------------------------
058:
059: public void build(String guiFile) throws Exception {
060: Element root = Xml.loadFile(appPath + File.separator + guiFile);
061:
062: packag = root.getChild("class").getAttributeValue("package");
063:
064: for (Object precon : root.getChildren("precon"))
065: addPrecon((Element) precon);
066:
067: for (Object cont : root.getChildren("container"))
068: addContainer((Element) cont);
069: }
070:
071: //---------------------------------------------------------------------------
072: //---
073: //--- Private methods
074: //---
075: //---------------------------------------------------------------------------
076:
077: private void addPrecon(Element precon) {
078: Precon p = new Precon();
079:
080: p.type = precon.getAttributeValue("type");
081: p.image = retrieveImage(precon.getChildText("image"));
082: p.tip = precon.getChildText("tip");
083:
084: hmPrecons.put(p.type, p);
085: }
086:
087: //---------------------------------------------------------------------------
088:
089: private void addContainer(Element cont) throws Exception {
090: String image = cont.getChildText("image");
091: String label = cont.getChildText("label");
092:
093: Object node = viewPanel.addContainer(label,
094: retrieveImage(image));
095:
096: for (Object form : cont.getChildren("form"))
097: addForm(node, (Element) form);
098: }
099:
100: //---------------------------------------------------------------------------
101:
102: private void addForm(Object cont, Element form) throws Exception {
103: String id = form.getChildText("id");
104: String image = form.getChildText("image");
105: String label = form.getChildText("label");
106: String title = form.getChildText("title");
107: String clazz = form.getChildText("class");
108: String descr = form.getChildText("description");
109:
110: FormPanel formPanel = buildForm(clazz);
111: List<JButton> buttons = buildButtons(
112: form.getChildren("button"), formPanel);
113: Precon precon = getPrecon(form.getChild("precon"));
114:
115: formPanel.init(title, descr, buttons, precon.image, precon.tip);
116: workPanel.add(id, formPanel);
117: viewPanel.addForm(cont, id, label, retrieveImage(image));
118: }
119:
120: //---------------------------------------------------------------------------
121:
122: private FormPanel buildForm(String className) throws Exception {
123: Class clazz = Class.forName(packag + "." + className);
124:
125: return (FormPanel) clazz.newInstance();
126: }
127:
128: //---------------------------------------------------------------------------
129:
130: private List<JButton> buildButtons(List buttons, FormPanel form) {
131: ArrayList<JButton> al = new ArrayList<JButton>();
132:
133: for (Object button : buttons)
134: al.add(buildButton((Element) button, form));
135:
136: return al;
137: }
138:
139: //---------------------------------------------------------------------------
140:
141: private JButton buildButton(Element button, FormPanel form) {
142: String image = button.getChildText("image");
143: String label = button.getChildText("label");
144: String action = button.getChildText("action");
145:
146: JButton btn = new JButton(label, retrieveImage(image));
147: btn.setActionCommand(action);
148: btn.addActionListener(form);
149:
150: return btn;
151: }
152:
153: //---------------------------------------------------------------------------
154:
155: private Precon getPrecon(Element precon) {
156: String type = precon.getAttributeValue("type");
157: Precon p = hmPrecons.get(type);
158:
159: return p;
160: }
161:
162: //---------------------------------------------------------------------------
163:
164: private Icon retrieveImage(String image) {
165: if (image == null)
166: return null;
167:
168: if (hmImages.containsKey(image))
169: return hmImages.get(image);
170:
171: ImageIcon icon = new ImageIcon(appPath + "/gast/images/"
172: + image);
173: hmImages.put(image, icon);
174:
175: return icon;
176: }
177:
178: //---------------------------------------------------------------------------
179: //---
180: //--- Variables
181: //---
182: //---------------------------------------------------------------------------
183:
184: private String appPath;
185: private String packag;
186:
187: private ViewPanel viewPanel;
188: private WorkPanel workPanel;
189:
190: private HashMap<String, Icon> hmImages = new HashMap<String, Icon>();
191: private HashMap<String, Precon> hmPrecons = new HashMap<String, Precon>();
192: }
193:
194: //==============================================================================
195:
196: class Precon {
197: public String type;
198: public Icon image;
199: public String tip;
200: }
201:
202: //==============================================================================
|