FileInput.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
3  * All rights reserved. This program is made available under the terms of the
4  * Eclipse Public License v1.0 which accompanies this distribution, and is
5  * available at http://www.eclipse.org/legal/epl-v10.html
6  * Contributors:
7  * General Robotix Inc.
8  * National Institute of Advanced Industrial Science and Technology (AIST)
9  */
17 package com.generalrobotix.ui.util;
18 
19 import javax.swing.*;
20 import java.awt.event.*;
21 import javax.swing.filechooser.*;
22 
23 
24 
25 @SuppressWarnings("serial")
26 public class FileInput extends JPanel {
27  private JTextField text_;
28  private String[] fileFilter_;
29  private String basedir_;
30 
31  static {
32  UIManager.put("FileChooser.cancelButtonText", MessageBundle.get("FileChooser.cancelButtonText"));
33  UIManager.put("FileChooser.cancelButtonToolTipText", MessageBundle.get("FileChooser.cancelButtonToolTipText"));
34  UIManager.put("FileChooser.acceptAllFileFilterText", MessageBundle.get("FileChooser.acceptAllFileFilterText"));
35  UIManager.put("FileChooser.filesOfTypeLabelText", MessageBundle.get("FileChooser.filesOfTypeLabelText"));
36  UIManager.put("FileChooser.fileNameLabelText", MessageBundle.get("FileChooser.fileNameLabelText"));
37  UIManager.put("FileChooser.lookInLabelText", MessageBundle.get("FileChooser.lookInLabelText"));
38  UIManager.put("FileChooser.upFolderToolTipText", MessageBundle.get("FileChooser.upFolderToolTipText"));
39  UIManager.put("FileChooser.homeFolderToolTipText", MessageBundle.get("FileChooser.homeFolderToolTipText"));
40  UIManager.put("FileChooser.newFolderToolTipText", MessageBundle.get("FileChooser.newFolderToolTipText"));
41  UIManager.put("FileChooser.listViewButtonToolTipText", MessageBundle.get("FileChooser.listViewButtonToolTipText"));
42  UIManager.put("FileChooser.detailsViewButtonToolTipText",MessageBundle.get("FileChooser.detailsViewButtonToolTipText"));
43  }
44 
45  public FileInput(String[] filter, String dir) {
46  fileFilter_ = filter;
47  text_ = new JTextField();
48  basedir_ = dir;
49  JButton button = new JButton(MessageBundle.get("dialog.fileinput.filechoose"));
50  button.addActionListener(
51  new ActionListener() {
52  public void actionPerformed(ActionEvent evt) {
53  JFileChooser chooser = new JFileChooser(basedir_);
54  for (int i = 0; i < fileFilter_.length; i ++) {
55  chooser.addChoosableFileFilter(createFilter(fileFilter_[i]));
56  }
57 
58  //chooser.setControlButtonsAreShown(false);
59  chooser.setApproveButtonMnemonic('o');
60  chooser.setApproveButtonToolTipText(MessageBundle.get("dialog.fileopen.title"));
61  int result = chooser.showDialog(FileInput.this, MessageBundle.get("dialog.fileopen.title"));
62  if (result == JFileChooser.APPROVE_OPTION) {
63  text_.setText( chkExtension(chooser.getSelectedFile().getPath()) );
64  }
65  }
66  }
67  );
68 
69  setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
70  add(text_);
71  add(Box.createHorizontalStrut(12));
72  add(button);
73  }
74 
75  public String getFileName() {
76  return chkExtension(text_.getText().trim());
77  }
78 
79  public void setText(String text) {
80  text_.setText(text);
81  }
82 
83  public FileFilter createFilter(final String filter) {
84  return new FileFilter() {
85  public boolean accept(java.io.File fileobj ) {
86  if (fileobj.isDirectory()) {
87  return true;
88  }
89 
90  String extension = "";
91 
92  if (fileobj.getPath().lastIndexOf('.') > 0) {
93  extension =
94  fileobj.getPath().substring(
95  fileobj.getPath().lastIndexOf('.') + 1
96  ).toLowerCase();
97  }
98 
99  return extension.equals(filter);
100 
101  /*
102  if (extension != "") {
103  return extension.equals(filter);
104  } else {
105  return fileobj.isDirectory();
106  }
107  */
108  }
109 
110  public String getDescription() {
111  return filter + " files (*." + filter + ")";
112  }
113  };
114  }
115 
116  private String chkExtension(String localPath){
117  if (fileFilter_.length == 1) {
118  if (!localPath.matches(".+\\." + fileFilter_[0] + "$")) {
119  localPath += ".";
120  localPath += fileFilter_[0];
121  }
122  }
123  return localPath;
124  }
125 }
static final String get(String key)
FileInput(String[] filter, String dir)
Definition: FileInput.java:45
png_uint_32 i
Definition: png.h:2735
String chkExtension(String localPath)
Definition: FileInput.java:116
FileFilter createFilter(final String filter)
Definition: FileInput.java:83


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sat May 8 2021 02:42:37