1 4 package org.oddjob.designer.view; 5 6 import java.awt.BorderLayout ; 7 import java.awt.Component ; 8 import java.awt.Container ; 9 import java.awt.Dialog ; 10 import java.awt.Dimension ; 11 import java.awt.Frame ; 12 import java.awt.GridBagConstraints ; 13 import java.awt.Insets ; 14 import java.awt.Window ; 15 import java.awt.event.ActionEvent ; 16 import java.awt.event.FocusEvent ; 17 import java.awt.event.FocusListener ; 18 import java.util.Observable ; 19 import java.util.Observer ; 20 21 import javax.swing.AbstractAction ; 22 import javax.swing.BoxLayout ; 23 import javax.swing.JButton ; 24 import javax.swing.JPanel ; 25 import javax.swing.JScrollPane ; 26 import javax.swing.JTextArea ; 27 import javax.swing.JTextField ; 28 import javax.swing.SwingUtilities ; 29 30 import org.oddjob.designer.Looks; 31 import org.oddjob.designer.model.TextInput; 32 33 36 public class TextInputView implements ViewProducer { 37 38 private final TextInput textInput; 39 private final JTextField text; 40 private final JTextArea textArea; 41 private final JButton button; 42 43 public TextInputView(TextInput ti) { 44 this.textInput = ti; 45 text = new JTextField (Looks.TEXT_FIELD_SIZE); 46 text.addFocusListener(new FocusListener () { 47 public void focusGained(FocusEvent e) { 48 } 49 public void focusLost(FocusEvent e) { 50 textInput.setText(text.getText()); 51 } 52 }); 53 54 textArea = new JTextArea (); 55 textArea.addFocusListener(new FocusListener () { 56 public void focusGained(FocusEvent e) { 57 } 58 public void focusLost(FocusEvent e) { 59 textInput.setText(textArea.getText()); 60 } 61 }); 62 63 textInput.addObserver(new Observer () { 64 public void update(Observable o, Object arg) { 65 updateView(); 66 } 67 }); 68 69 button = new JButton (); 70 button.setAction(new AbstractAction ("...") { 71 public void actionPerformed(ActionEvent e) { 72 Window w = SwingUtilities.windowForComponent(button); 73 Component form = ViewFactory.create(textInput).dialog(); 74 75 ValueDialog valueDialog = null; 76 if (w == null) { 77 valueDialog = new ValueDialog(form); 78 } 79 else { 80 if (w instanceof Frame ) { 81 valueDialog = new ValueDialog((Frame ) w, form); 82 } else { 83 valueDialog = new ValueDialog((Dialog ) w, form); 84 } 85 } 86 valueDialog.pack(); 87 valueDialog.setVisible(true); 88 text.setText(textInput.getText()); 89 } 90 }); 91 updateView(); 92 } 93 94 public Component dialog() { 95 return group(); 96 } 97 98 public Component group() { 99 JScrollPane scroll = new JScrollPane (); 100 scroll.setViewportView(textArea); 101 102 JPanel form = new JPanel (new BorderLayout ()); 103 form.setPreferredSize(new Dimension ( 104 Looks.DETAIL_USABLE_WIDTH - 30, 200)); 105 form.setBorder(Looks.groupBorder(textInput.getTitle())); 106 form.add(scroll, BorderLayout.CENTER); 107 return form; 108 } 109 110 113 public Component detailEdit() { 114 return ViewHelper.createDetailButton(textInput); 115 } 116 117 public Component cell() { 118 JPanel panel = new JPanel (); 119 panel.setLayout(new BoxLayout (panel, BoxLayout.X_AXIS)); 120 121 button.setMargin(new Insets (0, 0, 0, 0)); 122 panel.add(text); 123 panel.add(button); 124 return panel; 125 } 126 127 private void updateView() { 128 text.setText(textInput.getText()); 129 textArea.setText(textInput.getText()); 130 } 131 132 135 public int inline(Container container, int row, int column, 136 boolean selectionInGroup) { 137 GridBagConstraints c = new GridBagConstraints (); 138 139 c.weightx = 1.0; 140 c.weighty = 0.0; 141 142 c.fill = GridBagConstraints.HORIZONTAL; 143 c.anchor = GridBagConstraints.NORTHWEST; 144 145 c.gridx = column; 146 c.gridy = row; 147 148 c.gridwidth = GridBagConstraints.REMAINDER; 149 150 c.insets = new Insets (3, 3, 3, 3); 151 152 container.add(group(), c); 153 154 return row + 1; 155 } 156 157 160 public void setEnabled(boolean enabled) { 161 text.setEnabled(enabled); 162 if (!enabled) { 163 textInput.setText(""); 164 } 165 } 166 167 } 168 | Popular Tags |