📄 component.java
字号:
package java.awt;import java.awt.event.ActionEvent;import java.awt.event.AdjustmentEvent;import java.awt.event.ComponentEvent;import java.awt.event.ComponentListener;import java.awt.event.ContainerEvent;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import java.awt.event.ItemEvent;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.awt.event.PaintEvent;import java.awt.event.TextEvent;import java.awt.event.WindowEvent;import java.awt.image.ImageObserver;import java.awt.image.ImageProducer;import java.awt.peer.ComponentPeer;import java.io.Serializable;import java.util.Locale;import java.util.Vector;import kaffe.util.Ptr;/** * Component - abstract root of all widgets * * Copyright (c) 1998 * Transvirtual Technologies, Inc. All rights reserved. * * See the file "license.terms" for information on usage and redistribution * of this file. * * @author P.C.Mehlitz */abstract public class Component extends Object implements ImageObserver, MenuContainer, Serializable{ final private static long serialVersionUID = -7644114512714619750L; Container parent; int x; int y; int width; int height; Color fgClr; Color bgClr; Font font; Cursor cursor; ComponentListener cmpListener; KeyListener keyListener; FocusListener focusListener; MouseListener mouseListener; MouseMotionListener motionListener; String name; int eventMask; Locale locale; PopupMenu popup; Rectangle deco = noDeco; int flags = IS_VISIBLE;/** * linkedGraphs is a list of WeakReferences to NativeGraphics * objects, which is used to keep track of resident Graphics objects * for native-like Components (which we have to update in case * of a visibility or position change). See GraphicsLink for details */ GraphicsLink linkedGraphs; final public static float TOP_ALIGNMENT = (float)0.0; final public static float CENTER_ALIGNMENT = (float)0.5; final public static float BOTTOM_ALIGNMENT = (float)1.0; final public static float LEFT_ALIGNMENT = (float)0.0; final public static float RIGHT_ALIGNMENT = (float)1.0; final static int BORDER_WIDTH = 2; static Object treeLock = new TreeLock(); static Rectangle noDeco = new Rectangle(); final static int IS_VISIBLE = 0x01; final static int IS_VALID = 0x02; final static int IS_PARENT_SHOWING = 0x04; final static int IS_LAYOUTING = 0x08; final static int IS_IN_UPDATE = 0x10; final static int IS_OLD_EVENT = 0x20; final static int IS_RESIZABLE = 0x40; final static int IS_MODAL = 0x80; final static int IS_NATIVE_LIKE = 0x100; final static int IS_OPENED = 0x200; final static int IS_ADD_NOTIFIED = 0x400; final static int IS_FG_COLORED = 0x800; final static int IS_BG_COLORED = 0x1000; final static int IS_FONTIFIED = 0x2000; final static int IS_ASYNC_UPDATED = 0x4000; final static int IS_DIRTY = 0x8000; final static int IS_MOUSE_AWARE = 0x10000; final static int IS_TEMP_HIDDEN = 0x20000; final static int IS_SHOWING = IS_ADD_NOTIFIED | IS_PARENT_SHOWING | IS_VISIBLE;static class TreeLock{}/* * JDK serialization. * While this is serializing something like what JDK expects, we don't handle the more complex * things like Properties or popupMenus yet (because I'm not sure how to convert what we do * into what they expect). */class DefaultSerialization{ private Color bgColor; private java.beans.PropertyChangeSupport changeSupport; private int componentSerializedDataVersion; private Cursor cursor; private boolean enabled; private long eventMask; private Font font; private Color foreground; private boolean hasFocus; private int height; private boolean isPacked; private Locale locale; private Dimension minSize; private String name; private boolean nameExplicitlySet; private boolean newEventsOnly; private Font peerFont; private Vector popups; private Dimension prefSize; private boolean valid; private boolean visible; private int width; private int x; private int y;private void readDefaultObject() { setBackground(bgColor); setCursor(cursor); setEnabled(enabled); enableEvents(eventMask); setFont(font); setForeground(foreground); setSize(width, height); setLocale(locale); setName(name); setLocation(x, y); if (valid) { validate(); } else { invalidate(); } if (visible) { show(); } else { hide(); }}private void writeDefaultObject() { bgColor = Component.this.bgClr; changeSupport = null; componentSerializedDataVersion = 0; cursor = Component.this.cursor; enabled = isEnabled(); eventMask = Component.this.eventMask; font = Component.this.font; foreground = Component.this.fgClr; hasFocus = false; height = Component.this.height; isPacked = false; locale = Component.this.locale; minSize = getMinimumSize(); name = Component.this.name; nameExplicitlySet = true; newEventsOnly = !getClassProperties().useOldEvents; peerFont = Component.this.font; popups = null; prefSize = getPreferredSize(); valid = isValid(); visible = isVisible(); width = Component.this.width; x = Component.this.x; y = Component.this.y;}}protected Component () { cursor = Cursor.defaultCursor;}/** * @deprecated */public boolean action(Event evt, Object what) { return (false);}public void add ( PopupMenu menu ) { if ( menu.parent != null ) menu.parent.remove( menu); if ( (flags & IS_ADD_NOTIFIED) > 0 ) { menu.parent = this; menu.owner = this; menu.addNotify(); } if ( popup == null ) popup = menu; else { popup.addSeparator(); popup.addAll( menu); }}public void addComponentListener ( ComponentListener newListener ) { cmpListener = AWTEventMulticaster.add( cmpListener, newListener);}public void addFocusListener ( FocusListener newListener ) { focusListener = AWTEventMulticaster.add( focusListener, newListener);}public void addKeyListener ( KeyListener newListener ) { keyListener = AWTEventMulticaster.add( keyListener, newListener);}public void addMouseListener ( MouseListener newListener ) { mouseListener = AWTEventMulticaster.add( mouseListener, newListener); flags |= IS_MOUSE_AWARE;}public void addMouseMotionListener ( MouseMotionListener newListener ) { motionListener = AWTEventMulticaster.add( motionListener, newListener); flags |= IS_MOUSE_AWARE;}public void addNotify () { if ( (flags & IS_ADD_NOTIFIED) == 0 ) { flags |= IS_ADD_NOTIFIED; ClassProperties props = getClassProperties(); if ( props.isNativeLike ){ flags |= IS_NATIVE_LIKE; } if ( parent != null ) { // Note that this only works in case the parent is addNotified // *before* its childs. (we can't use isNativeLike to filter this out // unless we turn BarMenus into isNativeLike, which is bad) if ( (((parent.flags & IS_OLD_EVENT) != 0) || props.useOldEvents) ){ flags |= (IS_OLD_EVENT | IS_MOUSE_AWARE); } } else { // Window if ( props.useOldEvents ) flags |= (IS_OLD_EVENT | IS_MOUSE_AWARE); } if ( popup != null ) { popup.parent = this; popup.owner = this; popup.addNotify(); } }}public Rectangle bounds () { // DUP - we have to return fresh objects because (1) there are apps out there // modifying the return values (causing trouble for concurrent access to bounds), // and (2) because some apps (like Swing) temporarily store return values, relying // on its constness (e.g. for InternalFrame dragging) return new Rectangle( x, y, width, height);}public int checkImage (Image image, ImageObserver obs) { return (image.checkImage( -1, -1, obs, false));}public int checkImage (Image image, int width, int height, ImageObserver obs) { return (image.checkImage( width, height, obs, false));}void checkMouseAware () { if ( ((eventMask & AWTEvent.DISABLED_MASK) == 0) && ((mouseListener != null) || (motionListener != null) || (eventMask & (AWTEvent.MOUSE_EVENT_MASK|AWTEvent.MOUSE_MOTION_EVENT_MASK)) != 0 || (flags & IS_OLD_EVENT) != 0 )) { flags |= IS_MOUSE_AWARE; } else { flags &= ~IS_MOUSE_AWARE; }}void cleanUpNative () { // nothing native, all lightweight}public boolean contains ( Point pt ) { return contains( pt.x, pt.y);}public boolean contains(int x, int y) { return (inside(x, y));}public Image createImage ( ImageProducer producer ) { return new Image( producer);}public Image createImage ( int width, int height ) { return new Image( width, height);}void createNative () { // nothing native, all lightweight}/** * @deprecated */final public void deliverEvent(Event evt) { postEvent(evt);}void destroyNative () { // nothing native, all lightweight}/** * @deprecated, use setEnabled() */public void disable() { setEnabled(false);}public void disableEvents ( long disableMask ) { eventMask &= ~disableMask; checkMouseAware();}final public void dispatchEvent ( AWTEvent evt ) { // this is NOT our main entry point for Component event processing // (processEvent() is). Because this is a 'final' method, it can't be overloaded // by user classes. Well, almost, because the JDK obviously calls a // hidden dispatchEventImpl() from it, turning this into a "somewhat" final // method. Anyway, this (still?) can be considered as undocumented, // non-portable, and we ignore it for now (regarding the main entry point) dispatchEventImpl( evt);}void dispatchEventImpl ( AWTEvent event ) { // A hidden method that seems to be called automatically by the JDKs // 'final' dispatchEvent() method. We just provide it to get some more // compatibility (in case dispatchEvent is called explicitly), but // we don't route all events through it (since this is a private, // undocumented method) event.dispatch();}public void doLayout () { layout();}void dump ( String prefix ) { System.out.print( prefix); System.out.println( this);}/** * @deprecated, use setEnabled() */public void enable() { setEnabled(true);}/** * @deprecated, use setEnabled() */public void enable( boolean isEnabled) { setEnabled(isEnabled);}public void enableEvents ( long enableMask ) { eventMask |= enableMask; checkMouseAware();}public float getAlignmentX() { return CENTER_ALIGNMENT;}public float getAlignmentY() { return CENTER_ALIGNMENT;}public Color getBackground () { return bgClr;/* if ( bgClr != null ) return bgClr; for ( Component c=parent; c != null; c = c.parent ) { if ( c.bgClr != null ) return c.bgClr; } // even though not in the specs, some apps (e.g. swing) rely on the // JDK behavior of returning 'null' if there isn't a parent yet return null; //return Color.white;*/}/** * @deprecated, use getBounds() */public Rectangle getBounds () { return bounds();}ClassProperties getClassProperties () { // direct Component / Container derived classes can't use old events // (they had no protected ctor in 1.0.2) return ClassAnalyzer.analyzeProcessEvent( getClass(), false);}public Component getComponentAt ( Point pt ) { return getComponentAt( pt.x, pt.y );}public Component getComponentAt ( int x, int y ) { return locate( x, y);}public Cursor getCursor() { return cursor;}public Font getFont () { return font;}public FontMetrics getFontMetrics ( Font font ) { return FontMetrics.getFontMetrics( font);}public Color getForeground () { return fgClr;}public Graphics getGraphics () { if ( (flags & IS_ADD_NOTIFIED) != 0 ) return NativeGraphics.getClippedGraphics( null, this, 0, 0, 0, 0, width, height, false); else return null;}public Locale getLocale () { if (locale != null) { return (locale); } else if (parent != null) { return (parent.getLocale()); } else { return (Locale.getDefault()); }}public Point getLocation () { return location();}public Point getLocationOnScreen () { // this has to be resolved for Applets int u=0, v=0; for ( Component c=this; c != null; c=c.parent ) { u += c.x; v += c.y; } return new Point( u, v);}public Dimension getMaximumSize() { return Toolkit.singleton.getScreenSize();}public Dimension getMinimumSize() { return minimumSize();}public String getName () { return (name == null) ? getClass().getName() : name;}Ptr getNativeData () { return null; // no nativeData, all lightweight}public Container getParent() { return parent;}/** * @deprecated, should not be called. */public ComponentPeer getPeer() { // this is just a dummy, i.e. we share a single object that can be used // ONLY to "(getPeer() != null)" check if we already passed addNotify() return ((flags & IS_ADD_NOTIFIED) != 0) ? Toolkit.lightweightPeer : null;}public Dimension getPreferredSize() { return (preferredSize());}/** * @deprecated, use getSize() * this is never called automatically, override getSize in derived classes * to change the default behavior */public Dimension getSize () { return size();}public Toolkit getToolkit () { return Toolkit.singleton;}Component getToplevel () { Component c; for ( c=this; !(c instanceof Window) && c != null; c= c.parent ); return c;}final public Object getTreeLock() { return treeLock;}public int getX() { return x;}public int getY() { return y;}/** * @deprecated */public boolean gotFocus(Event evt, Object what) { return (false);}/** * @deprecated */public boolean handleEvent(Event evt) { switch (evt.id) { case Event.ACTION_EVENT: return (action(evt, evt.arg)); case Event.GOT_FOCUS: return (gotFocus(evt, evt.arg)); case Event.KEY_PRESS: return (keyDown(evt, evt.key)); case Event.KEY_RELEASE: return (keyUp(evt, evt.key)); case Event.LOST_FOCUS: return (lostFocus(evt, evt.arg)); case Event.MOUSE_DOWN: return (mouseDown(evt, evt.x, evt.y)); case Event.MOUSE_DRAG: return (mouseDrag(evt, evt.x, evt.y)); case Event.MOUSE_ENTER: return (mouseEnter(evt, evt.x, evt.y)); case Event.MOUSE_EXIT: return (mouseExit(evt, evt.x, evt.y)); case Event.MOUSE_MOVE: return (mouseMove(evt, evt.x, evt.y)); case Event.MOUSE_UP: return (mouseUp(evt, evt.x, evt.y)); default: return (false); }}public void hide () { // DEP this should be in setVisible !! But we have to keep it here // for compatibility reasons (Swing etc.) if ( (flags & IS_VISIBLE) != 0 ) { flags &= ~IS_VISIBLE; // if we are a toplevel, the native window manager will take care // of repainting, otherwise we have to do it explicitly if ( (parent != null) && ((parent.flags & IS_LAYOUTING) == 0) ) { if ( (flags & IS_PARENT_SHOWING) != 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -