Page builder, page model now works. It's ugly. The entire XML widget

building needs help in general.
This commit is contained in:
grail%cafebabe.org 1999-02-18 10:11:44 +00:00
Родитель d3e06b0f12
Коммит c60a823394
3 изменённых файлов: 173 добавлений и 37 удалений

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

@ -32,9 +32,12 @@ import java.text.MessageFormat;
import java.util.Hashtable;
import java.util.ResourceBundle;
import java.io.IOException;
import javax.mail.URLName;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
//import netscape.orion.dialogs.AttrNotFoundException;
//import netscape.orion.dialogs.PageModel;
@ -55,7 +58,9 @@ public class EditHostDialog extends GeneralDialog {
Hashtable fValues = null;
PageUI fPanel;
JPanel fPanel;
EditHostModel model;
class EditHostModel extends PageModel {
public EditHostModel(URLName aURL) {
@ -81,23 +86,7 @@ public class EditHostDialog extends GeneralDialog {
fValues.put(kOtherFieldKey, other ? proto : "");
fValues.put(kHostFieldKey, host == null ? "" : host);
fValues.put(kUserFieldKey, user == null ? "" : user);
}
public Object getAttribute(String aAttrib) throws AttrNotFoundException {
Object res = fValues.get(aAttrib);
if (res == null) {
res = fLabels.getString(aAttrib);
}
if (res == null) {
throw new AttrNotFoundException(aAttrib);
}
return res;
}
public void setAttribute(String aAttrib, Object aValue) {
if (fValues.containsKey(aAttrib)) {
fValues.put(aAttrib, aValue);
}
setStore(fValues);
}
public void actionPerformed(ActionEvent aEvent) {
@ -108,11 +97,13 @@ public class EditHostDialog extends GeneralDialog {
super(aParent);
setModal(true);
EditHostModel model = new EditHostModel(aURL);
model = new EditHostModel(aURL);
// use the XML parser to get the root XML node of the resource tree
XMLNode root = null;
// XMLNode root = null;
URL url = getClass().getResource("dialogs.xml");
/*
try {
root = xml.tree.TreeBuilder.build(url, getClass());
} catch (Exception e) {
@ -122,20 +113,26 @@ public class EditHostDialog extends GeneralDialog {
XMLNode editHost = root.getChild("dialog", "id", "editHost");
fPanel = new PageUI(url, editHost, model);
*/
XMLPageBuilder pb = new XMLPageBuilder("id", "editHost", model);
try {
pb.buildFrom(url.openStream());
fPanel = pb.getComponent();
} catch (IOException io) {
System.out.println(io);
}
JOptionPane actionPanel = new JOptionPane(fPanel,
JOptionPane.PLAIN_MESSAGE,
JOptionPane.OK_CANCEL_OPTION);
actionPanel.addPropertyChangeListener(new OptionListener());
add(actionPanel);
getContentPane().add(actionPanel);
// XXX This is a stupid hack because PageUI doesn't to a resource lookup
// on it's title. Bleh.
String title = fPanel.getTitle();
String title = pb.getTitle();
if (title.charAt(0) == '$') {
try {
title = (String) model.getAttribute(title.substring(1));
} catch (AttrNotFoundException e) {}
title = (String) model.getAttribute(title.substring(1));
}
setTitle(title);
@ -160,20 +157,21 @@ public class EditHostDialog extends GeneralDialog {
if (value == JOptionPane.OK_OPTION) {
// Grab all the values
fPanel.saveAll();
// fPanel.saveAll();
System.out.println(model.getAttribute("imapRadio"));
String proto;
Boolean imap = (Boolean) fValues.get(kIMAPRadioKey);
Boolean pop3 = (Boolean) fValues.get(kPOPRadioKey);
Boolean imap = (Boolean) model.getAttribute(kIMAPRadioKey);
Boolean pop3 = (Boolean) model.getAttribute(kPOPRadioKey);
if (imap.booleanValue()) {
proto = "imap";
} else if (pop3.booleanValue()) {
proto = "pop3";
} else {
proto = (String) fValues.get(kOtherFieldKey);
proto = (String) model.getAttribute(kOtherFieldKey);
}
String host = (String) fValues.get(kHostFieldKey);
String user = (String) fValues.get(kUserFieldKey);
String host = (String) model.getAttribute(kHostFieldKey);
String user = (String) model.getAttribute(kUserFieldKey);
if (user.equals("")) {
user = null;
@ -192,4 +190,11 @@ public class EditHostDialog extends GeneralDialog {
}
}
}
public static void main(String[] args) throws Exception {
javax.swing.JFrame frame = new javax.swing.JFrame("Foo bar");
EditHostDialog d = new EditHostDialog(frame, null);
frame.pack();
frame.setVisible(true);
}
}

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

@ -0,0 +1,130 @@
/* -*- 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>, 10 Feb 1999.
*/
package grendel.ui;
import java.util.Hashtable;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComponent;
import javax.swing.text.JTextComponent;
import javax.swing.JToggleButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
/**
* Model for a "page" or what is commonly termed as a panel.
*/
public class PageModel {
/**
* Hashtable of values the model will use to communicate with other
* objects.
*/
protected Hashtable values;
void setStore(Hashtable values) {
this.values = values;
}
/**
* Get an attribute from the model.
*
* @param attribute the attribute key for the object
*/
public Object getAttribute (String attribute) {
return values.get(attribute);
}
/**
* Puts an attribute into the model.
*/
public void setAttribute(String attribute, Object value) {
values.put(attribute, value);
}
public void add(JComponent comp, String key) {
if (comp instanceof JTextComponent) {
JTextComponent text = (JTextComponent)comp;
text.getDocument().addDocumentListener(new TextHandler(text, key));
} else if (comp instanceof JToggleButton) {
JToggleButton toggle = (JToggleButton)comp;
toggle.addItemListener(new ToggleHandler(toggle, key));
}
}
class ToggleHandler
implements ItemListener {
JToggleButton comp;
String key;
ToggleHandler(JToggleButton button, String key) {
comp = button;
this.key = key;
}
public void itemStateChanged(ItemEvent event) {
System.out.println(comp.isSelected());
}
}
class TextHandler
implements DocumentListener {
JTextComponent comp;
String key;
TextHandler(JTextComponent text, String key) {
comp = text;
this.key = key;
}
public void changedUpdate(DocumentEvent e) {
}
public void insertUpdate(DocumentEvent e) {
updateModel(e);
}
public void removeUpdate(DocumentEvent e) {
updateModel(e);
}
private void updateModel(DocumentEvent event) {
int length = event.getDocument().getLength();
String content;
try {
content = event.getDocument().getText(0, length);
values.put(key, content);
} catch (Exception e) {
// ignore if we can't get the document.
}
}
}
}

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

@ -25,7 +25,6 @@ import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Hashtable;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.NoSuchElementException;
@ -82,18 +81,19 @@ public class XMLPageBuilder extends XMLWidgetBuilder {
String title;
String id;
String attr;
Hashtable input = new Hashtable();
PageModel model;
/**
* 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
*/
public XMLPageBuilder(String attr, String id) {
public XMLPageBuilder(String attr, String id, PageModel model) {
this.attr = attr;
this.id = id;
this.model = model;
}
/**
@ -329,7 +329,7 @@ public class XMLPageBuilder extends XMLWidgetBuilder {
if (item != null && ID != null) {
System.out.println("Adding " + ID + " to list");
input.put(ID, item);
model.add(item, ID);
}
return item;
@ -374,7 +374,8 @@ public class XMLPageBuilder extends XMLWidgetBuilder {
public static void main(String[] args) throws Exception {
javax.swing.JFrame frame = new javax.swing.JFrame("Foo bar");
XMLPageBuilder builder = new XMLPageBuilder(args[0], args[1]);
PageModel model = new PageModel();
XMLPageBuilder builder = new XMLPageBuilder(args[0], args[1], model);
URL url = builder.getClass().getResource("dialogs.xml");
builder.buildFrom(url.openStream());
JPanel panel = builder.getComponent();