--- old/src/share/classes/javax/swing/colorchooser/DefaultSwatchChooserPanel.java 2012-08-21 15:59:44.000000000 +0800 +++ new/src/share/classes/javax/swing/colorchooser/DefaultSwatchChooserPanel.java 2012-08-21 15:59:43.000000000 +0800 @@ -57,6 +57,8 @@ RecentSwatchPanel recentSwatchPanel; MouseListener mainSwatchListener; MouseListener recentSwatchListener; + private KeyListener mainSwatchKeyListener; + private KeyListener recentSwatchKeyListener; public DefaultSwatchChooserPanel() { super(); @@ -151,10 +153,14 @@ recentSwatchPanel.putClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY, recentStr); + mainSwatchKeyListener = new MainSwatchKeyListener(); mainSwatchListener = new MainSwatchListener(); swatchPanel.addMouseListener(mainSwatchListener); + swatchPanel.addKeyListener(mainSwatchKeyListener); recentSwatchListener = new RecentSwatchListener(); + recentSwatchKeyListener = new RecentSwatchKeyListener(); recentSwatchPanel.addMouseListener(recentSwatchListener); + recentSwatchPanel.addKeyListener(recentSwatchKeyListener); JPanel mainHolder = new JPanel(new BorderLayout()); Border border = new CompoundBorder( new LineBorder(Color.black), @@ -196,7 +202,10 @@ public void uninstallChooserPanel(JColorChooser enclosingChooser) { super.uninstallChooserPanel(enclosingChooser); swatchPanel.removeMouseListener(mainSwatchListener); + swatchPanel.removeKeyListener(mainSwatchKeyListener); recentSwatchPanel.removeMouseListener(recentSwatchListener); + recentSwatchPanel.removeKeyListener(recentSwatchKeyListener); + swatchPanel = null; recentSwatchPanel = null; mainSwatchListener = null; @@ -209,11 +218,32 @@ } + private class RecentSwatchKeyListener extends KeyAdapter implements Serializable { + public void keyPressed(KeyEvent e) { + if (KeyEvent.VK_SPACE == e.getKeyCode()) { + Color color = recentSwatchPanel.getSelectedColor(); + getColorSelectionModel().setSelectedColor(color); + } + } + } + + private class MainSwatchKeyListener extends KeyAdapter implements Serializable { + public void keyPressed(KeyEvent e) { + if (KeyEvent.VK_SPACE == e.getKeyCode()) { + Color color = swatchPanel.getSelectedColor(); + getColorSelectionModel().setSelectedColor(color); + recentSwatchPanel.setMostRecentColor(color); + } + } + } + class RecentSwatchListener extends MouseAdapter implements Serializable { public void mousePressed(MouseEvent e) { if (isEnabled()) { Color color = recentSwatchPanel.getColorForLocation(e.getX(), e.getY()); - setSelectedColor(color); + recentSwatchPanel.setSelectedColorFromLocation(e.getX(), e.getY()); + getColorSelectionModel().setSelectedColor(color); + recentSwatchPanel.requestFocus(); } } } @@ -222,8 +252,10 @@ public void mousePressed(MouseEvent e) { if (isEnabled()) { Color color = swatchPanel.getColorForLocation(e.getX(), e.getY()); - setSelectedColor(color); + getColorSelectionModel().setSelectedColor(color); + swatchPanel.setSelectedColorFromLocation(e.getX(), e.getY()); recentSwatchPanel.setMostRecentColor(color); + swatchPanel.requestFocus(); } } } @@ -239,6 +271,9 @@ protected Dimension numSwatches; protected Dimension gap; + private int selRow; + private int selCol; + public SwatchPanel() { initValues(); initColors(); @@ -247,10 +282,66 @@ setBackground(Color.white); setRequestFocusEnabled(false); setInheritsPopupMenu(true); + + addFocusListener(new FocusAdapter() { + public void focusGained(FocusEvent e) { + repaint(); + } + + public void focusLost(FocusEvent e) { + repaint(); + } + }); + + addKeyListener(new KeyAdapter() { + public void keyPressed(KeyEvent e) { + int typed = e.getKeyCode(); + switch (typed) { + case KeyEvent.VK_UP: + if (selRow > 0) { + selRow--; + } + repaint(); + break; + case KeyEvent.VK_DOWN: + if (selRow < numSwatches.height - 1) { + selRow++; + } + repaint(); + break; + case KeyEvent.VK_LEFT: + if (selCol > 0) { + selCol--; + } + repaint(); + break; + case KeyEvent.VK_RIGHT: + if (selCol < numSwatches.width - 1) { + selCol++; + } + repaint(); + break; + case KeyEvent.VK_HOME: + selCol = 0; + selRow = 0; + repaint(); + break; + case KeyEvent.VK_END: + selCol = numSwatches.width - 1; + selRow = numSwatches.height - 1; + repaint(); + break; + } + } + }); + } + + public Color getSelectedColor() { + return getColorForCell(selCol, selRow); } public boolean isFocusTraversable() { - return false; + return true; } protected void initValues() { @@ -263,8 +354,8 @@ for (int row = 0; row < numSwatches.height; row++) { int y = row * (swatchSize.height + gap.height); for (int column = 0; column < numSwatches.width; column++) { - - g.setColor( getColorForCell(column, row) ); + Color c = getColorForCell(column,row); + g.setColor( c ); int x; if ((!this.getComponentOrientation().isLeftToRight()) && (this instanceof RecentSwatchPanel)) { @@ -276,6 +367,19 @@ g.setColor(Color.black); g.drawLine( x+swatchSize.width-1, y, x+swatchSize.width-1, y+swatchSize.height-1); g.drawLine( x, y+swatchSize.height-1, x+swatchSize.width-1, y+swatchSize.height-1); + + if (selRow == row && selCol == column && this.isFocusOwner()) { + Color c2 = new Color(c.getRed() < 125 ? 255 : 0, + c.getGreen() < 125 ? 255 : 0, + c.getBlue() < 125 ? 255 : 0); + g.setColor(c2); + g.drawLine(x, y, x+swatchSize.width, y); + g.drawLine(x, y, x, y+swatchSize.height); + g.drawLine(x + swatchSize.width, y, x+swatchSize.width, y+swatchSize.height); + g.drawLine(x, y+swatchSize.height, x+swatchSize.width, y+swatchSize.height); + g.drawLine(x, y, x+swatchSize.width, y+swatchSize.height); + g.drawLine(x, y+swatchSize.height, x+swatchSize.width, y); + } } } } @@ -296,6 +400,17 @@ return color.getRed()+", "+ color.getGreen() + ", " + color.getBlue(); } + public void setSelectedColorFromLocation(int x, int y) { + if ((!this.getComponentOrientation().isLeftToRight()) + && (this instanceof RecentSwatchPanel)) { + selCol = numSwatches.width - x / (swatchSize.width + gap.width) - 1; + } else { + selCol = x / (swatchSize.width + gap.width); + } + selRow = y / (swatchSize.height + gap.height); + repaint(); + } + public Color getColorForLocation( int x, int y ) { int column; if ((!this.getComponentOrientation().isLeftToRight()) &&