File Source: SubstitutionLabel.java

         /* 
    P/P   *  Method: com.dmdirc.addons.ui_swing.components.substitutions.SubstitutionLabel__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.substitutions;
    24  
    25  import com.dmdirc.addons.ui_swing.dialogs.actioneditor.StringTransferable;
    26  import java.awt.Color;
    27  import java.awt.Component;
    28  import java.awt.Cursor;
    29  import java.awt.Insets;
    30  import java.awt.dnd.DnDConstants;
    31  import java.awt.dnd.DragGestureEvent;
    32  import java.awt.dnd.DragGestureListener;
    33  import java.awt.dnd.DragSource;
    34  import java.awt.event.ActionEvent;
    35  import java.awt.event.ActionListener;
    36  import java.awt.event.FocusEvent;
    37  import java.awt.event.FocusListener;
    38  import java.awt.event.MouseEvent;
    39  import java.awt.event.MouseListener;
    40  
    41  import javax.swing.BorderFactory;
    42  import javax.swing.JButton;
    43  import javax.swing.text.JTextComponent;
    44  
    45  /**
    46   * Action substitution label.
    47   */
    48  public class SubstitutionLabel extends JButton implements MouseListener,
    49          DragGestureListener, ActionListener, FocusListener {
    50  
    51      /**
    52       * A version number for this class. It should be changed whenever the class
    53       * structure is changed (or anything else that would prevent serialized
    54       * objects being unserialized with the new class).
    55       */
    56      private static final long serialVersionUID = 1;
    57      /** Drag source. */
    58      private DragSource dragSource;
    59      /** Substitution. */
    60      private Substitution substition;
    61      /** Previously selected component. */
    62      private Component previousComponent;
    63  
    64      /** 
    65       * Instantiates the panel.
    66       * 
    67       * @param substition Action substitition
    68       */
    69      public SubstitutionLabel(final Substitution substition) {
                 /* 
    P/P           *  Method: void com.dmdirc.addons.ui_swing.components.substitutions.SubstitutionLabel(Substitution)
                  * 
                  *  Preconditions:
                  *    substition != null
                  * 
                  *  Presumptions:
                  *    java.awt.dnd.DragSource:getDefaultDragSource(...)@81 != null
                  * 
                  *  Postconditions:
                  *    this.dragSource != null
                  *    this.substition == substition
                  *    this.substition != null
                  */
    70          super();
    71  
    72          this.substition = substition;
    73  
    74          initComponents();
    75          addListeners();
    76          layoutComponents();
    77      }
    78  
    79      /** Initialises the components. */
    80      private void initComponents() {
                 /* 
    P/P           *  Method: void initComponents()
                  * 
                  *  Preconditions:
                  *    this.substition != null
                  * 
                  *  Presumptions:
                  *    com.dmdirc.addons.ui_swing.components.substitutions.SubstitutionLabel:getFont(...)@86 != null
                  *    init'ed(java.awt.Color.GRAY)
                  *    java.awt.dnd.DragSource:getDefaultDragSource(...)@81 != null
                  * 
                  *  Postconditions:
                  *    this.dragSource != null
                  */
    81          dragSource = DragSource.getDefaultDragSource();
    82          dragSource.createDefaultDragGestureRecognizer(this,
    83                  DnDConstants.ACTION_COPY, this);
    84  
    85          setText(substition.getName());
    86          setFont(getFont().deriveFont(getFont().getSize() - 2f));
    87          
    88          setBorder(
    89                  BorderFactory.createCompoundBorder(
    90                  BorderFactory.createLineBorder(Color.GRAY),
    91                  BorderFactory.createEmptyBorder(1, 1, 1, 1)
    92                  ));
    93          setContentAreaFilled(false);
    94          setMargin(new Insets(0, 0, 0, 0));
    95      }
    96  
    97      /** Adds the listeners. */
    98      private void addListeners() {
                 /* 
    P/P           *  Method: void addListeners()
                  */
    99          addMouseListener(this);
   100          addFocusListener(this);
   101          addActionListener(this);
   102      }
   103  
   104      /** Lays out the components. */
   105      private void layoutComponents() {
             /* 
    P/P       *  Method: void layoutComponents()
              */
   106      }
   107  
   108      /** 
   109       * {@inheritDoc}
   110       * 
   111       * @param e Mouse event
   112       */
   113      @Override
   114      public void mouseClicked(final MouseEvent e) {
   115      //Ignore
             /* 
    P/P       *  Method: void mouseClicked(MouseEvent)
              */
   116      }
   117  
   118      /** 
   119       * {@inheritDoc}
   120       * 
   121       * @param e Mouse event
   122       */
   123      @Override
   124      public void mousePressed(final MouseEvent e) {
   125      //Ignore
             /* 
    P/P       *  Method: void mousePressed(MouseEvent)
              */
   126      }
   127  
   128      /** 
   129       * {@inheritDoc}
   130       * 
   131       * @param e Mouse event
   132       */
   133      @Override
   134      public void mouseReleased(final MouseEvent e) {
   135      //Ignore
             /* 
    P/P       *  Method: void mouseReleased(MouseEvent)
              */
   136      }
   137  
   138      /** 
   139       * {@inheritDoc}
   140       * 
   141       * @param e Mouse event
   142       */
   143      @Override
   144      public void mouseEntered(final MouseEvent e) {
                 /* 
    P/P           *  Method: void mouseEntered(MouseEvent)
                  */
   145          setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
   146      }
   147  
   148      /** 
   149       * {@inheritDoc}
   150       * 
   151       * @param e Mouse event
   152       */
   153      @Override
   154      public void mouseExited(final MouseEvent e) {
   155      //Ignore
             /* 
    P/P       *  Method: void mouseExited(MouseEvent)
              */
   156      }
   157  
   158      /** {@inheritDoc} */
   159      @Override
   160      public void dragGestureRecognized(final DragGestureEvent dge) {
                 /* 
    P/P           *  Method: void dragGestureRecognized(DragGestureEvent)
                  * 
                  *  Preconditions:
                  *    this.dragSource != null
                  *    this.substition != null
                  */
   161          dragSource.startDrag(dge, Cursor.getPredefinedCursor(Cursor.HAND_CURSOR),
   162                  new StringTransferable(substition.toString()), null);
   163      }
   164  
   165      /** 
   166       * {@inheritDoc}
   167       * 
   168       * @param e Action event
   169       */
   170      @Override
   171      public void actionPerformed(ActionEvent e) {
                 /* 
    P/P           *  Method: void actionPerformed(ActionEvent)
                  * 
                  *  Preconditions:
                  *    init'ed(this.previousComponent)
                  *    (soft) this.substition != null
                  */
   172          if (previousComponent instanceof JTextComponent) {
   173              ((JTextComponent) previousComponent).replaceSelection(substition.toString());
   174          }
   175      }
   176  
   177      /** 
   178       * {@inheritDoc}
   179       * 
   180       * @param e Focus event
   181       */
   182      @Override
   183      public void focusGained(FocusEvent e) {
                 /* 
    P/P           *  Method: void focusGained(FocusEvent)
                  * 
                  *  Preconditions:
                  *    e != null
                  * 
                  *  Postconditions:
                  *    init'ed(this.previousComponent)
                  */
   184          previousComponent = e.getOppositeComponent();
   185      }
   186  
   187      /** 
   188       * {@inheritDoc}
   189       * 
   190       * @param e Focus event
   191       */
   192      @Override
   193      public void focusLost(FocusEvent e) {
   194          //Ignore
             /* 
    P/P       *  Method: void focusLost(FocusEvent)
              */
   195      }
   196  }








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