Coverage Report - org.jtheque.utils.ui.DisplayPopupThread
 
Classes in this File Line Coverage Branch Coverage Complexity
DisplayPopupThread
0%
0/22
N/A
1.143
DisplayPopupThread$1
0%
0/3
N/A
1.143
DisplayPopupThread$2
0%
0/3
N/A
1.143
 
 1  
 package org.jtheque.utils.ui;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import javax.swing.BorderFactory;
 20  
 import javax.swing.JComponent;
 21  
 import javax.swing.JLabel;
 22  
 import javax.swing.JPanel;
 23  
 import javax.swing.JTextField;
 24  
 import javax.swing.Popup;
 25  
 import javax.swing.PopupFactory;
 26  
 import javax.swing.SwingUtilities;
 27  
 import java.awt.Color;
 28  
 
 29  
 /**
 30  
  * A thread to display a popup on a field.
 31  
  *
 32  
  * @author Baptiste Wicht
 33  
  */
 34  0
 final class DisplayPopupThread extends Thread {
 35  0
     private final PopupFactory factory = PopupFactory.getSharedInstance();
 36  
     private final JTextField owner;
 37  
     private final JComponent content;
 38  
 
 39  
     private Popup popup;
 40  
 
 41  
     private static final int DISPLAY_TIME = 2000;
 42  
 
 43  
     /**
 44  
      * Construct a new <code>DisplayPopupThread</code> for a specific field.
 45  
      *
 46  
      * @param field the JTextField associated in which we display popup.
 47  
      */
 48  
     DisplayPopupThread(JTextField field) {
 49  0
         super();
 50  
 
 51  0
         owner = field;
 52  
 
 53  0
         content = new JPanel();
 54  0
         content.add(new JLabel("Too long"));
 55  0
         content.setBackground(Color.white);
 56  0
         content.setBorder(BorderFactory.createEtchedBorder());
 57  0
     }
 58  
 
 59  
     @Override
 60  
     public void run() {
 61  0
         popup = createPopup();
 62  
 
 63  0
         SwingUtilities.invokeLater(new Runnable() {
 64  
             @Override
 65  
             public void run() {
 66  0
                 popup.show();
 67  0
             }
 68  
         });
 69  
 
 70  
         try {
 71  0
             Thread.sleep(DISPLAY_TIME);
 72  0
         } catch (InterruptedException e) {
 73  
             //Nothing to do.
 74  0
         }
 75  
 
 76  0
         interrupt();
 77  0
     }
 78  
 
 79  
     @Override
 80  
     public void interrupt() {
 81  0
         close();
 82  
 
 83  0
         super.interrupt();
 84  0
     }
 85  
 
 86  
     /**
 87  
      * Create a popup for the actual field. The popup is configured to appear at the middle of the field.
 88  
      *
 89  
      * @return The popup configured at the good position.
 90  
      */
 91  
     private Popup createPopup() {
 92  0
         return factory.getPopup(
 93  
                 owner, content,
 94  
                 owner.getLocationOnScreen().x + owner.getWidth() / 2,
 95  
                 owner.getLocationOnScreen().y + owner.getHeight() / 2);
 96  
     }
 97  
 
 98  
     /**
 99  
      * Close the current popup.
 100  
      */
 101  
     void close() {
 102  0
         SwingUtilities.invokeLater(new Runnable() {
 103  
             @Override
 104  
             public void run() {
 105  0
                 popup.hide();
 106  0
             }
 107  
         });
 108  0
     }
 109  
 }