Package DisplayProject.actions

Source Code of DisplayProject.actions.ScrollBar$ScrollBarFilter

/*
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.event.AdjustmentListener;

import javax.swing.JScrollBar;

import DisplayProject.Constants;
import DisplayProject.actions.ActionMgr.Filter;

/**
* Provides EDT safe operations on a JScrollBar
*
*/
public class ScrollBar extends PendingAction {

  enum ScrollBarAction {
    ORIENTATION, VALUE, MAX, MIN, VIEWSIZE;
  }

  private ScrollBarAction action;
  private int orientation;
  private int value;
  private int max;
  private int min;
  private int viewSize;

  private ScrollBar(Component pComponent, int value, ScrollBarAction action) {
    super(pComponent);
    this.action = action;
    switch (this.action) {
    case ORIENTATION:
      this.orientation = value;
    case VALUE:
      this.value = value;
      break;
    case MAX:
      this.max = value;
      break;
    case MIN:
      this.min = value;
      break;
    case VIEWSIZE:
      this.viewSize = value;
      break;
    }
  }

  public void performAction() {
    JScrollBar sb = ((JScrollBar) this._component);
    switch (this.action) {
    case ORIENTATION:
      switch (this.orientation) {
      case Constants.FO_HORIZONTAL:
        sb.setOrientation(JScrollBar.HORIZONTAL);
        break;
      case Constants.FO_VERTICAL:
        sb.setOrientation(JScrollBar.VERTICAL);
        break;
      }
      break;
    case VALUE:
      AdjustmentListener[] als = sb.getAdjustmentListeners();
      for (int i = 0; i < als.length; i++)
        sb.removeAdjustmentListener(als[i]);
      sb.setValue(this.value);
      for (int i = 0; i < als.length; i++)
        sb.addAdjustmentListener(als[i]);

      break;
    case MAX:
      AdjustmentListener[] alsx = sb.getAdjustmentListeners();
      for (int i = 0; i < alsx.length; i++)
        sb.removeAdjustmentListener(alsx[i]);
      sb.setMaximum(this.max);
      for (int i = 0; i < alsx.length; i++)
        sb.addAdjustmentListener(alsx[i]);
      break;
    case MIN:
      AdjustmentListener[] alsn = sb.getAdjustmentListeners();
      for (int i = 0; i < alsn.length; i++)
        sb.removeAdjustmentListener(alsn[i]);
      sb.setMinimum(this.min);
      for (int i = 0; i < alsn.length; i++)
        sb.addAdjustmentListener(alsn[i]);
      break;
    case VIEWSIZE:
      AdjustmentListener[] alsv = sb.getAdjustmentListeners();
      for (int i = 0; i < alsv.length; i++)
        sb.removeAdjustmentListener(alsv[i]);
      sb.setBlockIncrement(this.viewSize);
      sb.setVisibleAmount(this.viewSize);
      for (int i = 0; i < alsv.length; i++)
        sb.addAdjustmentListener(alsv[i]);
      break;
    }
  }

  public static void setOrientation(JScrollBar comp, int value) {
    ActionMgr.addAction(new ScrollBar(comp, value, ScrollBarAction.ORIENTATION));
  }

  public static void setValue(JScrollBar comp, int value) {
    ActionMgr.addAction(new ScrollBar(comp, value, ScrollBarAction.VALUE));
  }

  public static void setValueImmediate(JScrollBar comp, int value) {
    AdjustmentListener[] als = comp.getAdjustmentListeners();
    for (int i = 0; i < als.length; i++)
      comp.removeAdjustmentListener(als[i]);
    comp.setValue(value);
    for (int i = 0; i < als.length; i++)
      comp.addAdjustmentListener(als[i]);
  }

  public static void setMinimumValueImmediate(JScrollBar comp, int value) {
    AdjustmentListener[] als = comp.getAdjustmentListeners();
    for (int i = 0; i < als.length; i++)
      comp.removeAdjustmentListener(als[i]);
    comp.setMinimum(value);
    for (int i = 0; i < als.length; i++)
      comp.addAdjustmentListener(als[i]);
  }

  public static void setMaximumValueImmediate(JScrollBar comp, int value) {
    AdjustmentListener[] als = comp.getAdjustmentListeners();
    for (int i = 0; i < als.length; i++)
      comp.removeAdjustmentListener(als[i]);
    comp.setMaximum(value);
    for (int i = 0; i < als.length; i++)
      comp.addAdjustmentListener(als[i]);
  }

  public static void setViewSizeImmediate(JScrollBar comp, int value) {
    AdjustmentListener[] als = comp.getAdjustmentListeners();
    for (int i = 0; i < als.length; i++)
      comp.removeAdjustmentListener(als[i]);
    comp.setBlockIncrement(value);
    comp.setVisibleAmount(value);
    for (int i = 0; i < als.length; i++)
      comp.addAdjustmentListener(als[i]);
  }

  public static void setMinimumValue(JScrollBar comp, int value) {
    ActionMgr.addAction(new ScrollBar(comp, value, ScrollBarAction.MIN));
  }

  public static void setMaximumValue(JScrollBar comp, int value) {
    ActionMgr.addAction(new ScrollBar(comp, value, ScrollBarAction.MAX));
  }

  public static void setViewSize(JScrollBar comp, int value) {
    ActionMgr.addAction(new ScrollBar(comp, value, ScrollBarAction.VIEWSIZE));
  }

  public static int getViewSize(JScrollBar comp) {
    ScrollBar action = (ScrollBar) ActionMgr.getAction(new ScrollBarFilter(comp, ScrollBarAction.VIEWSIZE));
    if (action != null) {
      return action.viewSize;
    }
    return comp.getUnitIncrement();
  }

  public static int getValue(JScrollBar comp) {
    ScrollBar action = (ScrollBar) ActionMgr.getAction(new ScrollBarFilter(comp, ScrollBarAction.VALUE));
    if (action != null) {
      return action.value;
    }
    return comp.getValue();
  }

  public static int getMaximumValue(JScrollBar comp) {
    ScrollBar action = (ScrollBar) ActionMgr.getAction(new ScrollBarFilter(comp, ScrollBarAction.MAX));
    if (action != null) {
      return action.max;
    }
    return comp.getMaximum();
  }

  public static int getMinimumValue(JScrollBar comp) {
    ScrollBar action = (ScrollBar) ActionMgr.getAction(new ScrollBarFilter(comp, ScrollBarAction.MIN));
    if (action != null) {
      return action.min;
    }
    return comp.getMinimum();
  }

  public static int getOrientation(JScrollBar comp) {
    int align = JScrollBar.HORIZONTAL;
    ScrollBar action = (ScrollBar) ActionMgr.getAction(new ScrollBarFilter(comp, ScrollBarAction.ORIENTATION));
    if (action != null) {
      align = action.orientation;
      return align;
    }
    switch (comp.getOrientation()) {
    case JScrollBar.HORIZONTAL:
      align = Constants.FO_HORIZONTAL;
      break;
    case JScrollBar.VERTICAL:
      align = Constants.FO_VERTICAL;
      break;
    }
    return align;
  }

  private static class ScrollBarFilter implements Filter {

    private Component component;
    private ScrollBarAction action;

    public ScrollBarFilter(Component comp, ScrollBarAction action) {
      this.component = comp;
      this.action = action;
    }

    public boolean filter(PendingAction pAction) {
      return (pAction instanceof ScrollBar) && ((ScrollBar) pAction).action.equals(action) && pAction.getComponent() == component;
    }
  }
}
TOP

Related Classes of DisplayProject.actions.ScrollBar$ScrollBarFilter

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.