001: package gnu.kawa.swingviews;
002:
003: import gnu.kawa.models.*;
004: import gnu.kawa.models.Box;
005: import javax.swing.*;
006: import javax.swing.text.*;
007: import java.awt.Component;
008: import java.awt.event.*;
009: import gnu.mapping.Procedure;
010:
011: public class SwingDisplay extends Display {
012: static final SwingDisplay instance = new SwingDisplay();
013:
014: public static Display getInstance() {
015: return instance;
016: }
017:
018: public Window makeWindow() {
019: SwingFrame window = new SwingFrame(null, null, null);
020: window.display = this ;
021: return window;
022: }
023:
024: public void addButton(Button model, Object where) {
025: addView(new SwingButton(model), where);
026: }
027:
028: public void addLabel(Label model, Object where) {
029: addView(new SwingLabel(model), where);
030: }
031:
032: public void addImage(DrawImage model, Object where) {
033: addView(new JLabel(new ImageIcon(model.getImage())), where);
034: }
035:
036: public void addText(Text model, Object where) {
037: addView(new JTextField(getSwingDocument(model), null, 50),
038: where);
039: }
040:
041: private static java.util.WeakHashMap documents = null;
042:
043: static synchronized javax.swing.text.Document getSwingDocument(
044: Text model) {
045: if (documents == null)
046: documents = new java.util.WeakHashMap();
047: Object existing = documents.get(model);
048: if (existing != null)
049: return (javax.swing.text.Document) existing;
050: javax.swing.text.Document doc = new javax.swing.text.PlainDocument(
051: new SwingContent(model.buffer));
052: documents.put(model, doc);
053: return doc;
054: }
055:
056: public void addBox(Box model, Object where) {
057: addView(new SwingBox(model, this ), where);
058: }
059:
060: public void addSpacer(Spacer model, Object where) {
061: addView(new javax.swing.Box.Filler(model.getMinimumSize(),
062: model.getPreferredSize(), model.getMaximumSize()),
063: where);
064: }
065:
066: public void addView(Object view, Object where) {
067: ((java.awt.Container) where).add((Component) view);
068: }
069:
070: public static ActionListener makeActionListener(Object command) {
071: if (command instanceof ActionListener)
072: return (ActionListener) command;
073: return new ProcActionListener((Procedure) command);
074: }
075:
076: public Model coerceToModel(Object component) {
077: if (component instanceof Component)
078: return new ComponentModel((Component) component);
079: if (component instanceof Paintable)
080: // Kludge - should create a Viewable. FIXME.
081: return new ComponentModel(new SwingPaintable(
082: (Paintable) component));
083: return super .coerceToModel(component);
084: }
085: }
086:
087: class ProcActionListener implements ActionListener {
088: Procedure proc;
089:
090: public ProcActionListener(Procedure proc) {
091: this .proc = proc;
092: }
093:
094: public void actionPerformed(ActionEvent e) {
095: try {
096: proc.apply1(e);
097: } catch (Throwable ex) {
098: throw new gnu.mapping.WrappedException(ex);
099: }
100: }
101: }
102:
103: class SwingBox extends javax.swing.Box implements ModelListener {
104: Box model;
105:
106: public SwingBox(Box model, Display display) {
107: super (model.getAxis());
108: model.addListener(this );
109: Viewable cellSpacing = model.getCellSpacing();
110: int n = model.getComponentCount();
111: for (int i = 0; i < n; i++) {
112: if (i > 0 && cellSpacing != null)
113: cellSpacing.makeView(display, this );
114: model.getComponent(i).makeView(display, this );
115: }
116: }
117:
118: public void modelUpdated(Model model, Object key) {
119: }
120: }
121:
122: class SwingLabel extends JLabel implements ModelListener {
123: Label model;
124:
125: public SwingLabel(Label model) {
126: this .model = model;
127: String text = model.getText();
128: if (text != null)
129: super .setText(text);
130: model.addListener(this );
131: }
132:
133: public void modelUpdated(Model model, Object key) {
134: if (key == "text" && model == this .model)
135: super .setText(this .model.getText());
136: }
137:
138: public void setText(String text) {
139: if (model == null)
140: super .setText(text);
141: else
142: model.setText(text);
143: }
144: }
145:
146: /** A Model wrapper around a Swing/AWT Component. */
147:
148: class ComponentModel extends Model {
149: Component component;
150:
151: public ComponentModel(Component component) {
152: this .component = component;
153: }
154:
155: public void makeView(Display display, Object where) {
156: display.addView(component, where);
157: }
158: }
|