Package DisplayProject.actions

Source Code of DisplayProject.actions.WidgetState

/*
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 java.awt.Container;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.MenuElement;
import javax.swing.text.JTextComponent;

import org.apache.log4j.Logger;

import DisplayProject.ArrayColumn;
import DisplayProject.ArrayColumnModel;
import DisplayProject.CompoundField;
import DisplayProject.Constants;
import DisplayProject.GridField;
import DisplayProject.OutlineColumnDesc;
import DisplayProject.RadioList;
import DisplayProject.SelectMouseListener;
import DisplayProject.UDSWindow;
import DisplayProject.UIutils;
import DisplayProject.WindowUsageMap;
import DisplayProject.controls.ArrayField;
import DisplayProject.controls.AutoResizingComboBox;
import DisplayProject.controls.ListView;
import DisplayProject.controls.MenuList;
import DisplayProject.controls.MenuSeparator;
import DisplayProject.controls.MultiLineTextField;
import DisplayProject.controls.OutlineField;
import DisplayProject.controls.Panel;
import DisplayProject.controls.TabFolder;
import DisplayProject.controls.ToggleField;
import DisplayProject.dnd.DragMouseAdapter;
import DisplayProject.events.ClientEventManager;
import DisplayProject.events.HorzMoveListener;
import DisplayProject.table.ArrayFieldCellHelper;
import DisplayProject.table.GenericCellEditor;

/**
* Implements the widget State behaviour. This is a helper class that delegates the
* responsibility for setting the state of widgets down to individual pending actions,
* and hence this is not a pending action itself.
*/
public class WidgetState {
  private static final Logger _log = Logger.getLogger(WidgetState.class);
  private static final String WIDGET_STATE = "qq_state";
 
  /**
   * Private constructor as only the static methods on this class should be used.
   */
    private WidgetState() {
    }

    /**
     * Get the state of the current component. This method can return null if there is no
     * state currently associated with the component.
     * @param comp
     * @return
     */
    public static Integer getWidgetState(JComponent comp) {
      return (Integer)comp.getClientProperty(WIDGET_STATE);
    }
   
    /**
     * Set the widget state of the passed component to the passed value. This method should only
     * be used by helper class methods, application code should call {@link #set(JComponent, int)}
     * @param comp
     * @param state
     */
    public static void setWidgetState(JComponent comp, int state) {
      comp.putClientProperty(WIDGET_STATE, Integer.valueOf(state));
    }
   
    /**
     * Sets the state on an OutlineColumnDesc
     * @param column
     * @param state
     */
    public static void set(OutlineColumnDesc column, int state) {
        column.setState(state);
    }

    /**
     * This method emulates the state attribute and gets the state on the ArrayColumn base on the forte constant
     * @param comp
     * @param state
     */
    public static void set(ArrayColumn comp, int state) {
        if (comp == null) {
            return;
        }
       
        // TF:04/05/2009:We should only not have a cell editor if we're either
        // an outline field or a list view.
        boolean isListViewOrOutlineField = (comp.getCellEditor() == null);
        switch (state) {
        case Constants.FS_UPDATE:
            if (isListViewOrOutlineField) {

            } else {
                Enabled.set((GenericCellEditor) comp.getEditor(), true);
                Visible.set((GenericCellEditor) comp.getEditor(), true);
            }
            break;
        case Constants.FS_DISABLED:
            if (isListViewOrOutlineField) {

            } else {
                Enabled.set((GenericCellEditor) comp.getEditor(), false);
                Visible.set((GenericCellEditor) comp.getEditor(), true);
            }
            break;
        case Constants.FS_INACTIVE:
            if (isListViewOrOutlineField) {
                Visible.set(comp, true);
            } else {
                Enabled.set((GenericCellEditor) comp.getEditor(), true);
                Visible.set((GenericCellEditor) comp.getEditor(), true);
            }
            break;
        case Constants.FS_INVISIBLE:
            if (isListViewOrOutlineField) {
                Visible.set(comp, false);
            } else {
                Enabled.set((GenericCellEditor) comp.getEditor(), false);
                Visible.set((GenericCellEditor) comp.getEditor(), false);
            }
            break;
        case Constants.FS_VIEWONLY:
            if (isListViewOrOutlineField) {
                Visible.set(comp, true);
            } else {
                Enabled.set((GenericCellEditor) comp.getEditor(), true);
                Visible.set((GenericCellEditor) comp.getEditor(), true);
            }
            break;
        case Constants.FS_DRAG:
            if (isListViewOrOutlineField) {
                Visible.set(comp, true);
                // and something else
            } else {
                Enabled.set((GenericCellEditor) comp.getEditor(), true);
                Visible.set((GenericCellEditor) comp.getEditor(), true);
            }
            break;
        }
        return;
    }

    /**
     * This method emulates the forte state attribute and gets the state on the selected ButtonGroup base on the forte
     * constant
     *
     * @param comp
     * @param state
     */
    public static void set(ButtonGroup comp, int state) {
        if (comp == null)
            return;
        Enumeration<AbstractButton> enum1 = comp.getElements();
        while (enum1.hasMoreElements()) {
            set((JComponent) enum1.nextElement(), state);
        }
    }

    /**
     * sets the Forte state on a JSeparator
     * @param comp
     * @param state
     */
    public static void set(JSeparator comp, int state) {
      // TF:09/01/2010:Changed this to explicitly list the states for the menu list
        if (comp == null)
            return;
        if (state == Constants.FS_USAGESTATE) {
            setDefaultState(comp);
            return;
        }
        setWidgetState(comp, state);
        switch (state) {
        case Constants.MS_ENABLED:
            Enabled.set(comp, true);
            Visible.set(comp, true);
            break;
        case Constants.MS_DISABLED:
            Enabled.set(comp, false);
            Visible.set(comp, true);
            break;
        case Constants.MS_INVISIBLE:
            Enabled.set(comp, false);
            Visible.set(comp, false);
            break;
        }
    }
    /**
     * sets the Forte state on a Menu
     * @param comp
     * @param state
     */
    public static void set(MenuElement menu, int state) {
      JComponent comp = (JComponent)menu;
        if (comp == null)
            return;
        if (state == Constants.FS_USAGESTATE) {
            setDefaultState(comp);
            return;
        }
        setWidgetState(comp, state);
        switch (state) {
        case Constants.MS_ENABLED:
            Enabled.set(comp, true);
            Visible.set(comp, true);
            break;
        case Constants.MS_DISABLED:
            Enabled.set(comp, false);
            Visible.set(comp, true);
            break;
        case Constants.MS_INVISIBLE:
            Enabled.set(comp, false);
            Visible.set(comp, false);
            break;
        }
    }

    /**
     * sets the Forte state on a MenuList
     * @param comp
     * @param state
     */
    public static void set(MenuList comp, int state) {
      // TF:09/01/2010:Changed this to reuse code
      set((MenuElement)comp, state);
    }

    /**
     * sets the Forte state on a MenuSeparator
     * @param comp
     * @param state
     */
    public static void set(MenuSeparator comp, int state) {
      // TF:09/01/2010:Changed this to reuse code
      set((MenuElement)comp, state);
    }

    /**
     * sets the forte state on a Menu.
     * @param mi
     * @param state
     */
    public static void set(JMenuItem mi, int state) {
      // TF:09/01/2010:Changed this to reuse code
      set((MenuElement)mi, state);
    }

    /**
     * sets the Forte state on a Menu
     * @param comp
     * @param state
     */
    public static void set(JMenu comp, int state) {
      // TF:09/01/2010:Changed this to reuse code
      set((MenuElement)comp, state);
    }

    /**
     * sets the Forte state on a MenuBar
     * @param comp
     * @param state
     */
    public static void set(JMenuBar comp, int state) {
      // TF:09/01/2010:Changed this to reuse code
      set((MenuElement)comp, state);
    }

    public static void set(JFrame comp, int state) {
      set(UIutils.getForm(comp), state);
    }


    /**
     * sets the Forte state on a popup menu
     * @param comp
     * @param state
     */
    public static void set(JPopupMenu comp, int state) {
      // TF:09/01/2010:Changed this to reuse code
      set((MenuElement)comp, state);
    }

    public static void setDefaultState(JComponent comp) {
      JFrame win = null;
      if ((comp instanceof JMenu) || (comp instanceof JMenuBar)
          || (comp instanceof JMenuItem)
          || (comp instanceof JCheckBoxMenuItem)
          || (comp instanceof JPopupMenu)
          || (comp instanceof JRadioButtonMenuItem)) {
        //PM:15/4/08 corrected getting the window
        win = UIutils.getFrame((MenuElement)comp);
      } else {
        win = (JFrame) UIutils.getDeclaredFrame(comp);//comp.getTopLevelAncestor();
        if (win == null) {
          JTable jt = ArrayFieldCellHelper.getArrayField(comp);
          if (jt == null)
            return; //give up
            win = UIutils.getDeclaredFrame(jt); //PM:9/10/07 used the declared JFrame to handle nested windows
            ///win = (JFrame) jt.getTopLevelAncestor();
        }
      }

      // CraigM:23/06/2008 - If the widget doesn't have a window, ignore it as we don't know what state it should be in.
      if (win == null) {
        return;
      }
     
      // CraigM:23/06/2008 - Corrected the usage state (also removed all unnecessary exception handling)
      int currUsage = WindowUsage.get(win);

      //PM:8/10/07 - otago change OTA-72
      int stateForUsage = getForUsage(win, comp, currUsage);
      set(comp, stateForUsage);
    }

//    /**
//     * This method emulates the forte state attribute and gets the state on the
//     * selected component base on the forte constant
//     *
//     * @param comp
//     * @param state
//     */
//    public static <T extends CompoundField & JComponent> void set(T comp, int state) {
//        if (comp == null) {
//            return;
//        }
//        else if (comp instanceof JComponent) {
//            WidgetState.set((JComponent)comp, state);
//        }
//        else {
//            WidgetState.set((Component)comp, state);
//        }
//    }

//    /**
//     * This method emulates the forte state attribute and gets the state on the
//     * selected component base on the forte constant
//     *
//     * @param comp
//     * @param state
//     */
//    public static void set(GridField comp, int state) {
//        set((CompoundField)comp, state);
//    }
//
//    /**
//     * This method emulates the forte state attribute and gets the state on the
//     * selected component base on the forte constant
//     *
//     * @param comp
//     * @param state
//     */
//    public static void set(Panel comp, int state) {
//        set((CompoundField)comp, state);
//    }
    /**
     * Go up the parent hierarchy until we find the top parent, returning the worst state found.
     * CraigM: 07/04/2008.
     *
     * @param comp the component where will will start inspecting the parents
     * @param state initial state to consider
     *
     * @return components worst state from its parents
     */
    public static int getWorstStateFromParents(JComponent comp, int state) {
      Component parent = comp.getParent();
      // TF:13/10/2009:If we're a child of an array field get the state from the array too.
      if (parent == null && ArrayFieldCellHelper.isInArrayField(comp)) {
        return getWorstState(ArrayFieldCellHelper.getArrayField(comp), state);
      }
      return getWorstState(comp.getParent(), state);
    }
   
    /**
     * Go up the widget hierarchy until we find the top parent, returning the worst state found.
     * CraigM: 07/04/2008.
     *
     * @param comp the component where will will start inspecting
     * @param state initial state to consider
     *
     * @return components worst state
     */
    private static int getWorstState(Component widget, int state) {
      Component c = widget;
        while (c != null) {
          if (c instanceof JFrame) {
            // TF:13/10/2009:We don't store state information on the window, just the first level under it.
            return state;
          }
            int parentState = get(c);
            if (parentState > state) {
                state = parentState;
            }
            // TF:22/10/2009:We need to ensure popup menus are not affected by their parents
            if (c instanceof JPopupMenu || c instanceof JRootPane) {
              break;
            }
            // TF:13/10/2009:If we're a child of an array field, we need to go up and compensate for the array field too
            Component oldComp = c;
            c = c.getParent();
            // TF:07/12/2009:DET-123: Instead of just testing if c is null, we need to test if c is not in an array field.
            // The reason for this is sometimes an array template is actually parented, ie when the column is being edited.
            // In this case however, isInArrayField(c) will return false, as the panel to which the template is parented is
            // not identified as being in an array field. This also allows the case for a grid field which contains children
            // to be used as a column editor.
            if (!ArrayFieldCellHelper.isInArrayField(c) && ArrayFieldCellHelper.isInArrayField(oldComp)) {
              return getWorstState(ArrayFieldCellHelper.getArrayField(oldComp), state);
            }
        }
        return state;
    }
   
    /**
     * This method emulates the forte state attribute and gets the state on the
     * selected component base on the forte constant
     *
     * @param comp
     * @param state
     */
    public static void set(JComponent comp, int state) {
        if (comp == null) {
            return;
        }
       
        // If the request state is FS_USAGESTATE, then we want to set the components state based on
        // the windows usage map and the windows current usage state.  CraigM: 24/04/2008.
        if (state == Constants.FS_USAGESTATE) {
          // CraigM:17/06/2008 - Use new method
          // int usageState = UIutils.getStateForUsage(comp, UIutils.getWindowUsage(comp));
          int usageState = WidgetState.getForUsage(comp, UIutils.getWindowUsage(comp));
          WidgetState.set(comp, usageState);
          return;
        }
       
        // TF:09/01/2010:DET-137:Menus are largely exempt from this worst state code, for if our parent menu
        // is disabled, there is no way of getting or seeing this menu. The same for invisible menus. Hence
        // we don't have to worry about this
        if (comp instanceof MenuElement) {
          set((MenuElement)comp, state);
          return;
        }
        else if (comp instanceof JSeparator) {
          set((JSeparator)comp, state);
          return;
        }

        // Our state is the worse (higher) of our state and any parent widget state
        // However, this affects only the display state, not the actual state value
        Integer oldState = (Integer) comp.getClientProperty("qq_state");
        // TF:27/10/2009:We need to check it we're in an array field before we skip this step, otherwise we
        // might not validly affect our children
        if (oldState != null && oldState.intValue() == state && ArrayFieldCellHelper.getArrayField(comp) == null) {
            // Nothing to do, return
            return;
        }

        // Set our state value
        comp.putClientProperty("qq_state", new Integer(state));

        // Get the worst state from the parents
        state = WidgetState.getWorstStateFromParents(comp, state);
        if (_log.isDebugEnabled()&&(comp instanceof Panel)){
          _log.debug("Setting Widget State on " + ((Panel)comp).getCaption() +" to " + state);
        }

        // TF:18/12/2009:DET-141:If our state is disabled, then we need to hide the popup menu.
        if (comp instanceof MenuElement == false) {
          FieldPopupMenu.setState(comp, state);
        }
       
        // Now actually set the physical characteristics of the widget
        _set(comp, state);

        // Now we must recurse down to our children too, otherwise setting a grid may
        // not result in setting our children to the correct state
        recursivelySetChildren(comp, state);
    }

    private static void processComponent(Component kid, int state) {
      // TF:26/05/2009:Extracted this into a separate method
      if (kid instanceof JComponent) {
        JComponent thisKid = (JComponent)kid;
        Integer oldState = (Integer)thisKid.getClientProperty("qq_state");

        // Forms do not always have a state set, however we seem to need it for
        // nested windows.
        if (oldState == null && thisKid.getName() != null
            && (thisKid.getName().endsWith(".form") || thisKid.getName().endsWith(".Form"))) {
          oldState = new Integer(Constants.FS_UPDATE);
          thisKid.putClientProperty("qq_state", oldState);
        }

        // TF:18/7/07: If the widget is a ScrollPane then get the state from the first Child
        if (thisKid instanceof JScrollPane) {
          JScrollPane myScrollPane = (JScrollPane)thisKid;
          if (myScrollPane.getViewport().getComponentCount() > 0) {
            JComponent myChild = (JComponent)myScrollPane.getViewport().getComponent(0);
            oldState = (Integer)myChild.getClientProperty("qq_state");
            if (oldState != null) {
              int stateToSet = (oldState.intValue() > state) ? oldState.intValue() : state;

              // View Only widgets still have their scroll pane active.  CraigM: 03/04/2008
              if (stateToSet == Constants.FS_VIEWONLY) {
                _set(myScrollPane, Constants.FS_UPDATE);
              }
              // This widget needs to change its state
              else {
                _set(myScrollPane, stateToSet);
              }
              thisKid = myChild;
              // Now fall through to set the child widget as well
            }
          }
        }
        if (oldState != null) {
          int stateToSet = (oldState.intValue() > state) ? oldState.intValue() : state;
          // This widget needs to change it's state
          _set(thisKid, stateToSet);
          // And recurse to it's kids too.
          recursivelySetChildren(thisKid, stateToSet);
        }
      }
    }

    private static void recursivelySetChildren(JComponent comp, int state) {
        //PM:21/11/07 - handle the special case for tab
        //the children should not be set invisible when the tab folder
        // is set invisible
        if (comp == null || (state == Constants.FS_INVISIBLE && comp instanceof JTabbedPane)) {
            return;
        }

        // TF:25/06/2009:Added special handling for a TabFolder. We just want to get all the components
        // in the tab folder and recurse down into them. Luckily, TabFolders include a method that will
        // give us all the components, irrespective of whether they are visible or not.
        if (comp instanceof TabFolder) {
          TabFolder pane = (TabFolder)comp;
          for (Component c : pane.getAllPages()) {
            processComponent(c, state);
          }
        }
        // TF:26/05/2009:Changed this to process the JTabbed panes diffferent to the other components.
        // A JTabbedPane can have 3 component prior to it's real components, depending on how it's
        // been set up. (Eg SCROLL_TAB_LAYOUT has 3 components, WRAP_TAB_LAYOUT doesnt)
        else if (comp instanceof JTabbedPane) {
          JTabbedPane pane = (JTabbedPane)comp;
          int tabs = pane.getTabCount();
          // TF:26/05/2009:Unfortunately, as we're setting the visibility, this may change
          // the component count and indexes, so remember the pages first.
          List<Component> tabPages = new ArrayList<Component>(tabs);
          for (int i = 0; i < tabs; i++) {
            tabPages.add(pane.getComponentAt(i));
          }
          for (Component c : tabPages) {
            processComponent(c, state);
          }
        }
        else if (comp instanceof ArrayField) {
          // TF:13/10/2009:Get our columns and process them as well
          ArrayField af = (ArrayField)comp;
          ArrayColumnModel model = (ArrayColumnModel)af.getColumnModel();
          for (int i = 0; i < model.getRealColumnCount(); i++) {
            ArrayColumn column = model.getRealColumn(i);
            Component childComp = column.getComponent();
            if (childComp != null) {
              // TF:29/10/2009:DET-123:Changed this to use processComponent to take into account the state of the child component.
              // set(childComp, state);
              processComponent(childComp, state);
            }
          }
        }
        else {
          Component[] kids = comp.getComponents();
          for (int i = 0; i < kids.length; i++) {
             processComponent(kids[i], state);
          }
        }
    }

    private static void _set(JComponent comp, int state) {
        if (comp == null)
            return;
        if (state == Constants.FS_USAGESTATE) {
            setDefaultState(comp);
            return;
        }

        // TF:07/12/2009:DET-141:If we have a popup menu on the component, enable / disable it based on the state of this widget
        if (FieldPopupMenu.get(comp) != null) {
          switch (state) {
            case Constants.FS_UPDATE:
            case Constants.FS_VIEWONLY:
              comp.setComponentPopupMenu(FieldPopupMenu.get(comp));
              break;
             
            case Constants.FS_DISABLED:
            case Constants.FS_INACTIVE:
              comp.setComponentPopupMenu(null);
              break;
          }
        }
       
        /*
         * Drag and drop special case
         */
        if (state == Constants.FS_DRAG) {
            comp.setTransferHandler(ClientEventManager.getObjectDropTransferHandler());
            comp.removeMouseListener(DragMouseAdapter.getInstance());//remove first to avoid duplications
            comp.addMouseListener(DragMouseAdapter.getInstance());
            return;
        } else {
          comp.removeMouseListener(DragMouseAdapter.getInstance());
        }


        if (state == Constants.FS_HORZMOVE) {
            HorzMoveListener hml = new HorzMoveListener(comp);
            comp.addMouseMotionListener(hml);
            comp.addMouseListener(hml);
            return;
        } else {
            MouseListener[] listeners = comp.getMouseListeners();
            for (int i = 0; i < listeners.length; i++) {
                if (listeners[i] instanceof HorzMoveListener) {
                    comp.removeMouseListener(listeners[i]);
                    break;
                }
            }
            MouseMotionListener[] mmListeners = comp.getMouseMotionListeners();
            for (int i = 0; i < mmListeners.length; i++) {
                if (mmListeners[i] instanceof HorzMoveListener) {
                    comp.removeMouseListener(listeners[i]);
                    break;
                }
            }
        }

        if (comp instanceof JButton
                || (comp instanceof JPanel && comp.getParent() instanceof JTabbedPane)) {
            switch (state) {
            case Constants.FS_UPDATE:
                if (comp instanceof JButton) {
                    Enabled.set(comp, true);
                    // TF:05/12/2009:DET-139:Set the editable property
                    Editable.set((JButton)comp, true);
                }
                // COLET 12/01/2009 : Enable GridField, toggles the paint() style on GridTitledBorder.
                if (comp instanceof GridField)
                    Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, true);
                removeSelectionListener(comp);
                break;
            case Constants.FS_DISABLED:
                if (comp instanceof JButton) {
                    Enabled.set(comp, false);
                }
                // COLET 12/01/2009 : Disable GridField, toggles the paint() style on GridTitledBorder.
                if (comp instanceof GridField)
                    Enabled.set(comp, false);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                removeSelectionListener(comp);
                break;
            case Constants.FS_INVISIBLE:
                if (comp instanceof JButton)
                    Enabled.set(comp, false);
                // COLET 12/01/2009 : Disable GridField, toggles the paint() style on GridTitledBorder.
                if (comp instanceof GridField)
                    Enabled.set(comp, false);               
                Visible.set(comp, false);
                Focusable.set(comp, false);
                removeSelectionListener(comp);
                break;
            case Constants.FS_VIEWONLY:
                if (comp instanceof JButton) {
                    Enabled.set(comp, true);
                    // TF:05/12/2009:DET-139:Set the editable property
                    Editable.set((JButton)comp, true);
                }
                // COLET 12/01/2009 : Disable GridField, toggles the paint() style on GridTitledBorder.
                if (comp instanceof GridField)
                    Enabled.set(comp, false);               
                Visible.set(comp, true);
                Focusable.set(comp, true);
                removeSelectionListener(comp);
                break;
            case Constants.FS_INACTIVE:
                //              FIX:DC When inactive enabled should be false -- TF -- For buttons!
                if (comp instanceof JButton) {
//                    Enabled.set(comp, (comp instanceof JButton && //check to see if it is a picture button
//                            (((JButton) comp).getIcon() == null)) ? false
//                                    : true);
                    // TF:05/12/2009:DET-139:Set the editable property
                  Enabled.set(comp, true);
                    Editable.set((JButton)comp, false);
                }
                // COLET 12/01/2009 : Disable GridField, toggles the paint() style on GridTitledBorder.
                if (comp instanceof GridField)
                    Enabled.set(comp, false);               
                Visible.set(comp, true);
                Focusable.set(comp, false);
                removeSelectionListener(comp);
                break;
            case Constants.FS_SELECTONLY:
                if (comp instanceof JButton) {
                    Enabled.set(comp, true);
                    // TF:05/12/2009:DET-139:Set the editable property
                    Editable.set((JButton)comp, true);
                }
                // COLET 12/01/2009 : Disable GridField, toggles the paint() style on GridTitledBorder.
                if (comp instanceof GridField)
                    Enabled.set(comp, false);               
                Visible.set(comp, true);
                Focusable.set(comp, false);
                addSelectionListener(comp);
                break;
            }
            return;
        }
        // TF: We don't want panels to be disabled, as this filters down to the children, which we do
        // explicitly. The exception is tab panels, handled above.
        else if (comp instanceof JPanel || comp instanceof JTree) {
            boolean isTree = (comp instanceof JTree && !(comp instanceof OutlineField));
            switch (state) {
            case Constants.FS_UPDATE:
                Visible.set(comp, true);
                Focusable.set(comp, true);
                removeSelectionListener(comp);
                if (isTree){
                    visibleScrollPane(comp, true);
                    enableScrollPane(comp, true);
                    focusableScrollPane(comp, true);
                }
               
                // COLET 12/01/2009 : Enable GridField, toggles the paint() style on GridTitledBorder.
                if (comp instanceof GridField) {
                    Enabled.set(comp, true);
                }
               
                break;
            case Constants.FS_DISABLED:
                Visible.set(comp, true);
                Focusable.set(comp, false);

                // We want to disable ListViews (which are also JPanels). CraigM: 05/02/2008.
                if (comp instanceof ListView) {
                    Enabled.set(comp, false);
                }
               
                // COLET 12/01/2009 : Disable GridField, toggles the paint() style on GridTitledBorder.
                if (comp instanceof GridField) {
                    Enabled.set(comp, false);
                }
               
                removeSelectionListener(comp);
                if (isTree){
                    visibleScrollPane(comp, true);
                    enableScrollPane(comp, false);
                    focusableScrollPane(comp, false);
                }
                break;
            case Constants.FS_INVISIBLE:
                Visible.set(comp, false);
                Focusable.set(comp, false);
               
                // COLET 12/01/2009 : Disable GridField, toggles the paint() style on GridTitledBorder.
                if (comp instanceof GridField) {
                    Enabled.set(comp, false);
                }
               
                removeSelectionListener(comp);
                if (isTree){
                    visibleScrollPane(comp, false);
                    focusableScrollPane(comp, false);
                }
                break;
            case Constants.FS_VIEWONLY:
                Visible.set(comp, true);
                Focusable.set(comp, false);
               
                // COLET 12/01/2009 : Disable GridField, toggles the paint() style on GridTitledBorder.
                if (comp instanceof GridField) {
                    Enabled.set(comp, false);
                }
               
                removeSelectionListener(comp);
                if (isTree){
                    visibleScrollPane(comp, true);
                    enableScrollPane(comp, true);
                    focusableScrollPane(comp, true);
                }
                break;
            case Constants.FS_INACTIVE:
                Visible.set(comp, true);
                Focusable.set(comp, false);
               
                // COLET 12/01/2009 : Disable GridField, toggles the paint() style on GridTitledBorder.
                if (comp instanceof GridField) {
                    Enabled.set(comp, false);
                }
               
                removeSelectionListener(comp);
                if (isTree) {
                    visibleScrollPane(comp, true);
                    enableScrollPane(comp, true);
                    focusableScrollPane(comp, false);
                }
                break;
            case Constants.FS_SELECTONLY:
                Visible.set(comp, true);
                Focusable.set(comp, false);
               
                // COLET 12/01/2009 : Disable GridField, toggles the paint() style on GridTitledBorder.
                if (comp instanceof GridField) {
                    Enabled.set(comp, false);
                }
               
                // TF:27/04/2009:Changed this to use the new selection listener method
                addSelectionListener(comp);
                if (isTree){
                    visibleScrollPane(comp, true);
                    enableScrollPane(comp, true);
                    focusableScrollPane(comp, false);
                }
                break;
            }
            return;
        } else if (comp instanceof JTable) {
            switch (state) {
            case Constants.FS_UPDATE:
                Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, true);
                ArrayColumnsViewOnly.set((JTable) comp, false);
                removeSelectionListener(comp);
                break;
            case Constants.FS_DISABLED:
                Enabled.set(comp, false);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                removeSelectionListener(comp);
                break;
            case Constants.FS_INVISIBLE:
                Enabled.set(comp, false);
                Visible.set(comp, false);
                Focusable.set(comp, false);
                removeSelectionListener(comp);
                break;
            case Constants.FS_VIEWONLY:
                Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, true);
                ArrayColumnsViewOnly.set((JTable) comp, true);
                removeSelectionListener(comp);
                break;
            case Constants.FS_INACTIVE:
                Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                removeSelectionListener(comp);
                break;
            case Constants.FS_SELECTONLY:
                Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                // TF:27/04/2009:Changed this to use the new selection listener method
                addSelectionListener(comp);
                break;
            case Constants.FS_DRAG:
                ((JTable) comp).setDragEnabled(true);
                break;
            }
            return;
        }

        //  17.01 - changed the code to check for JSeparator and not
        //           JMenuItem since a JMenuItem will force a different
        //           version of set() to execute
        // TF:30/10/2008:Added in JPopupMenu
        // TF:10/11/2009:Added in MenuList and re-ordered.
        else if (comp instanceof JSeparator || comp instanceof JMenu
                || comp instanceof JCheckBoxMenuItem
                || comp instanceof JRadioButtonMenuItem
                || comp instanceof JMenuItem
                || comp instanceof MenuList
                || comp instanceof JPopupMenu) {
            switch (state) {
            case Constants.MS_ENABLED:
                Enabled.set(comp, true);
                Visible.set(comp, true);
                break;
            case Constants.MS_DISABLED:
                Enabled.set(comp, false);
                Visible.set(comp, true);
                break;
            case Constants.MS_INVISIBLE:
                Enabled.set(comp, true);
                Visible.set(comp, false);
                break;
            }
            return;
        }

        // DET-148:Added in the capability to have a "read only" radio list
        else if (comp instanceof RadioList) {
          RadioList rl = (RadioList)comp;
            switch (state) {
            case Constants.FS_UPDATE:
              Editable.set(rl, true);
                Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, true);
                removeSelectionListener(comp);
                break;
            case Constants.FS_DISABLED:
              Editable.set(rl, false);
                Enabled.set(comp, false);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                removeSelectionListener(comp);
                break;
            case Constants.FS_INVISIBLE:
              Editable.set(rl, false);
                Enabled.set(comp, false);
                Visible.set(comp, false);
                Focusable.set(comp, false);
                removeSelectionListener(comp);
                break;
            case Constants.FS_VIEWONLY:
              Editable.set(rl, false);
              // TF:01/03/2010:DET-148:Made this enabled
                //Enabled.set(comp, false);
                Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, true);
                removeSelectionListener(comp);
                break;
            case Constants.FS_INACTIVE:
              Editable.set(rl, false);
              // TF:15/01/2010:By default, Forte set an INACTIVE radio list to white, rather than
              // greying it out like Forte did. We replicate this, but to set a disabled radio list
              // to white rather than grey change the following line to set it to false.
                Enabled.set(comp, true);
//                Enabled.set(comp, false);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                removeSelectionListener(comp);
                break;
            case Constants.FS_SELECTONLY:
              Editable.set(rl, false);
                Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                // TF:27/04/2009:Changed this to use the new selection listener method
                addSelectionListener(comp);
                break;
            }
            return;
        }
        // 19/01/07 usage of JList requires specific setting
        //           for FS_INACTIVE
        //------------------------------------------------------------
        else if (comp instanceof JList) {
            switch (state) {
            case Constants.FS_UPDATE:
                Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, true);
                removeSelectionListener(comp);
                break;
            case Constants.FS_DISABLED:
                Enabled.set(comp, false);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                removeSelectionListener(comp);
                break;
            case Constants.FS_INVISIBLE:
                Enabled.set(comp, false);
                Visible.set(comp, false);
                Focusable.set(comp, false);
                removeSelectionListener(comp);
                break;
            case Constants.FS_VIEWONLY:
                Enabled.set(comp, false);
                Visible.set(comp, true);
                Focusable.set(comp, true);
                removeSelectionListener(comp);
                break;
            case Constants.FS_INACTIVE:
                //Enabled.set(comp, true);
                Enabled.set(comp, false);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                removeSelectionListener(comp);
                break;
            case Constants.FS_SELECTONLY:
                Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                // TF:27/04/2009:Changed this to use the new selection listener method
                addSelectionListener(comp);
                break;
            }
            return;
        }

        // CraigM:03/07/2008 - Added a special read only flag so combo boxes can be focusable but not editable.
        else if (comp instanceof AutoResizingComboBox) {
          AutoResizingComboBox autoResizingComboBox = (AutoResizingComboBox)comp;
          switch (state) {
          case Constants.FS_UPDATE:
            autoResizingComboBox.setReadOnly(false);
            Enabled.set(comp, true);
            Visible.set(comp, true);
            Focusable.set(comp, true);
            removeSelectionListener(comp);
            break;
          case Constants.FS_DISABLED:
            autoResizingComboBox.setReadOnly(false);
            Enabled.set(comp, false);
            Visible.set(comp, true);
            Focusable.set(comp, false);
            removeSelectionListener(comp);
            break;
          case Constants.FS_INVISIBLE:
            autoResizingComboBox.setReadOnly(false);
            Enabled.set(comp, false);
            Visible.set(comp, false);
            Focusable.set(comp, false);
            removeSelectionListener(comp);
            break;
          case Constants.FS_VIEWONLY:
            autoResizingComboBox.setReadOnly(true);
            Enabled.set(comp, true);
            Visible.set(comp, true);
            Focusable.set(comp, true);
            removeSelectionListener(comp);
            break;
          case Constants.FS_INACTIVE:
            autoResizingComboBox.setReadOnly(false);
            Enabled.set(comp, false);
            Visible.set(comp, true);
            Focusable.set(comp, false);
            removeSelectionListener(comp);
            break;
          case Constants.FS_SELECTONLY:
            // TF:08/11/2009:Changed this so it's not enabled
            autoResizingComboBox.setReadOnly(true);
            Enabled.set(comp, false);
            Visible.set(comp, true);
            Focusable.set(comp, false);
                // TF:27/04/2009:Changed this to use the new selection listener method
                addSelectionListener(comp);
            break;
          }
        }
       
        else if (comp instanceof JTextComponent) {
            JTextComponent tComp = (JTextComponent) comp;

            //PM:9/10/07 added to ensure the scroll pane works
            // Check for null.  Will be null when cloning a component.  CraigM: 11/02/2008.
            if (comp instanceof MultiLineTextField && comp.getParent() != null) {
                JScrollPane sp = ((JScrollPane)comp.getParent().getParent());
                Enabled.set(sp, true);
            }
            switch (state) {
            case Constants.FS_UPDATE:
                Editable.set(tComp, true);
                Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, true);
                removeSelectionListener(comp);
                break;
            case Constants.FS_DISABLED:
                Editable.set(tComp, true);
                Enabled.set(comp, false);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                removeSelectionListener(comp);
                //PM:9/10/07 added to ensure the scroll pane is also disabled
                if (comp instanceof MultiLineTextField){
                    JScrollPane sp = ((JScrollPane)comp.getParent().getParent());
                    Enabled.set(sp, false);
                }
                break;
            case Constants.FS_INVISIBLE:
                Editable.set(tComp, false);
                Enabled.set(comp, false);
                Visible.set(comp, false);
                Focusable.set(comp, false);
                removeSelectionListener(comp);
                break;
            case Constants.FS_VIEWONLY:
                Editable.set(tComp, false);
                Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, true);
                removeSelectionListener(comp);
                break;
            case Constants.FS_INACTIVE:
                Editable.set(tComp, true);
                Enabled.set(comp, false);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                removeSelectionListener(comp);
                break;
            case Constants.FS_SELECTONLY:
                Editable.set(tComp, false);
                Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                // TF:27/04/2009:Changed this to use the new selection listener method
                addSelectionListener(comp);
                break;
            }
        } else if (comp instanceof JTabbedPane) {
            switch (state) {
            case Constants.FS_UPDATE:
                Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, true);
                removeSelectionListener(comp);
                break;
            case Constants.FS_DISABLED:
                Enabled.set(comp, false);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                removeSelectionListener(comp);
                break;
            case Constants.FS_INVISIBLE:
                Enabled.set(comp, false);
                Visible.set(comp, false);
                Focusable.set(comp, false);
                removeSelectionListener(comp);
                break;
            case Constants.FS_VIEWONLY:
                Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, true);
                removeSelectionListener(comp);
                break;
            case Constants.FS_INACTIVE:
                Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                removeSelectionListener(comp);
                break;
            case Constants.FS_SELECTONLY:
                Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                // TF:27/04/2009:Changed this to use the new selection listener method
                addSelectionListener(comp);
                break;
            }
        } else if (comp instanceof JLabel) {
            switch (state) {
            case Constants.FS_UPDATE:
                Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                break;
            case Constants.FS_DISABLED:
                Enabled.set(comp, false);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                break;
            case Constants.FS_INVISIBLE:
                Enabled.set(comp, false);
                Visible.set(comp, false);
                Focusable.set(comp, false);
                break;
            case Constants.FS_VIEWONLY:
                Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                break;
            case Constants.FS_INACTIVE:
                Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                break;
            case Constants.FS_SELECTONLY:
                Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                // TF:27/04/2009:Changed this to use the new selection listener method
                addSelectionListener(comp);
                break;
            }
        } else if (comp instanceof ToggleField){
            switch (state) {
            case Constants.FS_UPDATE:
                Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, true);
                Editable.set((ToggleField)comp, true);
                removeSelectionListener(comp);
                break;
            case Constants.FS_DISABLED:
                Enabled.set(comp, false);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                Editable.set((ToggleField)comp, true);
                removeSelectionListener(comp);
                break;
            case Constants.FS_INVISIBLE:
                Enabled.set(comp, false);
                Visible.set(comp, false);
                Focusable.set(comp, false);
                Editable.set((ToggleField)comp, true);
                removeSelectionListener(comp);
                break;
            case Constants.FS_VIEWONLY:
                Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, true);
                Editable.set((ToggleField)comp, false);
                removeSelectionListener(comp);
                break;
            case Constants.FS_INACTIVE:
              // TF:01/03/2010:DET-148:Changed this to be the same as Forte
                Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                Editable.set((ToggleField)comp, false);
                removeSelectionListener(comp);
                break;
            case Constants.FS_SELECTONLY:
                Enabled.set(comp, false);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                Editable.set((ToggleField)comp, true);
                // TF:27/04/2009:Changed this to use the new selection listener method
                addSelectionListener(comp);
                break;
            }
        } else {
            switch (state) {
            case Constants.FS_UPDATE:
                Enabled.set(comp, true);
                Visible.set(comp, true);
                Focusable.set(comp, true);
                removeSelectionListener(comp);
                break;
            case Constants.FS_DISABLED:
                Enabled.set(comp, false);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                removeSelectionListener(comp);
                break;
            case Constants.FS_INVISIBLE:
                Enabled.set(comp, false);
                Visible.set(comp, false);
                Focusable.set(comp, false);
                removeSelectionListener(comp);
                break;
            case Constants.FS_VIEWONLY:
                Enabled.set(comp, false);
                Visible.set(comp, true);
                Focusable.set(comp, true);
                removeSelectionListener(comp);
                break;
            case Constants.FS_INACTIVE:
                Enabled.set(comp, false);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                removeSelectionListener(comp);
                break;
            case Constants.FS_SELECTONLY:
                Enabled.set(comp, false);
                Visible.set(comp, true);
                Focusable.set(comp, false);
                // TF:27/04/2009:Changed this to use the new selection listener method
                addSelectionListener(comp);
                break;
            }
        }
       
        // CraigM:19/06/2008 - If we are disabling or enabling the component in an array field, we also need to enable/disable the array column
        if (ArrayFieldCellHelper.getArrayField(comp) != null) {
          ArrayField af = ArrayFieldCellHelper.getArrayField(comp);
          int col = ArrayFieldCellHelper.getArrayFieldColumn(comp);

          // CraigM:18/10/2008 - Don't switch the array column if we are modifying components inside a grid field in the array field
          if (ArrayFieldCellHelper.isInGridFieldInArrayField(comp) == false) {
            // TF:13/10/2009:Corrected this to set our column state also based on the state of the array field.
            int worstState = getWorstState(af, state);
            boolean isEditable = (worstState == Constants.FS_UPDATE || worstState == Constants.FS_VIEWONLY || worstState == Constants.FS_SELECTONLY);
            // CraigM:22/08/2008 - Fixed to use real column
            ((ArrayColumn)((ArrayColumnModel)af.getColumnModel()).getRealColumn(col)).setEditable(isEditable);
          }
        }
    }
   
    private static void addSelectionListener(Component comp) {
      //remove the listener first to avoid duplications
      comp.removeMouseListener(SelectMouseListener.getInstance());
      comp.addMouseListener(SelectMouseListener.getInstance());
    }

    private static void removeSelectionListener(Component comp) {
      comp.removeMouseListener(SelectMouseListener.getInstance());
    }

    private static void enableScrollPane(Component comp, boolean value){
        if (comp != null &&
                comp.getParent() != null &&
                comp.getParent().getParent() != null &&
                comp.getParent().getParent() instanceof JScrollPane){
            Enabled.set(comp, value);
        }
    }

    private static void visibleScrollPane(Component comp, boolean value){
        if (comp != null &&
                comp.getParent() != null &&
                comp.getParent().getParent() != null &&
                comp.getParent().getParent() instanceof JScrollPane){
            Visible.set(comp, value);
        }
    }

    private static void focusableScrollPane(Component comp, boolean value){
        if (comp != null &&
                comp.getParent() != null &&
                comp.getParent().getParent() != null &&
                comp.getParent().getParent() instanceof JScrollPane){
            Focusable.set(comp, value);
        }
    }

    /**
     * sets the forte state on a column in an ArrayField or ListView (JTable)
     * @param comp
     * @param state
     */
    public static void set(GenericCellEditor comp, int state) {

        switch (state) {
        case Constants.FS_UPDATE:
            Enabled.set(comp, true);
            Visible.set(comp, true);
            break;
        case Constants.FS_DISABLED:
            Enabled.set(comp, false);
            Visible.set(comp, true);
            break;
        case Constants.FS_INVISIBLE:
            Enabled.set(comp, false);
            Visible.set(comp, false);
            break;
        case Constants.FS_VIEWONLY:
            Enabled.set(comp, false);
            Visible.set(comp, true);
            break;
        case Constants.FS_VISIBLE:
            Enabled.set(comp, false);
            Visible.set(comp, true);
            break;

        }
    }

    /**
     * The SetStateForAll attribute sets a menu widget to one state for all its parent window�s usages.
     * @param frame
     * @param comp
     * @param state
     */
    public static void setForAll(JFrame frame, JMenuItem menu, int state) {
        setForUsage(frame, (MenuElement) menu, Constants.WU_UPDATE, state);
        setForUsage(frame, (MenuElement) menu, Constants.WU_VIEW, state);
        setForUsage(frame, (MenuElement) menu, Constants.WU_QUERY, state);
        setForUsage(frame, (MenuElement) menu, Constants.WU_USER1, state);
        setForUsage(frame, (MenuElement) menu, Constants.WU_USER2, state);
        setForUsage(frame, (MenuElement) menu, Constants.WU_USER3, state);
    }
    public static void setForAll(JFrame frame, JPopupMenu menu, int state) {
        setForUsage(frame, (MenuElement) menu, Constants.WU_UPDATE, state);
        setForUsage(frame, (MenuElement) menu, Constants.WU_VIEW, state);
        setForUsage(frame, (MenuElement) menu, Constants.WU_QUERY, state);
        setForUsage(frame, (MenuElement) menu, Constants.WU_USER1, state);
        setForUsage(frame, (MenuElement) menu, Constants.WU_USER2, state);
        setForUsage(frame, (MenuElement) menu, Constants.WU_USER3, state);
    }

    /**
     * The SetStateForAll attribute sets a menu widget to one state for all its parent window�s usages.
     * @param frame
     * @param comp
     * @param state
     */
    public static void setForAll(JFrame frame, JSeparator menu, int state) {
        setForUsage(frame, menu, Constants.WU_UPDATE, state);
        setForUsage(frame, menu, Constants.WU_VIEW, state);
        setForUsage(frame, menu, Constants.WU_QUERY, state);
        setForUsage(frame, menu, Constants.WU_USER1, state);
        setForUsage(frame, menu, Constants.WU_USER2, state);
        setForUsage(frame, menu, Constants.WU_USER3, state);
    }

    public static void setForUsage(JFrame window, CompoundField field, int usage, int state) {
      setForUsage(window, (JComponent)field, usage, state);
    }
    public static void setForUsage(JFrame window, GridField field, int usage, int state) {
      setForUsage(window, (JComponent)field, usage, state);
    }
    public static void setForUsage(JFrame window, Panel field, int usage, int state) {
      setForUsage(window, (JComponent)field, usage, state);
    }
    public static void setForUsage(JFrame window, TabFolder field, int usage, int state) {
      setForUsage(window, (JComponent)field, usage, state);
    }
    public static void setForUsage(JFrame window, ArrayField field, int usage, int state) {
      setForUsage(window, (JComponent)field, usage, state);
    }
    /**
     * The setForUsage method sets a widget�s state for a specific
     * usage in its parent window. A widget can be set to a different state
     * for every window usage. A window can have a certain usage, as set in
     * its Usage attribute. Using different window usages, you can set
     * widgets to behave according to a with its Parent window; you can
     * achieve the same effect by manipulating State settings alone.
     *
     * Like widgets, windows also have behavioral states, but define them
     * according to specialized attributes provided by the Window class.
     *
     * Author Peter.
     *
     * CraigM: 24/04/2008: Switched to use UIutils method as it takes into account nested windows.
     * CraigM: 17/06/2008: Rewrote for new usage mapping.
     */
    public static void setForUsage(JFrame window, JComponent comp, int usage, int state) {
      // TF:08/01/2010:DET-95:Added support for usage maps off menu elements that may not be able to discover their
      // frames properly, such as invisible popup menus.
      WindowUsageMap winUsage  = WindowUsageMap.get(comp);
      if (winUsage == null) {
        winUsage = WindowUsage.getWindowUsageMap(window);
      }
    winUsage.setStateForUsage(comp, usage, state);

        // CraigM:18/08/2008 - If we're in the usage that is being set, explicitly set the usage of this component.
    if (winUsage.getUsage() == usage) {

      // Only need to do this if we were unable to get the window usage from the component.  This can happen with popup menus.
      if (UIutils.getWindowUsage(comp) != winUsage.getUsage()) {
        WidgetState.set(comp, state);
      }
    }
    }

//    public static void setForUsage(CharacterField comp, int Usage, int State) {
//      //PM:12/3/08 LUM-3
//      //PM:12/5/08 added support for nested windows
//      JFrame win = UIutils.getWindowForComponent((JComponent)comp);
//        if (win != null) {
//          setForUsage(win, (JComponent)comp, Usage, State);
//        }
//    }

    /**
     * CraigM: 17/06/2008: Rewrote for new usage mapping.
     *
     * @param window
     * @param comp
     * @param Usage
     * @param State
     */
    public static void setForUsage(JFrame window, MenuElement comp, int usage, int state) {
      WidgetState.setForUsage(window, (JComponent)comp, usage, state);
    }
   
    /**
     * G Karekatte: 20/10/2008: Refactored MenuSeparator with JSeparator.
     *
     * @param window
     * @param comp
     * @param usage
     * @param state
     */
    public static void setForUsage(JFrame window, JSeparator comp, int usage, int state) {
      WidgetState.setForUsage(window, (JComponent)comp, usage, state);
    }

    public static void setForUsage(JComponent comp, int usage, int state) {
      // TF:08/01/2010:DET-95:Added support for usage maps off menu elements that may not be able to discover their
      // frames properly, such as invisible popup menus.
      WindowUsageMap winUsage  = WindowUsageMap.get(comp);
      if (winUsage != null) {
        winUsage.setStateForUsage(comp, usage, state);
      }
      //PM:12/5/08 added support for nested windows
      // TF:02/12/2009:DET-95:Changed this to use the declared from, not the frame for the window as the usage map is stored on the declared frame.
//      JFrame win = UIutils.getWindowForComponent(comp);
      JFrame win = UIutils.getDeclaredFrame(comp);
        if (win != null) {
          WidgetState.setForUsage(win, comp, usage, state);
        }
    }

    /**
     * Returns the State of a Widget as a Forte State
     */
    public static int get(ArrayField comp) {
      // TF:20/05/2009:added this overload to prevent ambiguous call errors.
        return WidgetState.get((JComponent)comp);
    }

    /**
     * Returns the State of a Widget as a Forte State
     */
    public static int get(CompoundField comp) {
        if (comp == null) {
            return Constants.FS_UPDATE;
        }
        else if (comp instanceof JComponent) {
            return WidgetState.get((JComponent)comp);
        }
        else {
            return WidgetState.get((Component)comp);
        }
    }
    /**
     * Returns the State of a Widget as a Forte State
     */
    public static int get(GridField comp) {
        return get((CompoundField)comp);
    }

    /**
     * Returns the State of a Widget as a Forte State
     */
    public static int get(Panel comp) {
        return get((CompoundField)comp);
    }

    /**
     * Returns the State of a Widget as a Forte State
     *
     * @param comp
     * @return
     */
    public static int get(Component comp) {
        if (comp instanceof JMenuItem || comp instanceof JSeparator) {

            if (comp.isEnabled() && comp.isVisible()) {
                return Constants.MS_ENABLED;
            }

            if (!comp.isEnabled() && comp.isVisible()) {
                return Constants.MS_DISABLED;
            }

            if (!comp.isVisible()) {
                return Constants.MS_INVISIBLE;
            }

            // default
            throw new RuntimeException("Menu state not supported for: "
                    + comp.getName());
        } else {

          // CraigM: 17/07/2008 - Call the correct WidgetState.get() if possible
          if (comp instanceof JComponent) {
            return WidgetState.get((JComponent)comp);
          }
         
            if (comp.isEnabled() && comp.isVisible() && comp.isFocusable())
                return Constants.FS_UPDATE;

            if (!comp.isEnabled() && comp.isVisible() && !comp.isFocusable())
                return Constants.FS_DISABLED;

            if (!comp.isEnabled() && !comp.isVisible() && !comp.isFocusable())
                return Constants.FS_INVISIBLE;

            if (comp.isEnabled() && comp.isVisible() && !comp.isFocusable())
                return Constants.FS_VIEWONLY;

            if (comp.isEnabled() && comp.isVisible() && !comp.isFocusable())
                return Constants.FS_INACTIVE;

            // default
            throw new RuntimeException("Widget state not supported for: "
                    + comp.getName());
        }
    }

    /**
     * This method returns the worst state out of this component and the parents. The "worst state" is defined to be
     * the most restrictive state. So if the current control is UPDATE but the parent is VIEWONLY, the VIEWONLY is the
     * most restrictive state and hence should be the one that is returned.
     * @param comp
     * @return
     */
    public static int getWorstState(Component comp) {
      int state = WidgetState.get(comp);
      return WidgetState.getWorstState(comp, state);
    }
   
    public static int get(JComponent comp) {
      // TF:27/10/2009:Added in a check for a scroll pane, which should get its state from the underlying control
      if (comp instanceof JScrollPane) {
        Component realComp = ((JScrollPane)comp).getViewport().getView();
        return get(realComp);
      }
        Integer state = (Integer) comp.getClientProperty("qq_state");
        return (state == null) ? Constants.FS_UPDATE : state.intValue();
    }
   
    public static int get(JFrame comp) {
      return get(UIutils.getForm(comp));
    }

    public static int get(Container comp) {
        if (comp instanceof JComponent)
            return get((JComponent) comp);
        else
            return Constants.FS_UPDATE;
    }

    //    public static int get(Component comp){
    //    if (comp instanceof JComponent)
    //        return get((JComponent)comp);
    //    else
    //        throw new UsageException("Widget State not implemented for type: " + comp.getClass().getSimpleName());
    //    }

    public static int get(OutlineColumnDesc column) {
        return column.getState();
    }

    /**
     * gets the state of an ArrayColumn
     * @param comp
     * @return
     */
    public static int get(ArrayColumn comp) {
        if (!(Visible.is(comp))) {
            return Constants.FS_INVISIBLE;

        } else {
            return Constants.FS_INACTIVE;
        }
    }

    /**
     * This method emulates the forte state attribute on a menu, it return the state as the forte
     * constant
     *
     * @param comp
     * @param state
     */
    public static int get(JMenuItem comp) {
        Integer state = (Integer) comp.getClientProperty("qq_state");
        return (state == null) ? Constants.MS_ENABLED : state.intValue();
    }

    public static int get(MenuList comp) {
        Integer state = (Integer) comp.getClientProperty("qq_state");
        return (state == null) ? Constants.MS_ENABLED : state.intValue();
    }

    public static int get(JMenuBar comp) {
        Integer state = (Integer) comp.getClientProperty("qq_state");
        return (state == null) ? Constants.MS_ENABLED : state.intValue();
    }
    /**
     * This method emulates the forte state attribute on a menu, it return the state as the forte
     * constant
     *
     * @param comp
     * @param state
     */
    public static int get(MenuElement comp) {
        if (comp instanceof JMenuItem) {
            return get((JMenuItem) comp);
        }
        //PM:25 sept. 2008:hqndle MenuList
        else if (comp instanceof MenuList) {
            return get((MenuList) comp);
        }
        // Handle MenuSeparators. CraigM: 28/01/2008.
        else if (comp instanceof MenuSeparator) {
            return Constants.MS_DISABLED;
        }
        // default
        throw new RuntimeException("Menu state not supported for: "
                + comp.getClass().getName());
    }

    /**
     * The getForUsage method retrieves the state setting information for a widget in a given usage.
     * You can use GetStateForUsage as a convenient way to dynamically determine a widget�s state for any usage its parent window can take.
     * The GetStateForUsage method returns an integer that identifies the state of the widget in the particular usage.
     * @param comp
     * @param usage
     * @return
     */
    public static int getForUsage(JComponent comp, int usage) {
      // TF:08/01/2010:DET-95:Added support for usage maps off menu elements that may not be able to discover their
      // frames properly, such as invisible popup menus.
      WindowUsageMap map = WindowUsageMap.get(comp);
      if (map != null) {
        return map.getStateForUsage(comp, usage);
      }

      //PM:12/5/08 added support for nested windows
      // JFrame win = (JFrame)UIutils.getWindowForComponent((JComponent)comp).getParent();
     
      // CraigM:17/06/2008 - JFrames don't have parents
      // TF:02/12/2009:DET-95:Changed this to use the declared from, not the frame for the window as the usage map is stored on the declared frame.
//      JFrame win = UIutils.getWindowForComponent((JComponent)comp);
      JFrame win = UIutils.getDeclaredFrame(comp);

      return getForUsage(win, comp, usage);
    }

    public static int getForUsage(MenuElement comp, int usage) {
      // TF:08/01/2010:DET-95:Added support for usage maps off menu elements that may not be able to discover their
      // frames properly, such as invisible popup menus.
      JComponent component = (JComponent)comp.getComponent();
      WindowUsageMap map = WindowUsageMap.get(component);
      if (map != null) {
        return map.getStateForUsage(component, usage);
      }
     
      //PM:12/5/08 added support for nested windows
      // JFrame win = (JFrame)UIutils.getWindowForComponent((JComponent)comp).getParent();

      // CraigM:17/06/2008 - JFrames don't have parents
      // TF:02/12/2009:DET-95:Changed this to use the declared from, not the frame for the window as the usage map is stored on the declared frame.
//      JFrame win = UIutils.getWindowForComponent((JComponent)comp);
      JFrame win = UIutils.getDeclaredFrame((JComponent)comp);

      //PM:12/5/08 JCT-540
      if (isUDSWindow(win) ){
        return getForUsage(win, (JComponent)comp, usage);
      } else {
        if (comp instanceof JMenu) {
          return getForUsage(win, (JMenu) comp, usage);
        } else if (comp instanceof JMenuBar) {
          return getForUsage(win, (JMenuBar) comp, usage);
        } else if (comp instanceof JMenuItem) {
          return getForUsage(win, (JMenuItem) comp, usage);
        } else if (comp instanceof JCheckBoxMenuItem) {
          return getForUsage(win, (JCheckBoxMenuItem) comp, usage);
        } else if (comp instanceof JRadioButtonMenuItem) {
          return getForUsage(win, (JRadioButtonMenuItem) comp, usage);
        }
        return Constants.MS_ENABLE;
      }
    }

    public static int getForUsage(JFrame window, JComponent comp, int Usage) {
      // CraigM:17/06/2008 - Check for null
      if (window == null) {
          // TF:02/12/2009:DET-95:Changed this to use the declared from, not the frame for the window as the usage map is stored on the declared frame.
//        window = UIutils.getWindowForComponent(comp);
        window = UIutils.getDeclaredFrame(comp);

        if (window == null) {
          return Constants.FS_UPDATE;
        }
      }
     
    WindowUsageMap winUsage = WindowUsage.getWindowUsageMap(window);
    return winUsage.getStateForUsage(comp, Usage);
    }

    public static void set(Component comp, int state) {
      if (comp instanceof JMenu) {
        set((JMenu)comp, state);
      } else if (comp instanceof JComponent) {
        set((JComponent)comp, state);
      }
      else {
          switch (state) {
          case Constants.FS_UPDATE:
              Enabled.set(comp, true);
              Visible.set(comp, true);
              Focusable.set(comp, true);
              //removeSelectionListener(comp);
              break;
          case Constants.FS_DISABLED:
              Enabled.set(comp, false);
              Visible.set(comp, true);
              Focusable.set(comp, false);
              //removeSelectionListener(comp);
              break;
          case Constants.FS_INVISIBLE:
              Enabled.set(comp, false);
              Visible.set(comp, false);
              Focusable.set(comp, false);
              //removeSelectionListener(comp);
              break;
          case Constants.FS_VIEWONLY:
              Enabled.set(comp, false);
              Visible.set(comp, true);
              Focusable.set(comp, true);
              //removeSelectionListener(comp);
              break;
          case Constants.FS_INACTIVE:
              Enabled.set(comp, true);
              Visible.set(comp, true);
              Focusable.set(comp, false);
              //removeSelectionListener(comp);
              break;
          case Constants.FS_SELECTONLY:
              Enabled.set(comp, true);
              Visible.set(comp, true);
              Focusable.set(comp, false);
              //e itcomp.addMouseListener(new SelectMouseListener());
              break;
          }
//          if (comp instanceof JFrame) {
//            Container contentPane = ((JFrame)comp).getContentPane();
//            set(contentPane, state);
//          }
      }
    }
    /**
     * Determines if a JFrame is declared as a UDS window
     * @param win
     * @return
     */
    public static boolean isUDSWindow(JFrame win){
      return (win.getClass().getAnnotation(UDSWindow.class) != null);
    }
}
TOP

Related Classes of DisplayProject.actions.WidgetState

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.