📄 component.java.svn-base
字号:
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
package com.sun.lwuit;
import com.sun.lwuit.geom.Rectangle;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.plaf.Style;
import com.sun.lwuit.animations.Animation;
import com.sun.lwuit.animations.Motion;
import com.sun.lwuit.events.FocusListener;
import com.sun.lwuit.events.StyleListener;
import com.sun.lwuit.plaf.Border;
import com.sun.lwuit.plaf.LookAndFeel;
import com.sun.lwuit.plaf.UIManager;
import java.util.Hashtable;
import java.util.Vector;
/**
* Base class for all the widgets in the toolkit using the composite pattern in
* a similar way to the AWT Container/Component relationship. All components are
* potentially animated (need to be registered in {@link Display}).
*
* @author Chen Fishbein
*/
public class Component implements Animation, StyleListener {
/**
* Allows us to determine which component will receive focus next when travering
* with the down key
*/
private Component nextFocusDown;
private Component nextFocusUp;
/**
* Indicates whether component is enabled or disabled
*/
private boolean enabled = true;
/**
* Allows us to determine which component will receive focus next when travering
* with the right key
*/
private Component nextFocusRight;
private Component nextFocusLeft;
/**
* Baseline resize behavior constant used to properly align components.
* Indicates as the size of the component
* changes the baseline remains a fixed distance from the top of the
* component.
* @see #getBaselineResizeBehavior
*/
public static final int BRB_CONSTANT_ASCENT = 1;
/**
* Baseline resize behavior constant used to properly align components. Indicates as the size of the component
* changes the baseline remains a fixed distance from the bottom of the
* component.
* @see #getBaselineResizeBehavior
*/
public static final int BRB_CONSTANT_DESCENT = 2;
/**
* Baseline resize behavior constant used to properly align components. Indicates as the size of the component
* changes the baseline remains a fixed distance from the center of the
* component.
* @see #getBaselineResizeBehavior
*/
public static final int BRB_CENTER_OFFSET = 3;
/**
* Baseline resize behavior constant used to properly align components. Indicates as the size of the component
* changes the baseline can not be determined using one of the other
* constants.
* @see #getBaselineResizeBehavior
*/
public static final int BRB_OTHER = 4;
private boolean visible = true;
/**
* Used as an optimization to mark that this component is currently being
* used as a cell renderer
*/
private boolean cellRenderer;
/**
* Indicates that this component is fixed into place and not affected by
* scrolling of the parent container. This is applicable for components such as
* menus etc...
*/
private boolean fixedPosition;
private Rectangle bounds = new Rectangle(0, 0, new Dimension(0, 0));
private int scrollX;
private int scrollY;
private Dimension preferredSize;
private Style style;
private Container parent;
private boolean focused = false;
private boolean focusPainted = true;
private EventDispatcher focusListeners = new EventDispatcher();
private boolean handlesInput = false;
private boolean shouldCalcPreferredSize = true;
private boolean focusable = true;
private boolean isScrollVisible = true;
/**
* Indicates that moving through the component should work as an animation
*/
private boolean smoothScrolling;
/**
* Animation speed in milliseconds allowing a developer to slow down or accelerate
* the smooth animation mode
*/
private int animationSpeed;
private Motion animationMotion;
private Motion draggedMotion;
/**
* Allows us to flag a drag operation in action thus preventing the mouse pointer
* release event from occuring.
*/
private boolean dragActivated;
private int initialScrollY = -1;
private int destScrollY = -1;
private int lastScrollY;
private int beforeLastScrollY;
private long[] lastTime = new long[2];
private int[] lastDragged = new int[2];
private int pLastDragged = 0;
/**
* Indicates if the component is in the initalized state, a component is initialized
* when its initComponent() method was invoked. The initMethod is invoked before showing the
* component to the user.
*/
private boolean initialized;
/**
* Indicates a Component center alignment
*/
public static final int CENTER = 4;
/**
* Box-orientation constant used to specify the top of a box.
*/
public static final int TOP = 0;
/**
* Box-orientation constant used to specify the left side of a box.
*/
public static final int LEFT = 1;
/**
* Box-orientation constant used to specify the bottom of a box.
*/
public static final int BOTTOM = 2;
/**
* Box-orientation constant used to specify the right side of a box.
*/
public static final int RIGHT = 3;
private Hashtable clientProperties;
private Rectangle dirtyRegion = null;
/**
* Creates a new instance of Component
*/
protected Component() {
style = UIManager.getInstance().getComponentStyle(getUIID());
if (style != null) {
style.addStyleListener(this);
style.setBgPainter(new BGPainter());
}
LookAndFeel laf = UIManager.getInstance().getLookAndFeel();
animationSpeed = laf.getDefaultSmoothScrollingSpeed();
setSmoothScrolling(laf.isDefaultSmoothScrolling());
}
/**
* Returns the current component x location relatively to its parent container
*
* @return the current x coordinate of the components origin
*/
public int getX() {
return bounds.getX();
}
/**
* Returns the component y location relatively to its parent container
*
* @return the current y coordinate of the components origin
*/
public int getY() {
return bounds.getY();
}
/**
* Returns whether the component is visible or not
*
* @return true if component is visible; otherwise false
*/
public boolean isVisible() {
return visible;
}
/**
* Client properties allow the association of meta-data with a component, this
* is useful for some applications that construct GUI's on the fly and need
* to track the connection between the UI and the data.
*
* @param key the key used for putClientProperty
* @return the value set to putClientProperty or null if no value is set to the property
*/
public Object getClientProperty(String key) {
if (clientProperties == null) {
return null;
}
return clientProperties.get(key);
}
/**
* Client properties allow the association of meta-data with a component, this
* is useful for some applications that construct GUI's on the fly and need
* to track the connection between the UI and the data. Setting the value to
* null will remove the client property from the component.
*
* @param key arbitrary key for the property
* @param value the value assigned to the given client property
*/
public void putClientProperty(String key, Object value) {
if (clientProperties == null) {
if (value == null) {
return;
}
clientProperties = new Hashtable();
}
if (value == null) {
clientProperties.remove(key);
if (clientProperties.size() == 0) {
clientProperties = null;
}
} else {
clientProperties.put(key, value);
}
}
/**
* gets the Component dirty region
*
* @return
*/
Rectangle getDirtyRegion() {
return dirtyRegion;
}
/**
* sets the Component dirty region
*
* @param dirty
*/
void setDirtyRegion(Rectangle dirty) {
this.dirtyRegion = dirty;
}
/**
* Toggles visibility of the component
*
* @param visible true if component is visible; otherwise false
*/
public void setVisible(boolean visible) {
this.visible = visible;
}
/**
* Returns the component width
*
* @return the component width
*/
public int getWidth() {
return bounds.getSize().getWidth();
}
/**
* Returns the component height
*
* @return the component height
*/
public int getHeight() {
return bounds.getSize().getHeight();
}
/**
* Sets the Component x location relative to the parent container, this method
* is exposed for the purpose of external layout managers and should not be invoked
* directly.
*
* @param x the current x coordinate of the components origin
*/
public void setX(int x) {
bounds.setX(x);
}
/**
* Sets the Component y location relative to the parent container, this method
* is exposed for the purpose of external layout managers and should not be invoked
* directly.
*
* @param y the current y coordinate of the components origin
*/
public void setY(int y) {
bounds.setY(y);
}
/**
* The baseline for the component text according to which it should be aligned
* with other components for best visual look.
*
*
* @param width the component width
* @param height the component height
* @return baseline value from the top of the component
*/
public int getBaseline(int width, int height) {
return getHeight() - getStyle().getPadding(BOTTOM);
}
/**
* Returns a constant indicating how the baseline varies with the size
* of the component.
*
* @return one of BRB_CONSTANT_ASCENT, BRB_CONSTANT_DESCENT,
* BRB_CENTER_OFFSET or BRB_OTHER
*/
public int getBaselineResizeBehavior() {
return BRB_OTHER;
}
/**
* Sets the Component Preferred Size, there is no garuntee the Component will
* be sized at its Preferred Size. The final size of the component may be
* smaller than its preferred size or even larger than the size.<br>
* The Layout manager can take this value into consideration, but there is
* no guarantee or requirement.
*
* @param d the component dimension
*/
public void setPreferredSize(Dimension d) {
preferredSize().setWidth(d.getWidth());
preferredSize().setHeight(d.getHeight());
sizeRequestedByUser = true;
}
/**
* Returns the Component Preferred Size, there is no garuntee the Component will
* be sized at its Preferred Size. The final size of the component may be
* smaller than its preferred size or even larger than the size.<br>
* The Layout manager can take this value into consideration, but there is
* no guarantee or requirement.
*
* @return the component preferred size
*/
public Dimension getPreferredSize() {
return preferredSize();
}
/**
* Helper method to set the preferred width of the component.
*
* @param preferredW the preferred width of the component
* @see #setPreferredSize
* @deprecated see setPreferredSize this method won't behave as you expect!
*/
public void setPreferredW(int preferredW) {
setPreferredSize(new Dimension(preferredW, getPreferredH()));
}
/**
* Helper method to set the preferred height of the component.
*
* @param preferredH the preferred height of the component
* @see #setPreferredSize
* @deprecated see setPreferredSize this method won't behave as you expect!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -