FileInput.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
00003  * All rights reserved. This program is made available under the terms of the
00004  * Eclipse Public License v1.0 which accompanies this distribution, and is
00005  * available at http://www.eclipse.org/legal/epl-v10.html
00006  * Contributors:
00007  * General Robotix Inc.
00008  * National Institute of Advanced Industrial Science and Technology (AIST) 
00009  */
00017 package com.generalrobotix.ui.util;
00018 
00019 import javax.swing.*;
00020 import java.awt.event.*;
00021 import javax.swing.filechooser.*;
00022 
00023 
00024 
00025 @SuppressWarnings("serial")
00026 public class FileInput extends JPanel {
00027     private JTextField text_;
00028     private String[] fileFilter_;
00029     private String basedir_;
00030 
00031     static {
00032         UIManager.put("FileChooser.cancelButtonText",            MessageBundle.get("FileChooser.cancelButtonText"));
00033         UIManager.put("FileChooser.cancelButtonToolTipText",     MessageBundle.get("FileChooser.cancelButtonToolTipText"));
00034         UIManager.put("FileChooser.acceptAllFileFilterText",     MessageBundle.get("FileChooser.acceptAllFileFilterText"));
00035         UIManager.put("FileChooser.filesOfTypeLabelText",        MessageBundle.get("FileChooser.filesOfTypeLabelText"));
00036         UIManager.put("FileChooser.fileNameLabelText",           MessageBundle.get("FileChooser.fileNameLabelText"));
00037         UIManager.put("FileChooser.lookInLabelText",             MessageBundle.get("FileChooser.lookInLabelText"));
00038         UIManager.put("FileChooser.upFolderToolTipText",         MessageBundle.get("FileChooser.upFolderToolTipText"));
00039         UIManager.put("FileChooser.homeFolderToolTipText",       MessageBundle.get("FileChooser.homeFolderToolTipText"));
00040         UIManager.put("FileChooser.newFolderToolTipText",        MessageBundle.get("FileChooser.newFolderToolTipText"));
00041         UIManager.put("FileChooser.listViewButtonToolTipText",   MessageBundle.get("FileChooser.listViewButtonToolTipText"));
00042         UIManager.put("FileChooser.detailsViewButtonToolTipText",MessageBundle.get("FileChooser.detailsViewButtonToolTipText"));
00043     }                                                            
00044                                                                  
00045     public FileInput(String[] filter, String dir) {
00046         fileFilter_ = filter;
00047         text_ = new JTextField();
00048         basedir_ = dir;
00049         JButton button = new JButton(MessageBundle.get("dialog.fileinput.filechoose"));
00050         button.addActionListener(
00051             new ActionListener() { 
00052                 public void actionPerformed(ActionEvent evt) {
00053                     JFileChooser chooser = new JFileChooser(basedir_);
00054                     for (int i = 0; i < fileFilter_.length; i ++) {
00055                         chooser.addChoosableFileFilter(createFilter(fileFilter_[i]));
00056                     }
00057                     
00058                     //chooser.setControlButtonsAreShown(false);
00059                     chooser.setApproveButtonMnemonic('o');
00060                     chooser.setApproveButtonToolTipText(MessageBundle.get("dialog.fileopen.title"));
00061                     int result = chooser.showDialog(FileInput.this, MessageBundle.get("dialog.fileopen.title"));
00062                     if (result == JFileChooser.APPROVE_OPTION) {
00063                         text_.setText( chkExtension(chooser.getSelectedFile().getPath()) );
00064                     }
00065                 }
00066             }
00067         );
00068 
00069         setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
00070         add(text_);
00071         add(Box.createHorizontalStrut(12));
00072         add(button);
00073     }
00074 
00075     public String getFileName() {
00076         return chkExtension(text_.getText().trim());
00077     }
00078 
00079     public void setText(String text) {
00080         text_.setText(text);
00081     }
00082 
00083     public FileFilter createFilter(final String filter) {
00084         return new FileFilter() {
00085             public boolean accept(java.io.File fileobj ) {
00086                 if (fileobj.isDirectory()) {
00087                     return true;
00088                 }
00089 
00090                 String extension = "";
00091 
00092                 if (fileobj.getPath().lastIndexOf('.') > 0) {
00093                     extension =
00094                         fileobj.getPath().substring(
00095                             fileobj.getPath().lastIndexOf('.') + 1
00096                         ).toLowerCase();
00097                 }
00098 
00099                 return extension.equals(filter);
00100 
00101                 /*
00102                 if (extension != "") {
00103                     return extension.equals(filter);
00104                 } else {
00105                     return fileobj.isDirectory();
00106                 }
00107                 */
00108             }
00109 
00110             public String getDescription() {
00111                 return filter + " files (*." + filter + ")";
00112             }
00113         };
00114     }
00115     
00116     private String chkExtension(String localPath){
00117         if (fileFilter_.length == 1) {
00118             if (!localPath.matches(".+\\." + fileFilter_[0] + "$")) {
00119                 localPath += ".";
00120                 localPath += fileFilter_[0];
00121             }
00122         }
00123         return localPath; 
00124     }
00125 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:16