KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > designer > view > FormFactory


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.designer.view;
5
6 import java.awt.Component JavaDoc;
7 import java.awt.GridBagConstraints JavaDoc;
8 import java.awt.GridBagLayout JavaDoc;
9 import java.awt.Insets JavaDoc;
10 import java.awt.event.ActionEvent JavaDoc;
11 import java.awt.event.FocusEvent JavaDoc;
12 import java.awt.event.FocusListener JavaDoc;
13
14 import javax.swing.AbstractAction JavaDoc;
15 import javax.swing.JMenu JavaDoc;
16 import javax.swing.JMenuItem JavaDoc;
17 import javax.swing.JPanel JavaDoc;
18 import javax.swing.JTextArea JavaDoc;
19
20 import org.oddjob.designer.Looks;
21 import org.oddjob.designer.factory.HierarchyVisitor;
22 import org.oddjob.designer.factory.SimpleHierarchy;
23 import org.oddjob.designer.model.ComponentAction;
24 import org.oddjob.designer.model.FormDefinition;
25 import org.oddjob.designer.model.StandardForm;
26 import org.oddjob.designer.model.TextInput;
27
28 /**
29  *
30  */

31 public class FormFactory {
32     
33     public static Component JavaDoc createForm(FormDefinition formDef) {
34         if (formDef instanceof StandardForm) {
35             StandardForm sf = (StandardForm) formDef;
36             JPanel JavaDoc form = new JPanel JavaDoc();
37             form.setLayout(new GridBagLayout JavaDoc());
38     
39             GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
40             c.weightx = 0.0;
41             c.weighty = 0.0;
42             c.fill = GridBagConstraints.HORIZONTAL;
43             c.anchor = GridBagConstraints.NORTH;
44     
45             c.insets = new Insets JavaDoc(Looks.DETAIL_FORM_BORDER,
46                     Looks.DETAIL_FORM_BORDER,
47                     Looks.DETAIL_FORM_BORDER,
48                     Looks.DETAIL_FORM_BORDER);
49     
50             c.gridx = 0;
51             c.gridy = 0;
52             form.add(Looks.typePanel(sf.getName()), c);
53     
54             for (int i = 0; i < sf.size(); ++i) {
55                 c.gridx = 0;
56                 c.gridy = i + 1;
57                 form.add(ViewFactory.create(sf.getDesignDefinition(i))
58                         .group(), c);
59             }
60             
61             JPanel JavaDoc wrapper = new JPanel JavaDoc(new GridBagLayout JavaDoc());
62             c.anchor = GridBagConstraints.FIRST_LINE_START;
63             c.fill = GridBagConstraints.NONE;
64             c.gridx = 0;
65             c.gridy = 0;
66             c.weightx = 1.0;
67             c.weighty = 1.0;
68             wrapper.add(form, c);
69             return wrapper;
70         }
71         else if (formDef instanceof TextInput){
72             final TextInput ti = (TextInput) formDef;
73             final JTextArea JavaDoc txt = new JTextArea JavaDoc();
74             txt.setText(ti.getText());
75             txt.addFocusListener(new FocusListener JavaDoc() {
76                 public void focusGained(FocusEvent JavaDoc e) {
77                 }
78                 public void focusLost(FocusEvent JavaDoc e) {
79                     ti.setText(txt.getText());
80                 }
81             });
82             JPanel JavaDoc form = new JPanel JavaDoc(new GridBagLayout JavaDoc());
83             GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
84             c.gridx = 0;
85             c.gridy = 0;
86             c.weightx = 1;
87             c.weighty = 1;
88             c.fill = GridBagConstraints.BOTH;
89             form.add(txt, c);
90             return txt;
91         }
92         else {
93             throw new IllegalStateException JavaDoc("FormDef not supported!");
94         }
95     }
96
97     
98     public static void createComponentMenu(JMenu JavaDoc menu, SimpleHierarchy hierarchy) {
99         class MenuCreator implements HierarchyVisitor {
100             final JMenu JavaDoc menu;
101             MenuCreator(JMenu JavaDoc menu) {
102                 this.menu = menu;
103             }
104             public void onHierarchy(SimpleHierarchy hierarchy) {
105                 JMenu JavaDoc next = new JMenu JavaDoc(hierarchy.getName());
106                 MenuCreator nextMC = new MenuCreator(next);
107                 hierarchy.iterate(nextMC);
108                 menu.add(next);
109             }
110             public void onLeaf(Object JavaDoc leaf) {
111                 final ComponentAction delegateAction = (ComponentAction)leaf;
112                 menu.add( new JMenuItem JavaDoc(new AbstractAction JavaDoc(delegateAction.getName()) {
113                     public void actionPerformed(ActionEvent JavaDoc e) {
114                         delegateAction.perform();
115                     }
116                 }));
117             }
118         }
119         MenuCreator mc = new MenuCreator(menu);
120         hierarchy.iterate(mc);
121     }
122 }
123
Popular Tags