Coverage Report - org.jtheque.utils.ui.SwingUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
SwingUtils
11%
6/52
10%
2/20
1.688
SwingUtils$1
0%
0/3
N/A
1.688
 
 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.Action;
 20  
 import javax.swing.JComponent;
 21  
 import javax.swing.JLabel;
 22  
 import javax.swing.JOptionPane;
 23  
 import javax.swing.JTextField;
 24  
 import javax.swing.KeyStroke;
 25  
 import javax.swing.SwingUtilities;
 26  
 import javax.swing.text.AbstractDocument;
 27  
 import javax.swing.text.Document;
 28  
 import javax.swing.text.DocumentFilter;
 29  
 import java.awt.Color;
 30  
 import java.awt.Component;
 31  
 import java.awt.DisplayMode;
 32  
 import java.awt.Font;
 33  
 import java.awt.GraphicsDevice;
 34  
 import java.awt.GraphicsEnvironment;
 35  
 import java.awt.Insets;
 36  
 import java.awt.Toolkit;
 37  
 import java.awt.Window;
 38  
 import java.awt.event.KeyEvent;
 39  
 
 40  
 /**
 41  
  * Provide utility methods for Swing Components.
 42  
  *
 43  
  * @author Baptiste Wicht
 44  
  */
 45  
 public final class SwingUtils {
 46  
     private static DisplayMode mode;
 47  
     private static GraphicsDevice device;
 48  
 
 49  
     private static Font defaultFont;
 50  
 
 51  
     /**
 52  
      * Private constructor, this class isn't instanciable.
 53  
      */
 54  
     private SwingUtils() {
 55  0
         super();
 56  0
     }
 57  
 
 58  
     /**
 59  
      * Load the display information for this computer.
 60  
      */
 61  
     private static void loadDisplayInfos() {
 62  0
         GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
 63  0
         device = gEnv.getDefaultScreenDevice();
 64  0
         mode = device.getDisplayMode();
 65  0
     }
 66  
 
 67  
     /**
 68  
      * Return the insets of the screen.
 69  
      *
 70  
      * @return The insets
 71  
      */
 72  
     private static Insets getInsets() {
 73  0
         return Toolkit.getDefaultToolkit().getScreenInsets(device.getDefaultConfiguration());
 74  
     }
 75  
 
 76  
     /**
 77  
      * Center a frame on the screen.
 78  
      *
 79  
      * @param frame The frame to be centered
 80  
      */
 81  
     public static void centerFrame(Window frame) {
 82  0
         if (mode == null) {
 83  0
             loadDisplayInfos();
 84  
         }
 85  
 
 86  0
         frame.setLocation((getWidth() - frame.getWidth()) / 2,
 87  
                 (getHeight() - frame.getHeight()) / 2);
 88  0
     }
 89  
 
 90  
     /**
 91  
      * Return the height of the screen.
 92  
      *
 93  
      * @return The height
 94  
      */
 95  
     private static int getHeight() {
 96  0
         if (mode == null) {
 97  0
             loadDisplayInfos();
 98  
         }
 99  
 
 100  0
         return mode.getHeight() - getInsets().bottom - getInsets().top;
 101  
     }
 102  
 
 103  
     /**
 104  
      * Return the width of the screen.
 105  
      *
 106  
      * @return The width
 107  
      */
 108  
     private static int getWidth() {
 109  0
         if (mode == null) {
 110  0
             loadDisplayInfos();
 111  
         }
 112  
 
 113  0
         return mode.getWidth() - getInsets().left - getInsets().right;
 114  
     }
 115  
 
 116  
     /**
 117  
      * Return the JOptionPane parent.
 118  
      *
 119  
      * @param c The component.
 120  
      * @return The parent JOptionPane.
 121  
      */
 122  
     public static JOptionPane getOptionPane(Component c) {
 123  0
         Component parent = c;
 124  
 
 125  0
         while (parent != null) {
 126  0
             if (parent instanceof JOptionPane) {
 127  0
                 return (JOptionPane) parent;
 128  
             }
 129  
 
 130  0
             parent = parent.getParent();
 131  
         }
 132  
 
 133  0
         return null;
 134  
     }
 135  
 
 136  
     /**
 137  
      * Set the option pane value.
 138  
      *
 139  
      * @param c     The component.
 140  
      * @param value The value to set to the JOptionPane.
 141  
      */
 142  
     public static void setOptionPaneValue(Component c, Object value) {
 143  0
         JOptionPane optionPane = SwingUtils.getOptionPane(c);
 144  0
         if (optionPane != null) {
 145  0
             optionPane.setValue(value);
 146  
         }
 147  0
     }
 148  
 
 149  
     /**
 150  
      * Create a button bar for actions.
 151  
      *
 152  
      * @param actions The actions to create the bar for.
 153  
      * @return A Bar containing a button for each action.
 154  
      */
 155  
     public static Component createButtonBar(Action... actions) {
 156  0
         return createButtonBar(false, actions);
 157  
     }
 158  
 
 159  
     /**
 160  
      * Create a button bar.
 161  
      *
 162  
      * @param leftAligned A boolean flag indicating if we want a left to right alignment or not.
 163  
      * @param actions     The actions to add to the menu bar.
 164  
      * @return The builded button bar.
 165  
      */
 166  
     public static Component createButtonBar(boolean leftAligned, Action... actions) {
 167  0
         ButtonBarBuilder builderButton = new ButtonBarBuilder();
 168  0
         builderButton.getPanel().setBackground(Color.white);
 169  
 
 170  0
         if (!leftAligned) {
 171  0
             builderButton.addGlue();
 172  
         }
 173  
 
 174  0
         builderButton.addActions(actions);
 175  
 
 176  0
         if (leftAligned) {
 177  0
             builderButton.addGlue();
 178  
         }
 179  
 
 180  0
         return builderButton.getPanel();
 181  
     }
 182  
 
 183  
     /**
 184  
      * Add an action to execute when the validation key (Enter) is pressed.
 185  
      *
 186  
      * @param field  The field to validate.
 187  
      * @param action The action to execute on validate.
 188  
      */
 189  
     public static void addFieldValidateAction(JComponent field, Action action) {
 190  0
         field.getActionMap().put("validate", action);
 191  0
         field.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "validate");
 192  0
     }
 193  
 
 194  
     /**
 195  
      * Limit the length of the field.
 196  
      *
 197  
      * @param field  The field to limit.
 198  
      * @param length The maximal length the field may contain.
 199  
      */
 200  
     public static void addFieldLengthLimit(JTextField field, int length) {
 201  0
         DocumentFilter filterNom = new DocumentLengthFilterAvert(length, field);
 202  0
         Document documentFieldNom = field.getDocument();
 203  0
         ((AbstractDocument) documentFieldNom).setDocumentFilter(filterNom);
 204  0
     }
 205  
 
 206  
     /**
 207  
      * Return the default font of the application.
 208  
      *
 209  
      * @return The default font.
 210  
      */
 211  
     public static Font getDefaultFont() {
 212  2
         if (defaultFont == null) {
 213  2
             defaultFont = new JLabel("Font").getFont();
 214  
         }
 215  
 
 216  2
         return defaultFont;
 217  
     }
 218  
 
 219  
     /**
 220  
      * Executed the specified runnable in the EDT.
 221  
      *
 222  
      * @param runnable The runnable to run in EDT.
 223  
      */
 224  
     public static void inEdt(Runnable runnable) {
 225  2
         if (SwingUtilities.isEventDispatchThread()) {
 226  0
             runnable.run();
 227  
         } else {
 228  2
             SwingUtilities.invokeLater(runnable);
 229  
         }
 230  2
     }
 231  
 
 232  
     /**
 233  
      * Refresh the specified component.
 234  
      *
 235  
      * @param component The component to refresh.
 236  
      */
 237  
     public static void refresh(final Component component) {
 238  0
         inEdt(new Runnable() {
 239  
             @Override
 240  
             public void run() {
 241  0
                 SwingUtilities.updateComponentTreeUI(component);
 242  0
             }
 243  
         });
 244  0
     }
 245  
 }