//{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.gadgets.ArrayLayout;
import com.nexirius.framework.swing.CFJRadioButton;
import com.nexirius.util.SortedVector;
import com.nexirius.util.StringVector;
import com.nexirius.util.assertion.Assert;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* This viewer is designed to show a list of Objects in a radio button box.
*
* @author Marcel Baumann
* @see com.nexirius.framework.datamodel.ComboBoxModel
* @see com.nexirius.framework.datamodel.LimitIntModel
*/
public class RadioBoxViewer extends DataViewer {
public static final String s_viewername = "RadioBoxViewer";
CFJRadioButton buttons[] = null;
boolean swap = false;
int hide[] = null;
boolean horizontal = false;
/**
* Creates a viewer associated to the given model
*/
public RadioBoxViewer(LimitIntModel model) {
super(model);
}
/**
* Creates a viewer associated to the given model
*/
public RadioBoxViewer(LimitIntModel model, int hide[]) {
super(model);
this.hide = hide;
}
/**
* Creates a viewer associated to the given model
*/
public RadioBoxViewer(BooleanModel model, boolean swap) {
super(model);
this.swap = swap;
}
public void setHorizontal(boolean horizontal) {
this.horizontal = horizontal;
}
/**
* Returns the associated data model
*/
public DataModel getModel() {
return getDataModel();
}
boolean mustHide(int index) {
if (hide == null) {
return false;
}
for (int i = 0; i < hide.length; ++i) {
if (hide[i] == index) {
return true;
}
}
return false;
}
/**
* Creates the actual ArrayPanel
*/
public void create() {
if (getModel() instanceof LimitIntModel) {
if (!((LimitIntModel) getModel()).getObjectList().hasList()) {
setVector(new StringVector("...", ""));
}
ObjectList list = ((LimitIntModel) getModel()).getObjectList();
JPanel box = new JPanel(new ArrayLayout(horizontal, ArrayLayout.FULL_SIZE));
ButtonGroup group = new ButtonGroup();
buttons = new CFJRadioButton[list.getSize()];
MyActionListener myActionListener = new MyActionListener(group);
Insets margin = new Insets(0, 30, 0, 30);
for (int i = 0; i < list.getSize(); ++i) {
Object o = list.getElementAt(i);
CFJRadioButton button = new CFJRadioButton(getFactory().getClientResource(), o.toString());
button.setMargin(margin);
group.add(button);
if (!mustHide(i)) {
box.add(button);
}
buttons[i] = button;
button.addActionListener(myActionListener);
}
setJComponent(box);
} else if (getModel() instanceof BooleanModel) {
ArrayLayout al = new ArrayLayout(true, ArrayLayout.FULL_SIZE);
JPanel box = new JPanel(al);
ButtonGroup group = new ButtonGroup();
al.setMargin(0);
buttons = new CFJRadioButton[2];
MyBooleanListener myBooleanListener = new MyBooleanListener(group);
SimpleArrayModel labelArray = ((BooleanModel) getModel()).getTrueFalseLabelModel();
String labels[];
if (labelArray == null) {
labels = BooleanModel.TRUE_FALSE;
} else {
labels = new String[2];
labels[0] = labelArray.getItem(0).toString();
labels[1] = labelArray.getItem(1).toString();
}
buttons[0] = new CFJRadioButton(getFactory().getClientResource(), labels[0]);
buttons[1] = new CFJRadioButton(getFactory().getClientResource(), labels[1]);
group.add(buttons[0]);
group.add(buttons[1]);
if (swap) {
box.add(buttons[0]);
box.add(buttons[1]);
} else {
box.add(buttons[1]);
box.add(buttons[0]);
}
buttons[0].addActionListener(myBooleanListener);
buttons[1].addActionListener(myBooleanListener);
setJComponent(box);
}
update();
}
class MyActionListener implements ActionListener {
ButtonGroup group;
MyActionListener(ButtonGroup g) {
group = g;
}
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < buttons.length; ++i) {
if (buttons[i].getModel() == group.getSelection()) {
((LimitIntModel) getModel()).setInt(i);
break;
}
}
}
}
class MyBooleanListener implements ActionListener {
ButtonGroup group;
MyBooleanListener(ButtonGroup g) {
group = g;
}
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < buttons.length; ++i) {
if (buttons[i].getModel() == group.getSelection()) {
((BooleanModel) getModel()).setBoolean(i != 0);
break;
}
}
}
}
public void update() {
if (getModel() instanceof LimitIntModel) {
int newValue = ((LimitIntModel) getModel()).getInt();
if (buttons != null && newValue < buttons.length) {
buttons[newValue].getModel().setSelected(true);
}
} else {
int newValue = 0;
if (((BooleanModel) getModel()).getBoolean()) {
newValue = 1;
}
if (buttons != null && newValue < buttons.length) {
buttons[newValue].getModel().setSelected(true);
}
}
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) getModel()).getObjectList();
l.setVector(v);
}
}