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