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    
025    import javax.swing.AbstractAction;
026    import javax.swing.JButton;
027    import javax.swing.JFileChooser;
028    import javax.swing.JOptionPane;
029    import javax.swing.JTextField;
030    
031    import org.mactor.framework.spec.ProjectContext;
032    
033    public class ProjectInternalFileSelector extends SimpleRowPanel {
034            JTextField pathText = new JTextField(20);
035            static JFileChooser fc = new JFileChooser(ProjectContext.getGlobalInstance().getProjectDir());
036            JButton selectButton = new JButton(new AbstractAction("Select..") {
037                    public void actionPerformed(ActionEvent e) {
038                            int returnVal = fc.showOpenDialog(ProjectInternalFileSelector.this);
039                            if (returnVal == JFileChooser.APPROVE_OPTION) {
040                                    File f = fc.getSelectedFile();
041                                    pathText.setText(ProjectContext.getGlobalInstance().getRelativePath(f));
042                                    if (l != null)
043                                            l.fileChanged(f);
044                            }
045                    }
046            });
047            JButton editButton = new JButton(new AbstractAction("Edit...") {
048                    public void actionPerformed(ActionEvent e) {
049                            File path = ProjectContext.getGlobalInstance().getAbsolutePath(pathText.getText());
050                            if (!path.exists()) {
051                                    if (JOptionPane.OK_OPTION != JOptionPane.showConfirmDialog(ProjectInternalFileSelector.this, "The file '" + pathText.getText() + "' does not exist. Create it?", "Create file",
052                                                    JOptionPane.OK_CANCEL_OPTION)) {
053                                            return;
054                                    }
055                            }
056                            new TextFileEditor(pathText.getText(), "Edit", null).setVisible(true);
057                    }
058            });
059            public ProjectInternalFileSelector() {
060                    this(true);
061            }
062            public ProjectInternalFileSelector(boolean showPath) {
063                    if (showPath)
064                            add(pathText);
065                    add(selectButton);
066                    add(editButton);
067            }
068            public String getPath() {
069                    return pathText.getText();
070            }
071            public void setPath(String path) {
072                    if (path != null && path.length() > 0)
073                            fc.setSelectedFile(ProjectContext.getGlobalInstance().getAbsolutePath(path));
074                    this.pathText.setText(path);
075            }
076            FileChangedListener l;
077            public void setFileChangedListener(FileChangedListener l) {
078                    this.l = l;
079            }
080            public interface FileChangedListener {
081                    void fileChanged(File newFile);
082            }
083    }