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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (c) 2006-2010 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.components.colours.ColourPickerDialog;
  24. import com.dmdirc.ui.messages.ColourManager;
  25. import com.dmdirc.util.ListenerList;
  26. import com.dmdirc.addons.ui_swing.UIUtilities;
  27. import java.awt.Color;
  28. import java.awt.Dimension;
  29. import java.awt.Insets;
  30. import java.awt.Window;
  31. import java.awt.event.ActionEvent;
  32. import java.awt.event.ActionListener;
  33. import javax.swing.BorderFactory;
  34. import javax.swing.JButton;
  35. import javax.swing.JCheckBox;
  36. import javax.swing.JPanel;
  37. import net.miginfocom.swing.MigLayout;
  38. /**
  39. * Colour chooser widget.
  40. */
  41. public final class OptionalColourChooser extends JPanel implements ActionListener {
  42. /**
  43. * A version number for this class. It should be changed whenever the class
  44. * structure is changed (or anything else that would prevent serialized
  45. * objects being unserialized with the new class).
  46. */
  47. private static final long serialVersionUID = 1;
  48. /** Enabled checkbox. */
  49. private JCheckBox enabled;
  50. /** Edit button. */
  51. private JButton editButton;
  52. /** Panel to show the colour preview. */
  53. private JPanel previewPanel;
  54. /** Colours picking dialog. */
  55. private ColourPickerDialog cpd;
  56. /** show irc colours. */
  57. private boolean showIRC;
  58. /** show hex colours. */
  59. private boolean showHex;
  60. /** The value of this component. */
  61. private String value;
  62. /** Our listeners. */
  63. private final ListenerList listeners = new ListenerList();
  64. /** Parent window. */
  65. private Window window;
  66. /** Creates a new instance of ColourChooser. */
  67. public OptionalColourChooser() {
  68. this("", false, true, true);
  69. }
  70. /**
  71. * Creates a new instance of ColourChooser.
  72. *
  73. * @param window Parent window
  74. *
  75. * @since 0.6
  76. */
  77. public OptionalColourChooser(final Window window) {
  78. this("", false, true, true, window);
  79. }
  80. /**
  81. * Creates a new instance of ColourChooser.
  82. *
  83. * @param initialColour Snitial colour
  84. * @param initialState Initial state
  85. * @param ircColours Show irc colours
  86. * @param hexColours Show hex colours
  87. */
  88. public OptionalColourChooser(final String initialColour,
  89. final boolean initialState, final boolean ircColours,
  90. final boolean hexColours) {
  91. this(initialColour, initialState, ircColours, hexColours, null);
  92. }
  93. /**
  94. * Creates a new instance of ColourChooser.
  95. *
  96. * @param initialColour Snitial colour
  97. * @param initialState Initial state
  98. * @param ircColours Show irc colours
  99. * @param hexColours Show hex colours
  100. * @param window Parent window
  101. *
  102. * @since 0.6
  103. */
  104. public OptionalColourChooser(final String initialColour,
  105. final boolean initialState, final boolean ircColours,
  106. final boolean hexColours, final Window window) {
  107. super();
  108. this.window = window;
  109. showIRC = ircColours;
  110. showHex = hexColours;
  111. value = initialColour;
  112. editButton = new JButton("Edit");
  113. if (UIUtilities.isWindowsUI()) {
  114. editButton.setMargin(new Insets(2, 4, 2, 4));
  115. } else {
  116. editButton.setMargin(new Insets(0, 2, 0, 2));
  117. }
  118. editButton.addActionListener(this);
  119. if (!initialState) {
  120. editButton.setEnabled(false);
  121. }
  122. previewPanel = new JPanel();
  123. previewPanel.setPreferredSize(new Dimension(40, 10));
  124. previewPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
  125. enabled = new JCheckBox();
  126. enabled.setPreferredSize(new Dimension(40, 40));
  127. enabled.setSelected(initialState);
  128. enabled.addActionListener(this);
  129. setLayout(new MigLayout("fill, ins 0"));
  130. add(enabled, "sgy all");
  131. add(previewPanel, "growx, pushx, sgy all");
  132. add(editButton, "sgy all");
  133. updateColour(initialColour);
  134. }
  135. /** Sets the colour back to white. */
  136. public void clearColour() {
  137. value = "ffffff";
  138. previewPanel.setBackground(ColourManager.getColour("ffffff"));
  139. previewPanel.setToolTipText("");
  140. }
  141. /** {@inheritDoc} */
  142. @Override
  143. public boolean isEnabled() {
  144. return enabled.isSelected();
  145. }
  146. /**
  147. * Returns the selected colour from this component.
  148. *
  149. * @return This components colour, as a string
  150. */
  151. public String getColour() {
  152. return value;
  153. }
  154. /**
  155. * Updates the colour panel.
  156. * @param newColour The new colour to use.
  157. */
  158. private void updateColour(final String newColour) {
  159. if (newColour.isEmpty()) {
  160. previewPanel.setBackground(ColourManager.getColour("ffffff"));
  161. previewPanel.setToolTipText("");
  162. } else {
  163. previewPanel.setBackground(ColourManager.parseColour(newColour));
  164. previewPanel.setToolTipText(newColour);
  165. }
  166. }
  167. /**
  168. * {@inheritDoc}.
  169. *
  170. * @param e Action event
  171. */
  172. @Override
  173. public void actionPerformed(final ActionEvent e) {
  174. if (e.getSource() == editButton) {
  175. cpd = new ColourPickerDialog(showIRC, showHex, window);
  176. cpd.addActionListener(this);
  177. cpd.display(editButton);
  178. } else if (e.getSource() == enabled) {
  179. editButton.setEnabled(enabled.isSelected());
  180. fireActionEvent();
  181. } else {
  182. value = e.getActionCommand();
  183. updateColour(e.getActionCommand());
  184. fireActionEvent();
  185. cpd.dispose();
  186. }
  187. }
  188. /**
  189. * Adds an action listener to this optional colour chooser. Action
  190. * listeners are notified whenever the state changes in some way.
  191. *
  192. * @param l The listener to be added
  193. */
  194. public void addActionListener(final ActionListener l) {
  195. listeners.add(ActionListener.class, l);
  196. }
  197. /**
  198. * Informs all action listeners that an action has occured.
  199. */
  200. protected void fireActionEvent() {
  201. for (ActionListener listener : listeners.get(ActionListener.class)) {
  202. listener.actionPerformed(new ActionEvent(this, 1, "stuffChanged"));
  203. }
  204. }
  205. /**
  206. * Sets the Parent window.
  207. *
  208. * @param window Parent window
  209. */
  210. public void setWindow(final Window window) {
  211. this.window = window;
  212. }
  213. }