//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
*
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.framework.dataviewer;
import com.nexirius.framework.datamodel.*;
import com.nexirius.framework.swing.CFJComboBox;
import com.nexirius.util.SortedVector;
import com.nexirius.util.StringVector;
import com.nexirius.util.assertion.Assert;
import javax.swing.*;
import javax.swing.ComboBoxModel;
import java.awt.*;
/**
* This viewer is designed to show a list of Objects in a drop down selection.
*
* @author Marcel Baumann
* @see com.nexirius.framework.datamodel.ComboBoxModel
* @see com.nexirius.framework.datamodel.LimitIntModel
*/
public class ComboBoxViewer extends DataViewer {
protected ModelFlag selectedFlag = null;
protected Color normalBackground = SystemColor.window;
public static final String s_viewername = "ComboBoxViewer";
/**
* Creates a viewer associated to the given model
*/
public ComboBoxViewer(DataModel model) {
super(model);
}
public ModelFlag getSelectedFlag() {
return selectedFlag;
}
/**
* Use a DataModel flag instead of the selected item mechanism for the combo box selection
* @param selectedFlag
*/
public void setSelectedFlag(ModelFlag selectedFlag) {
this.selectedFlag = selectedFlag;
}
public Color getNormalBackground() {
return normalBackground;
}
public void setNormalBackground(Color normalBackground) {
this.normalBackground = normalBackground;
}
private ComboBoxModel getComboBoxModel() {
if (getDataModel() instanceof ArrayModel) {
return new ArrayModelComboBoxModel((ArrayModel) getDataModel(), selectedFlag);
}
if (getDataModel() instanceof InstanceNameComboBoxModel) {
return new ArrayModelInstanceNameComboBoxModel((InstanceNameComboBoxModel) getDataModel());
}
return ((LimitIntModel) getDataModel()).getObjectList();
}
/**
* Creates the actual Swing JComboBox
*/
public void create() {
ComboBoxModel comboBoxModel = getComboBoxModel();
/*if (!getModel().getObjectList().hasList()) {
setVector(new StringVector("...", ""));
} */
//ObjectList objectList = getModel().getObjectList();
if (getDataModel().getFlag(ModelFlag.TRANSLATE_VALUE)) {
if (comboBoxModel instanceof ObjectList) {
((ObjectList) comboBoxModel).activateTranslation(factory.getClientResource());
}
}
CFJComboBox comboBox = new CFJComboBox(comboBoxModel);
setJComponent(comboBox);
updateUI();
}
public CFJComboBox getComboBox() {
return (CFJComboBox) getJComponent();
}
/**
* Empty implementation
*/
public void update() {
updateUI();
}
/**
* Used for debugging
*/
public String getViewerName() {
return s_viewername;
}
/**
* Assign an object list which is used with the associated LimitIntModel.
*
* @see com.nexirius.framework.datamodel.LimitIntModel
*/
public void setVector(SortedVector v) {
Assert.pre(v != null && v.size() > 0, "The ComboBoxViewer needs a list with at least one element");
ObjectList l = ((LimitIntModel) getDataModel()).getObjectList();
l.setVector(v);
}
/**
* this method is called when a struct layout component was created and the background color
* has to be changed to a specific color (setGray)
*/
public void updateUI() {
if (isCreated()/* && !getJTextComponent().hasFocus() */) {
super.updateUI();
if (getDataModel().isGray()) {
getComboBox().setEnabled(false);
} else {
getComboBox().setEnabled(true);
}
}
}
/**
* This method sets back the original background color of the text field
*/
public void setNormalBackgroundColor() {
if (normalBackground != null) {
getJComponent().setBackground(normalBackground);
}
}
/**
* This method set the background color of the text field to gray.
*/
public void setDisabledBackgroundColor() {
getJComponent().setBackground(SystemColor.textInactiveText);
}
public void dataModelChangeValue(DataModelEvent event) {
super.dataModelChangeValue(event);
if (getDataModel() instanceof InstanceNameComboBoxModel) {
JComboBox jComboBox = (JComboBox) getJComponent();
jComboBox.setSelectedItem(((InstanceNameComboBoxModel) getDataModel()).getSelectedDataModel());
jComboBox.repaint();
}
}
public void dataModelEdit(DataModelEvent event) {
if (event.getId() == DataModelEvent.HIGHLIGHT_CHANGED) {
if (getJComponent() != null && getDataModel() instanceof ArrayModel) {
JComboBox jComboBox = (JComboBox) getJComponent();
jComboBox.setSelectedItem(((ArrayModel) getDataModel()).getHighlightedItem());
jComboBox.repaint();
}
}
}
}