File Source: OptionalColourChooser.java

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








SofCheck Inspector Build Version : 2.17854
OptionalColourChooser.java 2009-Jun-25 01:54:24
OptionalColourChooser.class 2009-Sep-02 17:04:15