You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OptionalColourChooser.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.components.colours;
  23. import com.dmdirc.addons.ui_swing.UIUtilities;
  24. import com.dmdirc.ui.messages.ColourManager;
  25. import com.dmdirc.util.ListenerList;
  26. import java.awt.Color;
  27. import java.awt.Dimension;
  28. import java.awt.Insets;
  29. import java.awt.Window;
  30. import java.awt.event.ActionEvent;
  31. import java.awt.event.ActionListener;
  32. import javax.swing.BorderFactory;
  33. import javax.swing.JButton;
  34. import javax.swing.JCheckBox;
  35. import javax.swing.JPanel;
  36. import net.miginfocom.swing.MigLayout;
  37. /**
  38. * Colour chooser widget.
  39. */
  40. public final class OptionalColourChooser extends JPanel implements ActionListener {
  41. /**
  42. * A version number for this class. It should be changed whenever the class
  43. * structure is changed (or anything else that would prevent serialized
  44. * objects being unserialized with the new class).
  45. */
  46. private static final long serialVersionUID = 1;
  47. /** Enabled checkbox. */
  48. private JCheckBox enabled;
  49. /** Edit button. */
  50. private JButton editButton;
  51. /** Panel to show the colour preview. */
  52. private JPanel previewPanel;
  53. /** Colours picking dialog. */
  54. private ColourPickerDialog cpd;
  55. /** show irc colours. */
  56. private boolean showIRC;
  57. /** show hex colours. */
  58. private boolean showHex;
  59. /** The value of this component. */
  60. private String value;
  61. /** Our listeners. */
  62. private final ListenerList listeners = new ListenerList();
  63. /** Parent window. */
  64. private Window window;
  65. /** Creates a new instance of ColourChooser. */
  66. public OptionalColourChooser() {
  67. this("", false, true, true);
  68. }
  69. /**
  70. * Creates a new instance of ColourChooser.
  71. *
  72. * @param window Parent window
  73. *
  74. * @since 0.6
  75. */
  76. public OptionalColourChooser(final Window window) {
  77. this("", false, true, true, window);
  78. }
  79. /**
  80. * Creates a new instance of ColourChooser.
  81. *
  82. * @param initialColour Snitial colour
  83. * @param initialState Initial state
  84. * @param ircColours Show irc colours
  85. * @param hexColours Show hex colours
  86. */
  87. public OptionalColourChooser(final String initialColour,
  88. final boolean initialState, final boolean ircColours,
  89. final boolean hexColours) {
  90. this(initialColour, initialState, ircColours, hexColours, null);
  91. }
  92. /**
  93. * Creates a new instance of ColourChooser.
  94. *
  95. * @param initialColour Snitial colour
  96. * @param initialState Initial state
  97. * @param ircColours Show irc colours
  98. * @param hexColours Show hex colours
  99. * @param window Parent window
  100. *
  101. * @since 0.6
  102. */
  103. public OptionalColourChooser(final String initialColour,
  104. final boolean initialState, final boolean ircColours,
  105. final boolean hexColours, final Window window) {
  106. super();
  107. this.window = window;
  108. showIRC = ircColours;
  109. showHex = hexColours;
  110. value = initialColour;
  111. editButton = new JButton("Edit");
  112. if (UIUtilities.isWindowsUI()) {
  113. editButton.setMargin(new Insets(2, 4, 2, 4));
  114. } else {
  115. editButton.setMargin(new Insets(0, 2, 0, 2));
  116. }
  117. editButton.addActionListener(this);
  118. if (!initialState) {
  119. editButton.setEnabled(false);
  120. }
  121. previewPanel = new JPanel();
  122. previewPanel.setPreferredSize(new Dimension(40, 10));
  123. previewPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
  124. enabled = new JCheckBox();
  125. enabled.setPreferredSize(new Dimension(40, 40));
  126. enabled.setSelected(initialState);
  127. enabled.addActionListener(this);
  128. enabled.setOpaque(false);
  129. setOpaque(false);
  130. setLayout(new MigLayout("fill, ins 0"));
  131. add(enabled, "sgy all");
  132. add(previewPanel, "growx, pushx, sgy all");
  133. add(editButton, "sgy all");
  134. updateColour(initialColour);
  135. }
  136. /** Sets the colour back to white. */
  137. public void clearColour() {
  138. value = "ffffff";
  139. previewPanel.setBackground(ColourManager.getColour("ffffff"));
  140. previewPanel.setToolTipText("");
  141. }
  142. /** {@inheritDoc} */
  143. @Override
  144. public boolean isEnabled() {
  145. return enabled.isSelected();
  146. }
  147. /**
  148. * Returns the selected colour from this component.
  149. *
  150. * @return This components colour, as a string
  151. */
  152. public String getColour() {
  153. return value;
  154. }
  155. /**
  156. * Updates the colour panel.
  157. * @param newColour The new colour to use.
  158. */
  159. private void updateColour(final String newColour) {
  160. if (newColour.isEmpty()) {
  161. previewPanel.setBackground(ColourManager.getColour("ffffff"));
  162. previewPanel.setToolTipText("");
  163. } else {
  164. previewPanel.setBackground(ColourManager.parseColour(newColour));
  165. previewPanel.setToolTipText(newColour);
  166. }
  167. }
  168. /**
  169. * {@inheritDoc}.
  170. *
  171. * @param e Action event
  172. */
  173. @Override
  174. public void actionPerformed(final ActionEvent e) {
  175. if (e.getSource() == editButton) {
  176. cpd = new ColourPickerDialog(showIRC, showHex, window);
  177. cpd.addActionListener(this);
  178. cpd.display(editButton);
  179. } else if (e.getSource() == enabled) {
  180. editButton.setEnabled(enabled.isSelected());
  181. fireActionEvent();
  182. } else {
  183. value = e.getActionCommand();
  184. updateColour(e.getActionCommand());
  185. fireActionEvent();
  186. cpd.dispose();
  187. }
  188. }
  189. /**
  190. * Adds an action listener to this optional colour chooser. Action
  191. * listeners are notified whenever the state changes in some way.
  192. *
  193. * @param l The listener to be added
  194. */
  195. public void addActionListener(final ActionListener l) {
  196. listeners.add(ActionListener.class, l);
  197. }
  198. /**
  199. * Informs all action listeners that an action has occured.
  200. */
  201. protected void fireActionEvent() {
  202. for (ActionListener listener : listeners.get(ActionListener.class)) {
  203. listener.actionPerformed(new ActionEvent(this, 1, "stuffChanged"));
  204. }
  205. }
  206. /**
  207. * Sets the Parent window.
  208. *
  209. * @param window Parent window
  210. */
  211. public void setWindow(final Window window) {
  212. this.window = window;
  213. }
  214. }