1. /*
  2. * @(#)DefaultSwatchChooserPanel.java 1.15 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.colorchooser;
  8. import javax.swing.*;
  9. import javax.swing.border.*;
  10. import javax.swing.event.*;
  11. import java.awt.*;
  12. import java.awt.image.*;
  13. import java.awt.event.*;
  14. import java.beans.PropertyChangeEvent;
  15. import java.beans.PropertyChangeListener;
  16. import java.io.Serializable;
  17. /**
  18. * The standard color swatch chooser.
  19. * <p>
  20. * <strong>Warning:</strong>
  21. * Serialized objects of this class will not be compatible with
  22. * future Swing releases. The current serialization support is appropriate
  23. * for short term storage or RMI between applications running the same
  24. * version of Swing. A future release of Swing will provide support for
  25. * long term persistence.
  26. *
  27. * @version 1.15 11/29/01
  28. * @author Steve Wilson
  29. */
  30. class DefaultSwatchChooserPanel extends AbstractColorChooserPanel {
  31. SwatchPanel swatchPanel;
  32. RecentSwatchPanel recentSwatchPanel;
  33. MouseListener mainSwatchListener;
  34. MouseListener recentSwatchListener;
  35. private static String recentStr = UIManager.getString("ColorChooser.swatchesRecentText");
  36. public DefaultSwatchChooserPanel() {
  37. super();
  38. }
  39. public String getDisplayName() {
  40. return UIManager.getString("ColorChooser.swatchesNameText");
  41. }
  42. public Icon getSmallDisplayIcon() {
  43. return null;
  44. }
  45. public Icon getLargeDisplayIcon() {
  46. return null;
  47. }
  48. /**
  49. * The background color, foreground color, and font are already set to the
  50. * defaults from the defaults table before this method is called.
  51. */
  52. public void installChooserPanel(JColorChooser enclosingChooser) {
  53. super.installChooserPanel(enclosingChooser);
  54. }
  55. protected void buildChooser() {
  56. JPanel superHolder = new JPanel(new BorderLayout());
  57. swatchPanel = new MainSwatchPanel();
  58. swatchPanel.getAccessibleContext().setAccessibleName(getDisplayName());
  59. recentSwatchPanel = new RecentSwatchPanel();
  60. recentSwatchPanel.getAccessibleContext().setAccessibleName(recentStr);
  61. mainSwatchListener = new MainSwatchListener();
  62. swatchPanel.addMouseListener(mainSwatchListener);
  63. recentSwatchListener = new RecentSwatchListener();
  64. recentSwatchPanel.addMouseListener(recentSwatchListener);
  65. JPanel mainHolder = new JPanel(new BorderLayout());
  66. Border border = new CompoundBorder( new LineBorder(Color.black),
  67. new LineBorder(Color.white) );
  68. mainHolder.setBorder(border);
  69. mainHolder.add(swatchPanel, BorderLayout.CENTER);
  70. superHolder.add( mainHolder, BorderLayout.CENTER );
  71. JPanel recentHolder = new JPanel( new BorderLayout() );
  72. recentSwatchPanel.addMouseListener(recentSwatchListener);
  73. recentHolder.setBorder(border);
  74. recentHolder.add(recentSwatchPanel, BorderLayout.CENTER);
  75. JPanel recentLabelHolder = new JPanel(new BorderLayout());
  76. recentLabelHolder.add(recentHolder, BorderLayout.CENTER);
  77. JLabel l = new JLabel(recentStr);
  78. l.setLabelFor(recentSwatchPanel);
  79. recentLabelHolder.add(l, BorderLayout.NORTH);
  80. JPanel recentHolderHolder = new JPanel(new CenterLayout());
  81. recentHolderHolder.setBorder(new EmptyBorder(2,10,2,2));
  82. recentHolderHolder.add(recentLabelHolder);
  83. superHolder.add( recentHolderHolder, BorderLayout.EAST );
  84. add(superHolder);
  85. }
  86. public void uninstallChooserPanel(JColorChooser enclosingChooser) {
  87. super.uninstallChooserPanel(enclosingChooser);
  88. swatchPanel.removeMouseListener(mainSwatchListener);
  89. recentSwatchPanel.removeMouseListener(recentSwatchListener);
  90. swatchPanel = null;
  91. recentSwatchPanel = null;
  92. mainSwatchListener = null;
  93. recentSwatchListener = null;
  94. removeAll(); // strip out all the sub-components
  95. }
  96. public void updateChooser() {
  97. }
  98. class RecentSwatchListener extends MouseAdapter implements Serializable {
  99. public void mousePressed(MouseEvent e) {
  100. Color color = recentSwatchPanel.getColorForLocation(e.getX(), e.getY());
  101. getColorSelectionModel().setSelectedColor(color);
  102. }
  103. }
  104. class MainSwatchListener extends MouseAdapter implements Serializable {
  105. public void mousePressed(MouseEvent e) {
  106. Color color = swatchPanel.getColorForLocation(e.getX(), e.getY());
  107. getColorSelectionModel().setSelectedColor(color);
  108. recentSwatchPanel.setMostRecentColor(color);
  109. }
  110. }
  111. }
  112. class SwatchPanel extends JPanel {
  113. protected Color[] colors;
  114. protected Dimension swatchSize;
  115. protected Dimension numSwatches;
  116. protected Dimension gap;
  117. public SwatchPanel() {
  118. initValues();
  119. initColors();
  120. setToolTipText(""); // register for events
  121. setOpaque(true);
  122. setBackground(Color.white);
  123. setRequestFocusEnabled(false);
  124. }
  125. public boolean isFocusTraversable() {
  126. return false;
  127. }
  128. protected void initValues() {
  129. }
  130. public void paintComponent(Graphics g) {
  131. g.setColor(getBackground());
  132. g.fillRect(0,0,getWidth(), getHeight());
  133. for (int row = 0; row < numSwatches.height; row++) {
  134. for (int column = 0; column < numSwatches.width; column++) {
  135. g.setColor( getColorForCell(column, row) );
  136. int x = column * (swatchSize.width + gap.width);
  137. int y = row * (swatchSize.height + gap.height);
  138. g.fillRect( x, y, swatchSize.width, swatchSize.height);
  139. g.setColor(Color.black);
  140. g.drawLine( x+swatchSize.width-1, y, x+swatchSize.width-1, y+swatchSize.height-1);
  141. g.drawLine( x, y+swatchSize.height-1, x+swatchSize.width-1, y+swatchSize.width-1);
  142. }
  143. }
  144. }
  145. public Dimension getPreferredSize() {
  146. int x = numSwatches.width * (swatchSize.width + gap.width) -1;
  147. int y = numSwatches.height * (swatchSize.height + gap.height) -1;
  148. return new Dimension( x, y );
  149. }
  150. protected void initColors() {
  151. }
  152. public String getToolTipText(MouseEvent e) {
  153. Color color = getColorForLocation(e.getX(), e.getY());
  154. return color.getRed()+", "+ color.getGreen() + ", " + color.getBlue();
  155. }
  156. public Color getColorForLocation( int x, int y ) {
  157. int column = x / (swatchSize.width + gap.width);
  158. int row = y / (swatchSize.height + gap.height);
  159. return getColorForCell(column, row);
  160. }
  161. private Color getColorForCell( int column, int row) {
  162. return colors[ (row * numSwatches.width) + column ]; // (STEVE) - change data orientation here
  163. }
  164. }
  165. class RecentSwatchPanel extends SwatchPanel {
  166. protected void initValues() {
  167. swatchSize = UIManager.getDimension("ColorChooser.swatchesRecentSwatchSize");
  168. numSwatches = new Dimension( 5, 7 );
  169. gap = new Dimension(1, 1);
  170. }
  171. protected void initColors() {
  172. Color defaultRecentColor = UIManager.getColor("ColorChooser.swatchesDefaultRecentColor");
  173. int numColors = numSwatches.width * numSwatches.height;
  174. colors = new Color[numColors];
  175. for (int i = 0; i < numColors ; i++) {
  176. colors[i] = defaultRecentColor;
  177. }
  178. }
  179. public void setMostRecentColor(Color c) {
  180. System.arraycopy( colors, 0, colors, 1, colors.length-1);
  181. colors[0] = c;
  182. repaint();
  183. }
  184. }
  185. class MainSwatchPanel extends SwatchPanel {
  186. protected void initValues() {
  187. swatchSize = UIManager.getDimension("ColorChooser.swatchesSwatchSize");
  188. numSwatches = new Dimension( 31, 10 );
  189. gap = new Dimension(1, 1);
  190. }
  191. protected void initColors() {
  192. int[] rawValues = initRawValues();
  193. int numColors = rawValues.length / 3;
  194. colors = new Color[numColors];
  195. for (int i = 0; i < numColors ; i++) {
  196. colors[i] = new Color( rawValues[(i*3)], rawValues[(i*3)+1], rawValues[(i*3)+2] );
  197. }
  198. }
  199. private int[] initRawValues() {
  200. int[] rawValues = { 255, 255, 255,
  201. 255, 255, 255,
  202. 255, 255, 255,
  203. 255, 255, 255,
  204. 255, 255, 255,
  205. 255, 255, 255,
  206. 255, 255, 255,
  207. 255, 255, 255,
  208. 255, 255, 255,
  209. 255, 255, 255,
  210. 255, 255, 255,
  211. 255, 255, 255,
  212. 255, 255, 255,
  213. 255, 255, 255,
  214. 255, 255, 255,
  215. 255, 255, 255,
  216. 255, 255, 255,
  217. 255, 255, 255,
  218. 255, 255, 255,
  219. 255, 255, 255,
  220. 255, 255, 255,
  221. 255, 255, 255,
  222. 255, 255, 255,
  223. 255, 255, 255,
  224. 255, 255, 255,
  225. 255, 255, 255,
  226. 255, 255, 255,
  227. 255, 255, 255,
  228. 255, 255, 255,
  229. 255, 255, 255,
  230. 255, 255, 255,
  231. 204, 255, 255,
  232. 204, 204, 255,
  233. 204, 204, 255,
  234. 204, 204, 255,
  235. 204, 204, 255,
  236. 204, 204, 255,
  237. 204, 204, 255,
  238. 204, 204, 255,
  239. 204, 204, 255,
  240. 204, 204, 255,
  241. 255, 204, 255,
  242. 255, 204, 204,
  243. 255, 204, 204,
  244. 255, 204, 204,
  245. 255, 204, 204,
  246. 255, 204, 204,
  247. 255, 204, 204,
  248. 255, 204, 204,
  249. 255, 204, 204,
  250. 255, 204, 204,
  251. 255, 255, 204,
  252. 204, 255, 204,
  253. 204, 255, 204,
  254. 204, 255, 204,
  255. 204, 255, 204,
  256. 204, 255, 204,
  257. 204, 255, 204,
  258. 204, 255, 204,
  259. 204, 255, 204,
  260. 204, 255, 204,
  261. 204, 255, 255,
  262. 153, 255, 255,
  263. 153, 204, 255,
  264. 153, 153, 255,
  265. 153, 153, 255,
  266. 153, 153, 255,
  267. 153, 153, 255,
  268. 153, 153, 255,
  269. 153, 153, 255,
  270. 153, 153, 255,
  271. 204, 153, 255,
  272. 255, 153, 255,
  273. 255, 153, 204,
  274. 255, 153, 153,
  275. 255, 153, 153,
  276. 255, 153, 153,
  277. 255, 153, 153,
  278. 255, 153, 153,
  279. 255, 153, 153,
  280. 255, 153, 153,
  281. 255, 204, 153,
  282. 255, 255, 153,
  283. 204, 255, 153,
  284. 153, 255, 153,
  285. 153, 255, 153,
  286. 153, 255, 153,
  287. 153, 255, 153,
  288. 153, 255, 153,
  289. 153, 255, 153,
  290. 153, 255, 153,
  291. 153, 255, 204,
  292. 153, 255, 255,
  293. 102, 255, 255,
  294. 102, 204, 255,
  295. 102, 153, 255,
  296. 102, 102, 255,
  297. 102, 102, 255,
  298. 102, 102, 255,
  299. 102, 102, 255,
  300. 102, 102, 255,
  301. 153, 102, 255,
  302. 204, 102, 255,
  303. 255, 102, 255,
  304. 255, 102, 204,
  305. 255, 102, 153,
  306. 255, 102, 102,
  307. 255, 102, 102,
  308. 255, 102, 102,
  309. 255, 102, 102,
  310. 255, 102, 102,
  311. 255, 153, 102,
  312. 255, 204, 102,
  313. 255, 255, 102,
  314. 204, 255, 102,
  315. 153, 255, 102,
  316. 102, 255, 102,
  317. 102, 255, 102,
  318. 102, 255, 102,
  319. 102, 255, 102,
  320. 102, 255, 102,
  321. 102, 255, 153,
  322. 102, 255, 204,
  323. 102, 255, 255,
  324. 51, 255, 255,
  325. 51, 204, 255,
  326. 51, 153, 255,
  327. 51, 102, 255,
  328. 51, 51, 255,
  329. 51, 51, 255,
  330. 51, 51, 255,
  331. 102, 51, 255,
  332. 153, 51, 255,
  333. 204, 51, 255,
  334. 255, 51, 255,
  335. 255, 51, 204,
  336. 255, 51, 153,
  337. 255, 51, 102,
  338. 255, 51, 51,
  339. 255, 51, 51,
  340. 255, 51, 51,
  341. 255, 102, 51,
  342. 255, 153, 51,
  343. 255, 204, 51,
  344. 255, 255, 51,
  345. 204, 255, 51,
  346. 153, 244, 51,
  347. 102, 255, 51,
  348. 51, 255, 51,
  349. 51, 255, 51,
  350. 51, 255, 51,
  351. 51, 255, 102,
  352. 51, 255, 153,
  353. 51, 255, 204,
  354. 51, 255, 255,
  355. 0, 255, 255,
  356. 0, 204, 255,
  357. 0, 153, 255,
  358. 0, 102, 255,
  359. 0, 51, 255,
  360. 0, 0, 255,
  361. 51, 0, 255,
  362. 102, 0, 255,
  363. 153, 0, 255,
  364. 204, 0, 255,
  365. 255, 0, 255,
  366. 255, 0, 204,
  367. 255, 0, 153,
  368. 255, 0, 102,
  369. 255, 0, 51,
  370. 255, 0 , 0,
  371. 255, 51, 0,
  372. 255, 102, 0,
  373. 255, 153, 0,
  374. 255, 204, 0,
  375. 255, 255, 0,
  376. 204, 255, 0,
  377. 153, 255, 0,
  378. 102, 255, 0,
  379. 51, 255, 0,
  380. 0, 255, 0,
  381. 0, 255, 51,
  382. 0, 255, 102,
  383. 0, 255, 153,
  384. 0, 255, 204,
  385. 0, 255, 255,
  386. 0, 204, 204,
  387. 0, 204, 204,
  388. 0, 153, 204,
  389. 0, 102, 204,
  390. 0, 51, 204,
  391. 0, 0, 204,
  392. 51, 0, 204,
  393. 102, 0, 204,
  394. 153, 0, 204,
  395. 204, 0, 204,
  396. 204, 0, 204,
  397. 204, 0, 204,
  398. 204, 0, 153,
  399. 204, 0, 102,
  400. 204, 0, 51,
  401. 204, 0, 0,
  402. 204, 51, 0,
  403. 204, 102, 0,
  404. 204, 153, 0,
  405. 204, 204, 0,
  406. 204, 204, 0,
  407. 204, 204, 0,
  408. 153, 204, 0,
  409. 102, 204, 0,
  410. 51, 204, 0,
  411. 0, 204, 0,
  412. 0, 204, 51,
  413. 0, 204, 102,
  414. 0, 204, 153,
  415. 0, 204, 204,
  416. 0, 204, 204,
  417. 0, 153, 153,
  418. 0, 153, 153,
  419. 0, 153, 153,
  420. 0, 102, 153,
  421. 0, 51, 153,
  422. 0, 0, 153,
  423. 51, 0, 153,
  424. 102, 0, 153,
  425. 153, 0, 153,
  426. 153, 0, 153,
  427. 153, 0, 153,
  428. 153, 0, 153,
  429. 153, 0, 153,
  430. 153, 0, 102,
  431. 153, 0, 51,
  432. 153, 0, 0,
  433. 153, 51, 0,
  434. 153, 102, 0,
  435. 153, 153, 0,
  436. 153, 153, 0,
  437. 153, 153, 0,
  438. 153, 153, 0,
  439. 153, 153, 0,
  440. 102, 153, 0,
  441. 51, 153, 0,
  442. 0, 153, 0,
  443. 0, 153, 51,
  444. 0, 153, 102,
  445. 0, 153, 153,
  446. 0, 153, 153,
  447. 0, 153, 153,
  448. 0, 102, 102,
  449. 0, 102, 102,
  450. 0, 102, 102,
  451. 0, 102, 102,
  452. 0, 51, 102,
  453. 0, 0, 102,
  454. 51, 0, 102,
  455. 102, 0, 102,
  456. 102, 0, 102,
  457. 102, 0, 102,
  458. 102, 0, 102,
  459. 102, 0, 102,
  460. 102, 0, 102,
  461. 102, 0, 102,
  462. 102, 0, 51,
  463. 102, 0, 0,
  464. 102, 51, 0,
  465. 102, 102, 0,
  466. 102, 102, 0,
  467. 102, 102, 0,
  468. 102, 102, 0,
  469. 102, 102, 0,
  470. 102, 102, 0,
  471. 102, 102, 0,
  472. 51, 102, 0,
  473. 0, 102, 0,
  474. 0, 102, 51,
  475. 0, 102, 102,
  476. 0, 102, 102,
  477. 0, 102, 102,
  478. 0, 102, 102,
  479. 0, 51, 51,
  480. 0, 51, 51,
  481. 0, 51, 51,
  482. 0, 51, 51,
  483. 0, 51, 51,
  484. 0, 0, 51,
  485. 51, 0, 51,
  486. 51, 0, 51,
  487. 51, 0, 51,
  488. 51, 0, 51,
  489. 51, 0, 51,
  490. 51, 0, 51,
  491. 51, 0, 51,
  492. 51, 0, 51,
  493. 51, 0, 51,
  494. 51, 0, 0,
  495. 51, 51, 0,
  496. 51, 51, 0,
  497. 51, 51, 0,
  498. 51, 51, 0,
  499. 51, 51, 0,
  500. 51, 51, 0,
  501. 51, 51, 0,
  502. 51, 51, 0,
  503. 51, 51, 0,
  504. 0, 51, 0,
  505. 0, 51, 51,
  506. 0, 51, 51,
  507. 0, 51, 51,
  508. 0, 51, 51,
  509. 0, 51, 51 };
  510. return rawValues;
  511. }
  512. }