PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/gui/ColorWellButton.java

#
Java | 113 lines | 63 code | 13 blank | 37 comment | 5 complexity | 87a749f7dfb2a86f93c4f961654c9f60 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * ColorWellButton.java - Shows color chooser when clicked
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2002 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.gui;
  23. //{{{ Imports
  24. import javax.swing.*;
  25. import java.awt.event.*;
  26. import java.awt.*;
  27. import org.gjt.sp.jedit.jEdit;
  28. import org.gjt.sp.jedit.OperatingSystem;
  29. //}}}
  30. /**
  31. * A button that, when clicked, shows a color chooser.
  32. *
  33. * You can get and set the currently selected color using
  34. * <code>getSelectedColor()</code> and <code>setSelectedColor().
  35. * @author Slava Pestov
  36. * @version $Id: ColorWellButton.java 4301 2002-08-12 20:08:01Z spestov $
  37. */
  38. public class ColorWellButton extends JButton
  39. {
  40. //{{{ ColorWellButton constructor
  41. public ColorWellButton(Color color)
  42. {
  43. setIcon(new ColorWell(color));
  44. setMargin(new Insets(2,2,2,2));
  45. addActionListener(new ActionHandler());
  46. // according to krisk this looks better on OS X...
  47. if(OperatingSystem.isMacOSLF())
  48. putClientProperty("JButton.buttonType","toolbar");
  49. } //}}}
  50. //{{{ getSelectedColor() method
  51. public Color getSelectedColor()
  52. {
  53. return ((ColorWell)getIcon()).color;
  54. } //}}}
  55. //{{{ setSelectedColor() method
  56. public void setSelectedColor(Color color)
  57. {
  58. ((ColorWell)getIcon()).color = color;
  59. repaint();
  60. } //}}}
  61. //{{{ ColorWell class
  62. static class ColorWell implements Icon
  63. {
  64. Color color;
  65. ColorWell(Color color)
  66. {
  67. this.color = color;
  68. }
  69. public int getIconWidth()
  70. {
  71. return 35;
  72. }
  73. public int getIconHeight()
  74. {
  75. return 10;
  76. }
  77. public void paintIcon(Component c, Graphics g, int x, int y)
  78. {
  79. if(color == null)
  80. return;
  81. g.setColor(color);
  82. g.fillRect(x,y,getIconWidth(),getIconHeight());
  83. g.setColor(color.darker());
  84. g.drawRect(x,y,getIconWidth()-1,getIconHeight()-1);
  85. }
  86. } //}}}
  87. //{{{ ActionHandler class
  88. class ActionHandler implements ActionListener
  89. {
  90. public void actionPerformed(ActionEvent evt)
  91. {
  92. Color color = JColorChooser.showDialog(
  93. ColorWellButton.this,
  94. jEdit.getProperty("colorChooser.title"),
  95. getSelectedColor());
  96. if(color != null)
  97. setSelectedColor(color);
  98. }
  99. } //}}}
  100. }