001: package org.gui4j.core.swing;
002:
003: import java.awt.Component;
004: import java.awt.Font;
005: import java.util.Collection;
006: import java.util.Iterator;
007: import java.util.Map;
008:
009: import javax.swing.CellEditor;
010: import javax.swing.DefaultCellEditor;
011: import javax.swing.DefaultComboBoxModel;
012: import javax.swing.DefaultListCellRenderer;
013: import javax.swing.JComboBox;
014: import javax.swing.JList;
015: import javax.swing.plaf.basic.BasicComboBoxEditor;
016:
017: import org.gui4j.Gui4jCallBase;
018: import org.gui4j.core.Gui4jCall;
019: import org.gui4j.core.util.ComboBoxNullItem;
020:
021: public class ComboBoxCellEdit extends DefaultCellEditor {
022: private final JComboBox mComboBox;
023: private final CellComboBoxModel mComboBoxModel;
024: protected final Gui4jCallBase mGui4jController;
025: protected final String mParamName;
026: protected final Gui4jCall mValue;
027: protected final Gui4jCall mStringConvert;
028: private Font mFont;
029: protected Map mParamMap;
030:
031: public static ComboBoxCellEdit getInstance(
032: final Gui4jCallBase gui4jController, final Gui4jCall value,
033: final Gui4jCall stringConvert, final String paramName,
034: Font font) {
035: ComboBoxHorizontalScroll comboBox = new ComboBoxHorizontalScroll();
036: ComboBoxCellEdit comboBoxCellEdit = new ComboBoxCellEdit(
037: comboBox, gui4jController, value, stringConvert,
038: paramName, font);
039: comboBox.setCellEditor(comboBoxCellEdit);
040: return comboBoxCellEdit;
041: }
042:
043: public void setEditable(boolean editable) {
044: mComboBox.setEditable(editable);
045: }
046:
047: public void setContent(Collection collection, String nullItemText) {
048: int idx = mComboBox.getSelectedIndex();
049: mComboBoxModel.setContent(collection, nullItemText);
050: if (idx != -1 && idx < collection.size()) {
051: mComboBox.setSelectedIndex(idx);
052: } else {
053: Object item = getCellEditorValue();
054: idx = mComboBoxModel.getIndexOf(item);
055: if (idx != -1) {
056: mComboBox.setSelectedIndex(idx);
057: } else {
058: }
059: }
060:
061: }
062:
063: public void setParamMap(Map paramMap) {
064: mParamMap = paramMap;
065: }
066:
067: public void setSelectedItem(Object item) {
068: if (item != null) {
069: mComboBox.setSelectedItem(item);
070: } else if (mComboBox.getItemCount() > 0) {
071: for (int index = 0; index < mComboBox.getItemCount(); index++) {
072: if (mComboBox.getItemAt(index) instanceof ComboBoxNullItem) {
073: mComboBox.setSelectedIndex(index);
074: return;
075: }
076: }
077: }
078:
079: }
080:
081: private ComboBoxCellEdit(final JComboBox comboBox,
082: final Gui4jCallBase gui4jController, final Gui4jCall value,
083: final Gui4jCall stringConvert, final String paramName,
084: Font font) {
085: super (comboBox);
086: mFont = font;
087: mGui4jController = gui4jController;
088: mComboBox = comboBox;
089: mParamName = paramName;
090: mValue = value;
091: mStringConvert = stringConvert;
092: ComboBoxCellEditFocus editor = new ComboBoxCellEditFocus();
093: comboBox.setFont(mFont);
094: comboBox.setEditor(editor);
095: editor.setCellEditor(this );
096:
097: final TransformValue transformValue = new TransformValue() {
098: public Object transform(Object pValue) {
099: if (pValue instanceof ComboBoxNullItem) {
100: return ((ComboBoxNullItem) pValue).getText();
101: }
102: if (mParamMap != null) {
103: Object oldValue = mParamMap.get(paramName);
104: mParamMap.put(paramName, pValue);
105: Object val = mValue.getValue(mGui4jController,
106: mParamMap, pValue);
107: if (oldValue == null) {
108: mParamMap.remove(paramName);
109: } else {
110: mParamMap.put(paramName, oldValue);
111: }
112: return val;
113: } else {
114: return pValue;
115: }
116: }
117: };
118:
119: comboBox.setKeySelectionManager(new Gui4jKeySelectionManager(
120: transformValue));
121: DefaultListCellRenderer cellRenderer = new DefaultListCellRenderer() {
122: public Component getListCellRendererComponent(JList list,
123: Object pValue, int index, boolean isSelected,
124: boolean cellHasFocus) {
125: return super .getListCellRendererComponent(list,
126: transformValue.transform(pValue), index,
127: isSelected, cellHasFocus);
128: }
129: };
130: mComboBox.setRenderer(cellRenderer);
131: mComboBoxModel = new CellComboBoxModel();
132: mComboBox.setModel(mComboBoxModel);
133: }
134:
135: public void setFont(Font font) {
136: mFont = font;
137: mComboBox.setFont(font);
138: }
139:
140: // *******************************************************************************
141:
142: private class CellComboBoxModel extends DefaultComboBoxModel {
143: public void setContent(Collection collection,
144: String nullItemText) {
145: removeAllElements();
146: if (nullItemText != null) {
147: addElement(new ComboBoxNullItem(nullItemText));
148: }
149: if (collection != null) {
150: for (Iterator it = collection.iterator(); it.hasNext();) {
151: Object element = it.next();
152: addElement(element);
153: }
154: }
155: }
156:
157: }
158:
159: public final class ComboBoxCellEditFocus extends
160: BasicComboBoxEditor {
161: private final org.gui4j.core.swing.Gui4jCellEditor.CellEditorTextField cellEditorTextField;
162:
163: public ComboBoxCellEditFocus() {
164: super ();
165: cellEditorTextField = new org.gui4j.core.swing.Gui4jCellEditor.CellEditorTextField(
166: false);
167: editor = cellEditorTextField;
168: }
169:
170: /**
171: * @see javax.swing.ComboBoxEditor#getItem()
172: */
173: public Object getItem() {
174: Object object = super .getItem();
175: if (object != null
176: && String.class.isAssignableFrom(object.getClass())
177: && mStringConvert != null) {
178: return mStringConvert.getValueUseDefaultParam(
179: mGui4jController, object, null);
180: }
181: return object;
182: }
183:
184: /**
185: * @see javax.swing.ComboBoxEditor#setItem(Object)
186: */
187: public void setItem(Object anObject) {
188: // System.out.println("setItem: anObject=" + anObject);
189: if ((anObject != null)
190: && (mParamMap != null)
191: && !(String.class.isAssignableFrom(anObject
192: .getClass()))) {
193: mParamMap.put(mParamName, anObject);
194: anObject = mValue.getValue(mGui4jController, mParamMap,
195: null);
196: }
197: super .setItem(anObject);
198: }
199:
200: public void setCellEditor(CellEditor cellEditor) {
201: cellEditorTextField.setCellEditor(cellEditor);
202: }
203: }
204:
205: }
|