001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.properties.editors;
020:
021: import java.awt.Component;
022: import java.awt.Dimension;
023: import java.awt.FlowLayout;
024: import java.awt.Font;
025: import java.awt.FontMetrics;
026: import java.util.Collection;
027: import java.util.Iterator;
028:
029: import javax.swing.JLabel;
030: import javax.swing.JPanel;
031:
032: import com.jeta.forms.store.properties.ItemsProperty;
033: import com.jeta.open.gui.framework.JETADialog;
034: import com.jeta.open.gui.utils.JETAToolbox;
035: import com.jeta.open.i18n.I18N;
036: import com.jeta.swingbuilder.gui.components.list.ItemsView;
037: import com.jeta.swingbuilder.gui.properties.JETAPropertyEditor;
038:
039: /**
040: * Editor for handling items for a combo box
041: *
042: * @author Jeff Tassin
043: */
044: public class ItemsEditor extends JETAPropertyEditor {
045:
046: /**
047: * The label used to display the items
048: */
049: private JLabel m_items_label;
050:
051: /**
052: * The panel that contains the items label
053: */
054: private JPanel m_panel;
055:
056: /**
057: * ctor
058: */
059: public ItemsEditor() {
060: m_items_label = new JLabel();
061:
062: Font font = javax.swing.UIManager.getFont("Table.font");
063: m_items_label.setFont(font);
064: FontMetrics fm = m_items_label.getFontMetrics(font);
065:
066: /**
067: * We use a text field to get the preferred size of this renderer
068: * instead of relying on the JLabel.
069: */
070: final javax.swing.JTextField tf = new javax.swing.JTextField(
071: "foo");
072: tf.setFont(font);
073: m_panel = new JPanel(new FlowLayout(FlowLayout.LEFT)) {
074: public Dimension getPreferredSize() {
075: return tf.getPreferredSize();
076: }
077: };
078:
079: m_panel.add(m_items_label);
080: m_panel.setOpaque(false);
081: }
082:
083: /**
084: * @return the editor component
085: */
086: public Component getCustomEditor() {
087: return m_panel;
088: }
089:
090: /**
091: * Invokes a dialog used to update the property
092: */
093: public void invokePropertyDialog(Component comp) {
094: JETADialog dlg = (JETADialog) JETAToolbox.createDialog(
095: JETADialog.class, comp, true);
096: Object value = getValue();
097:
098: Collection items = null;
099: if (value instanceof ItemsProperty) {
100: items = ((ItemsProperty) value).getItems();
101: }
102:
103: ItemsView view = new ItemsView(items);
104: dlg.setTitle(I18N.getLocalizedMessage("Items"));
105: dlg.setPrimaryPanel(view);
106: dlg.setSize(dlg.getPreferredSize());
107: dlg.showCenter();
108: if (dlg.isOk()) {
109: setValue(new ItemsProperty(view.getItems()));
110: }
111: }
112:
113: /**
114: * @return true since we have a custom editor dialog for this type
115: */
116: public boolean supportsCustomEditor() {
117: return true;
118: }
119:
120: /**
121: * Override setValue so we can nullify the descriptor for the font
122: */
123: public void setValue(Object value) {
124: super .setValue(value);
125:
126: if (value instanceof ItemsProperty) {
127: ItemsProperty iprop = (ItemsProperty) value;
128:
129: Collection c = iprop.getItems();
130:
131: if (c != null && c.size() > 0) {
132: StringBuffer buff = new StringBuffer();
133: Iterator iter = c.iterator();
134: if (iter.hasNext()) {
135: buff.append(iter.next());
136: }
137: if (iter.hasNext()) {
138: buff.append("...");
139: }
140:
141: m_items_label.setText(buff.toString());
142: } else {
143: m_items_label.setText("");
144: }
145:
146: } else {
147: if (value != null) {
148: m_items_label.setText(value.toString());
149: } else {
150: m_items_label.setText("");
151: }
152: }
153: }
154:
155: }
|