001: /* SwingML
002: * Copyright (C) 2002 SwingML Team
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 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
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: *
019: * Authors:
020: * Ezequiel Cuellar <ecuellar@crosslogic.com>
021: * Hamdi Mohs Yusof <hamdi@medical-online.net>
022: *
023: */
024:
025: package org.swingml.component;
026:
027: import java.awt.event.*;
028:
029: import javax.swing.*;
030:
031: import org.swingml.*;
032: import org.swingml.event.*;
033: import org.swingml.model.*;
034: import org.swingml.tablebrowser.ext.*;
035:
036: public class JComboBoxComponent extends JComboBox implements
037: XMLTranslatable, ItemListener, ISwingMLTextContainer {
038:
039: private EventHandler eventHandler = EventHandler.getInstance();
040: private JComboBoxModel model = null;
041:
042: public JComboBoxComponent(JComboBoxModel aModel) {
043: super (aModel.getChildren().toArray());
044: this .model = aModel;
045: super .setName(model.getName());
046: super .setToolTipText(model.getTooltip());
047: super .setSelectedIndex(model.getSelectedIndex());
048: super .addItemListener(this );
049: super .setEditable(aModel.isEditable());
050: super .setEnabled(aModel.isEnabled());
051: }
052:
053: public String getText() {
054: return getXMLValue();
055: }
056:
057: public String getXMLValue() {
058: String theXMLValue = null;
059: ItemModel theItemModel = (ItemModel) super .getSelectedItem();
060: theXMLValue = theItemModel.getText();
061: if (theItemModel.getValue() != null) {
062: theXMLValue = theItemModel.getValue();
063: }
064: return theXMLValue;
065: }
066:
067: /**
068: * Don't allow focus if the field is not editable
069: */
070: public boolean isFocusable() {
071: return isEnabled();
072: }
073:
074: public void itemStateChanged(ItemEvent e) {
075: /*
076: * FIX Troy Tolle: 04.06.2005
077: * Added the ability to determine the difference between the state change
078: * events for the combo box. This event was causing a global event to be
079: * fired on selected and on deselected.
080: */
081: ItemModel modelFromEvent = null;
082: if (e.getItem() instanceof CellDataValue) {
083: for (int i = 0; i < getModel().getSize()
084: && modelFromEvent == null; i++) {
085: ItemModel anItemModel = (ItemModel) getModel()
086: .getElementAt(i);
087: if (anItemModel.getValue().equals(
088: ((CellDataValue) e.getItem()).getValue())) {
089: modelFromEvent = anItemModel;
090: }
091: }
092: } else {
093: modelFromEvent = (ItemModel) e.getItem();
094: }
095:
096: if (e.getStateChange() == ItemEvent.SELECTED) {
097: // Tell the model it is selected
098: modelFromEvent.setSelected(true);
099: this .eventHandler.handleEvent(this .model,
100: Constants.ITEM_STATE_CHANGED_SELECTED, this );
101: } else if (e.getStateChange() == ItemEvent.DESELECTED) {
102: // Tell the model it is not selected
103: modelFromEvent.setSelected(false);
104: this.eventHandler.handleEvent(this.model,
105: Constants.ITEM_STATE_CHANGED_DESELECTED, this);
106: }
107: this.eventHandler.handleEvent(this.model,
108: Constants.ITEM_STATE_CHANGED, this);
109: }
110:
111: }
|