001: /*
002: * @(#)ShortcutField.java 7/9/2002
003: *
004: * Copyright 2002 - 2002 JIDE Software Inc. All rights reserved.
005: */
006: package com.jidesoft.swing;
007:
008: import com.jidesoft.plaf.UIDefaultsLookup;
009: import com.jidesoft.utils.SystemInfo;
010:
011: import javax.swing.*;
012: import java.awt.*;
013: import java.awt.event.MouseAdapter;
014: import java.awt.event.MouseEvent;
015: import java.lang.reflect.InvocationTargetException;
016: import java.lang.reflect.Method;
017:
018: /**
019: * <code>LabeledTextField</code> is a combo component which includes text field and
020: * an optional JLabel in the front and another optionial AbstractButton at the end.
021: */
022: public class LabeledTextField extends JPanel {
023:
024: protected JTextField _textField;
025: protected JLabel _label;
026: protected AbstractButton _button;
027:
028: protected String _labelText;
029: protected Icon _icon;
030:
031: public LabeledTextField() {
032: this (null, null);
033: }
034:
035: public LabeledTextField(Icon icon) {
036: this (icon, null);
037: }
038:
039: public LabeledTextField(Icon icon, String labelText) {
040: super (new BorderLayout(3, 3));
041: _icon = icon;
042: _labelText = labelText;
043: initComponent();
044: }
045:
046: protected void initComponent() {
047: _label = createLabel();
048: if (_label != null) {
049: _label.addMouseListener(new MouseAdapter() {
050: @Override
051: public void mouseClicked(MouseEvent e) {
052: }
053:
054: @Override
055: public void mousePressed(MouseEvent e) {
056: showMenu();
057: }
058:
059: @Override
060: public void mouseReleased(MouseEvent e) {
061: }
062:
063: protected void showMenu() {
064: if (isEnabled()) {
065: JidePopupMenu menu = createContextMenu();
066: if (menu != null
067: && menu.getComponentCount() > 0) {
068: Point location = _label.getLocation();
069: menu
070: .show(
071: LabeledTextField.this ,
072: location.x
073: + (_label.getIcon() == null ? 1
074: : _label
075: .getIcon()
076: .getIconWidth() / 2),
077: location.y
078: + _label
079: .getHeight()
080: + 1);
081: }
082: }
083: }
084: });
085: }
086:
087: _button = createButton();
088:
089: _textField = createTextField();
090:
091: if (_label != null) {
092: add(_label, BorderLayout.BEFORE_LINE_BEGINS);
093: }
094: add(_textField);
095: if (_button != null) {
096: add(_button, BorderLayout.AFTER_LINE_ENDS);
097: }
098:
099: updateUI();
100: }
101:
102: /**
103: * Creates a text field. By default it will return a JTextField with opaque set to false. Subclass
104: * can override this method to create their own text field such as JFormattedTextField.
105: *
106: * @return a text field.
107: */
108: protected JTextField createTextField() {
109: JTextField textField = new JTextField();
110: SelectAllUtils.install(textField);
111: textField.setOpaque(false);
112: textField.setColumns(20);
113: return textField;
114: }
115:
116: /**
117: * Creates a context menu. The context menu will be shown when user clicks on the label.
118: *
119: * @return a context menu.
120: */
121: protected JidePopupMenu createContextMenu() {
122: return null;
123: }
124:
125: @Override
126: public void updateUI() {
127: super .updateUI();
128: setBorder(BorderFactory.createCompoundBorder(UIDefaultsLookup
129: .getBorder("TextField.border"), BorderFactory
130: .createEmptyBorder(2, 2, 2, 2)));
131: if (isEnabled()) {
132: LookAndFeel.installColors(this , "TextField.background",
133: "TextField.foreground");
134: } else {
135: LookAndFeel.installColors(this ,
136: "TextField.inactiveBackground",
137: "TextField.foreground");
138: }
139: if (_textField != null) {
140: _textField.setBorder(BorderFactory.createEmptyBorder());
141: }
142: }
143:
144: /**
145: * Creates the button that appears after the text field. By default it returns null so there is no button. Subclass can
146: * override it to create their own button. A typical usage of this is to create a browse button to browse a file or directory.
147: *
148: * @return the button.
149: */
150: protected AbstractButton createButton() {
151: return null;
152: }
153:
154: /**
155: * Creates the label that appears before the text field. By default, it only has a search icon.
156: *
157: * @return the label.
158: */
159: protected JLabel createLabel() {
160: JLabel label = new JLabel(_icon);
161: label.setText(_labelText);
162: return label;
163: }
164:
165: /**
166: * Sets the text that appears before the text field.
167: *
168: * @param text the text that appears before the text field.
169: */
170: public void setLabelText(String text) {
171: _labelText = text;
172: if (_label != null) {
173: _label.setText(text);
174: }
175: }
176:
177: /**
178: * Gets the text that appears before the text field.
179: *
180: * @return the text that appears before the text field. By default it's null, meaning no text.
181: */
182: public String getLabelText() {
183: if (_label != null) {
184: return _label.getText();
185: } else {
186: return _labelText;
187: }
188: }
189:
190: /**
191: * Sets the icon that appears before the text field.
192: *
193: * @param icon the icon that appears before the text field.
194: */
195: public void setIcon(Icon icon) {
196: _icon = icon;
197: if (_label != null) {
198: _label.setIcon(icon);
199: }
200: }
201:
202: /**
203: * Gets the icon that appears before the text field.
204: *
205: * @return the icon that appears before the text field.
206: */
207: public Icon getIcon() {
208: if (_label != null) {
209: return _label.getIcon();
210: } else {
211: return _icon;
212: }
213: }
214:
215: /**
216: * Gets the JLabel that appears before text field.
217: *
218: * @return the JLabel that appears before text field.
219: */
220: public JLabel getLabel() {
221: return _label;
222: }
223:
224: /**
225: * Gets the AbstractButton that appears after text field.
226: *
227: * @return the AbstractButton that appears after text field.
228: */
229: public AbstractButton getButton() {
230: return _button;
231: }
232:
233: /**
234: * Sets the number of columns in this TextField, and then invalidate the layout.
235: *
236: * @param columns the number of columns for this text field.
237: */
238: public void setColumns(int columns) {
239: if (getTextField() != null) {
240: getTextField().setColumns(columns);
241: }
242: }
243:
244: /**
245: * Gets the actual text field.
246: *
247: * @return the actual text field.
248: */
249: public JTextField getTextField() {
250: return _textField;
251: }
252:
253: @Override
254: public void setEnabled(boolean enabled) {
255: super .setEnabled(enabled);
256: if (enabled) {
257: if (getTextField() != null) {
258: getTextField().setEnabled(true);
259: }
260: if (getLabel() != null) {
261: getLabel().setEnabled(true);
262: }
263: if (getButton() != null) {
264: getButton().setEnabled(true);
265: }
266: setBackground(UIDefaultsLookup
267: .getColor("TextField.background"));
268: } else {
269: if (getTextField() != null) {
270: getTextField().setEnabled(false);
271: }
272: if (getLabel() != null) {
273: getLabel().setEnabled(false);
274: }
275: if (getButton() != null) {
276: getButton().setEnabled(false);
277: }
278: setBackground(UIDefaultsLookup.getColor("control"));
279: }
280: }
281:
282: public int getBaseline(int width, int height) {
283: if (SystemInfo.isJdk6Above()) {
284: try {
285: Method method = Component.class.getMethod(
286: "getBaseline", new Class[] { int.class,
287: int.class });
288: Object value = method.invoke(_textField, width, height);
289: if (value instanceof Integer) {
290: return (Integer) value;
291: }
292: } catch (NoSuchMethodException e) {
293: e.printStackTrace();
294: } catch (IllegalAccessException e) {
295: e.printStackTrace();
296: } catch (InvocationTargetException e) {
297: e.printStackTrace();
298: }
299: }
300: return -1;
301: }
302: }
|