MOA 12.03
Real Time Analytics for Data Streams
FileOptionEditComponent.java
Go to the documentation of this file.
00001 /*
00002  *    FileOptionEditComponent.java
00003  *    Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
00004  *    @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
00005  *
00006  *    This program is free software; you can redistribute it and/or modify
00007  *    it under the terms of the GNU General Public License as published by
00008  *    the Free Software Foundation; either version 3 of the License, or
00009  *    (at your option) any later version.
00010  *
00011  *    This program is distributed in the hope that it will be useful,
00012  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *    GNU General Public License for more details.
00015  *
00016  *    You should have received a copy of the GNU General Public License
00017  *    along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  *    
00019  */
00020 package moa.gui;
00021 
00022 import java.awt.BorderLayout;
00023 import java.awt.event.ActionEvent;
00024 import java.awt.event.ActionListener;
00025 import java.io.File;
00026 
00027 import javax.swing.JButton;
00028 import javax.swing.JFileChooser;
00029 import javax.swing.JPanel;
00030 import javax.swing.JTextField;
00031 
00032 import moa.options.FileOption;
00033 import moa.options.Option;
00034 
00041 public class FileOptionEditComponent extends JPanel implements
00042         OptionEditComponent {
00043 
00044     private static final long serialVersionUID = 1L;
00045 
00046     protected FileOption editedOption;
00047 
00048     protected JTextField textField = new JTextField();
00049 
00050     protected JButton browseButton = new JButton("Browse");
00051 
00052     public FileOptionEditComponent(FileOption option) {
00053         this.editedOption = option;
00054         setLayout(new BorderLayout());
00055         add(this.textField, BorderLayout.CENTER);
00056         add(this.browseButton, BorderLayout.EAST);
00057         this.browseButton.addActionListener(new ActionListener() {
00058 
00059             @Override
00060             public void actionPerformed(ActionEvent arg0) {
00061                 browseForFile();
00062             }
00063         });
00064         setEditState(this.editedOption.getValueAsCLIString());
00065     }
00066 
00067     @Override
00068     public void applyState() {
00069         this.editedOption.setValueViaCLIString(this.textField.getText().length() > 0 ? this.textField.getText() : null);
00070     }
00071 
00072     @Override
00073     public Option getEditedOption() {
00074         return this.editedOption;
00075     }
00076 
00077     @Override
00078     public void setEditState(String cliString) {
00079         this.textField.setText(cliString);
00080     }
00081 
00082     public void browseForFile() {
00083         JFileChooser fileChooser = new JFileChooser();
00084         fileChooser.setAcceptAllFileFilterUsed(true);
00085         String extension = this.editedOption.getDefaultFileExtension();
00086         if (extension != null) {
00087             fileChooser.addChoosableFileFilter(new FileExtensionFilter(
00088                     extension));
00089         }
00090         fileChooser.setSelectedFile(new File(this.textField.getText()));
00091         if (this.editedOption.isOutputFile()) {
00092             if (fileChooser.showSaveDialog(this.browseButton) == JFileChooser.APPROVE_OPTION) {
00093                 File chosenFile = fileChooser.getSelectedFile();
00094                 String fileName = chosenFile.getPath();
00095                 if (!chosenFile.exists()) {
00096                     if ((extension != null) && !fileName.endsWith(extension)) {
00097                         fileName = fileName + "." + extension;
00098                     }
00099                 }
00100                 this.textField.setText(fileName);
00101             }
00102         } else {
00103             if (fileChooser.showOpenDialog(this.browseButton) == JFileChooser.APPROVE_OPTION) {
00104                 this.textField.setText(fileChooser.getSelectedFile().getPath());
00105             }
00106         }
00107     }
00108 }
 All Classes Namespaces Files Functions Variables Enumerations