Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

OptionalColourChooser.java 7.9KB

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