1 4 package org.oddjob.designer.view; 5 6 import java.awt.Component ; 7 import java.awt.GridBagConstraints ; 8 import java.awt.GridBagLayout ; 9 import java.awt.Insets ; 10 import java.awt.event.ActionEvent ; 11 import java.awt.event.FocusEvent ; 12 import java.awt.event.FocusListener ; 13 14 import javax.swing.AbstractAction ; 15 import javax.swing.JMenu ; 16 import javax.swing.JMenuItem ; 17 import javax.swing.JPanel ; 18 import javax.swing.JTextArea ; 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 31 public class FormFactory { 32 33 public static Component createForm(FormDefinition formDef) { 34 if (formDef instanceof StandardForm) { 35 StandardForm sf = (StandardForm) formDef; 36 JPanel form = new JPanel (); 37 form.setLayout(new GridBagLayout ()); 38 39 GridBagConstraints c = new GridBagConstraints (); 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 (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 wrapper = new JPanel (new GridBagLayout ()); 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 txt = new JTextArea (); 74 txt.setText(ti.getText()); 75 txt.addFocusListener(new FocusListener () { 76 public void focusGained(FocusEvent e) { 77 } 78 public void focusLost(FocusEvent e) { 79 ti.setText(txt.getText()); 80 } 81 }); 82 JPanel form = new JPanel (new GridBagLayout ()); 83 GridBagConstraints c = new GridBagConstraints (); 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 ("FormDef not supported!"); 94 } 95 } 96 97 98 public static void createComponentMenu(JMenu menu, SimpleHierarchy hierarchy) { 99 class MenuCreator implements HierarchyVisitor { 100 final JMenu menu; 101 MenuCreator(JMenu menu) { 102 this.menu = menu; 103 } 104 public void onHierarchy(SimpleHierarchy hierarchy) { 105 JMenu next = new JMenu (hierarchy.getName()); 106 MenuCreator nextMC = new MenuCreator(next); 107 hierarchy.iterate(nextMC); 108 menu.add(next); 109 } 110 public void onLeaf(Object leaf) { 111 final ComponentAction delegateAction = (ComponentAction)leaf; 112 menu.add( new JMenuItem (new AbstractAction (delegateAction.getName()) { 113 public void actionPerformed(ActionEvent e) { 114 delegateAction.perform(); 115 } 116 })); 117 } 118 } 119 MenuCreator mc = new MenuCreator(menu); 120 hierarchy.iterate(mc); 121 } 122 } 123 | Popular Tags |