File Source: ActionGroupSettingsPanel.java

         /* 
    P/P   *  Method: com.dmdirc.addons.ui_swing.dialogs.actionsmanager.ActionGroupSettingsPanel__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.dialogs.actionsmanager;
    24  
    25  import com.dmdirc.actions.ActionGroup;
    26  import com.dmdirc.config.prefs.PreferencesSetting;
    27  import com.dmdirc.addons.ui_swing.PrefsComponentFactory;
    28  import com.dmdirc.addons.ui_swing.components.ColourChooser;
    29  import com.dmdirc.addons.ui_swing.components.OptionalColourChooser;
    30  import com.dmdirc.addons.ui_swing.components.durationeditor.DurationDisplay;
    31  
    32  import java.awt.Window;
    33  import java.awt.event.ActionEvent;
    34  import java.awt.event.ActionListener;
    35  import java.util.ArrayList;
    36  import java.util.Collection;
    37  import java.util.HashMap;
    38  import java.util.Map;
    39  
    40  import javax.swing.JButton;
    41  import javax.swing.JComponent;
    42  import javax.swing.JLabel;
    43  import javax.swing.JPanel;
    44  
    45  import net.miginfocom.swing.MigLayout;
    46  
    47  /**
    48   * Action group settings panel.
    49   */
    50  public final class ActionGroupSettingsPanel extends JPanel implements ActionListener {
    51  
    52      /**
    53       * A version number for this class. It should be changed whenever the class
    54       * structure is changed (or anything else that would prevent serialized
    55       * objects being unserialized with the new class).
    56       */
    57      private static final long serialVersionUID = 1;
    58      /** Settings list. */
    59      private Collection<PreferencesSetting> settings;
    60      /** Button -> Component map. */
    61      private Map<JButton, PreferencesSetting> settingMap;
    62      /** Parent dialog. */
    63      private Window window;
    64      
    65      /**
    66       * Initialises a new action group information panel.
    67       * 
    68       * @param group Action group
    69       */
    70      public ActionGroupSettingsPanel(final ActionGroup group) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.dialogs.actionsmanager.ActionGroupSettingsPanel(ActionGroup)
                  * 
                  *  Postconditions:
                  *    this.settingMap == &amp;new HashMap(initComponents#1)
                  *    this.settings != null
                  *    this.window == null
                  *    new ArrayList(setActionGroup#1) num objects <= 1
                  *    new HashMap(initComponents#1) num objects == 1
                  */
    71          this(group, null);
    72      }
    73  
    74      /**
    75       * Initialises a new action group information panel.
    76       * 
    77       * @param group Action group
    78       * @param window Parent window
    79       * 
    80       * @since 0.6
    81       */
    82      public ActionGroupSettingsPanel(final ActionGroup group, final Window window) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.dialogs.actionsmanager.ActionGroupSettingsPanel(ActionGroup, Window)
                  * 
                  *  Postconditions:
                  *    this.settingMap == &amp;new HashMap(initComponents#1)
                  *    this.settings != null
                  *    this.window == window
                  *    init'ed(this.window)
                  *    new ArrayList(setActionGroup#1) num objects <= 1
                  *    new HashMap(initComponents#1) num objects == 1
                  */
    83          super();
    84          
    85          this.window = window;
    86  
    87          initComponents();
    88          addListeners();
    89  
    90          setActionGroup(group);
    91      }
    92  
    93      /**
    94       * Initialises the components.
    95       */
    96      private void initComponents() {
                 /* 
    P/P           *  Method: void initComponents()
                  * 
                  *  Postconditions:
                  *    this.settingMap == &amp;new HashMap(initComponents#1)
                  *    new HashMap(initComponents#1) num objects == 1
                  */
    97          settingMap = new HashMap<JButton, PreferencesSetting>();
    98      }
    99  
   100      /**
   101       * Adds listeners.
   102       */
   103      private void addListeners() {
   104      //Empty
             /* 
    P/P       *  Method: void addListeners()
              */
   105      }
   106  
   107      /**
   108       * Lays out the components.
   109       */
   110      private void layoutComponents() {
                 /* 
    P/P           *  Method: void layoutComponents()
                  * 
                  *  Preconditions:
                  *    this.settings != null
                  *    (soft) this.settingMap != null
                  *    (soft) init'ed(this.window)
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@114 != null
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@114: {0}, {1}
                  */
   111          removeAll();
   112          setLayout(new MigLayout("fill, hidemode 3"));
   113  
   114          for (PreferencesSetting setting : settings) {
   115              final JLabel label = new JLabel(setting.getTitle());
   116              label.setToolTipText(setting.getTitle());
   117              final JComponent component =
   118                      PrefsComponentFactory.getComponent(setting);
   119              if (component instanceof DurationDisplay) {
   120              ((DurationDisplay) component).setWindow(window);
   121          } else if (component instanceof ColourChooser) {
   122              ((ColourChooser) component).setWindow(window);
   123          } else if (component instanceof OptionalColourChooser) {
   124              ((OptionalColourChooser) component).setWindow(window);
   125          }
   126              final JButton button = new SettingsRevertButton(setting);
   127              settingMap.put(button, setting);
   128              button.addActionListener(this);
   129              add(label, "newline");
   130              add(component, "growx, pushx");
   131              add(button, "");
   132          }
   133      }
   134  
   135      /**
   136       * Sets the action group for the panel.
   137       * 
   138       * @param group New action group
   139       */
   140      public void setActionGroup(final ActionGroup group) {
                 /* 
    P/P           *  Method: void setActionGroup(ActionGroup)
                  * 
                  *  Preconditions:
                  *    (soft) this.settingMap != null
                  *    (soft) init'ed(this.window)
                  * 
                  *  Presumptions:
                  *    com.dmdirc.actions.ActionGroup:getSettings(...)@141 != null
                  *    com.dmdirc.actions.ActionGroup:getSettings(...)@144 != null
                  *    java.util.Map:values(...)@144 != null
                  * 
                  *  Postconditions:
                  *    this.settings != null
                  *    new ArrayList(setActionGroup#1) num objects <= 1
                  * 
                  *  Test Vectors:
                  *    group: Addr_Set{null}, Inverse{null}
                  *    java.util.Map:isEmpty(...)@141: {0}, {1}
                  */
   141          if (group == null || group.getSettings().isEmpty()) {
   142              this.settings = new ArrayList<PreferencesSetting>();
   143          } else {
   144              this.settings = group.getSettings().values();
   145          }
   146  
   147          layoutComponents();
   148      }
   149  
   150      /**
   151       * Should the settings panel be shown?
   152       * 
   153       * @return true iif the panel should be shown
   154       */
   155      public boolean shouldDisplay() {
                 /* 
    P/P           *  Method: bool shouldDisplay()
                  * 
                  *  Preconditions:
                  *    this.settings != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
   156          return !settings.isEmpty();
   157      }
   158  
   159      /**
   160       * Saves the changes to the settings.
   161       */
   162      public void save() {
                 /* 
    P/P           *  Method: void save()
                  * 
                  *  Preconditions:
                  *    this.settings != null
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@163 != null
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@163: {0}, {1}
                  */
   163          for (PreferencesSetting setting : settings) {
   164              setting.save();
   165          }
   166      }
   167  
   168      /** 
   169       * {@inheritDoc}
   170       * 
   171       * @param e Action event
   172       */
   173      @Override
   174      public void actionPerformed(final ActionEvent e) {
                 /* 
    P/P           *  Method: void actionPerformed(ActionEvent)
                  * 
                  *  Preconditions:
                  *    e != null
                  *    this.settingMap != null
                  *    this.settings != null
                  *    (soft) init'ed(this.window)
                  * 
                  *  Presumptions:
                  *    java.util.Map:get(...)@176 != null
                  */
   175          setVisible(false);
   176          settingMap.get(e.getSource()).dismiss();
   177          layoutComponents();
   178          setVisible(true);
   179      }
   180  }








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