123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- /*
- * Copyright (c) 2006-2013 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.addons.ui_swing.components.colours;
-
- import com.dmdirc.addons.ui_swing.UIUtilities;
- import com.dmdirc.ui.IconManager;
- import com.dmdirc.ui.messages.ColourManager;
- import com.dmdirc.util.collections.ListenerList;
-
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Insets;
- import java.awt.Window;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
-
- import javax.swing.BorderFactory;
- import javax.swing.JButton;
- import javax.swing.JCheckBox;
- import javax.swing.JPanel;
-
- import net.miginfocom.swing.MigLayout;
-
- /**
- * Colour chooser widget.
- */
- public final class OptionalColourChooser extends JPanel implements
- ActionListener {
-
- /** Serial version UID. */
- private static final long serialVersionUID = 1;
- private String value;
- /** Our listeners. */
- private final ListenerList listeners = new ListenerList();
- /** Icon manager. */
- private final IconManager iconManager;
- /** Enabled checkbox. */
- private final JCheckBox enabled;
- /** Edit button. */
- private final JButton editButton;
- /** Panel to show the colour preview. */
- private final JPanel previewPanel;
- /** Colours picking dialog. */
- private ColourPickerDialog cpd;
- /** show irc colours. */
- private final boolean showIRC;
- /** show hex colours. */
- private final boolean showHex;
- /** The value of this component. */
- /** Parent window. */
- private Window window;
-
- /**
- * Creates a new instance of ColourChooser.
- *
- * @param iconManager Icon manager
- */
- public OptionalColourChooser(final IconManager iconManager) {
- this(iconManager, "", false, true, true);
- }
-
- /**
- * Creates a new instance of ColourChooser.
- *
- * @param window Parent window
- *
- * @since 0.6
- */
- public OptionalColourChooser(final IconManager iconManager,
- final Window window) {
- this(iconManager, "", false, true, true, window);
- }
-
- /**
- * Creates a new instance of ColourChooser.
- *
- * @param iconManager Icon manager
- * @param initialColour Snitial colour
- * @param initialState Initial state
- * @param ircColours Show irc colours
- * @param hexColours Show hex colours
- */
- public OptionalColourChooser(final IconManager iconManager,
- final String initialColour,
- final boolean initialState, final boolean ircColours,
- final boolean hexColours) {
- this(iconManager, initialColour, initialState, ircColours, hexColours,
- null);
- }
-
- /**
- * Creates a new instance of ColourChooser.
- *
- * @param iconManager Icon manager
- * @param initialColour Snitial colour
- * @param initialState Initial state
- * @param ircColours Show irc colours
- * @param hexColours Show hex colours
- * @param window Parent window
- *
- * @since 0.6
- */
- public OptionalColourChooser(final IconManager iconManager,
- final String initialColour,
- final boolean initialState, final boolean ircColours,
- final boolean hexColours, final Window window) {
- super();
-
- this.iconManager = iconManager;
- this.window = window;
- showIRC = ircColours;
- showHex = hexColours;
-
- editButton = new JButton("Edit");
- if (UIUtilities.isWindowsUI()) {
- editButton.setMargin(new Insets(2, 4, 2, 4));
- } else {
- editButton.setMargin(new Insets(0, 2, 0, 2));
- }
- editButton.addActionListener(this);
- if (!initialState) {
- editButton.setEnabled(false);
- }
-
- previewPanel = new JPanel();
- previewPanel.setPreferredSize(new Dimension(40, 10));
- previewPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
-
- enabled = new JCheckBox();
- enabled.setPreferredSize(new Dimension(40, 40));
- enabled.setSelected(initialState);
- enabled.addActionListener(this);
- enabled.setOpaque(false);
- setOpaque(false);
-
- setLayout(new MigLayout("fill, ins 0"));
-
- add(enabled, "sgy all");
- add(previewPanel, "growx, pushx, sgy all");
- add(editButton, "sgy all");
-
- if (initialColour.contains(":")) {
- value = initialColour.substring(initialColour.indexOf(':') + 1);
- } else {
- value = initialColour;
- }
- updateColour(value);
- }
-
- /** Sets the colour back to white. */
- public void clearColour() {
- value = "ffffff";
- previewPanel.setBackground(Color.WHITE);
- previewPanel.setToolTipText("");
- }
-
- /** {@inheritDoc} */
- @Override
- public boolean isEnabled() {
- return enabled.isSelected();
- }
-
- /**
- * Returns the selected colour from this component.
- *
- * @return This components colour, as a string
- */
- public String getColour() {
- return value;
- }
-
- /**
- * Updates the colour panel.
- *
- * @param newColour The new colour to use.
- */
- private void updateColour(final String newColour) {
- if (newColour.isEmpty()) {
- previewPanel.setBackground(Color.WHITE);
- previewPanel.setToolTipText("");
- } else {
- previewPanel.setBackground(UIUtilities.convertColour(
- ColourManager.parseColour(newColour)));
- previewPanel.setToolTipText(newColour);
- }
- }
-
- /**
- * {@inheritDoc}.
- *
- * @param e Action event
- */
- @Override
- public void actionPerformed(final ActionEvent e) {
- if (e.getSource() == editButton) {
- cpd = new ColourPickerDialog(iconManager, showIRC, showHex, window);
- cpd.addActionListener(this);
- cpd.display(editButton);
- } else if (e.getSource() == enabled) {
- editButton.setEnabled(enabled.isSelected());
- fireActionEvent();
- } else {
- value = e.getActionCommand();
- updateColour(e.getActionCommand());
- fireActionEvent();
- cpd.dispose();
- }
- }
-
- /**
- * Adds an action listener to this optional colour chooser. Action
- * listeners are notified whenever the state changes in some way.
- *
- * @param l The listener to be added
- */
- public void addActionListener(final ActionListener l) {
- listeners.add(ActionListener.class, l);
- }
-
- /**
- * Informs all action listeners that an action has occured.
- */
- protected void fireActionEvent() {
- for (final ActionListener listener : listeners
- .get(ActionListener.class)) {
- listener.actionPerformed(new ActionEvent(this, 1, "stuffChanged"));
- }
- }
-
- /**
- * Sets the Parent window.
- *
- * @param window Parent window
- */
- public void setWindow(final Window window) {
- this.window = window;
- }
- }
|