PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/ColorWellButton.java

#
Java | 127 lines | 77 code | 13 blank | 37 comment | 7 complexity | d7eea6c087fd7b42cabe54571f172ea4 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.GUIUtilities;
  29. import org.gjt.sp.jedit.OperatingSystem;
  30. //}}}
  31. /**
  32. * A button that, when clicked, shows a color chooser.
  33. *
  34. * You can get and set the currently selected color using
  35. * {@link #getSelectedColor()} and {@link #setSelectedColor(Color)}.
  36. * @author Slava Pestov
  37. * @version $Id: ColorWellButton.java 18910 2010-10-31 14:04:47Z daleanson $
  38. */
  39. public class ColorWellButton extends JButton
  40. {
  41. //{{{ ColorWellButton constructor
  42. public ColorWellButton(Color color)
  43. {
  44. setIcon(new ColorWell(color));
  45. setMargin(new Insets(2,2,2,2));
  46. addActionListener(new ActionHandler());
  47. // according to krisk this looks better on OS X...
  48. if(OperatingSystem.isMacOSLF())
  49. putClientProperty("JButton.buttonType","toolbar");
  50. } //}}}
  51. //{{{ getSelectedColor() method
  52. public Color getSelectedColor()
  53. {
  54. return ((ColorWell)getIcon()).color;
  55. } //}}}
  56. //{{{ setSelectedColor() method
  57. public void setSelectedColor(Color color)
  58. {
  59. ((ColorWell)getIcon()).color = color;
  60. repaint();
  61. fireStateChanged();
  62. } //}}}
  63. //{{{ ColorWell class
  64. static class ColorWell implements Icon
  65. {
  66. Color color;
  67. ColorWell(Color color)
  68. {
  69. this.color = color;
  70. }
  71. public int getIconWidth()
  72. {
  73. return 35;
  74. }
  75. public int getIconHeight()
  76. {
  77. return 10;
  78. }
  79. public void paintIcon(Component c, Graphics g, int x, int y)
  80. {
  81. if(color == null)
  82. return;
  83. g.setColor(color);
  84. g.fillRect(x,y,getIconWidth(),getIconHeight());
  85. g.setColor(color.darker());
  86. g.drawRect(x,y,getIconWidth()-1,getIconHeight()-1);
  87. }
  88. } //}}}
  89. //{{{ ActionHandler class
  90. class ActionHandler implements ActionListener
  91. {
  92. public void actionPerformed(ActionEvent evt)
  93. {
  94. JDialog parent = GUIUtilities.getParentDialog(ColorWellButton.this);
  95. Color c = null;
  96. if (parent != null)
  97. {
  98. c = JColorChooser.showDialog(parent,
  99. jEdit.getProperty("colorChooser.title"),
  100. ColorWellButton.this.getSelectedColor());
  101. }
  102. else
  103. {
  104. c = JColorChooser.showDialog(
  105. SwingUtilities.getRoot(ColorWellButton.this),
  106. jEdit.getProperty("colorChooser.title"),
  107. ColorWellButton.this.getSelectedColor());
  108. }
  109. if (c != null) {
  110. setSelectedColor(c);
  111. }
  112. }
  113. } //}}}
  114. }