📄 component.java
字号:
/** * Component - abstract root of all widgets * * Copyright (c) 1998 * Transvirtual Technologies, Inc. All rights reserved. * Copyright (c) 2006 * Kaffe.org developers. See ChangeLog for details. * * See the file "license.terms" for information on usage and redistribution * of this file. * * original code P.C.Mehlitz * some code taken or adapted from Classpath */ package java.awt;import gnu.classpath.Pointer;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.MouseWheelListener;import java.awt.event.MouseMotionListener;import java.awt.event.PaintEvent;import java.awt.event.TextEvent;import java.awt.event.WindowEvent;import java.awt.image.ColorModel;import java.awt.image.ImageObserver;import java.awt.image.ImageProducer;import java.awt.peer.ComponentPeer;import java.io.PrintStream;import java.io.PrintWriter;import java.io.Serializable;import java.util.Locale;import java.awt.dnd.DropTarget;import org.kaffe.awt.DoNothingPeer; abstract public class Component extends Object implements ImageObserver, MenuContainer, Serializable{ // We're not actually compatible with Sun's serialization format, so don't claim to be: //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 = 0.0f; final public static float CENTER_ALIGNMENT = 0.5f; final public static float BOTTOM_ALIGNMENT = 1.0f; final public static float LEFT_ALIGNMENT = 0.0f; final public static float RIGHT_ALIGNMENT = 1.0f; 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). */// 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 */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;*/}public Rectangle getBounds () { return bounds();}// TODO this is a tentative implementationpublic Rectangle getBounds (Rectangle rv) { if (rv == null) return bounds(); rv.x = x; rv.y = y; rv.width= width; rv.height = height; return rv;}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);}// TODO this is only a stubprotected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {}// TODO this is only a stubprotected void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {}// TODO this is only a stubprotected void firePropertyChange(String propertyName, int oldValue, int newValue) {}public ColorModel getColorModel() { return Toolkit.getDefaultToolkit().getColorModel();}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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -