Coverage Report - org.jtheque.core.managers.view.impl.components.filthy.FilthyFileChooserPanel
 
Classes in this File Line Coverage Branch Coverage Complexity
FilthyFileChooserPanel
0 %
0/46
0 %
0/6
1.357
FilthyFileChooserPanel$1
N/A
N/A
1.357
FilthyFileChooserPanel$BrowseAction
0 %
0/8
0 %
0/4
1.357
FilthyFileChooserPanel$BrowseButton
0 %
0/7
N/A
1.357
 
 1  
 package org.jtheque.core.managers.view.impl.components.filthy;
 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 org.jtheque.core.managers.Managers;
 20  
 import org.jtheque.core.managers.language.ILanguageManager;
 21  
 import org.jtheque.core.managers.language.Internationalizable;
 22  
 import org.jtheque.core.managers.view.able.IViewManager;
 23  
 import org.jtheque.core.utils.ui.Borders;
 24  
 import org.jtheque.core.utils.ui.PanelBuilder;
 25  
 import org.jtheque.utils.io.SimpleFilter;
 26  
 
 27  
 import javax.swing.AbstractAction;
 28  
 import javax.swing.Action;
 29  
 import javax.swing.BorderFactory;
 30  
 import javax.swing.JButton;
 31  
 import javax.swing.JComponent;
 32  
 import javax.swing.JLabel;
 33  
 import javax.swing.JPanel;
 34  
 import java.awt.BorderLayout;
 35  
 import java.awt.Color;
 36  
 import java.awt.Container;
 37  
 import java.awt.Dimension;
 38  
 import java.awt.GridBagConstraints;
 39  
 import java.awt.Insets;
 40  
 import java.awt.event.ActionEvent;
 41  
 
 42  
 /**
 43  
  * A panel with a label, a text field and a button to choose a file.
 44  
  *
 45  
  * @author Baptiste Wicht
 46  
  */
 47  0
 public final class FilthyFileChooserPanel extends JPanel implements Internationalizable {
 48  
     private FilthyTextField fieldFilePath;
 49  
     private JLabel label;
 50  
     private JButton button;
 51  
     private SimpleFilter filter;
 52  
 
 53  
     private boolean directoriesOnly;
 54  
 
 55  
     private String key;
 56  
 
 57  
     /**
 58  
      * Construct a new FileChooserPanel.
 59  
      *
 60  
      * @param label A boolean tag indicating if we must display the label or not.
 61  
      */
 62  
     public FilthyFileChooserPanel(boolean label) {
 63  0
         super();
 64  
 
 65  0
         build(label);
 66  0
     }
 67  
 
 68  
     /**
 69  
      * Construct a new FilthyFileChooserPanel.
 70  
      */
 71  
     public FilthyFileChooserPanel() {
 72  0
         this(true);
 73  0
     }
 74  
 
 75  
     /**
 76  
      * Build the panel.
 77  
      *
 78  
      * @param displayLabel A boolean tag indicating if we must display the label or not.
 79  
      */
 80  
     private void build(boolean displayLabel) {
 81  0
         PanelBuilder builder = new PanelBuilder(this);
 82  
 
 83  0
         setOpaque(false);
 84  0
         setBorder(Borders.EMPTY_BORDER);
 85  
 
 86  0
         builder.setDefaultInsets(new Insets(0, 0, 0, 0));
 87  
 
 88  0
         int x = 0;
 89  
 
 90  0
         if (displayLabel) {
 91  0
             label = builder.add(new JLabel(),
 92  
                     builder.gbcSet(0, 0, GridBagConstraints.NONE, GridBagConstraints.ABOVE_BASELINE_LEADING, 1, 0));
 93  0
             label.setForeground(Color.white);
 94  
 
 95  0
             x++;
 96  
         }
 97  
 
 98  0
         JComponent panel = new JPanel(new BorderLayout());
 99  0
         panel.setOpaque(false);
 100  0
         panel.setBorder(BorderFactory.createEtchedBorder(1));
 101  
 
 102  0
         fieldFilePath = new FilthyTextField(15);
 103  0
         Insets insets = fieldFilePath.getTextField().getMargin();
 104  0
         fieldFilePath.setBorder(BorderFactory.createEmptyBorder(insets.top, insets.left, insets.bottom, insets.right));
 105  0
         panel.add(fieldFilePath, BorderLayout.CENTER);
 106  
 
 107  0
         addBrowseButton(panel);
 108  
 
 109  0
         if (displayLabel) {
 110  0
             builder.setDefaultInsets(new Insets(0, 5, 0, 0));
 111  
         }
 112  
 
 113  0
         builder.add(panel, builder.gbcSet(x, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.ABOVE_BASELINE_LEADING, 0, 0, 1.0, 1.0));
 114  0
     }
 115  
 
 116  
     /**
 117  
      * Add the browse button.
 118  
      *
 119  
      * @param panel The parent panel.
 120  
      */
 121  
     private void addBrowseButton(Container panel) {
 122  0
         button = new BrowseButton(new BrowseAction());
 123  
 
 124  0
         button.setMargin(new Insets(2, 0, 2, 0));
 125  
 
 126  0
         panel.add(button, BorderLayout.EAST);
 127  0
     }
 128  
 
 129  
     /**
 130  
      * Set the text of the label.
 131  
      *
 132  
      * @param text The text of the label.
 133  
      */
 134  
     void setText(String text) {
 135  0
         label.setText(text);
 136  0
     }
 137  
 
 138  
     /**
 139  
      * Set the text key for the label.
 140  
      *
 141  
      * @param key The internationalization key.
 142  
      */
 143  
     public void setTextKey(String key) {
 144  0
         this.key = key;
 145  
 
 146  0
         setText(Managers.getManager(ILanguageManager.class).getMessage(key));
 147  0
     }
 148  
 
 149  
     /**
 150  
      * Return the path to the file.
 151  
      *
 152  
      * @return The path to the file.
 153  
      */
 154  
     public String getFilePath() {
 155  0
         return fieldFilePath.getText();
 156  
     }
 157  
 
 158  
     /**
 159  
      * Set the path to the file.
 160  
      *
 161  
      * @param path The path to the file.
 162  
      */
 163  
     public void setFilePath(String path) {
 164  0
         fieldFilePath.setText(path);
 165  0
     }
 166  
 
 167  
     @Override
 168  
     public void setEnabled(boolean enable) {
 169  0
         super.setEnabled(enable);
 170  
 
 171  0
         fieldFilePath.setEnabled(enable);
 172  0
         button.setEnabled(enable);
 173  0
     }
 174  
 
 175  
     @Override
 176  
     public void refreshText() {
 177  0
         if (key != null) {
 178  0
             setText(Managers.getManager(ILanguageManager.class).getMessage(key));
 179  
         }
 180  0
     }
 181  
 
 182  
     /**
 183  
      * A button to integrate in the component.
 184  
      *
 185  
      * @author Baptiste Wicht
 186  
      */
 187  0
     private static final class BrowseButton extends JButton {
 188  
         /**
 189  
          * Construct a new BrowseButton.
 190  
          *
 191  
          * @param action The action to launch on the button click.
 192  
          */
 193  
         private BrowseButton(Action action) {
 194  0
             super(action);
 195  0
         }
 196  
 
 197  
         @Override
 198  
         public Dimension getPreferredSize() {
 199  0
             Dimension size = super.getPreferredSize();
 200  0
             size.height = 10;
 201  0
             size.width = 20;
 202  0
             return size;
 203  
         }
 204  
     }
 205  
 
 206  
     /**
 207  
      * A Browse action.
 208  
      *
 209  
      * @author Baptiste Wicht
 210  
      */
 211  0
     private final class BrowseAction extends AbstractAction {
 212  
         /**
 213  
          * Construct a new BrowseAction.
 214  
          */
 215  0
         private BrowseAction() {
 216  0
             super("...");
 217  0
         }
 218  
 
 219  
         @Override
 220  
         public void actionPerformed(ActionEvent e) {
 221  0
             String file = directoriesOnly ? Managers.getManager(IViewManager.class).chooseDirectory() : Managers.getManager(IViewManager.class).chooseFile(filter);
 222  
 
 223  0
             if (file != null) {
 224  0
                 fieldFilePath.setText(file);
 225  
             }
 226  0
         }
 227  
     }
 228  
 }