Package DisplayProject.actions

Source Code of DisplayProject.actions.ElementSelected

/*
Copyright (c) 2003-2008 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 javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.event.ListSelectionListener;

import DisplayProject.ListField;
import DisplayProject.PaletteList;
import DisplayProject.RadioList;
import DisplayProject.controls.DropList;
import DisplayProject.controls.FillInField;
import DisplayProject.controls.ScrollList;
import Framework.ErrorMgr;
import Framework.UsageException;

/**
* Gets and sets the selected items on a JList or ListField
* AD:26/6/2008 Renamed from Selection to conform to the name used in UDS
*
*/
public class ElementSelected extends PendingAction {
    private boolean isSelected;
    private int index;

    private ElementSelected(Component pComponent, int index, boolean isSelected) {
        super(pComponent);
        this.isSelected = isSelected;
        this.index = index;
    }

    public void performAction() {    
      try {
        if (this.getComponent() instanceof JList) {
              JList list = (JList)this.getComponent();
 
  //        Prevent events being fired when setting selected items thru code
             ListSelectionListener[] listeners = list.getListSelectionListeners();
             for (int i=0; i<listeners.length; i++) {
                     list.removeListSelectionListener(listeners[i]);
             }
             try {
                 if (this.isSelected) {
                     list.addSelectionInterval(index, index);
                 }
                 else {
                     list.removeSelectionInterval(index, index);
                 }
             }
             finally {
                 for (int i=0; i<listeners.length; i++) {
                         list.addListSelectionListener(listeners[i]);
                 }     
             }
       
      }
        else if (this.getComponent() instanceof ListField) {
        ListField listField = (ListField) this.getComponent();
        listField.setElementSelected(index, isSelected)
      }
        else {
          UsageException errorVar = new UsageException("This method should not be called directly but go through the ElementSelected pending action.");
          ErrorMgr.addError(errorVar);
          throw errorVar;
        }
      }
      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.
      }
    }

    /**
   * Pending action to Set Element Selected. It selects or unselects the
   * element at the specified index.
   *
   * @param comp
   *            Component to have element selected on.
   * @param index
   *            The index parameter specifies the element of the list field to
   *            select or unselect.
   * @param isSelected
   *            The isSelected parameter indicates whether the element is to
   *            be selected (true) or unselected (false).
   */
    public static void set(JList comp, int index, boolean isSelected) {
        ActionMgr.addAction(new ElementSelected(comp, index, isSelected));
    }

    /**
   * Pending action to Set Element Selected. It selects or unselects the
   * element at the specified index.
   *
   * @param comp
   *            Component to have element selected on.
   * @param index
   *            The index parameter specifies the element of the list field to
   *            select or unselect.
   * @param isSelected
   *            The isSelected parameter indicates whether the element is to
   *            be selected (true) or unselected (false).
   *          
   * @author AD:26/6/2008
   */
    public static void set(ListField comp, int index, boolean isSelected) {
        ActionMgr.addAction(new ElementSelected((Component)comp, index, isSelected));
    }
    /**
   * Pending action to Set Element Selected. It selects or unselects the
   * element at the specified index.
   *
   * @param comp
   *            Component to have element selected on.
   * @param index
   *            The index parameter specifies the element of the list field to
   *            select or unselect.
   * @param isSelected
   *            The isSelected parameter indicates whether the element is to
   *            be selected (true) or unselected (false).
   *          
   * @author AD:26/6/2008
   */
    public static void set(DropList comp, int index, boolean isSelected) {
        set((ListField)comp, index, isSelected);
    }
    /**
   * Pending action to Set Element Selected. It selects or unselects the
   * element at the specified index.
   *
   * @param comp
   *            Component to have element selected on.
   * @param index
   *            The index parameter specifies the element of the list field to
   *            select or unselect.
   * @param isSelected
   *            The isSelected parameter indicates whether the element is to
   *            be selected (true) or unselected (false).
   *          
   * @author AD:26/6/2008
   */
    public static void set(FillInField comp, int index, boolean isSelected) {
        set((ListField)comp, index, isSelected);
    }
    /**
   * Pending action to Set Element Selected. It selects or unselects the
   * element at the specified index.
   *
   * @param comp
   *            Component to have element selected on.
   * @param index
   *            The index parameter specifies the element of the list field to
   *            select or unselect.
   * @param isSelected
   *            The isSelected parameter indicates whether the element is to
   *            be selected (true) or unselected (false).
   *          
   * @author AD:26/6/2008
   */
    public static void set(PaletteList comp, int index, boolean isSelected) {
        set((ListField)comp, index, isSelected);
    }
    /**
   * Pending action to Set Element Selected. It selects or unselects the
   * element at the specified index.
   *
   * @param comp
   *            Component to have element selected on.
   * @param index
   *            The index parameter specifies the element of the list field to
   *            select or unselect.
   * @param isSelected
   *            The isSelected parameter indicates whether the element is to
   *            be selected (true) or unselected (false).
   *          
   * @author AD:26/6/2008
   */
    public static void set(RadioList comp, int index, boolean isSelected) {
        set((ListField)comp, index, isSelected);
    }
    /**
   * Pending action to Set Element Selected. It selects or unselects the
   * element at the specified index.
   *
   * @param comp
   *            Component to have element selected on.
   * @param index
   *            The index parameter specifies the element of the list field to
   *            select or unselect.
   * @param isSelected
   *            The isSelected parameter indicates whether the element is to
   *            be selected (true) or unselected (false).
   *          
   * @author AD:26/6/2008
   */
    public static void set(ScrollList comp, int index, boolean isSelected) {
        set((ListField)comp, index, isSelected);
    }
   
    /**
   * The IsElementSelected method returns true if the element at the index
   * value is selected or false if it is not.
   *
   * @param comp
   *            Component to get check.
   * @param index
   *            The index parameter specifies the element of the list field to
   *            test for selection.
   * @return This method is useful if allows multiple select and is used for
   *         determining which elements are selected.
   * @author AD:26/6/2008
   */
    public static boolean get(JList comp, int index){
            return _get(comp, index);
    }
   
    /**
   * The IsElementSelected method returns true if the element at the index
   * value is selected or false if it is not.
   *
   * @param comp
   *            Component to get check.
   * @param index
   *            The index parameter specifies the element of the list field to
   *            test for selection.
   * @return This method is useful if allows multiple select and is used for
   *         determining which elements are selected.
   * @author AD:26/6/2008
   */
    public static boolean get(ListField comp, int index){
        if (comp instanceof JComponent){
            return _get((JComponent)comp, index);
        }
        return false;
    }
   
    /**
   * The IsElementSelected method returns true if the element at the index
   * value is selected or false if it is not.
   *
   * @param comp
   *            Component to get check.
   * @param index
   *            The index parameter specifies the element of the list field to
   *            test for selection.
   * @return This method is useful if allows multiple select and is used for
   *         determining which elements are selected.
   * @author AD:26/6/2008
   */
    public static boolean get(DropList comp, int index){
            return _get(comp, index);
    }
   
    /**
   * The IsElementSelected method returns true if the element at the index
   * value is selected or false if it is not.
   *
   * @param comp
   *            Component to get check.
   * @param index
   *            The index parameter specifies the element of the list field to
   *            test for selection.
   * @return This method is useful if allows multiple select and is used for
   *         determining which elements are selected.
   * @author AD:26/6/2008
   */
    public static boolean get(PaletteList comp, int index){
            return _get(comp, index);
    }
   
    /**
   * The IsElementSelected method returns true if the element at the index
   * value is selected or false if it is not.
   *
   * @param comp
   *            Component to get check.
   * @param index
   *            The index parameter specifies the element of the list field to
   *            test for selection.
   * @return This method is useful if allows multiple select and is used for
   *         determining which elements are selected.
   * @author AD:26/6/2008
   */
    public static boolean get(RadioList comp, int index){
            return _get(comp, index);
    }
   
    /**
   * The IsElementSelected method returns true if the element at the index
   * value is selected or false if it is not.
   *
   * @param comp
   *            Component to get check.
   * @param index
   *            The index parameter specifies the element of the list field to
   *            test for selection.
   * @return This method is useful if allows multiple select and is used for
   *         determining which elements are selected.
   * @author AD:26/6/2008
   */
    public static boolean get(ScrollList comp, int index){
            return _get(comp, index);
    }
   
    /**
   * The IsElementSelected method returns true if the element at the index
   * value is selected or false if it is not.
   *
   * @param comp
   *            Component to get check.
   * @param index
   *            The 0-based index parameter specifies the element of the list field to
   *            test for selection.
   * @return This method is useful if allows multiple select and is used for
   *         determining which elements are selected.
   * @author AD:26/6/2008
   */
    public static boolean get(FillInField comp, int index){
            return _get(comp, index);
    }
   
    private static boolean _get(final JComponent comp, final int index) {
      boolean value = false;
      ElementSelected action = (ElementSelected)ActionMgr.getAction(new ActionMgr.Filter() {
            public boolean filter(PendingAction action) {
                return action instanceof ElementSelected && action.getComponent() == comp && ((ElementSelected)action).getIndex() == index;
            }
        });
        if (action != null) {
            value = ((ElementSelected)action).isSelected();
        } else {
          if (comp instanceof JList) {
        JList list = (JList)comp;
        value = list.isSelectedIndex(index);
      } else if (comp instanceof ListField) {
        ListField listField = (ListField) comp;
        value = listField.getSelectedIndex() == index;
      } else {
                UnsupportedOperationException errorVar = new UnsupportedOperationException("getIndexValue() is not implemented");
                ErrorMgr.addError(errorVar);
                throw errorVar;
      }
        }
        return value;
    }

  /**
   * @return the isSelected
   */
  private boolean isSelected() {
    return isSelected;
  }

  /**
   * @return the index
   */
  public int getIndex() {
    return index;
  }

}
TOP

Related Classes of DisplayProject.actions.ElementSelected

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.