Package DisplayProject.actions

Source Code of DisplayProject.actions.WidgetSelected

/*
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.Color;
import java.awt.Component;
import java.awt.Window;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.HashSet;
import java.util.Iterator;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;

import DisplayProject.controls.MenuList;
import DisplayProject.controls.PictureGraphic;

/**
* Gets and sets the the Widget as being a 'selected' widget on a form
*
*/
public class WidgetSelected extends PendingAction {

    private static final String OLD_OPAQUE = "qq_OldOpaque";
  private static final String OLD_BACKGROUND = "qq_OldBackground";
  private static final String OLD_FOREGROUND = "qq_OldForeground";
  private static final String IS_SELECTED = "qq_IsSelected";
 
  // TF:01/11/2008:Made sure that if the window was hidden, the selection is removed. This is
  // necessary in case people hide and re-show the window.
  private static final WindowListener listener = new WindowAdapter() {
    @SuppressWarnings("unchecked")
    public void windowClosed(WindowEvent e) {
      // Remove all the children from being selected
          JRootPane root;
          if (e.getComponent() instanceof JFrame) {
            root = ((JFrame)e.getComponent()).getRootPane();
            if (root!=null){
                HashSet<JComponent> selectedKids = (HashSet<JComponent>)root.getClientProperty("qq_selectedKids");
                if (selectedKids == null){
                  return;
                }
                  Iterator<JComponent> it = selectedKids.iterator();
                  while (it.hasNext()){
                      JComponent kid = it.next();
                      resetSelectedComponent(kid, selectedKids, it);
                      kid.putClientProperty(IS_SELECTED, new Boolean(false));
                  }
                  selectedKids.clear();
              }
          }
    };
  };
 
  private boolean value;
    private boolean add;
    public WidgetSelected(Component pComponent, boolean pValue) {
        super(pComponent);
        this.value = pValue;
        this.add = false;
    }
    public WidgetSelected(Component pComponent, boolean pValue, boolean add) {
        this(pComponent, pValue);
        this.add = add;
    }
    @SuppressWarnings("unchecked")
  public void performAction() {

        JRootPane root = ((JComponent)this._component).getRootPane();
      JComponent target = (JComponent)this._component;
        if (root!=null){
            HashSet<JComponent> selectedKids = (HashSet<JComponent>)root.getClientProperty("qq_selectedKids");
            if (selectedKids == null){
                selectedKids = new HashSet<JComponent>();
                root.putClientProperty("qq_selectedKids", selectedKids);
            }
            if (!add){
                // no shift key
                Iterator<JComponent> it = selectedKids.iterator();
                while (it.hasNext()){
                    JComponent kid = it.next();
                    resetSelected(kid, selectedKids, it);
                    kid.putClientProperty(IS_SELECTED, new Boolean(false));
                }
                selectedKids.clear();
            }
            boolean isSelectedAlready = (target.getClientProperty(IS_SELECTED)==null) ? false : ((Boolean)((JComponent)this._component).getClientProperty(IS_SELECTED)).booleanValue();

            if ((this.value) && !isSelectedAlready){
              setSelected(target, selectedKids);
            } else {
              resetSelected(target, selectedKids, null);
            }
        }
    }
   
    private void setSelected(JComponent target, HashSet<JComponent> selectedKids){
      // Don't border menu lists.  CraigM 22/10/2007
      if (this._component instanceof MenuList) {
        return;
      }
      if (target instanceof PictureGraphic){
        PictureGraphic pg = (PictureGraphic)target;
            pg.setNegative(true);
      }
      target.putClientProperty(OLD_OPAQUE, new Boolean(target.isOpaque()));
      target.putClientProperty(OLD_BACKGROUND, target.getBackground());
      target.putClientProperty(OLD_FOREGROUND, target.getForeground());
      target.setOpaque(true);
      target.setBackground(Color.black);
      target.setForeground(Color.white);

      target.putClientProperty(IS_SELECTED, new Boolean(true));
      selectedKids.add(target);
      Window frame = (Window)target.getTopLevelAncestor();
      frame.removeWindowListener(listener);
      frame.addWindowListener(listener);
    }
   
    private void resetSelected(JComponent target, HashSet<JComponent> selectedKids, Iterator<JComponent> it){
      // Don't border menu lists.  CraigM 22/10/2007
      if (this._component instanceof MenuList) {
        return;
      }
      else {
        resetSelectedComponent(target, selectedKids, it);
      }
    }
   
    private static void resetSelectedComponent(JComponent target, HashSet<JComponent> selectedKids, Iterator<JComponent> it){
      if (target instanceof PictureGraphic){
        PictureGraphic pg = (PictureGraphic)target;
            pg.setNegative(false);
      }

      Boolean oldOpaque = (Boolean)target.getClientProperty(OLD_OPAQUE);
      Color OldBackground = (Color)target.getClientProperty(OLD_BACKGROUND);
      Color OldForeground = (Color)target.getClientProperty(OLD_FOREGROUND);
      target.setOpaque(true);
      target.setBackground(OldBackground);
      target.setForeground(OldForeground);
      if (oldOpaque != null)
        target.setOpaque(oldOpaque);
      target.putClientProperty(IS_SELECTED, new Boolean(false));
      if (it != null) {
        it.remove();
      }
      else {
        selectedKids.remove(target);
      }
      if (selectedKids.size() == 0) {
          Window frame = (Window)target.getTopLevelAncestor();
          frame.removeWindowListener(listener);
      }
    }

   
    public static void set(Component comp, boolean value, boolean add){
      WidgetSelected action = new WidgetSelected(comp, value, add);
      if (SwingUtilities.isEventDispatchThread())
        action.performAction();
      else
        ActionMgr.addAction(action);
    }
    public static void set(Component comp, boolean value){
      WidgetSelected action = new WidgetSelected(comp, value);
      if (SwingUtilities.isEventDispatchThread())
        action.performAction();
      else
        ActionMgr.addAction(action);
    }

    public static boolean is(JComponent comp){
      WidgetSelected action = ActionMgr.getAction(comp, WidgetSelected.class);
      if (action != null) {
        return action.value;
      }
      return (comp.getClientProperty(IS_SELECTED) == null) ? false : ((Boolean)comp.getClientProperty(IS_SELECTED)).booleanValue();
    }

}
TOP

Related Classes of DisplayProject.actions.WidgetSelected

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.