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

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

#
Java | 205 lines | 134 code | 27 blank | 44 comment | 11 complexity | 39d8aa6ea93440f2f34ca1354d1dece0 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 5067 2004-06-28 06:45:27Z spestov $
  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. } //}}}
  62. //{{{ ColorWell class
  63. static class ColorWell implements Icon
  64. {
  65. Color color;
  66. ColorWell(Color color)
  67. {
  68. this.color = color;
  69. }
  70. public int getIconWidth()
  71. {
  72. return 35;
  73. }
  74. public int getIconHeight()
  75. {
  76. return 10;
  77. }
  78. public void paintIcon(Component c, Graphics g, int x, int y)
  79. {
  80. if(color == null)
  81. return;
  82. g.setColor(color);
  83. g.fillRect(x,y,getIconWidth(),getIconHeight());
  84. g.setColor(color.darker());
  85. g.drawRect(x,y,getIconWidth()-1,getIconHeight()-1);
  86. }
  87. } //}}}
  88. //{{{ ActionHandler class
  89. class ActionHandler implements ActionListener
  90. {
  91. public void actionPerformed(ActionEvent evt)
  92. {
  93. JDialog parent = GUIUtilities.getParentDialog(ColorWellButton.this);
  94. JDialog dialog;
  95. if (parent != null)
  96. {
  97. dialog = new ColorPickerDialog(parent,
  98. jEdit.getProperty("colorChooser.title"),
  99. true);
  100. }
  101. else
  102. {
  103. dialog = new ColorPickerDialog(
  104. JOptionPane.getFrameForComponent(
  105. ColorWellButton.this),
  106. jEdit.getProperty("colorChooser.title"),
  107. true);
  108. }
  109. dialog.pack();
  110. dialog.setVisible(true);
  111. }
  112. } //}}}
  113. //{{{ ColorPickerDialog class
  114. /**
  115. * Replacement for the color picker dialog provided with Swing. This version
  116. * supports dialog as well as frame parents.
  117. * @since jEdit 4.1pre7
  118. */
  119. private class ColorPickerDialog extends EnhancedDialog implements ActionListener
  120. {
  121. public ColorPickerDialog(Frame parent, String title, boolean modal)
  122. {
  123. super(parent,title,modal);
  124. init();
  125. }
  126. public ColorPickerDialog(Dialog parent, String title, boolean modal)
  127. {
  128. super(parent,title,modal);
  129. getContentPane().setLayout(new BorderLayout());
  130. init();
  131. }
  132. public void ok()
  133. {
  134. Color c = chooser.getColor();
  135. if (c != null)
  136. setSelectedColor(c);
  137. setVisible(false);
  138. }
  139. public void cancel()
  140. {
  141. setVisible(false);
  142. }
  143. public void actionPerformed(ActionEvent evt)
  144. {
  145. if (evt.getSource() == ok)
  146. ok();
  147. else
  148. cancel();
  149. }
  150. //{{{ Private members
  151. private JColorChooser chooser;
  152. private JButton ok;
  153. private JButton cancel;
  154. private void init()
  155. {
  156. Color c = getSelectedColor();
  157. if(c == null)
  158. chooser = new JColorChooser();
  159. else
  160. chooser = new JColorChooser(c);
  161. getContentPane().add(BorderLayout.CENTER, chooser);
  162. Box buttons = new Box(BoxLayout.X_AXIS);
  163. buttons.add(Box.createGlue());
  164. ok = new JButton(jEdit.getProperty("common.ok"));
  165. ok.addActionListener(this);
  166. buttons.add(ok);
  167. buttons.add(Box.createHorizontalStrut(6));
  168. getRootPane().setDefaultButton(ok);
  169. cancel = new JButton(jEdit.getProperty("common.cancel"));
  170. cancel.addActionListener(this);
  171. buttons.add(cancel);
  172. buttons.add(Box.createGlue());
  173. getContentPane().add(BorderLayout.SOUTH, buttons);
  174. pack();
  175. setLocationRelativeTo(getParent());
  176. } //}}}
  177. } //}}}
  178. }