001package arcommon.ui.component;
002
003import java.awt.BorderLayout;
004import java.awt.Dimension;
005import java.awt.FlowLayout;
006import java.awt.event.ActionEvent;
007import java.awt.event.ActionListener;
008import java.io.File;
009
010import javax.swing.JButton;
011import javax.swing.JPanel;
012
013import arcommon.FileUtil;
014import arcommon.Util;
015import arcommon.ui.other.GUIUtil;
016
017public class PathJComponent extends JPanel {
018    
019    private MyJTextField pathMyJTextField;
020    
021    public PathJComponent() {
022        this("");
023    }
024
025    public PathJComponent(String ppath) {
026        setLayout(new BorderLayout());
027        pathMyJTextField = new MyJTextField();
028        add(pathMyJTextField, BorderLayout.CENTER);
029        pathMyJTextField.setText(ppath);
030        pathMyJTextField.setPreferredSize(new Dimension(700, pathMyJTextField.getPreferredSize().height - 10));
031        pathMyJTextField.addActionListener(new ActionListener() {
032            public void actionPerformed(ActionEvent e) {
033                String path = pathMyJTextField.getText();
034                if (path.length() == 0 && Util.isLinuxOS()) {
035                    path = File.separator;
036                    setPath(path);
037                }
038                if (addFileSeparator()) {
039                    path = FileUtil.addDirectorySeparatorToPath(path);
040                    setPath(path);
041                }
042
043                changed(path);
044            }
045        });
046        
047        JPanel panel = new JPanel();
048        add(panel, BorderLayout.EAST);
049        panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 1));
050        
051        JButton jbutton = new JButton("Browse");
052        panel.add(jbutton);
053        jbutton.addActionListener(
054            new ActionListener() {
055                public void actionPerformed(ActionEvent e) {
056                    eventBrowseJButtonActionPerformed();
057                }
058            }
059        );
060    }
061    
062    protected boolean addFileSeparator() {
063        return true;
064    }
065    
066    protected void changed(String path) {
067        ;
068    }
069    
070    protected void eventBrowseJButtonActionPerformed() {
071        String path = GUIUtil.selectADirectory(pathMyJTextField.getText(), true, "Select a directory");
072        if (path == null || path.equals("")) {
073            return ;
074        }
075        pathMyJTextField.setText(path);
076        changed(path);
077    }
078    
079    public String getPath() {
080        return pathMyJTextField.getText();
081    }
082    
083    public void setPath(String ppath) {
084        pathMyJTextField.setText(ppath);
085    }
086    
087    @Override
088    public void requestFocus() {
089        pathMyJTextField.requestFocus();
090    }
091
092    public void selectAll() {
093        pathMyJTextField.selectAll();
094    }
095    
096    @Override
097    public boolean requestFocusInWindow() {
098        return pathMyJTextField.requestFocusInWindow();
099    }
100    
101    private static final long serialVersionUID = -8328711764851023378L;
102}