PageUI class similar to original PageUI class prior to code

release. Many fixes with respect to structures in regards to widget
builders that read from XML.
This commit is contained in:
grail%cafebabe.org 1999-02-19 09:02:35 +00:00
Родитель 41f7180ab2
Коммит 8577cb6351
4 изменённых файлов: 240 добавлений и 49 удалений

Просмотреть файл

@ -42,7 +42,8 @@ import javax.swing.event.DocumentListener;
/** /**
* Model for a "page" or what is commonly termed as a panel. * Model for a "page" or what is commonly termed as a panel.
*/ */
public class PageModel { public class PageModel
implements ActionListener {
/** /**
* Hashtable of values the model will use to communicate with other * Hashtable of values the model will use to communicate with other
@ -79,6 +80,9 @@ public class PageModel {
toggle.addItemListener(new ToggleHandler(toggle, key)); toggle.addItemListener(new ToggleHandler(toggle, key));
} }
} }
public void actionPerformed(ActionEvent event) {
}
class ToggleHandler class ToggleHandler
implements ItemListener { implements ItemListener {

137
grendel/ui/PageUI.java Normal file
Просмотреть файл

@ -0,0 +1,137 @@
/* -*- Mode: java; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is the Grendel mail/news client.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are Copyright (C) 1997
* Netscape Communications Corporation. All Rights Reserved.
*
* Created: Giao Nguyen <grail@cafebabe.org>, 13 Jan 1999.
*/
package grendel.ui;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.NoSuchElementException;
import java.awt.Container;
import java.awt.Component;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JFrame;
import javax.swing.JDialog;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.RootPaneContainer;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import com.sun.xml.parser.Resolver;
import com.sun.xml.parser.Parser;
import com.sun.xml.tree.XmlDocument;
import com.sun.xml.tree.XmlDocumentBuilder;
import com.sun.xml.tree.TreeWalker;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class PageUI extends JPanel {
Hashtable table = new Hashtable();
String title;
public PageUI() {
}
public PageUI(URL url, String attribute, String id, PageModel model,
Class reference) {
XMLPageBuilder pb =
new XMLPageBuilder(attribute, id, model, this, reference);
try {
pb.buildFrom(url.openStream());
title = pb.getTitle();
} catch (Exception e) {
}
}
public PageUI(URL url, String attribute, String id, PageModel model) {
XMLPageBuilder pb =
new XMLPageBuilder(attribute, id, model, this);
try {
pb.buildFrom(url.openStream());
title = pb.getTitle();
} catch (Exception e) {
}
}
public void addCtrl(String name, JComponent component) {
if (name != null) table.put(name, component);
add(component);
}
public void addCtrl(String name, JComponent component,
Object constraints) {
if (name != null) table.put(name, component);
add(component, constraints);
}
/**
* Get a component by its name.
*
* @param key the name of the component to retrieve
* @param return the component identiified by the key
*/
public JComponent getCtrlByName(String key) {
return (JComponent)table.get(key);
}
/**
* Get the title of this page.
*
* @return the title for this page
*/
public String getTitle() {
return title;
}
/**
* Store the values set in the input fields of the page.
*/
public void saveAll() {
System.out.println(table.size() + " elements");
}
/**
* Initialize input fields to known values.
*/
public void initAll() {
}
}

Просмотреть файл

@ -25,6 +25,7 @@ import java.io.InputStream;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.Hashtable;
import java.util.Properties; import java.util.Properties;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
@ -39,6 +40,7 @@ import javax.swing.JList;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JDialog; import javax.swing.JDialog;
import javax.swing.ButtonGroup; import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.RootPaneContainer; import javax.swing.RootPaneContainer;
@ -78,12 +80,43 @@ public class XMLPageBuilder extends XMLWidgetBuilder {
static final String layout_attr = "layout"; static final String layout_attr = "layout";
static final int ELEMENT_TYPE = 1; static final int ELEMENT_TYPE = 1;
JPanel component; PageUI component;
String title; String title;
String id; String id;
String attr; String attr;
PageModel model; PageModel model;
Class ref; Hashtable group = new Hashtable();
Hashtable everything = new Hashtable();
/**
* Build a menu builder which operates on XML formatted data
*
* @param attr attribute
* @param id the value of the attribute to have a match
* @param model the page model for the page to be created
* @param panel the PageUI to be used as the basis for this builder
* @param reference the reference point used to find urls
*/
public XMLPageBuilder(String attr, String id, PageModel model,
PageUI panel, Class reference) {
this(attr, id, model, panel);
setReference(reference);
}
/**
* Build a menu builder which operates on XML formatted data
*
* @param attr attribute
* @param id the value of the attribute to have a match
* @param model the page model for the page to be created
* @param panel the PageUI to be used as the basis for this builder
*/
public XMLPageBuilder(String attr, String id, PageModel model,
PageUI panel) {
this(attr, id, model);
component = panel;
}
/** /**
* Build a menu builder which operates on XML formatted data * Build a menu builder which operates on XML formatted data
@ -98,10 +131,6 @@ public class XMLPageBuilder extends XMLWidgetBuilder {
this.model = model; this.model = model;
} }
public void setReference(Class reference) {
ref = reference;
}
/** /**
* Read the input stream and build a menubar from it * Read the input stream and build a menubar from it
* *
@ -132,34 +161,6 @@ public class XMLPageBuilder extends XMLWidgetBuilder {
} }
} }
/**
* Set the element as the item containing configuration for the
* builder
*
* @param config the element containing configuration data
*/
public void setConfiguration(Element config) {
try {
URL linkURL;
// get the string properties
if (config.getAttribute("href") != null
&& config.getAttribute("role").equals("stringprops")
&& config.getTagName().equals("link")) {
if (ref == null) {
linkURL = getClass().getResource(config.getAttribute("href"));
} else {
linkURL = ref.getResource(config.getAttribute("href"));
}
properties = new Properties();
if (linkURL != null) {
properties.load(linkURL.openStream());
}
}
} catch (IOException io) {
io.printStackTrace();
}
}
/** /**
* Build a menu bar from the data in the tree * Build a menu bar from the data in the tree
* *
@ -224,14 +225,14 @@ public class XMLPageBuilder extends XMLWidgetBuilder {
node = node.getFirstChild().getNextSibling(); node = node.getFirstChild().getNextSibling();
processNode(node, component); processNode(node, component);
} else if (tag.equals(panel_tag)) { // panel tag ... meat! } else if (tag.equals(panel_tag)) { // panel tag ... meat!
JPanel panel = new JPanel(); PageUI panel = new PageUI();
// first panel // first panel
if (component == null) { if (component == null) {
component = panel; component = panel;
} }
panel.setLayout(new GridBagLayout()); component.setLayout(new GridBagLayout());
node = node.getFirstChild().getNextSibling(); node = node.getFirstChild().getNextSibling();
processNode(node, panel); processNode(node, component);
} else { } else {
item = buildComponent(current, parent); item = buildComponent(current, parent);
@ -272,8 +273,11 @@ public class XMLPageBuilder extends XMLWidgetBuilder {
// gridwidth // gridwidth
width = current.getAttribute("gridwidth"); width = current.getAttribute("gridwidth");
if (width != null) { if (width != null) {
if (width.trim().equals("remainder")) { width = width.trim();
if (width.equals("remainder")) {
constraints.gridwidth = GridBagConstraints.REMAINDER; constraints.gridwidth = GridBagConstraints.REMAINDER;
} else if (width.equals("relative")) {
constraints.gridwidth = GridBagConstraints.RELATIVE;
} }
} }
@ -304,9 +308,10 @@ public class XMLPageBuilder extends XMLWidgetBuilder {
int column = Integer.parseInt(s); int column = Integer.parseInt(s);
item = new JTextField(column); item = new JTextField(column);
} catch (NumberFormatException nfe) { } catch (NumberFormatException nfe) {
item = new JTextField();
} }
} else { }
if (item == null) {
item = new JTextField(); item = new JTextField();
} }
@ -316,11 +321,26 @@ public class XMLPageBuilder extends XMLWidgetBuilder {
protected JRadioButton buildRadioButton(Element current) { protected JRadioButton buildRadioButton(Element current) {
JRadioButton item = null; JRadioButton item = null;
String label = getReferencedLabel(current, "title"); String label = getReferencedLabel(current, "title");
String group_str = current.getAttribute("group");
// the label
item = new JRadioButton(); item = new JRadioButton();
if (label != null) { if (label != null) {
item.setText(label); item.setText(label);
} }
// button group matters
if (group_str != null) {
ButtonGroup bg = null;
if (group.containsKey(group_str)) {
bg = (ButtonGroup)group.get(group_str);
} else {
bg = new ButtonGroup();
group.put(group_str, bg);
}
bg.add(item);
}
return item; return item;
} }
@ -334,7 +354,12 @@ public class XMLPageBuilder extends XMLWidgetBuilder {
} else if (type.equals("text")) { // text type } else if (type.equals("text")) { // text type
item = buildTextField(current); item = buildTextField(current);
} else if (type.equals("button")) { // buttons } else if (type.equals("button")) { // buttons
item = new JButton(getReferencedLabel(current, "command")); JButton button = new JButton(getReferencedLabel(current, "title"));
button.addActionListener(model);
button.setActionCommand(getReferencedLabel(current, "command"));
item = button;
} else if (type.equals("checkbox")) {
item = new JCheckBox(getReferencedLabel(current, "title"));
} else if (type.equals("jlist")) { } else if (type.equals("jlist")) {
item = buildList(current); item = buildList(current);
} else if (type.equals("custom")) { } else if (type.equals("custom")) {
@ -342,13 +367,17 @@ public class XMLPageBuilder extends XMLWidgetBuilder {
} }
if (item != null && ID != null) { if (item != null && ID != null) {
System.out.println("Adding " + ID + " to list");
model.add(item, ID); model.add(item, ID);
everything.put(ID, item);
} }
return item; return item;
} }
public JComponent getCtrlByName(String key) {
return (JComponent)everything.get(key);
}
protected JList buildList(Element current) { protected JList buildList(Element current) {
JList list = new JList(); JList list = new JList();
@ -375,10 +404,21 @@ public class XMLPageBuilder extends XMLWidgetBuilder {
cons = buildConstraints(current); cons = buildConstraints(current);
} }
if (cons == null) { if (cons == null) { // no constraints
parent.add(item); if (parent instanceof PageUI) {
} else { ((PageUI)parent).addCtrl(current.getAttribute("ID"), item);
parent.add(item, cons); }
else {
parent.add(item);
}
} else { // we have constraints
if (parent instanceof PageUI) {
((PageUI)parent).addCtrl(current.getAttribute("ID"),
item, cons);
}
else {
parent.add(item, cons);
}
} }
} }
@ -443,4 +483,3 @@ public class XMLPageBuilder extends XMLWidgetBuilder {
} }
} }
} }

Просмотреть файл

@ -56,6 +56,15 @@ public abstract class XMLWidgetBuilder {
*/ */
protected abstract void processNode(Node node, JComponent component); protected abstract void processNode(Node node, JComponent component);
/**
* Set the reference point for URL location.
*
* @param ref the reference point for local urls to be loaded from.
*/
public void setReference(Class ref) {
this.ref = ref;
}
/** /**
* Set the element as the item containing configuration for the * Set the element as the item containing configuration for the
* builder. This would usually be the link tag in the head. * builder. This would usually be the link tag in the head.
@ -65,11 +74,13 @@ public abstract class XMLWidgetBuilder {
public void setConfiguration(Element config) { public void setConfiguration(Element config) {
try { try {
URL linkURL; URL linkURL;
Class local = ((ref == null) ? getClass() : ref);;
// get the string properties // get the string properties
if (config.getAttribute("href") != null if (config.getAttribute("href") != null
&& config.getAttribute("role").equals("stringprops") && config.getAttribute("role").equals("stringprops")
&& config.getTagName().equals("link")) { && config.getTagName().equals("link")) {
linkURL = ref.getResource(config.getAttribute("href"));
linkURL = local.getResource(config.getAttribute("href"));
properties = new Properties(); properties = new Properties();
if (linkURL != null) properties.load(linkURL.openStream()); if (linkURL != null) properties.load(linkURL.openStream());
} }