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.

ColourChooser.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (c) 2006-2014 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.Colour;
  25. import com.dmdirc.ui.IconManager;
  26. import com.dmdirc.ui.messages.ColourManager;
  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.JPanel;
  36. import javax.swing.event.EventListenerList;
  37. import net.miginfocom.swing.MigLayout;
  38. /**
  39. * Colour chooser widget.
  40. */
  41. public class ColourChooser extends JPanel implements ActionListener {
  42. /** Serial version UID. */
  43. private static final long serialVersionUID = 1;
  44. /** Edit button. */
  45. private final JButton editButton;
  46. /** Panel to show the colour preview. */
  47. private final JPanel previewPanel;
  48. /** Event listeners. */
  49. private final EventListenerList listeners;
  50. /** Icon manager. */
  51. private final IconManager iconManager;
  52. /** The colour manager to use to parse colours. */
  53. private final ColourManager colourManager;
  54. /** Colours picking dialog. */
  55. private ColourPickerDialog cpd;
  56. /** show irc colours. */
  57. private final boolean showIRC;
  58. /** show hex colours. */
  59. private final boolean showHex;
  60. /** The value of this component. */
  61. private String value;
  62. /** Action command. */
  63. private String command;
  64. /** Parent window. */
  65. private Window window;
  66. /**
  67. * Creates a new instance of ColourChooser.
  68. *
  69. * @param colourManager The colour manager to use to parse colours.
  70. * @param iconManager Icon manager
  71. * @param initialColour initial colour
  72. * @param ircColours show irc colours
  73. * @param hexColours show hex colours
  74. *
  75. * @since 0.6
  76. */
  77. public ColourChooser(
  78. final ColourManager colourManager, final IconManager iconManager,
  79. final String initialColour, final boolean ircColours,
  80. final boolean hexColours) {
  81. super();
  82. this.colourManager = colourManager;
  83. this.iconManager = iconManager;
  84. showIRC = ircColours;
  85. showHex = hexColours;
  86. value = initialColour;
  87. listeners = new EventListenerList();
  88. command = "";
  89. editButton = new JButton("Edit");
  90. if (UIUtilities.isWindowsUI()) {
  91. editButton.setMargin(new Insets(2, 4, 2, 4));
  92. } else {
  93. editButton.setMargin(new Insets(0, 2, 0, 2));
  94. }
  95. editButton.addActionListener(this);
  96. previewPanel = new JPanel();
  97. previewPanel.setPreferredSize(new Dimension(40, 10));
  98. previewPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
  99. setOpaque(false);
  100. setLayout(new MigLayout("ins 0, fill"));
  101. add(previewPanel, "growx, pushx, sgy all");
  102. add(editButton, "sgy all");
  103. updateColour(initialColour);
  104. }
  105. /**
  106. * Returns the selected colour from this component.
  107. *
  108. * @return This components colour, as a string
  109. */
  110. public String getColour() {
  111. return value;
  112. }
  113. /**
  114. * Sets the selected colour for this component.
  115. *
  116. * @param newValue New colour
  117. */
  118. public void setColour(final String newValue) {
  119. value = newValue;
  120. updateColour(value);
  121. }
  122. /** Sets the colour back to white. */
  123. public void clearColour() {
  124. value = "ffffff";
  125. previewPanel.setBackground(Color.WHITE);
  126. previewPanel.setToolTipText("");
  127. }
  128. /**
  129. * Updates the colour panel.
  130. *
  131. * @param newColour The new colour to use.
  132. */
  133. private void updateColour(final String newColour) {
  134. if (newColour == null || newColour.isEmpty()) {
  135. previewPanel.setBackground(Color.WHITE);
  136. previewPanel.setToolTipText("");
  137. } else {
  138. previewPanel.setBackground(UIUtilities.convertColour(
  139. colourManager.getColourFromString(newColour, Colour.WHITE)));
  140. previewPanel.setToolTipText(newColour);
  141. }
  142. }
  143. /**
  144. * {@inheritDoc}.
  145. *
  146. * @param e Action event
  147. */
  148. @Override
  149. public void actionPerformed(final ActionEvent e) {
  150. if (e.getSource() == editButton) {
  151. cpd = new ColourPickerDialog(editButton, colourManager, iconManager, showIRC,
  152. showHex, window);
  153. cpd.addActionListener(this);
  154. cpd.setVisible(true);
  155. } else {
  156. value = e.getActionCommand();
  157. updateColour(e.getActionCommand());
  158. fireActionPerformed();
  159. cpd.dispose();
  160. }
  161. }
  162. /**
  163. * Sets this colour choosers action command.
  164. *
  165. * @param command New action command
  166. */
  167. public void setActionCommand(final String command) {
  168. this.command = command;
  169. }
  170. /**
  171. * Adds a ActionListener to the listener list.
  172. *
  173. * @param listener Listener to add
  174. */
  175. public void addActionListener(final ActionListener listener) {
  176. synchronized (listeners) {
  177. if (listener == null) {
  178. return;
  179. }
  180. listeners.add(ActionListener.class, listener);
  181. }
  182. }
  183. /**
  184. * Removes a ActionListener from the listener list.
  185. *
  186. * @param listener Listener to remove
  187. */
  188. public void removeActionListener(final ActionListener listener) {
  189. listeners.remove(ActionListener.class, listener);
  190. }
  191. /**
  192. * Fires the action performed method on all listeners.
  193. */
  194. protected void fireActionPerformed() {
  195. final Object[] localListenerList = listeners.getListenerList();
  196. for (int i = 0; i < localListenerList.length; i += 2) {
  197. if (localListenerList[i] == ActionListener.class) {
  198. ((ActionListener) localListenerList[i + 1]).actionPerformed(
  199. new ActionEvent(this,
  200. ActionEvent.ACTION_PERFORMED, command));
  201. }
  202. }
  203. }
  204. /**
  205. * Sets the Parent window.
  206. *
  207. * @param window Parent window
  208. */
  209. public void setWindow(final Window window) {
  210. this.window = window;
  211. }
  212. }