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;
021    
022    import java.awt.BorderLayout;
023    import java.awt.FlowLayout;
024    import java.awt.event.ActionEvent;
025    import java.io.File;
026    import java.io.IOException;
027    
028    import javax.swing.AbstractAction;
029    import javax.swing.BorderFactory;
030    import javax.swing.JButton;
031    import javax.swing.JDialog;
032    import javax.swing.JLabel;
033    import javax.swing.JPanel;
034    
035    import org.mactor.framework.MactorException;
036    import org.mactor.framework.spec.ProjectContext;
037    import org.mactor.ui.gui.AsyncAction;
038    import org.mactor.ui.gui.project.editors.FileNodeComboBox;
039    import org.mactor.ui.gui.project.editors.ProjectInternalDirSelector;
040    import org.mactor.ui.gui.project.editors.SimpleFormPanel;
041    
042    public class ProjectSettingsDlg extends JDialog {
043            ProjectSettingsPanel pp;
044            JButton okButton = new JButton(new AsyncAction("Ok", false, new AsyncAction.AsyncRunnable() {
045                    public void run() throws MactorException {
046                            pp.saveSettings();
047                            dispose();
048                    }
049            }));
050            JButton cancelButton = new JButton(new AbstractAction("Cancel") {
051                    public void actionPerformed(ActionEvent arg0) {
052                            dispose();
053                    };
054            });
055            public ProjectSettingsDlg(ProjectController projectController) throws IOException {
056                    super(projectController.getControllerFrame());
057                    setModal(true);
058                    setLayout(new BorderLayout());
059                    setTitle("Project Settings");
060                    this.pp = new ProjectSettingsPanel(projectController);
061                    add(pp, BorderLayout.CENTER);
062                    JPanel buttonPanel = new JPanel(new FlowLayout());
063                    buttonPanel.add(okButton);
064                    buttonPanel.add(cancelButton);
065                    add(buttonPanel, BorderLayout.SOUTH);
066                    pack();
067                    setResizable(false);
068            }
069            static class ProjectSettingsPanel extends JPanel {
070                    File configDir;
071                    ProjectInternalDirSelector configDirSel = new ProjectInternalDirSelector();
072                    FileNodeComboBox gcList = new FileNodeComboBox(ProjectNodeType.G_GLOBAL_CONFIG);
073                    FileNodeComboBox mbList = new FileNodeComboBox(ProjectNodeType.MBC_MESSAGE_BROKERS);
074                    private boolean settingChanged = false;
075                    ProjectController projectController;
076                    public ProjectSettingsPanel(ProjectController projectController) {
077                            super(new BorderLayout());
078                            setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
079                            this.projectController = projectController;
080                            SimpleFormPanel form = new SimpleFormPanel();
081                            form.add(new JLabel("Project Config Directory:"));
082                            form.add(configDirSel);
083                            form.add(new JLabel("Current Global Config:"));
084                            form.add(gcList);
085                            form.add(new JLabel("Current Message Broker Config:"));
086                            form.add(mbList);
087                            add(form, BorderLayout.WEST);
088                            configDir = ProjectContext.getGlobalInstance().getProjectConfigDir();
089                            loadSettings();
090                    }
091                    public void loadSettings() {
092                            gcList.setConfig(projectController.getProjectModel().getRoot());
093                            gcList.setSelectedName(ProjectContext.getGlobalInstance().getGlobalConfigName());
094                            mbList.setConfig(projectController.getProjectModel().getRoot());
095                            mbList.setSelectedName(ProjectContext.getGlobalInstance().getMessageBrokerConfigName());
096                            configDirSel.setPath(ProjectContext.getGlobalInstance().getProjectConfigDir());
097                    }
098                    public void saveSettings() throws MactorException {
099                            ProjectContext.getGlobalInstance().setProjectConfigDir(ProjectContext.getGlobalInstance().getAbsolutePath(configDirSel.getPath()));
100                            ProjectContext.getGlobalInstance().setMessageBrokerConfigName(mbList.getSelectedName());
101                            ProjectContext.getGlobalInstance().setGlobalConfigName(gcList.getSelectedName());
102                            // if
103                            // (!configDir.equals(ProjectContext.getGlobalInstance().getProjectConfigDir()))
104                            projectController.reloadProject();
105                    }
106            }
107    }