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