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    /**
021     * 
022     */
023    package org.mactor.ui.gui.actions;
024    
025    import java.awt.BorderLayout;
026    import java.awt.FlowLayout;
027    import java.io.File;
028    
029    import javax.swing.AbstractAction;
030    import javax.swing.BorderFactory;
031    import javax.swing.JButton;
032    import javax.swing.JDialog;
033    import javax.swing.JFileChooser;
034    import javax.swing.JLabel;
035    import javax.swing.JOptionPane;
036    import javax.swing.JPanel;
037    import javax.swing.JTextField;
038    
039    import org.mactor.framework.MactorException;
040    import org.mactor.framework.spec.ProjectContext;
041    import org.mactor.ui.gui.AsyncAction;
042    import org.mactor.ui.gui.project.GuiAction;
043    import org.mactor.ui.gui.project.ProjectController;
044    import org.mactor.ui.gui.project.ProjectNodeType;
045    import org.mactor.ui.gui.project.ProjectTreeNode;
046    import org.mactor.ui.gui.project.ProjectTreeNodeBuilder;
047    import org.mactor.ui.gui.project.editors.SimpleFormPanel;
048    import org.mactor.ui.gui.project.editors.SimpleRowPanel;
049    
050    public class NewProjectAction implements GuiAction {
051            public boolean isPermitted(ProjectTreeNode selectedTreeNode, ProjectController projectController, String[] parameters) {
052                    return true;
053            }
054            public void perform(ProjectTreeNode selectedTreeNode, ProjectController projectController, String[] parameters) throws MactorException {
055                    new NewProjectDlg(projectController).setVisible(true);
056            }
057            static JFileChooser fc;
058            private class NewProjectDlg extends JDialog {
059                    ProjectController projectController;
060                    JTextField nameText = new JTextField(20);
061                    JTextField dirText = new JTextField(20);
062                    JButton selectDir = new JButton(new AbstractAction("Select...") {
063                            public void actionPerformed(java.awt.event.ActionEvent arg0) {
064                                    if (fc == null) {
065                                            if (ProjectContext.getGlobalInstance().getProjectDir() != null)
066                                                    fc = new JFileChooser(ProjectContext.getGlobalInstance().getProjectDir());
067                                            else
068                                                    fc = new JFileChooser(new File("."));
069                                    }
070                                    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
071                                    int returnVal = fc.showOpenDialog(projectController.getControllerFrame());
072                                    if (returnVal == JFileChooser.APPROVE_OPTION) {
073                                            dirText.setText(fc.getSelectedFile().getAbsolutePath());
074                                    }
075                            };
076                    });
077                    JButton okButton = new JButton(new AsyncAction("Ok", false, new AsyncAction.AsyncRunnable() {
078                            public void run() throws MactorException {
079                                    if (nameText.getText() == null || nameText.getText().trim().length() == 0)
080                                            throw new MactorException("Project Name must be specified");
081                                    if (dirText.getText() == null || dirText.getText().trim().length() == 0)
082                                            throw new MactorException("Project Parent Directory must be specified");
083                                    File pdir = new File(dirText.getText() + "/" + nameText.getText().trim());
084                                    File f = new File(dirText.getText());
085                                    if (!f.exists()) {
086                                            if (JOptionPane.YES_OPTION != JOptionPane.showConfirmDialog(null, "The selected project parent directory '" + f.getAbsolutePath() + "' does not exist. Create it?"))
087                                                    return;
088                                    }
089                                    if (!pdir.mkdirs())
090                                            throw new MactorException("Unable to create project directory '" + pdir.getAbsolutePath() + "'");
091                                    projectController.openProject(pdir);
092                                    ProjectTreeNode gc = ProjectTreeNodeBuilder.createNewNode(ProjectNodeType.G_GLOBAL_CONFIG, CreateEntityAction.getTemplateContent("G_GLOBAL_CONFIG"));
093                                    ProjectTreeNode mbc = ProjectTreeNodeBuilder.createNewNode(ProjectNodeType.MBC_MESSAGE_BROKERS, CreateEntityAction.getTemplateContent("MBC_MESSAGE_BROKERS"));
094                                    ProjectContext.getGlobalInstance().setGlobalConfigName(gc.getName());
095                                    ProjectContext.getGlobalInstance().setMessageBrokerConfigName(mbc.getName());
096                                    projectController.reloadProject();
097                                    dispose();
098                            }
099                    }));
100                    JButton cancelButton = new JButton(new AbstractAction("Cancel") {
101                            public void actionPerformed(java.awt.event.ActionEvent e) {
102                                    dispose();
103                            };
104                    });
105                    public NewProjectDlg(ProjectController projectController) {
106                            super(projectController.getControllerFrame(), "MActor - New Project");
107                            setModal(true);
108                            setLayout(new BorderLayout());
109                            this.projectController = projectController;
110                            SimpleFormPanel form = new SimpleFormPanel();
111                            form.add(new JLabel("Project Name:"));
112                            form.add(nameText);
113                            form.add(new JLabel("Project Parent Directory:"));
114                            SimpleRowPanel row = new SimpleRowPanel();
115                            row.add(dirText);
116                            row.add(selectDir);
117                            form.add(row);
118                            JPanel bottomPanel = new JPanel(new FlowLayout());
119                            bottomPanel.add(okButton);
120                            bottomPanel.add(cancelButton);
121                            JPanel p = new JPanel(new BorderLayout());
122                            p.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
123                            p.add(form, BorderLayout.CENTER);
124                            p.add(bottomPanel, BorderLayout.SOUTH);
125                            add(p, BorderLayout.CENTER);
126                            pack();
127                    }
128            }
129    }