File Source: URLConfigPanel.java

         /* 
    P/P   *  Method: com.dmdirc.addons.ui_swing.dialogs.prefs.URLConfigPanel__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.prefs;
    24  
    25  import com.dmdirc.addons.ui_swing.components.renderers.URISchemeCellRenderer;
    26  import com.dmdirc.addons.ui_swing.components.renderers.URIHandlerCellRenderer;
    27  import com.dmdirc.config.IdentityManager;
    28  import com.dmdirc.config.prefs.PreferencesInterface;
    29  import com.dmdirc.config.prefs.validator.URLProtocolValidator;
    30  import com.dmdirc.addons.ui_swing.components.PackingTable;
    31  import com.dmdirc.addons.ui_swing.components.StandardInputDialog;
    32  import com.dmdirc.addons.ui_swing.components.URLProtocolPanel;
    33  
    34  import java.awt.Dialog.ModalityType;
    35  import java.awt.Window;
    36  import java.awt.event.ActionEvent;
    37  import java.awt.event.ActionListener;
    38  import java.net.URI;
    39  import java.net.URISyntaxException;
    40  import java.util.HashMap;
    41  import java.util.Map;
    42  import java.util.Map.Entry;
    43  import java.util.Set;
    44  
    45  import javax.swing.JButton;
    46  import javax.swing.JPanel;
    47  import javax.swing.JScrollPane;
    48  import javax.swing.ListSelectionModel;
    49  import javax.swing.event.ListSelectionEvent;
    50  import javax.swing.event.ListSelectionListener;
    51  import javax.swing.table.TableCellRenderer;
    52  
    53  import net.miginfocom.swing.MigLayout;
    54  
    55  /**
    56   * URL Config panel. List all known url protocols and allows them to be 
    57   * configured.
    58   */
         /* 
    P/P   *  Method: Map access$100(URLConfigPanel)
          * 
          *  Preconditions:
          *    x0 != null
          *    init'ed(x0.details)
          * 
          *  Postconditions:
          *    return_value == x0.details
          *    init'ed(return_value)
          */
    59  public class URLConfigPanel extends JPanel implements
    60          ListSelectionListener, ActionListener, PreferencesInterface {
    61  
    62      /**
    63       * A version number for this class. It should be changed whenever the class
    64       * structure is changed (or anything else that would prevent serialized
    65       * objects being unserialized with the new class).
    66       */
    67      private static final long serialVersionUID = 1;
    68      /** Protocol list. */
    69      private PackingTable table;
    70      /** Table mode. */
    71      private URLHandlerTableModel model;
    72      /** Table scrollpane. */
    73      private JScrollPane tableScrollPane;
    74      /** Protocol config panel. */
    75      private Map<URI, URLProtocolPanel> details;
    76      /** Empty info panel. */
    77      private URLProtocolPanel empty;
    78      /** Current component. */
    79      private URLProtocolPanel activeComponent;
    80      /** Add button. */
    81      private JButton add;
    82      /** Removed button. */
    83      private JButton remove;
    84      /** Selected row. */
    85      private int selectedRow;
    86      /** Parent window. */
    87      private Window parentWindow;
    88  
    89      /**
    90       * Instantiates a new URL config panel.
    91       * 
    92       * @param parentWindow Parent window
    93       */
    94      public URLConfigPanel(final Window parentWindow) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.dialogs.prefs.URLConfigPanel(Window)
                  * 
                  *  Preconditions:
                  *    init'ed(com/dmdirc/addons/ui_swing/dialogs/prefs/SwingPreferencesDialog.CLIENT_HEIGHT)
                  * 
                  *  Postconditions:
                  *    this.activeComponent == &amp;new URLProtocolPanel(initComponents#5)
                  *    this.empty == &amp;new URLProtocolPanel(initComponents#5)
                  *    this.add == &amp;new JButton(initComponents#6)
                  *    this.details == &amp;new HashMap(initComponents#4)
                  *    this.model == &amp;new URLHandlerTableModel(initComponents#2)
                  *    this.parentWindow == parentWindow
                  *    init'ed(this.parentWindow)
                  *    this.remove == &amp;new JButton(initComponents#7)
                  *    this.selectedRow == -1
                  *    this.table == &amp;new URLConfigPanel$1(initComponents#3)
                  *    ...
                  */
    95          super();
    96          
    97          this.parentWindow = parentWindow;
    98  
    99          initComponents();
   100          addListeners();
   101          layoutComponents();
   102          selectedRow = -1;
   103      }
   104  
   105      /**
   106       * Initialises the components.
   107       */
   108      private void initComponents() {
                 /* 
    P/P           *  Method: void initComponents()
                  * 
                  *  Presumptions:
                  *    com.dmdirc.addons.ui_swing.components.PackingTable:getRowSorter(...)@136 != null
                  *    com.dmdirc.config.ConfigManager:getOptions(...)@146 != null
                  *    com.dmdirc.config.IdentityManager:getGlobalConfig(...)@146 != null
                  *    java.util.Map:keySet(...)@146 != null
                  * 
                  *  Postconditions:
                  *    this.activeComponent == &amp;new URLProtocolPanel(initComponents#5)
                  *    this.empty == &amp;new URLProtocolPanel(initComponents#5)
                  *    this.add == &amp;new JButton(initComponents#6)
                  *    this.details == &amp;new HashMap(initComponents#4)
                  *    this.model == &amp;new URLHandlerTableModel(initComponents#2)
                  *    this.remove == &amp;new JButton(initComponents#7)
                  *    this.table == &amp;new URLConfigPanel$1(initComponents#3)
                  *    this.tableScrollPane == &amp;new JScrollPane(initComponents#1)
                  *    new ArrayList(URLHandlerTableModel#1) num objects == 1
                  *    new ArrayList(URLHandlerTableModel#2) num objects == 1
                  *    ...
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@149: {0}, {1}
                  */
   109          tableScrollPane = new JScrollPane();
   110          model = new URLHandlerTableModel();
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.dialogs.prefs.URLConfigPanel$1(URLConfigPanel, TableModel, bool, JScrollPane)
                  */
   111          table = new PackingTable(model, false, tableScrollPane) {
   112  
   113              private static final long serialVersionUID = 1;
   114  
   115              /** {@inheritDoc} */
   116              @Override
   117              public TableCellRenderer getCellRenderer(final int row,
   118                      final int column) {
                         /* 
    P/P                   *  Method: TableCellRenderer getCellRenderer(int, int)
                          * 
                          *  Postconditions:
                          *    init'ed(return_value)
                          *    new URIHandlerCellRenderer(getCellRenderer#2) num objects <= 1
                          *    new URISchemeCellRenderer(getCellRenderer#1) num objects <= 1
                          * 
                          *  Test Vectors:
                          *    column: {0}, {1}, {-231..-1, 2..232-1}
                          */
   119                  switch (column) {
   120                      case 0:
   121                          return new URISchemeCellRenderer();
   122                      case 1:
   123                          return new URIHandlerCellRenderer();
   124                      default:
   125                          return super.getCellRenderer(row, column);
   126                  }
   127              }
   128          };
   129          table.setAutoCreateRowSorter(true);
   130          table.setAutoCreateColumnsFromModel(true);
   131          table.setColumnSelectionAllowed(false);
   132          table.setCellSelectionEnabled(false);
   133          table.setFillsViewportHeight(false);
   134          table.setRowSelectionAllowed(true);
   135          table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
   136          table.getRowSorter().toggleSortOrder(0);
   137          details = new HashMap<URI, URLProtocolPanel>();
   138          empty = new URLProtocolPanel(null, true);
   139          activeComponent = empty;
   140          add = new JButton("Add");
   141          remove = new JButton("Remove");
   142          remove.setEnabled(false);
   143  
   144          tableScrollPane.setViewportView(table);
   145  
   146          final Set<String> options = IdentityManager.getGlobalConfig().
   147                  getOptions("protocol").keySet();
   148  
   149          for (String option : options) {
   150              try {
   151                  final URI uri = new URI(option + "://example.test.com");
   152                  model.addURI(uri);
   153                  details.put(uri, new URLProtocolPanel(uri, true));
   154              } catch (URISyntaxException ex) {
   155                  //Ignore wont happen
   156              }
   157          }
   158      }
   159  
   160      /**
   161       * Adds listeners.
   162       */
   163      private void addListeners() {
                 /* 
    P/P           *  Method: void addListeners()
                  * 
                  *  Preconditions:
                  *    this.add != null
                  *    this.remove != null
                  *    this.table != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.addons.ui_swing.components.PackingTable:getSelectionModel(...)@164 != null
                  */
   164          table.getSelectionModel().addListSelectionListener(this);
   165          add.addActionListener(this);
   166          remove.addActionListener(this);
   167      }
   168  
   169      /**
   170       * Lays out the components.
   171       */
   172      private void layoutComponents() {
                 /* 
    P/P           *  Method: void layoutComponents()
                  * 
                  *  Preconditions:
                  *    init'ed(com/dmdirc/addons/ui_swing/dialogs/prefs/SwingPreferencesDialog.CLIENT_HEIGHT)
                  *    init'ed(this.activeComponent)
                  *    init'ed(this.add)
                  *    init'ed(this.remove)
                  *    init'ed(this.tableScrollPane)
                  */
   173          removeAll();
   174          setLayout(new MigLayout("ins 0, wrap 1, hmax " +
   175                  SwingPreferencesDialog.CLIENT_HEIGHT));
   176  
   177          add(tableScrollPane, "growx, pushx");
   178          add(add, "split 2, growx, pushx");
   179          add(remove, "growx, pushx");
   180          add(activeComponent, "growx, pushx");
   181      }
   182  
   183      /** {@inheritDoc} */
   184      @Override
   185      public void save() {
                 /* 
    P/P           *  Method: void save()
                  * 
                  *  Preconditions:
                  *    (soft) init'ed(com/dmdirc/addons/ui_swing/dialogs/prefs/SwingPreferencesDialog.CLIENT_HEIGHT)
                  *    (soft) this.selectedRow >= -1
                  *    (soft) this.add != null
                  *    (soft) this.details != null
                  *    (soft) init'ed(this.empty)
                  *    (soft) this.model != null
                  *    (soft) this.model.handlers != null
                  *    (soft) this.model.uris != null
                  *    (soft) this.remove != null
                  *    (soft) this.table != null
                  *    ...
                  * 
                  *  Presumptions:
                  *    com.dmdirc.config.ConfigManager:getOptions(...)@188 != null
                  *    com.dmdirc.config.IdentityManager:getGlobalConfig(...)@188 != null
                  *    java.util.Iterator:next(...)@204 != null
                  *    java.util.Map:entrySet(...)@204 != null
                  *    java.util.Map:get(...)@198 != null
                  *    ...
                  * 
                  *  Postconditions:
                  *    possibly_updated(this.activeComponent)
                  *    init'ed(this.selectedRow)
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@190: {0}, {1}
                  *    java.util.Iterator:hasNext(...)@204: {0}, {1}
                  *    java.util.Map:containsKey(...)@197: {0}, {1}
                  */
   186          valueChanged(null);
   187          final Map<URI, String> handlers = model.getURLHandlers();
   188          final Set<String> protocols = IdentityManager.getGlobalConfig().
   189                  getOptions("protocol").keySet();
   190          for (String protocol : protocols) {
   191              URI uri;
   192              try {
   193                  uri = new URI(protocol + "://example.test.com");
   194              } catch (URISyntaxException ex) {
   195                  uri = null;
   196              }
   197              if (uri != null && handlers.containsKey(uri)) {
   198                  saveHandler(protocol, handlers.get(uri));
   199              } else {
   200                  saveHandler(protocol, "");
   201              }
   202              handlers.remove(uri);
   203          }
   204          for (Entry<URI, String> entry : handlers.entrySet()) {
   205              saveHandler(entry.getKey().getScheme(), entry.getValue());
   206          }
   207      }
   208  
   209      /**
   210       * Saves or updates a handler to the config.
   211       * 
   212       * @param protocol Protocol for the handler
   213       * @param handler Handler for the protocol
   214       */
   215      private void saveHandler(final String protocol, final String handler) {
                 /* 
    P/P           *  Method: void saveHandler(String, String)
                  * 
                  *  Preconditions:
                  *    handler != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.config.IdentityManager:getConfigIdentity(...)@217 != null
                  *    com.dmdirc.config.IdentityManager:getConfigIdentity(...)@220 != null
                  * 
                  *  Test Vectors:
                  *    java.lang.String:isEmpty(...)@216: {0}, {1}
                  */
   216          if (handler.isEmpty()) {
   217              IdentityManager.getConfigIdentity().unsetOption("protocol",
   218                      protocol);
   219          } else {
   220              IdentityManager.getConfigIdentity().setOption("protocol",
   221                      protocol, handler);
   222  
   223          }
   224  
   225      }
   226  
   227      /** {@inheritDoc} */
   228      @Override
   229      public void valueChanged(final ListSelectionEvent e) {
                 /* 
    P/P           *  Method: void valueChanged(ListSelectionEvent)
                  * 
                  *  Preconditions:
                  *    (soft) init'ed(com/dmdirc/addons/ui_swing/dialogs/prefs/SwingPreferencesDialog.CLIENT_HEIGHT)
                  *    (soft) this.selectedRow >= -1
                  *    (soft) this.add != null
                  *    (soft) this.details != null
                  *    (soft) init'ed(this.empty)
                  *    (soft) this.model != null
                  *    (soft) this.model.handlers != null
                  *    (soft) this.model.uris != null
                  *    (soft) this.remove != null
                  *    (soft) this.table != null
                  *    ...
                  * 
                  *  Presumptions:
                  *    com.dmdirc.addons.ui_swing.components.PackingTable:getRowSorter(...)@244 != null
                  *    com.dmdirc.addons.ui_swing.components.PackingTable:getRowSorter(...)@250 != null
                  *    java.util.Map:get(...)@233 != null
                  *    javax.swing.RowSorter:convertRowIndexToModel(...)@244 in {0..232-2}
                  * 
                  *  Postconditions:
                  *    possibly_updated(this.activeComponent)
                  *    init'ed(this.selectedRow)
                  * 
                  *  Test Vectors:
                  *    e: Addr_Set{null}, Inverse{null}
                  *    this.selectedRow: {-1}, {0..232-2}
                  *    com.dmdirc.addons.ui_swing.components.PackingTable:getSelectedRow(...)@237: {-231..-2, 0..232-1}, {-1}
                  *    javax.swing.event.ListSelectionEvent:getValueIsAdjusting(...)@230: {1}, {0}
                  */
   230          if (e == null || !e.getValueIsAdjusting()) {
   231              setVisible(false);
   232              if (selectedRow != -1 && selectedRow < model.getRowCount()) {
   233                  final URLProtocolPanel panel =
   234                          details.get(model.getValueAt(selectedRow, 0));
   235                  model.setValueAt(panel.getSelection(), selectedRow, 1);
   236              }
   237              if (table.getSelectedRow() == -1) {
   238                  activeComponent = empty;
   239                  layoutComponents();
   240                  add.setEnabled(false);
   241                  remove.setEnabled(false);
   242                  selectedRow = -1;
   243              } else {
   244                  activeComponent =
   245                          details.get(model.getValueAt(table.getRowSorter().
   246                          convertRowIndexToModel(table.getSelectedRow()), 0));
   247                  layoutComponents();
   248                  add.setEnabled(true);
   249                  remove.setEnabled(true);
   250                  selectedRow = table.getRowSorter().convertRowIndexToModel(table.
   251                          getSelectedRow());
   252              }
   253              setVisible(true);
   254          }
   255      }
   256  
   257      /** 
   258       * {@inheritDoc}
   259       * 
   260       * @param e Action event
   261       */
   262      @Override
   263      public void actionPerformed(final ActionEvent e) {
                 /* 
    P/P           *  Method: void actionPerformed(ActionEvent)
                  * 
                  *  Preconditions:
                  *    e != null
                  *    init'ed(this.add)
                  *    (soft) this.model != null
                  *    (soft) this.model.handlers != null
                  *    (soft) this.model.uris != null
                  *    (soft) init'ed(this.parentWindow)
                  *    (soft) init'ed(this.remove)
                  *    (soft) this.table != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.addons.ui_swing.components.PackingTable:getRowSorter(...)@299 != null
                  *    init'ed(java.awt.Dialog$ModalityType.MODELESS)
                  */
   264          if (e.getSource() == add) {
   265              new StandardInputDialog(parentWindow, ModalityType.MODELESS, 
   266                      "DMDIRC: New URL handler", 
   267                      "Please enter the name of the new protocol.",
                             /* 
    P/P                       *  Method: void com.dmdirc.addons.ui_swing.dialogs.prefs.URLConfigPanel$2(URLConfigPanel, Window, Dialog$ModalityType, String, String, Validator)
                              */
   268                      new URLProtocolValidator()) {
   269  
   270                  /**
   271                   * A version number for this class. It should be changed whenever the
   272                   * class structure is changed (or anything else that would prevent
   273                   * serialized objects being unserialized with the new class).
   274                   */
   275                  private static final long serialVersionUID = 1;
   276  
   277                  /** {@inheritDoc} */
   278                  @Override
   279                  public boolean save() {
   280                      try {
                                 /* 
    P/P                           *  Method: bool save()
                                  * 
                                  *  Preconditions:
                                  *    (soft) this.details != null
                                  *    (soft) this.model != null
                                  *    (soft) this.model.handlers != null
                                  *    (soft) this.model.uris != null
                                  * 
                                  *  Postconditions:
                                  *    init'ed(return_value)
                                  */
   281                          final URI uri = new URI(getText() +
   282                                  "://example.test.com");
   283                          model.addURI(uri);
   284                          details.put(uri, new URLProtocolPanel(uri, true));
   285                          return true;
   286                      } catch (URISyntaxException ex) {
   287                          return false;
   288                      }
   289                  }
   290  
   291                  /** {@inheritDoc} */
   292                  @Override
   293                  public void cancelled() {
   294                      //Ignore
                         /* 
    P/P                   *  Method: void cancelled()
                          */
   295                  }
   296              }.display();
   297  
   298          } else if (e.getSource() == remove) {
   299              model.removeURI(table.getRowSorter().convertRowIndexToModel(table.
   300                      getSelectedRow()));
   301          }
   302      }
   303  }








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