1 19 24 25 package org.netbeans.core.output2.ui; 26 27 import java.awt.event.FocusEvent ; 28 import java.awt.event.FocusListener ; 29 import org.netbeans.core.output2.Controller; 30 import org.openide.awt.Mnemonics; 31 import org.openide.util.NbBundle; 32 33 import javax.swing.*; 34 import javax.swing.border.Border ; 35 import java.awt.*; 36 import java.awt.event.ActionEvent ; 37 import java.awt.event.ActionListener ; 38 import org.openide.util.Utilities; 39 40 45 final class InputPanel extends JPanel implements ActionListener , FocusListener { 46 public static final String ACTION_EOF = "eof"; public static final String ACTION_NEWTEXT = "text"; 49 private JTextField field = new JTextField(); 50 private JButton eof = 51 new JButton(); 53 private ActionListener listener = null; 54 private JLabel lbl = 55 new JLabel(); 57 58 public InputPanel() { 59 init(); 60 setFocusable(false); 61 } 62 63 public void requestFocus() { 64 field.requestFocus(); 65 } 66 67 public boolean requestFocusInWindow() { 68 return field.requestFocusInWindow(); 69 } 70 71 private void init() { 72 setLayout (new BorderLayout()); 73 74 add (lbl, BorderLayout.WEST); 75 add (field, BorderLayout.CENTER); 76 field.setEditable(true); 77 add (eof, BorderLayout.EAST); 78 79 field.addActionListener(this); 80 eof.addActionListener(this); 81 82 eof.setToolTipText(NbBundle.getMessage(InputPanel.class, "TIP_EOF")); field.setToolTipText(NbBundle.getMessage(InputPanel.class, "TIP_INPUT")); 85 field.getAccessibleContext().setAccessibleName(lbl.getText()); 86 eof.getAccessibleContext().setAccessibleName(eof.getText()); 87 field.getAccessibleContext().setAccessibleDescription(field.getToolTipText()); 88 eof.getAccessibleContext().setAccessibleDescription(eof.getToolTipText()); 89 90 Mnemonics.setLocalizedText(lbl, NbBundle.getMessage(InputPanel.class, "LBL_INPUT")); 91 Mnemonics.setLocalizedText(eof, NbBundle.getMessage(InputPanel.class, "LBL_EOF")); 92 93 Border b = field.getBorder(); 94 field.setBorder (BorderFactory.createCompoundBorder ( 95 BorderFactory.createMatteBorder(3, 3, 3, 3, getBackground()), b)); 96 lbl.setBorder (BorderFactory.createEmptyBorder(0,0,0,5)); 97 98 lbl.setLabelFor(field); 99 100 setBorder (BorderFactory.createCompoundBorder ( 101 BorderFactory.createMatteBorder( 102 1,0,0,0,UIManager.getColor("controlShadow")), 103 BorderFactory.createEmptyBorder (4,4,4,4))); 104 105 field.addFocusListener(this); 106 } 107 108 public void doLayout() { 109 Dimension lblp = lbl.getPreferredSize(); 110 Dimension eofp = eof.getPreferredSize(); 111 Dimension fp = field.getPreferredSize(); 112 113 Insets ins = getInsets(); 114 115 lbl.setBounds (ins.left, ins.top, lblp.width, getHeight() - (ins.top + ins.bottom)); 116 117 int ftop = (getHeight() / 2) - (fp.height / 2); 118 119 int fright = getWidth() - (ins.right + eofp.width); 120 int fleft = ins.left + lblp.width; 121 122 field.setBounds (fleft, ftop, fright - fleft, fp.height); 123 124 int btop = (getHeight() / 2) - (eofp.height / 2); 125 eof.setBounds (fright, btop, getWidth() - (fright + ins.right), eofp.height); 126 } 127 128 public Dimension getPreferredSize() { 129 Dimension lblp = lbl.getPreferredSize(); 130 Dimension eofp = eof.getPreferredSize(); 131 Dimension fp = field.getPreferredSize(); 132 133 Insets ins = getInsets(); 134 135 int h = ins.top + ins.bottom + Math.max (Math.max (lblp.height, eofp.height), fp.height); 136 int w = ins.left + lblp.width + eofp.width + fp.width + ins.right; 137 138 return new Dimension (w, h); 139 } 140 141 public Dimension getMinimumSize() { 142 Dimension lblp = lbl.getMinimumSize(); 143 Dimension eofp = eof.getMinimumSize(); 144 Dimension fp = field.getMinimumSize(); 145 146 Insets ins = getInsets(); 147 148 int h = ins.top + ins.bottom + Math.max (Math.max (lblp.height, eofp.height), fp.height); 149 int w = ins.left + lblp.width + eofp.width + fp.width + ins.right; 150 151 return new Dimension (w, h); 152 } 153 154 155 public String getText() { 156 return field.getText(); 157 } 158 159 public void addActionListener (ActionListener listener) { 160 if (this.listener != null) { 161 throw new IllegalStateException (this.listener + " is already " + "listening"); } 164 this.listener = listener; 165 } 166 167 public void removeActionListener (ActionListener listener) { 168 if (listener != this.listener) { 169 throw new IllegalArgumentException (listener + " is not " + this.listener); 171 } 172 this.listener = null; 173 } 174 175 public void actionPerformed(ActionEvent ae) { 176 ActionEvent e = new ActionEvent (this, ActionEvent.ACTION_PERFORMED, 177 ae.getSource() == eof ? ACTION_EOF : ACTION_NEWTEXT); 178 if (Controller.LOG) Controller.log ("Got action event from " + ae.getSource()); if (Controller.LOG) Controller.log (" Posting event to listener(tab) " + e); 181 if (ae.getSource() == field && field.getText().length() > 0) { 182 field.setSelectionStart(0); 183 field.setSelectionEnd(field.getText().length()); 184 } 185 186 listener.actionPerformed(e); 187 } 188 189 private AbstractOutputTab findOutputTab() { 190 if (getParent() != null) { 191 return (AbstractOutputTab) SwingUtilities.getAncestorOfClass(AbstractOutputTab.class, this); 192 } else { 193 return null; 194 } 195 } 196 197 public void focusGained (FocusEvent fe) { 198 AbstractOutputTab tab = findOutputTab(); 199 if (tab != null) { 200 tab.notifyInputFocusGained(); 201 } 202 } 203 204 public void focusLost (FocusEvent fe) { 205 206 } 207 208 } 209 | Popular Tags |