File Source: NickColourPanel.java

         /* 
    P/P   *  Method: com.dmdirc.addons.nickcolours.NickColourPanel$1__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.nickcolours;
    24  
    25  import com.dmdirc.addons.ui_swing.dialogs.prefs.SwingPreferencesDialog;
    26  import com.dmdirc.config.IdentityManager;
    27  import com.dmdirc.config.prefs.PreferencesInterface;
    28  
    29  import java.awt.Color;
    30  import java.awt.event.ActionEvent;
    31  import java.awt.event.ActionListener;
    32  import java.util.ArrayList;
    33  import java.util.List;
    34  import java.util.Vector;
    35  import javax.swing.JButton;
    36  
    37  import javax.swing.JPanel;
    38  import javax.swing.JScrollPane;
    39  import javax.swing.JTable;
    40  import javax.swing.table.DefaultTableModel;
    41  import javax.swing.table.TableCellRenderer;
    42  import net.miginfocom.swing.MigLayout;
    43  
    44  /**
    45   * Panel used for the custom nick colour settings component in the plugin's
    46   * config dialog.
    47   *
    48   * @author Chris
    49   */
    50  public class NickColourPanel extends JPanel implements ActionListener,
    51          PreferencesInterface {
    52  
    53      /**
    54       * A version number for this class. It should be changed whenever the class
    55       * structure is changed (or anything else that would prevent serialized
    56       * objects being unserialized with the new class).
    57       */
    58      private static final long serialVersionUID = 1;
    59      /** The table used for displaying the options. */
    60      private final JTable table;
    61      /** The plugin we're associated with. */
    62      private final transient NickColourPlugin plugin;
    63      /** The table headings. */
             /* 
    P/P       *  Method: com.dmdirc.addons.nickcolours.NickColourPanel__static_init
              * 
              *  Postconditions:
              *    headers == &new String[](NickColourPanel__static_init#1)
              *    new String[](NickColourPanel__static_init#1) num objects == 1
              *    headers.length == 4
              *    headers[0] == &"Network"
              *    headers[1] == &"Nickname"
              *    headers[2] == &"Text colour"
              *    headers[3] == &"Nicklist colour"
              */
    64      private static final String[] headers = {"Network", "Nickname",
    65          "Text colour", "Nicklist colour"};
    66  
    67      /**
    68       * Creates a new instance of NickColourPanel.
    69       * 
    70       * @param plugin The plugin that owns this panel
    71       */
    72      public NickColourPanel(final NickColourPlugin plugin) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.nickcolours.NickColourPanel(NickColourPlugin)
                  * 
                  *  Preconditions:
                  *    plugin != null
                  * 
                  *  Presumptions:
                  *    init'ed(com.dmdirc.addons.ui_swing.dialogs.prefs.SwingPreferencesDialog.CLIENT_HEIGHT)
                  * 
                  *  Postconditions:
                  *    this.plugin == plugin
                  *    this.plugin != null
                  *    this.table == &new NickColourPanel$1(NickColourPanel#1)
                  *    new ColourRenderer(NickColourPanel$1#1) num objects == 1
                  *    new NickColourPanel$1(NickColourPanel#1) num objects == 1
                  *    this.table.colourRenderer == &new ColourRenderer(NickColourPanel$1#1)
                  */
    73          super();
    74  
    75          this.plugin = plugin;
    76  
    77          final Object[][] data = plugin.getData();
    78  
                 /* 
    P/P           *  Method: void com.dmdirc.addons.nickcolours.NickColourPanel$1(NickColourPanel, TableModel)
                  * 
                  *  Postconditions:
                  *    this.colourRenderer == &new ColourRenderer(NickColourPanel$1#1)
                  *    new ColourRenderer(NickColourPanel$1#1) num objects == 1
                  */
    79          table = new JTable(new DefaultTableModel(data, headers)) {
    80  
    81              /**
    82               * A version number for this class. It should be changed whenever the class
    83               * structure is changed (or anything else that would prevent serialized
    84               * objects being unserialized with the new class).
    85               */
    86              private static final long serialVersionUID = 1;
    87              /** The colour renderer we're using for colour cells. */
    88              private final ColourRenderer colourRenderer = new ColourRenderer();
    89  
    90              /** {@inheritDoc} */
    91              @Override
    92              public TableCellRenderer getCellRenderer(final int row,
    93                      final int column) {
                         /* 
    P/P                   *  Method: TableCellRenderer getCellRenderer(int, int)
                          * 
                          *  Postconditions:
                          *    init'ed(return_value)
                          * 
                          *  Test Vectors:
                          *    column: {2}, {-231..1, 4..232-1}, {3}
                          */
    94                  if (column == 2 || column == 3) {
    95                      return colourRenderer;
    96                  } else {
    97                      return super.getCellRenderer(row, column);
    98                  }
    99              }
   100  
   101              /** {@inheritDoc} */
   102              @Override
   103              public boolean isCellEditable(final int row, final int column) {
                         /* 
    P/P                   *  Method: bool isCellEditable(int, int)
                          * 
                          *  Postconditions:
                          *    return_value == 0
                          */
   104                  return false;
   105              }
   106          };
   107  
   108          final JScrollPane scrollPane = new JScrollPane(table);
   109  
   110          table.setFillsViewportHeight(true);
   111          table.setDefaultRenderer(Color.class, new ColourRenderer());
   112  
   113          setLayout(new MigLayout("ins 0, fill, h " +
   114                  SwingPreferencesDialog.CLIENT_HEIGHT));
   115          add(scrollPane, "grow, wrap, spanx, hmax 100%");
   116  
   117          JButton button;
   118          button = new JButton("Add");
   119          button.addActionListener(this);
   120          add(button, "sg button, growx, pushx");
   121          button = new JButton("Edit");
   122          button.addActionListener(this);
   123          add(button, "sg button, growx, pushx");
   124          button = new JButton("Delete");
   125          button.addActionListener(this);
   126          add(button, "sg button, growx, pushx");
   127      }
   128  
   129      /** 
   130       * {@inheritDoc}
   131       * 
   132       * @param e Action event
   133       */
   134      @Override
   135      public void actionPerformed(final ActionEvent e) {
                 /* 
    P/P           *  Method: void actionPerformed(ActionEvent)
                  * 
                  *  Preconditions:
                  *    e != null
                  *    this.table != null
                  * 
                  *  Presumptions:
                  *    java.awt.event.ActionEvent:getActionCommand(...)@138 != null
                  *    java.awt.event.ActionEvent:getActionCommand(...)@140 != null
                  *    java.awt.event.ActionEvent:getActionCommand(...)@159 != null
                  *    javax.swing.JTable:getModel(...)@136 != null
                  * 
                  *  Test Vectors:
                  *    java.lang.String:equals(...)@138: {0}, {1}
                  *    java.lang.String:equals(...)@140: {0}, {1}
                  *    java.lang.String:equals(...)@159: {0}, {1}
                  *    javax.swing.JTable:getSelectedRow(...)@160: {-231..-1}, {0..232-1}
                  *    javax.swing.table.DefaultTableModel:getValueAt(...)@146: Inverse{null}, Addr_Set{null}
                  *    javax.swing.table.DefaultTableModel:getValueAt(...)@147: Inverse{null}, Addr_Set{null}
                  */
   136          final DefaultTableModel model = ((DefaultTableModel) table.getModel());
   137  
   138          if (e.getActionCommand().equals("Add")) {
   139              new NickColourInputDialog(this);
   140          } else if (e.getActionCommand().equals("Edit")) {
   141              final int row = table.getSelectedRow();
   142  
   143              final String network = (String) model.getValueAt(row, 0);
   144              final String nickname = (String) model.getValueAt(row, 1);
   145  
   146              String textcolour = (String) model.getValueAt(row, 2);
   147              String nickcolour = (String) model.getValueAt(row, 3);
   148  
   149              if (textcolour == null) {
   150                  textcolour = "";
   151              }
   152  
   153              if (nickcolour == null) {
   154                  nickcolour = "";
   155              }
   156  
   157              new NickColourInputDialog(this, row, nickname, network, textcolour,
   158                      nickcolour);
   159          } else if (e.getActionCommand().equals("Delete")) {
   160              final int row = table.getSelectedRow();
   161  
   162              if (row > -1) {
   163                  model.removeRow(row);
   164              }
   165          }
   166      }
   167  
   168      /**
   169       * Removes a row from the table.
   170       * 
   171       * @param row The row to be removed
   172       */
   173      void removeRow(final int row) {
                 /* 
    P/P           *  Method: void removeRow(int)
                  * 
                  *  Preconditions:
                  *    this.table != null
                  * 
                  *  Presumptions:
                  *    javax.swing.JTable:getModel(...)@174 != null
                  */
   174          ((DefaultTableModel) table.getModel()).removeRow(row);
   175      }
   176  
   177      /**
   178       * Adds a row to the table.
   179       * 
   180       * @param network The network setting
   181       * @param nickname The nickname setting
   182       * @param textcolour The textpane colour setting
   183       * @param nickcolour The nick list colour setting
   184       */
   185      void addRow(final String network, final String nickname,
   186              final String textcolour, final String nickcolour) {
                 /* 
    P/P           *  Method: void addRow(String, String, String, String)
                  * 
                  *  Preconditions:
                  *    this.table != null
                  * 
                  *  Presumptions:
                  *    javax.swing.JTable:getModel(...)@187 != null
                  */
   187          final DefaultTableModel model = ((DefaultTableModel) table.getModel());
   188          model.addRow(new Object[]{network, nickname, textcolour, nickcolour});
   189      }
   190  
   191      /**
   192       * Retrieves the current data in use by this panel.
   193       * 
   194       * @return This panel's current data.
   195       */
   196      List<Object[]> getData() {
                 /* 
    P/P           *  Method: List getData()
                  * 
                  *  Preconditions:
                  *    this.table != null
                  * 
                  *  Presumptions:
                  *    java.util.Iterator:next(...)@200 != null
                  *    javax.swing.JTable:getModel(...)@198 != null
                  *    javax.swing.table.DefaultTableModel:getDataVector(...)@200 != null
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new ArrayList(getData#1)
                  *    new ArrayList(getData#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@200: {0}, {1}
                  */
   197          final List<Object[]> res = new ArrayList<Object[]>();
   198          final DefaultTableModel model = ((DefaultTableModel) table.getModel());
   199  
   200          for (Object row : model.getDataVector()) {
   201              final Vector vrow = (Vector) row;
   202  
   203              res.add(new Object[]{vrow.elementAt(0), vrow.elementAt(1), vrow.
   204                          elementAt(2), vrow.elementAt(3)});
   205          }
   206  
   207          return res;
   208      }
   209  
   210      /** {@inheritDoc} */
   211      @Override
   212      public void save() {
   213          // Remove all old config entries
                 /* 
    P/P           *  Method: void save()
                  * 
                  *  Preconditions:
                  *    this.plugin != null
                  *    this.table != null
                  * 
                  *  Presumptions:
                  *    arr$[i$] != null
                  *    com.dmdirc.config.IdentityManager:getConfigIdentity(...)@215 != null
                  *    com.dmdirc.config.IdentityManager:getConfigIdentity(...)@221 != null
                  *    java.util.Iterator:next(...)@220 != null
                  *    parts.length >= 2
                  *    ...
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@220: {0}, {1}
                  */
   214          for (Object[] parts : plugin.getData()) {
   215              IdentityManager.getConfigIdentity().unsetOption(
   216                      plugin.getDomain(), "color:" + parts[0] + ":" + parts[1]);
   217          }
   218  
   219          // And write the new ones
   220          for (Object[] row : getData()) {
   221              IdentityManager.getConfigIdentity().setOption(plugin.getDomain(),
   222                      "color:" + row[0] + ":" + row[1], row[2] + ":" + row[3]);
   223          }
   224      }
   225  }








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