📄 oyoahautilities.java
字号:
/* ====================================================================
* Copyright (c) 2001-2003 OYOAHA. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. The names "OYOAHA" must not be used to endorse or promote products
* derived from this software without prior written permission.
* For written permission, please contact email@oyoaha.com.
*
* 3. Products derived from this software may not be called "OYOAHA",
* nor may "OYOAHA" appear in their name, without prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 OYOAHA OR ITS 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 com.oyoaha.swing.plaf.oyoaha;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;
import com.oyoaha.swing.plaf.oyoaha.ui.*;
import com.oyoaha.swing.plaf.oyoaha.pool.*;
public class OyoahaUtilities
{
//component state
public final static int UNSELECTED_ENABLED = 1000;
public final static int UNSELECTED_PRESSED = 1001;
public final static int UNSELECTED_ROLLOVER = 1002;
public final static int UNSELECTED_DISABLED = 1003;
public final static int SELECTED_ENABLED = 1004;
public final static int SELECTED_PRESSED = 1005;
public final static int SELECTED_ROLLOVER = 1006;
public final static int SELECTED_DISABLED = 1007;
//invisible state
public final static int UNVISIBLE = 1008;
//extra state and color
public final static int FOCUS = 100;
//color
public final static int WHITE = 101;
public final static int BLACK = 102;
public final static int GRAY = 103;
private static OyoahaPaintUtilities paint;
private static OyoahaSoundUtilities sound;
private static OyoahaLookAndFeel oyoahaLookAndFeel;
private static OyoahaImageLoader loader;
//default color pool
protected static OyoahaColorPool pool;
//private static Hashtable alphaClasses;
public final static boolean isVersion(String _string)
{
String p = System.getProperty("java.version");
if(p!=null)
{
if(p.compareTo(_string)<0)
{
return false;
}
else
{
return true;
}
}
return false;
}
/*public final static boolean isThisVersion(String _string)
{
String p = System.getProperty("java.version");
return p.equals(_string);
}*/
public final static void initialize(OyoahaLookAndFeel _oyoahaLookAndFeel)
{
uninitialize(oyoahaLookAndFeel);
oyoahaLookAndFeel = _oyoahaLookAndFeel;
//to keep information about alpha class
//alphaClasses = new Hashtable();
//to load image with a mediatracker
loader = new OyoahaImageLoader();
pool = null;
boolean java2 = isVersion("1.2");
if(!java2)
{
paint = new OyoahaJava1PaintUtilities();
}
else
{
paint = new OyoahaJava2PaintUtilities();
}
sound = null;
paint.initialize();
}
/**
*
*/
public final static void uninitialize(OyoahaLookAndFeel _oyoahaLookAndFeel)
{
if(oyoahaLookAndFeel==null || oyoahaLookAndFeel!=_oyoahaLookAndFeel)
return;
if(animation!=null)
animation.stop();
if(paint!=null)
paint.uninitialize();
if(sound!=null)
sound.uninitialize();
}
//- - - - - - - - - - - - - - - -
//
// GENERAL ROUTINE
//
//- - - - - - - - - - - - - - - -
private static OyoahaRolloverListener mouseListener;
/**
*
*/
public final static OyoahaRolloverListener getOyoahaRolloverListener()
{
if(mouseListener==null)
{
mouseListener = new OyoahaRolloverListener();
}
return mouseListener;
}
/**
*
*/
public final static void installRolloverListener(Component c)
{
if(OyoahaUtilities.isRolloverEnabled(c))
{
if(c instanceof AbstractButton && ((AbstractButton)c).isRolloverEnabled())
{
return;
}
c.addMouseListener(getOyoahaRolloverListener());
}
}
/**
*
*/
public final static void uninstallRolloverListener(Component c)
{
if(mouseListener!=null)
{
c.removeMouseListener(mouseListener);
}
}
/**
*
*/
public final static int getStatus(Component c)
{
int r=UNSELECTED_ENABLED;
if(c instanceof OyoahaListCellRenderer)
{
r = ((OyoahaListCellRenderer)c).getStatus();
}
else
if(c instanceof OyoahaTreeCellRenderer)
{
r = ((OyoahaTreeCellRenderer)c).getStatus();
}
else
if(c instanceof AbstractButton)
{
AbstractButton b = (AbstractButton)c;
ButtonModel m = b.getModel();
Container parent = b.getParent();
if(b.isEnabled())
{
if(!(b instanceof MenuElement))
{
if(m.isSelected())
{
if(m.isArmed() && m.isPressed())
r=SELECTED_PRESSED;
else
if(m.isRollover() || isRollover(c))
r=SELECTED_ROLLOVER;
else
r=SELECTED_ENABLED;
}
else
{
if(m.isArmed() && m.isPressed())
r=UNSELECTED_PRESSED;
else
if(m.isRollover() || isRollover(c))
r=UNSELECTED_ROLLOVER;
}
}
else
{
if(m.isSelected())
{
if(isPressed(c) || m.isPressed())
r=SELECTED_PRESSED;
else
if(m.isArmed() || m.isRollover() || isRollover(c))
r=SELECTED_ROLLOVER;
else
r=SELECTED_ENABLED;
}
else
{
if(isPressed(c) || m.isPressed())
r=UNSELECTED_PRESSED;
else
if(m.isArmed() || m.isRollover() || isRollover(c))
r=UNSELECTED_ROLLOVER;
}
}
}
else
{
if(m.isSelected())
r=SELECTED_DISABLED;
else
r=UNSELECTED_DISABLED;
}
}
else
if(c instanceof JInternalFrame)
{
if(!c.isEnabled())
{
if(((JInternalFrame)c).isSelected())
r=SELECTED_DISABLED;
else
r=UNSELECTED_DISABLED;
}
else
if(((JInternalFrame)c).isSelected())
{
if(isPressed(c))
{
r=SELECTED_PRESSED;
}
else
if(isRollover(c))
{
r=SELECTED_ROLLOVER;
}
else
{
r=SELECTED_ENABLED;
}
}
else
{
if(isPressed(c))
{
r=UNSELECTED_PRESSED;
}
else
if(isRollover(c))
{
r=UNSELECTED_ROLLOVER;
}
}
}
else
{
if(!c.isEnabled())
{
r=UNSELECTED_DISABLED;
}
else
if(isPressed(c))
{
r=UNSELECTED_PRESSED;
}
else
if(isRollover(c))
{
r=UNSELECTED_ROLLOVER;
}
}
return r;
}
/**
*
*/
public final static boolean isPressed(Component c)
{
if(mouseListener!=null)
{
return mouseListener.isPressed(c);
}
return false;
}
/**
*
*/
public final static boolean isRollover(Component c)
{
if(mouseListener!=null)
{
return mouseListener.isRollover(c);
}
return false;
}
/**
*
*/
public final static Component getPressed()
{
if(mouseListener!=null)
{
return mouseListener.getPressed();
}
return null;
}
/**
*
*/
public final static Component getRollover()
{
if(mouseListener!=null)
{
return mouseListener.getRollover();
}
return null;
}
//- - - - - - - - - - - - - - - -
//
// SOUND ROUTINE
//
//- - - - - - - - - - - - - - - -
/**
*
*/
public static void playStartClick()
{
if(sound!=null)
sound.playStartClick();
}
/**
*
*/
public static void playStopClick()
{
if(sound!=null)
sound.playStopClick();
}
//- - - - - - - - - - - - - - - -
//
// ANIMATED ROUTINE
//
//- - - - - - - - - - - - - - - -
private static OyoahaUpdater animation;
public static void initializeAnimation(boolean _focus, boolean _background)
{
animation = new OyoahaUpdater(_focus, _background);
animation.start();
}
public static void initializeSound(String pressed, String scrolled)
{
if(pressed==null && scrolled==null)
return;
if(!isVersion("1.3"))
{
sound = new OyoahaJava1SoundUtilities();
}
else
{
sound = new OyoahaJava2SoundUtilities();
}
sound.initialize(pressed, scrolled);
}
//- - - - - - - - - - - - - - - -
//
// GET SOME INFORMATION ABOUT A COMPONENT
//
//- - - - - - - - - - - - - - - -
/**
*
*/
public final static boolean hasFocus(Component c)
{
return paint.hasFocus(c);
}
/**
*
*/
public final static boolean isDefaultButton(Component c)
{
return paint.isDefaultButton(c);
}
/**
*
*/
public final static boolean isLeftToRight(Component c)
{
return paint.isLeftToRight(c);
}
/**
*
*/
public final static Rectangle getFullRect(Component c)
{
Rectangle r = c.getBounds();
r.x=0; r.y=0;
return r;
}
/**
*
*/
public final static Rectangle getViewRect2(Component c)
{
Rectangle r = getFullRect(c);
if (c instanceof JComponent)
{
Insets i = ((Container)c).getInsets();
r.x += i.left;
r.y += i.top;
r.width -= i.right+r.x;
r.height -= i.bottom+r.y;
}
else
if(c instanceof Container)
{
Insets i = ((Container)c).getInsets();
r.x += i.left;
r.y += i.top;
r.width -= i.right+r.x;
r.height -= i.bottom+r.y;
}
return r;
}
/**
*
*/
public final static Rectangle getViewRect(Component c)
{
Rectangle r = getFullRect(c);
Insets i = getRealInsets(c);
if(i!=null)
{
r.x += i.left;
r.y += i.top;
r.width -= i.right+r.x;
r.height -= i.bottom+r.y;
}
else
{
if (c instanceof JComponent)
{
Border border = ((JComponent)c).getBorder();
if (border instanceof OyoahaButtonBorderLike)
{
OyoahaButtonBorderLike bbl = (OyoahaButtonBorderLike)border;
r.x += bbl.getLeftInsets();
r.y += bbl.getTopInsets();
r.width -= bbl.getRightInsets()+r.x;
r.height -= bbl.getBottomInsets()+r.y;
}
else
{
i = ((Container)c).getInsets();
r.x += i.left;
r.y += i.top;
r.width -= i.right+r.x;
r.height -= i.bottom+r.y;
}
}
else
if(c instanceof Container)
{
i = ((Container)c).getInsets();
r.x += i.left;
r.y += i.top;
r.width -= i.right+r.x;
r.height -= i.bottom+r.y;
}
}
return r;
}
public final static Insets getRealInsets(Component c)
{
return getRealInsets(getUIClassID(c));
}
/**
*
*/
public final static Insets getRealInsets(String ui)
{
Object o = UIManager.get(ui+".realInsets");
if(o!=null && o instanceof Insets)
{
return (Insets)o;
}
return null;
}
/**
*
*/
public final static Point getAPosition(Component c)
{
return paint.getAPosition(c);
}
/**
*
*/
public final static boolean intersects(int x, int y, int width, int height, int x1, int y1, int width1, int height1)
{
return paint.intersects(x, y, width, height, x1, y1, width1, height1);
}
/**
*
*/
public final static Shape normalizeClip(Graphics g, int x, int y, int width, int height)
{
return paint.normalizeClip(g, x, y, width, height);
}
/**
*
*/
public final static boolean isComponentOpaque(Component c)
{
return paint.isOpaque(c);
}
/**
*
*/
public final static boolean isOpaque(Component c)
{
/*if (c instanceof OyoahaButtonLikeComponent)
{
//return ((OyoahaButtonLikeComponent)c).isOpaque();
return true;
}
ComponentUI cui = (ComponentUI)UIManager.getUI((JComponent)c);
if (cui!=null && cui instanceof OyoahaButtonLikeComponent)
{
if(!((OyoahaButtonLikeComponent)cui).isBorderPainted(c))
return false;
return true; //((OyoahaButtonLikeComponent)c).isAlphaClasses();
}*/
/*String ui = OyoahaUtilities.getUIClassID(c);
if(alphaClasses.containsKey(ui))
{
if(((Boolean)alphaClasses.get(ui)).booleanValue())
{
return true;
}
}*/
OyoahaButtonLikeComponent blc = getOyoahaButtonLikeComponent(c);
if (blc!=null)
{
return true;
}
return paint.isOpaque(c);
}
/**
*
*/
public final static Color getBackground(Component c)
{
return paint.getBackground(c);
}
/**
*
*/
public final static Color getForeground(Component c)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -