< prev index next >
src/share/classes/javax/swing/JComponent.java
Print this page
rev 1527 : 6727662: Code improvement and warnings removing from swing packages
Summary: Removed unnecessary castings and other warnings
Reviewed-by: malenkov
rev 1556 : 6794764: Translucent windows are completely repainted on every paint event, on Windows
6719382: Printing of AWT components on windows is not working
6726866: Repainting artifacts when resizing or dragging JInternalFrames in non-opaque toplevel
6683775: Painting artifacts is seen when panel is made setOpaque(false) for a translucent window
Reviewed-by: anthony, tdv, alexp
rev 1566 : 6680988: KeyEvent is still missing VK values for many keyboards
Summary: 2 new methods and some fields added to KeyEvent, plus hash of constants introduced
Reviewed-by: art
@@ -51,10 +51,11 @@
import javax.swing.event.*;
import javax.swing.plaf.*;
import static javax.swing.ClientPropertyKey.*;
import javax.accessibility.*;
+import sun.awt.AWTAccessor;
import sun.swing.SwingUtilities2;
import sun.swing.UIClientPropertyKey;
/**
* The base class for all Swing components except top-level containers.
@@ -180,11 +181,12 @@
private static final String uiClassID = "ComponentUI";
/**
* @see #readObject
*/
- private static final Hashtable readObjectCallbacks = new Hashtable(1);
+ private static final Hashtable<ObjectInputStream, ReadObjectCallback> readObjectCallbacks =
+ new Hashtable<ObjectInputStream, ReadObjectCallback>(1);
/**
* Keys to use for forward focus traversal when the JComponent is
* managing focus.
*/
@@ -343,11 +345,11 @@
private static final int REVALIDATE_RUNNABLE_SCHEDULED = 28;
/**
* Temporary rectangles.
*/
- private static java.util.List tempRectangles = new java.util.ArrayList(11);
+ private static java.util.List<Rectangle> tempRectangles = new java.util.ArrayList<Rectangle>(11);
/** Used for <code>WHEN_FOCUSED</code> bindings. */
private InputMap focusInputMap;
/** Used for <code>WHEN_ANCESTOR_OF_FOCUSED_COMPONENT</code> bindings. */
private InputMap ancestorInputMap;
@@ -437,11 +439,11 @@
private static Rectangle fetchRectangle() {
synchronized(tempRectangles) {
Rectangle rect;
int size = tempRectangles.size();
if (size > 0) {
- rect = (Rectangle)tempRectangles.remove(size - 1);
+ rect = tempRectangles.remove(size - 1);
}
else {
rect = new Rectangle(0, 0, 0, 0);
}
return rect;
@@ -792,11 +794,11 @@
}
// If we are only to paint to a specific child, determine
// its index.
if (paintingChild != null &&
(paintingChild instanceof JComponent) &&
- ((JComponent)paintingChild).isOpaque()) {
+ paintingChild.isOpaque()) {
for (; i >= 0; i--) {
if (getComponent(i) == paintingChild){
break;
}
}
@@ -861,11 +863,11 @@
((JComponent)comp).setFlag(
IS_PAINTING_TILE,true);
shouldSetFlagBack = true;
}
if(!printing) {
- ((JComponent)comp).paint(cg);
+ comp.paint(cg);
}
else {
if (!getFlag(IS_PRINTING_ALL)) {
comp.print(cg);
}
@@ -1006,12 +1008,14 @@
shouldClearPaintFlags = true;
}
int bw,bh;
boolean printing = getFlag(IS_PRINTING);
- if(!printing && repaintManager.isDoubleBufferingEnabled() &&
- !getFlag(ANCESTOR_USING_BUFFER) && isDoubleBuffered()) {
+ if (!printing && repaintManager.isDoubleBufferingEnabled() &&
+ !getFlag(ANCESTOR_USING_BUFFER) && isDoubleBuffered() &&
+ (getFlag(IS_REPAINTING) || repaintManager.isPainting()))
+ {
repaintManager.beginPaint();
try {
repaintManager.paint(this, this, co, clipX, clipY, clipW,
clipH);
} finally {
@@ -1084,11 +1088,11 @@
}
return false;
}
private void adjustPaintFlags() {
- JComponent jparent = null;
+ JComponent jparent;
Container parent;
for(parent = getParent() ; parent != null ; parent =
parent.getParent()) {
if(parent instanceof JComponent) {
jparent = (JComponent) parent;
@@ -2079,11 +2083,11 @@
* are pushed to the <code>KeyboardManager</code>
*/
private void registerWithKeyboardManager(boolean onlyIfNew) {
InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW, false);
KeyStroke[] strokes;
- Hashtable registered = (Hashtable)getClientProperty
+ Hashtable<KeyStroke, KeyStroke> registered = (Hashtable)getClientProperty
(WHEN_IN_FOCUSED_WINDOW_BINDINGS);
if (inputMap != null) {
// Push any new KeyStrokes to the KeyboardManager.
strokes = inputMap.allKeys();
@@ -2103,22 +2107,22 @@
else {
strokes = null;
}
// Remove any old ones.
if (registered != null && registered.size() > 0) {
- Enumeration keys = registered.keys();
+ Enumeration<KeyStroke> keys = registered.keys();
while (keys.hasMoreElements()) {
- KeyStroke ks = (KeyStroke)keys.nextElement();
+ KeyStroke ks = keys.nextElement();
unregisterWithKeyboardManager(ks);
}
registered.clear();
}
// Updated the registered Hashtable.
if (strokes != null && strokes.length > 0) {
if (registered == null) {
- registered = new Hashtable(strokes.length);
+ registered = new Hashtable<KeyStroke, KeyStroke>(strokes.length);
putClientProperty(WHEN_IN_FOCUSED_WINDOW_BINDINGS, registered);
}
for (int counter = strokes.length - 1; counter >= 0; counter--) {
registered.put(strokes[counter], strokes[counter]);
}
@@ -2157,11 +2161,11 @@
*/
void componentInputMapChanged(ComponentInputMap inputMap) {
InputMap km = getInputMap(WHEN_IN_FOCUSED_WINDOW, false);
while (km != inputMap && km != null) {
- km = (ComponentInputMap)km.getParent();
+ km = km.getParent();
}
if (km != null) {
registerWithKeyboardManager(false);
}
}
@@ -2870,21 +2874,34 @@
boolean processKeyBindings(KeyEvent e, boolean pressed) {
if (!SwingUtilities.isValidKeyEventForKeyBindings(e)) {
return false;
}
// Get the KeyStroke
+ // There may be two keystrokes associated with a low-level key event;
+ // in this case a keystroke made of an extended key code has a priority.
KeyStroke ks;
+ KeyStroke ksE = null;
if (e.getID() == KeyEvent.KEY_TYPED) {
ks = KeyStroke.getKeyStroke(e.getKeyChar());
}
else {
ks = KeyStroke.getKeyStroke(e.getKeyCode(),e.getModifiers(),
(pressed ? false:true));
+ int ekc = AWTAccessor.getKeyEventAccessor().getExtendedKeyCode(e);
+ if (e.getKeyCode() != ekc) {
+ ksE = KeyStroke.getKeyStroke(ekc,e.getModifiers(),
+ (pressed ? false:true));
+ }
}
- /* Do we have a key binding for e? */
+ // Do we have a key binding for e?
+ // If we have a binding by an extended code, use it.
+ // If not, check for regular code binding.
+ if(ksE != null && processKeyBinding(ksE, e, WHEN_FOCUSED, pressed)) {
+ return true;
+ }
if(processKeyBinding(ks, e, WHEN_FOCUSED, pressed))
return true;
/* We have no key binding. Let's try the path from our parent to the
* window excluded. We store the path components so we can avoid
@@ -2892,10 +2909,13 @@
*/
Container parent = this;
while (parent != null && !(parent instanceof Window) &&
!(parent instanceof Applet)) {
if(parent instanceof JComponent) {
+ if(ksE != null && ((JComponent)parent).processKeyBinding(ksE, e,
+ WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, pressed))
+ return true;
if(((JComponent)parent).processKeyBinding(ks, e,
WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, pressed))
return true;
}
// This is done so that the children of a JInternalFrame are
@@ -3645,19 +3665,19 @@
public void componentAdded(ContainerEvent e) {
Component c = e.getChild();
if (c != null && c instanceof Accessible) {
AccessibleJComponent.this.firePropertyChange(
AccessibleContext.ACCESSIBLE_CHILD_PROPERTY,
- null, ((Accessible) c).getAccessibleContext());
+ null, c.getAccessibleContext());
}
}
public void componentRemoved(ContainerEvent e) {
Component c = e.getChild();
if (c != null && c instanceof Accessible) {
AccessibleJComponent.this.firePropertyChange(
AccessibleContext.ACCESSIBLE_CHILD_PROPERTY,
- ((Accessible) c).getAccessibleContext(), null);
+ c.getAccessibleContext(), null);
}
}
}
/**
@@ -4349,11 +4369,11 @@
if(child instanceof JComponent) {
// System.out.println("A) checking opaque: " + ((JComponent)child).isOpaque() + " " + child);
// System.out.print("B) ");
// Thread.dumpStack();
- return ((JComponent)child).isOpaque();
+ return child.isOpaque();
} else {
/** Sometimes a heavy weight can have a bound larger than its peer size
* so we should always draw under heavy weights
*/
return false;
@@ -4665,11 +4685,11 @@
else if (listenerType == PropertyChangeListener.class) {
// PropertyChangeListeners are handled by PropertyChangeSupport
result = (T[])getPropertyChangeListeners();
}
else {
- result = (T[])listenerList.getListeners(listenerType);
+ result = listenerList.getListeners(listenerType);
}
if (result.length == 0) {
return super.getListeners(listenerType);
}
@@ -4876,11 +4896,11 @@
Component parent;
if(!isShowing()) {
return;
}
- while(!((JComponent)c).isOpaque()) {
+ while(!c.isOpaque()) {
parent = c.getParent();
if(parent != null) {
x += c.getX();
y += c.getY();
c = parent;
@@ -5170,11 +5190,11 @@
continue;
}
Rectangle siblingRect;
boolean opaque;
if (sibling instanceof JComponent) {
- opaque = ((JComponent)sibling).isOpaque();
+ opaque = sibling.isOpaque();
if (!opaque) {
if (retValue == PARTIALLY_OBSCURED) {
continue;
}
}
@@ -5317,11 +5337,11 @@
* @see java.io.ObjectInputStream#registerValidation
* @see SwingUtilities#updateComponentTreeUI
*/
private class ReadObjectCallback implements ObjectInputValidation
{
- private final Vector roots = new Vector(1);
+ private final Vector<JComponent> roots = new Vector<JComponent>(1);
private final ObjectInputStream inputStream;
ReadObjectCallback(ObjectInputStream s) throws Exception {
inputStream = s;
s.registerValidation(this, 0);
@@ -5333,12 +5353,11 @@
* the UI property of all of the copmonents with
* <code>SwingUtilities.updateComponentTreeUI</code>.
*/
public void validateObject() throws InvalidObjectException {
try {
- for(int i = 0; i < roots.size(); i++) {
- JComponent root = (JComponent)(roots.elementAt(i));
+ for (JComponent root : roots) {
SwingUtilities.updateComponentTreeUI(root);
}
}
finally {
readObjectCallbacks.remove(inputStream);
@@ -5354,12 +5373,11 @@
private void registerComponent(JComponent c)
{
/* If the Component c is a descendant of one of the
* existing roots (or it IS an existing root), we're done.
*/
- for(int i = 0; i < roots.size(); i++) {
- JComponent root = (JComponent)roots.elementAt(i);
+ for (JComponent root : roots) {
for(Component p = c; p != null; p = p.getParent()) {
if (p == root) {
return;
}
}
@@ -5368,11 +5386,11 @@
/* Otherwise: if Component c is an ancestor of any of the
* existing roots then remove them and add c (the "new root")
* to the roots vector.
*/
for(int i = 0; i < roots.size(); i++) {
- JComponent root = (JComponent)roots.elementAt(i);
+ JComponent root = roots.elementAt(i);
for(Component p = root.getParent(); p != null; p = p.getParent()) {
if (p == c) {
roots.removeElementAt(i--); // !!
break;
}
@@ -5400,11 +5418,11 @@
* this is the first call to JComponent.readObject() for this
* graph of objects, then create a callback and stash it
* in the readObjectCallbacks table. Note that the ReadObjectCallback
* constructor takes care of calling s.registerValidation().
*/
- ReadObjectCallback cb = (ReadObjectCallback)(readObjectCallbacks.get(s));
+ ReadObjectCallback cb = readObjectCallbacks.get(s);
if (cb == null) {
try {
readObjectCallbacks.put(s, cb = new ReadObjectCallback(s));
}
catch (Exception e) {
< prev index next >