File Source: ConfigPanel.java

         /* 
    P/P   *  Method: com.dmdirc.addons.nowplaying.ConfigPanel$DummyMediaSource__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.nowplaying;
    24  
    25  import com.dmdirc.config.IdentityManager;
    26  import com.dmdirc.config.prefs.PreferencesInterface;
    27  import com.dmdirc.addons.ui_swing.components.text.TextLabel;
    28  import com.dmdirc.addons.ui_swing.components.reorderablelist.ReorderableJList;
    29  
    30  import java.awt.event.KeyEvent;
    31  import java.awt.event.KeyListener;
    32  import java.util.Arrays;
    33  import java.util.Enumeration;
    34  import java.util.LinkedList;
    35  import java.util.List;
    36  import java.util.Timer;
    37  import java.util.TimerTask;
    38  
    39  import javax.swing.BorderFactory;
    40  import javax.swing.JLabel;
    41  import javax.swing.JPanel;
    42  import javax.swing.JScrollPane;
    43  import javax.swing.JTextField;
    44  import javax.swing.SwingUtilities;
    45  
    46  import net.miginfocom.swing.MigLayout;
    47  
    48  /**
    49   * Now playing plugin config panel.
    50   */
         /* 
    P/P   *  Method: void access$200(ConfigPanel)
          * 
          *  Preconditions:
          *    x0 != null
          *    x0.plugin != null
          *    x0.plugin.managers != null
          *    init'ed(x0.plugin.order)
          *    x0.preview != null
          *    x0.textfield != null
          *    x0.updateTimer != null
          */
    51  public class ConfigPanel extends JPanel implements PreferencesInterface, KeyListener {
    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  
    60      /** Media source order list. */
    61      private ReorderableJList list;
    62  
    63      /** Media sources. */
    64      private final List<String> sources;
    65  
    66      /** The plugin that owns this panel. */
    67      private final NowPlayingPlugin plugin;
    68  
    69      /** Text field for our setting. */
    70      private JTextField textfield;
    71  
    72      /** Panel that the preview is in. */
    73      private JPanel previewPanel;
    74  
    75      /** Label for previews. */
    76      private TextLabel preview;
    77      
    78      /** Update timer. */
    79      private Timer updateTimer;
    80  
    81      /**
    82       * Creates a new instance of ConfigPanel.
    83       *
    84       * @param plugin The plugin that owns this panel
    85       * @param sources A list of sources to be used in the panel
    86       */
    87      public ConfigPanel(final NowPlayingPlugin plugin, final List<String> sources) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.nowplaying.ConfigPanel(NowPlayingPlugin, List)
                  * 
                  *  Preconditions:
                  *    plugin != null
                  * 
                  *  Postconditions:
                  *    this.list == &amp;new ReorderableJList(initComponents#1)
                  *    this.plugin == plugin
                  *    this.plugin != null
                  *    this.preview == &amp;new TextLabel(initComponents#3)
                  *    this.previewPanel == &amp;new JPanel(initComponents#9)
                  *    this.sources in Addr_Set{&amp;new LinkedList(ConfigPanel#2),&amp;new LinkedList(ConfigPanel#1)}
                  *    this.textfield == &amp;new JTextField(initComponents#2)
                  *    this.updateTimer == &amp;new Timer(schedulePreviewUpdate#1)
                  *    new JPanel(initComponents#5) num objects == 1
                  *    new JPanel(initComponents#9) num objects == 1
                  *    ...
                  * 
                  *  Test Vectors:
                  *    sources: Inverse{null}, Addr_Set{null}
                  */
    88          super();
    89  
    90          if (sources == null) {
    91              this.sources = new LinkedList<String>();
    92          } else {
    93              this.sources = new LinkedList<String>(sources);
    94          }
    95          this.plugin = plugin;
    96  
    97          initComponents();
    98      }
    99  
   100      /**
   101       * Initialises the components.
   102       */
   103      private void initComponents() {
                 /* 
    P/P           *  Method: void initComponents()
                  * 
                  *  Preconditions:
                  *    init'ed(this.updateTimer)
                  *    this.plugin != null
                  *    this.sources != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.addons.ui_swing.components.reorderablelist.ReorderableJList:getModel(...)@107 != null
                  *    com.dmdirc.config.IdentityManager:getGlobalConfig(...)@110 != null
                  * 
                  *  Postconditions:
                  *    this.list == &amp;new ReorderableJList(initComponents#1)
                  *    this.preview == &amp;new TextLabel(initComponents#3)
                  *    this.previewPanel == &amp;new JPanel(initComponents#9)
                  *    this.textfield == &amp;new JTextField(initComponents#2)
                  *    this.updateTimer == &amp;new Timer(schedulePreviewUpdate#1)
                  *    new JPanel(initComponents#5) num objects == 1
                  *    new JPanel(initComponents#9) num objects == 1
                  *    new JTextField(initComponents#2) num objects == 1
                  *    new ReorderableJList(initComponents#1) num objects == 1
                  *    new TextLabel(initComponents#3) num objects == 1
                  *    ...
                  * 
                  *  Test Vectors:
                  *    java.util.Iterator:hasNext(...)@106: {0}, {1}
                  */
   104          list = new ReorderableJList();
   105  
   106          for (String source : sources) {
   107              list.getModel().addElement(source);
   108          }
   109  
   110          textfield = new JTextField(IdentityManager.getGlobalConfig()
   111                  .getOption(plugin.getDomain(), "format"));
   112          textfield.addKeyListener(this);
   113          preview = new TextLabel("Preview:\n");
   114  
   115          setLayout(new MigLayout("fillx, ins 0"));
   116  
   117          JPanel panel = new JPanel();
   118  
   119          panel.setBorder(BorderFactory.createTitledBorder("Source order"));
   120          panel.setLayout(new MigLayout("fillx, ins 5"));
   121  
   122          panel.add(new JLabel("Drag and drop items to reorder"), "wrap");
   123          panel.add(new JScrollPane(list), "growx");
   124  
   125          add(panel, "growx, wrap");
   126  
   127          panel = new JPanel();
   128  
   129          panel.setBorder(BorderFactory.createTitledBorder("Output format"));
   130          panel.setLayout(new MigLayout("fillx, ins 5"));
   131  
   132          panel.add(textfield, "span, growx, wrap");
   133          panel.add(preview, "span, grow, wrap, gaptop 10");
   134          add(panel, "growx, wrap");
   135  
   136          previewPanel = panel;
   137  
   138          add(new NowPlayingSubsitutionPanel(Arrays.asList(new String[]{"app", 
   139          "title", "artist", "album", "bitrate", "format", "length", "time", 
   140          "state"})), "growx");
   141          schedulePreviewUpdate();
   142      }
   143  
   144      /**
   145       * Updates the preview text.
   146       */
   147      private void updatePreview() {
                 /* 
    P/P           *  Method: void updatePreview()
                  * 
                  *  Preconditions:
                  *    this.plugin != null
                  *    this.plugin.managers != null
                  *    init'ed(this.plugin.order)
                  *    this.preview != null
                  *    this.textfield != null
                  *    this.updateTimer != null
                  * 
                  *  Presumptions:
                  *    javax.swing.JTextField:getText(...)@156 != null
                  */
   148          updateTimer.cancel();
   149          
   150          MediaSource source = plugin.getBestSource();
   151  
   152          if (source == null) {
   153              source = new DummyMediaSource();
   154          }
   155  
   156          preview.setText("Preview:\n" + plugin.doSubstitution(textfield.getText(),
   157                  source));
   158          preview.repaint();
   159  
                 /* 
    P/P           *  Method: void com.dmdirc.addons.nowplaying.ConfigPanel$1(ConfigPanel)
                  */
   160          SwingUtilities.invokeLater(new Runnable() {
   161              @Override
   162              public void run() {
                         /* 
    P/P                   *  Method: void run()
                          * 
                          *  Preconditions:
                          *    this.previewPanel != null
                          */
   163                  previewPanel.revalidate();
   164                  revalidate();
   165              }
   166          });
   167      }
   168  
   169      /**
   170       * Retrieves the (new) source order from this config panel.
   171       *
   172       * @return An ordered list of sources
   173       */
   174      public List<String> getSources() {
                 /* 
    P/P           *  Method: List getSources()
                  * 
                  *  Preconditions:
                  *    this.list != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.addons.ui_swing.components.reorderablelist.ReorderableJList:getModel(...)@177 != null
                  *    javax.swing.DefaultListModel:elements(...)@177 != null
                  * 
                  *  Postconditions:
                  *    return_value == &amp;new LinkedList(getSources#1)
                  *    new LinkedList(getSources#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    java.util.Enumeration:hasMoreElements(...)@179: {0}, {1}
                  */
   175          final List<String> newSources = new LinkedList<String>();
   176  
   177          final Enumeration<?> values = list.getModel().elements();
   178  
   179          while (values.hasMoreElements()) {
   180              newSources.add((String) values.nextElement());
   181          }
   182  
   183          return newSources;
   184      }
   185  
   186      /** {@inheritDoc} */
   187      @Override
   188      public void save() {
                 /* 
    P/P           *  Method: void save()
                  * 
                  *  Preconditions:
                  *    this.list != null
                  *    this.plugin != null
                  *    this.textfield != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.config.IdentityManager:getConfigIdentity(...)@190 != null
                  * 
                  *  Postconditions:
                  *    this.plugin.order == &amp;new LinkedList(getSources#1)
                  *    new LinkedList(getSources#1) num objects == 1
                  */
   189          plugin.saveSettings(getSources());
   190          IdentityManager.getConfigIdentity().setOption(plugin.getDomain(),
   191                  "format", textfield.getText());
   192      }
   193  
   194      /** {@inheritDoc} */
   195      @Override
   196      public void keyTyped(final KeyEvent e) {
   197          // Do nothing
             /* 
    P/P       *  Method: void keyTyped(KeyEvent)
              */
   198      }
   199  
   200      /** {@inheritDoc} */
   201      @Override
   202      public void keyPressed(final KeyEvent e) {
   203          // Do nothing
             /* 
    P/P       *  Method: void keyPressed(KeyEvent)
              */
   204      }
   205  
   206      /** {@inheritDoc} */
   207      @Override
   208      public void keyReleased(final KeyEvent e) {
                 /* 
    P/P           *  Method: void keyReleased(KeyEvent)
                  * 
                  *  Preconditions:
                  *    init'ed(this.updateTimer)
                  * 
                  *  Postconditions:
                  *    this.updateTimer == &amp;new Timer(schedulePreviewUpdate#1)
                  *    new Timer(schedulePreviewUpdate#1) num objects == 1
                  */
   209          schedulePreviewUpdate();
   210      }
   211      
   212      /**
   213       * Schedules an update to the preview text.
   214       */
   215      private void schedulePreviewUpdate() {
                 /* 
    P/P           *  Method: void schedulePreviewUpdate()
                  * 
                  *  Preconditions:
                  *    init'ed(this.updateTimer)
                  * 
                  *  Postconditions:
                  *    this.updateTimer == &amp;new Timer(schedulePreviewUpdate#1)
                  *    new Timer(schedulePreviewUpdate#1) num objects == 1
                  * 
                  *  Test Vectors:
                  *    this.updateTimer: Addr_Set{null}, Inverse{null}
                  */
   216          if (updateTimer != null) {
   217              updateTimer.cancel();
   218          }
   219          
   220          updateTimer = new Timer("Nowplaying config timer");
                 /* 
    P/P           *  Method: void com.dmdirc.addons.nowplaying.ConfigPanel$2(ConfigPanel)
                  */
   221          updateTimer.schedule(new TimerTask() {
   222              /** {@inheritDoc} */
   223              @Override
   224              public void run() {
                         /* 
    P/P                   *  Method: void run()
                          * 
                          *  Preconditions:
                          *    this.plugin != null
                          *    this.plugin.managers != null
                          *    init'ed(this.plugin.order)
                          *    this.preview != null
                          *    this.textfield != null
                          *    this.updateTimer != null
                          */
   225                  updatePreview();
   226              }
   227          }, 500);
   228      }
   229  
   230      /**
   231       * A dummy media source for use in previews.
   232       */
             /* 
    P/P       *  Method: void com.dmdirc.addons.nowplaying.ConfigPanel$DummyMediaSource(ConfigPanel, ConfigPanel$1)
              */
   233      private class DummyMediaSource implements MediaSource {
   234  
   235          /** {@inheritDoc} */
   236          @Override
   237          public MediaSourceState getState() {
                     /* 
    P/P               *  Method: MediaSourceState getState()
                      * 
                      *  Postconditions:
                      *    return_value == &amp;com.dmdirc.addons.nowplaying.MediaSourceState__static_init.new MediaSourceState(MediaSourceState__static_init#4)
                      */
   238              return MediaSourceState.PLAYING;
   239          }
   240  
   241          /** {@inheritDoc} */
   242          @Override
   243          public String getAppName() {
                     /* 
    P/P               *  Method: String getAppName()
                      * 
                      *  Postconditions:
                      *    return_value == &amp;"MyProgram"
                      */
   244              return "MyProgram";
   245          }
   246  
   247          /** {@inheritDoc} */
   248          @Override
   249          public String getArtist() {
                     /* 
    P/P               *  Method: String getArtist()
                      * 
                      *  Postconditions:
                      *    return_value == &amp;"The Artist"
                      */
   250              return "The Artist";
   251          }
   252  
   253          /** {@inheritDoc} */
   254          @Override
   255          public String getTitle() {
                     /* 
    P/P               *  Method: String getTitle()
                      * 
                      *  Postconditions:
                      *    return_value == &amp;"Song about nothing"
                      */
   256              return "Song about nothing";
   257          }
   258  
   259          /** {@inheritDoc} */
   260          @Override
   261          public String getAlbum() {
                     /* 
    P/P               *  Method: String getAlbum()
                      * 
                      *  Postconditions:
                      *    return_value == &amp;"Album 45"
                      */
   262              return "Album 45";
   263          }
   264  
   265          /** {@inheritDoc} */
   266          @Override
   267          public String getLength() {
                     /* 
    P/P               *  Method: String getLength()
                      * 
                      *  Postconditions:
                      *    return_value == &amp;"3:45"
                      */
   268              return "3:45";
   269          }
   270  
   271          /** {@inheritDoc} */
   272          @Override
   273          public String getTime() {
                     /* 
    P/P               *  Method: String getTime()
                      * 
                      *  Postconditions:
                      *    return_value == &amp;"1:20"
                      */
   274              return "1:20";
   275          }
   276  
   277          /** {@inheritDoc} */
   278          @Override
   279          public String getFormat() {
                     /* 
    P/P               *  Method: String getFormat()
                      * 
                      *  Postconditions:
                      *    return_value == &amp;"flac"
                      */
   280              return "flac";
   281          }
   282  
   283          /** {@inheritDoc} */
   284          @Override
   285          public String getBitrate() {
                     /* 
    P/P               *  Method: String getBitrate()
                      * 
                      *  Postconditions:
                      *    return_value == &amp;"128"
                      */
   286              return "128";
   287          }
   288  
   289      }
   290  
   291  }








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