001    /******************************************************************************
002     * Copyright (C) MActor Developers. All rights reserved.                        *
003     * ---------------------------------------------------------------------------*
004     * This file is part of MActor.                                               *
005     *                                                                            *
006     * MActor is free software; you can redistribute it and/or modify             *
007     * it under the terms of the GNU General Public License as published by       *
008     * the Free Software Foundation; either version 2 of the License, or          *
009     * (at your option) any later version.                                        *
010     *                                                                            *
011     * MActor is distributed in the hope that it will be useful,                  *
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
014     * GNU General Public License for more details.                               *
015     *                                                                            *
016     * You should have received a copy of the GNU General Public License          *
017     * along with MActor; if not, write to the Free Software                      *
018     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
019     ******************************************************************************/
020    package org.mactor.ui.gui.project.editors;
021    
022    import java.awt.event.ActionEvent;
023    import java.io.File;
024    import java.io.IOException;
025    
026    import javax.swing.AbstractAction;
027    import javax.swing.JButton;
028    import javax.swing.JFileChooser;
029    import javax.swing.JTextField;
030    
031    import org.mactor.framework.spec.ProjectContext;
032    
033    public class ProjectInternalDirSelector extends SimpleRowPanel {
034            JTextField path = new JTextField(20);
035            static JFileChooser fc = new JFileChooser(ProjectContext.getGlobalInstance().getProjectDir());
036            String relativePath;
037            JButton selectButton = new JButton(new AbstractAction("Select..") {
038                    public void actionPerformed(ActionEvent e) {
039                            int returnVal = fc.showOpenDialog(ProjectInternalDirSelector.this);
040                            if (returnVal == JFileChooser.APPROVE_OPTION) {
041                                    File f = fc.getSelectedFile();
042                                    try {
043                                            f = new File(f.getCanonicalFile().getAbsolutePath());
044                                    } catch (IOException ioe) {
045                                            ioe.printStackTrace();
046                                    }
047                                    setPath(f);
048                                    if (l != null)
049                                            l.dirChanged(f);
050                            }
051                    }
052            });
053            public ProjectInternalDirSelector() {
054                    add(path);
055                    add(selectButton);
056                    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
057                    path.setEditable(false);
058            }
059            public String getPath() {
060                    return relativePath;
061            }
062            public void setPath(File path) {
063                    relativePath = ProjectContext.getGlobalInstance().getRelativePath(path);
064                    if (relativePath != null && relativePath.length() > 0) {
065                            fc.setSelectedFile(path);
066                            this.path.setText(relativePath);
067                    } else {
068                            fc.setSelectedFile(ProjectContext.getGlobalInstance().getProjectDir());
069                            this.path.setText("[project directory]");
070                    }
071            }
072            DirChangedListener l;
073            public void setDirChangedListener(DirChangedListener l) {
074                    this.l = l;
075            }
076            public interface DirChangedListener {
077                    void dirChanged(File newConfigDir);
078            }
079    }