/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package DisplayProject.actions;
import java.awt.Component;
import java.util.Enumeration;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTabbedPane;
import DisplayProject.DropListModel;
import DisplayProject.ListField;
import DisplayProject.PaletteList;
import DisplayProject.RadioList;
import DisplayProject.controls.DropList;
import DisplayProject.controls.FillInField;
import DisplayProject.controls.MenuList;
import DisplayProject.controls.ScrollList;
import DisplayProject.controls.TabFolder;
import Framework.ErrorMgr;
/**
* This action sets or gets the Index value (1 based) on a:
* <li>DropList (JComboBox)</li>
* <li>FillInFIeld (JComboBox)</li>
* <li>RadioList</li>
* <li>ScrollList (JList)</li>
* <li>JTabberPane</li>
*
*/
public class IndexValue extends PendingAction {
private int value;
private ButtonGroup bg;
public IndexValue(Component pComponent, int pValue) {
super(pComponent);
this.value = pValue;
}
public IndexValue(ButtonGroup bg, int pValue) {
super(null);
this.bg = bg;
this.value = pValue;
}
public ButtonGroup getButtonGroup(){
return this.bg;
}
public int getValue() {
return this.value;
}
@SuppressWarnings("unchecked")
public void performAction() {
try {
if (this.bg == null){
if (this._component instanceof JComboBox){
DropListModel dlm = (DropListModel)((JComboBox)this._component).getModel();
dlm.setSelectedIndex(value-1);
}
else if (this._component instanceof RadioList) {
// TF:23/07/2008:Changed this to use the radio list instead of the model to make the code cleaner
((RadioList)this._component).setIndexValue(value-1);
}
else if (this._component instanceof PaletteList) {
//PM12/3/08 added paletteList
((PaletteList)this._component).setSelectedIndex(value-1);
}
else if (this._component instanceof MenuList) {
// TF:09/11/2009:Changed this to compensate for 0-based MenuList
((MenuList)this._component).setIndexValue(value-1);
}
else if (this._component instanceof JList) {
// TF:26/07/2009:Changed this to just call the method on the control, as the model would defer to this anyway
// ((ScrollListModel)((JList)this._component).getModel()).setSelectedIndex(value-1);
((JList)this._component).setSelectedIndex(value-1);
}
// CraigM:16/01/2009 - Corrected to cater for hidden pages
else if (this._component instanceof TabFolder) {
TabFolder tf = (TabFolder)this._component;
int realIndex = tf.getTabIndex(value-1);
if (realIndex != -1) {
tf.setSelectedIndex(realIndex);
}
}
else {
UnsupportedOperationException errorVar = new UnsupportedOperationException("getIndexValue() is not implemented for components of type " + this._component.getClass());
ErrorMgr.addError(errorVar);
throw errorVar;
}
} else {
Enumeration els = this.bg.getElements();
int ind = 1;
while (els.hasMoreElements()){
JRadioButtonMenuItem item = (JRadioButtonMenuItem)els.nextElement();
if (ind == value)
item.setSelected(true);
ind++;
}
//ind++;
}
}
catch (IllegalArgumentException iae) {
// TF:13/08/2009:DET-107:If we set the value in the list to something that is out of range, an
// IllegalArgumentException can be thrown in Java. Forte just silently ignored this condition,
// so this code does too.
}
}
public static void set(ButtonGroup comp, int value){
ActionMgr.addAction(new IndexValue(comp, value));
}
public static void set(MenuList menuList, int value) {
ActionMgr.addAction(new IndexValue(menuList, value));
}
public static void set(ListField comp, int value){
if (comp instanceof RadioList)
ActionMgr.addAction(new IndexValue((RadioList)comp, value));
else if (comp instanceof DropList)
ActionMgr.addAction(new IndexValue((DropList)comp, value));
else if (comp instanceof ScrollList)
ActionMgr.addAction(new IndexValue((ScrollList)comp, value));
else if (comp instanceof FillInField)
ActionMgr.addAction(new IndexValue((FillInField)comp, value));
else if (comp instanceof PaletteList) //PM:12/3/08 added paletteList
ActionMgr.addAction(new IndexValue((PaletteList)comp, value));
}
/**
* Sets the selected Tab index, 1 based
* @param tabFolder
* @param index
*/
public static void set(JTabbedPane tabFolder, int index) {
ActionMgr.addAction(new IndexValue(tabFolder, index));
}
/**
* Returns the IndexValue of ButtonGroup
* @param comp
* @return
*/
public static int get(ButtonGroup comp){
int value = -1;
IndexValue action = ActionMgr.getAction(comp, IndexValue.class);
if (action != null) {
value = ((IndexValue)action).getValue();
} else {
Enumeration<?> els = comp.getElements();
int ind = 1;
while (els.hasMoreElements()){
JRadioButtonMenuItem item = (JRadioButtonMenuItem)els.nextElement();
if (item.isSelected()){
value = ind;
break;
} else{
value = -1;
}
ind++;
}
}
return value;
}
/**
* returns the index value of the selected tab in a {@link javax.swing.JTabbedPane}
* @param comp
* @return index value
*/
public static int get(JTabbedPane comp){
return _get(comp);
}
/**
* returns the index value of the {@link DisplayProject.ListField}
* @param comp
* @return index value
*/
public static int get(ListField comp){
if (comp instanceof JComponent){
return _get((JComponent)comp);
}
return -1;
}
/**
* returns the index value of the {@link DisplayProject.controls.MenuList}
* @param comp
* @return index value
*/
public static int get(MenuList comp){
return _get((JComponent)comp);
}
/**
* Returns the IndexValue of JComponent if one exists, as a one-based index
* @param comp
* @return
*/
private static int _get(JComponent comp){
int value = -1;
if (comp instanceof TabFolder) {
// TF:14/8/07: Many actions can affect the top page, for example, setting the pages, the Index value, adding pages,
// setting the top page, removing pages, etc. For this reason we have to process the actions first then return the real value
ActionMgr.processGUIActions();
value = ((TabFolder)comp).getSelectedIndex();
// CraigM:19/01/2009 - Convert to the model index (taking into account hidden tabs)
value = ((TabFolder)comp).getModelIndex(value) + 1;
}
else {
IndexValue action = ActionMgr.getAction(comp, IndexValue.class);
if (action != null) {
value = ((IndexValue)action).getValue();
} else {
if (comp instanceof JComboBox)
value = ((DropListModel)((JComboBox)comp).getModel()).getSelectedIndex()+1;
else if (comp instanceof RadioList)
value = ((RadioList)comp).getIndexValue()+1;
else if (comp instanceof PaletteList) //PM:12/3/08 added PaletteList
value = ((PaletteList)comp).getSelectedIndex()+1;
else if (comp instanceof MenuList) {
// TF:9/11/07:A menuList is acutally a list, so we need to do this first
value = ((MenuList)comp).getIndexValue() + 1;
}
else if (comp instanceof JList) {
// TF:26/07/2009:Changed this to just call the method on the control, as the model would defer to this anyway
// value = ((ScrollListModel)((JList)comp).getModel()).getSelectedIndex()+1;
value = ((JList)comp).getSelectedIndex()+1;
}
else {
UnsupportedOperationException errorVar = new UnsupportedOperationException("getIndexValue() is not implemented for components of type " + comp.getClass());
ErrorMgr.addError(errorVar);
throw errorVar;
}
}
}
return value;
}
}