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.Frame;
024    import java.awt.event.MouseAdapter;
025    import java.awt.event.MouseEvent;
026    import java.io.File;
027    
028    import javax.swing.BorderFactory;
029    import javax.swing.JOptionPane;
030    import javax.swing.JPanel;
031    import javax.swing.JPopupMenu;
032    import javax.swing.JScrollPane;
033    import javax.swing.SwingUtilities;
034    import javax.swing.event.TreeSelectionListener;
035    import javax.swing.tree.TreePath;
036    
037    import org.mactor.framework.MactorException;
038    import org.mactor.framework.spec.ProjectContext;
039    import org.mactor.ui.gui.ActionsManager;
040    import org.mactor.ui.gui.MenuBuilder;
041    
042    public class ProjectExplorerPanel extends JPanel {
043            ActionsManager am;
044            MenuBuilder mb;
045            ProjectModel model;
046            ProjectTree tree;
047            JPopupMenu treePopupMessageBrokerConfig;
048            JPopupMenu treePopupTest;
049            JPopupMenu treePopupMockBattery;
050            JPopupMenu treePopupTestRun;
051            JPopupMenu treePopupGlobalConfig;
052            ProjectControlListener projectControlListener;
053            String nodeName;
054            TreeSelectionListener tl = new TreeSelectionListener() {
055                    public void valueChanged(javax.swing.event.TreeSelectionEvent e) {
056                            ProjectTreeNode oldNode = null;
057                            ProjectTreeNode newNode = null;
058                            if (e.getNewLeadSelectionPath() != null && e.getNewLeadSelectionPath().getPathCount() > 0) {
059                                    newNode = (ProjectTreeNode) e.getNewLeadSelectionPath().getLastPathComponent();
060                            }
061                            if (e.getOldLeadSelectionPath() != null && e.getOldLeadSelectionPath().getPathCount() > 0) {
062                                    oldNode = (ProjectTreeNode) e.getOldLeadSelectionPath().getLastPathComponent();
063                            }
064                            projectControlListener.onProjectTreeSelectionChanged(newNode, oldNode);
065                            if (oldNode != null && nodeName != null) {
066                                    if (!nodeName.equals(oldNode.getName()))
067                                            model.notifyNodeNameChanged(oldNode);
068                            }
069                            if (newNode != null)
070                                    nodeName = newNode.getName();
071                    };
072            };
073            public ProjectController getProjectController() {
074                    return projectController;
075            }
076            private ProjectContext.ProjectContextListener projectContextListener = new ProjectContext.ProjectContextListener() {
077                    public void onDirty() {
078                            SwingUtilities.invokeLater(new Runnable() {
079                                    public void run() {
080                                            projectControlListener.onProjectDirty();
081                                    }
082                            });
083                    }
084            };
085            ProjectController projectController = new ProjectController() {
086                    public ProjectModel getProjectModel() {
087                            return model;
088                    }
089                    public void closeProject() throws MactorException {
090                            // TODO Auto-generated method stub
091                    }
092                    public Frame getControllerFrame() {
093                            return JOptionPane.getFrameForComponent(ProjectExplorerPanel.this);
094                    }
095                    public void startEditingNode(ProjectTreeNode node) {
096                            tree.startEditingAtPath(new TreePath(ProjectModel.getPath(node)));
097                    }
098                    public void insertNodeIntoNode(ProjectTreeNode destNode, ProjectTreeNode nodeToInsert) throws MactorException {
099                            model.insertInto(destNode, nodeToInsert);
100                            tree.expandPath(new TreePath(ProjectModel.getPath(destNode)));
101                            int row = tree.getRowForPath(new TreePath(ProjectModel.getPath(nodeToInsert)));
102                            tree.setSelectionRow(row);
103                    }
104                    public void insertNodeAfterNode(ProjectTreeNode destNode, ProjectTreeNode nodeToInsert) throws MactorException {
105                            model.insertAfter(destNode, nodeToInsert);
106                            tree.expandPath(new TreePath(ProjectModel.getPath(nodeToInsert)));
107                            tree.setSelectionRow(tree.getRowForPath(new TreePath(ProjectModel.getPath(nodeToInsert))));
108                    }
109                    public void openProject(File path) throws MactorException {
110                            ProjectContext.getGlobalInstance().setProjectDir(path);
111                            reloadProject();
112                            projectControlListener.onProjectLoaded();
113                    }
114                    public void reloadProject() throws MactorException {
115                            model.reload(ProjectContext.getGlobalInstance().getProjectDir());
116                            tree.setSelectionRow(0);
117                            projectControlListener.onProjectLoaded();
118                            ProjectContext.getGlobalInstance().setDirty(false);
119                            ProjectContext.getGlobalInstance().setListener(projectContextListener);
120                    }
121                    public void deleteNode(ProjectTreeNode node) throws MactorException {
122                            model.delete(node);
123                    }
124                    public void requestGuiAction(ProjectTreeNode node, String action) {
125                            projectControlListener.onRequestGuiAction(node, action);
126                    }
127            };
128            public MenuBuilder getMenuBuilder() {
129                    return mb;
130            }
131            public ProjectExplorerPanel(ProjectControlListener projectControlListener) throws MactorException {
132                    super(new BorderLayout());
133                    this.projectControlListener = projectControlListener;
134                    model = new ProjectModel(ProjectContext.getGlobalInstance().getProjectDir());
135                    tree = new ProjectTree(model);
136                    am = new ActionsManager(projectController, tree);
137                    mb = new MenuBuilder(am);
138                    setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
139                    JScrollPane treeSp = new JScrollPane(tree);
140                    add(treeSp, BorderLayout.CENTER);
141                    tree.setEditable(true);
142                    tree.addTreeSelectionListener(tl);
143                    treePopupGlobalConfig = mb.buildPopuMenu("Global Configuration");
144                    treePopupMessageBrokerConfig = mb.buildPopuMenu("Message Broker Configuration");
145                    treePopupTest = mb.buildPopuMenu("Test");
146                    treePopupMockBattery = mb.buildPopuMenu("Mock Battery");
147                    treePopupTestRun = mb.buildPopuMenu("Test Run");
148                    tree.setInvokesStopCellEditing(true);
149                    tree.addMouseListener(new MouseAdapter() {
150                            public void mousePressed(MouseEvent evt) {
151                                    int row = tree.getRowForLocation(evt.getX(), evt.getY());
152                                    tree.setSelectionRow(row);
153                                    showPopup(evt);
154                            }
155                            public void mouseReleased(MouseEvent evt) {
156                                    if (evt.isPopupTrigger()) {
157                                            showPopup(evt);
158                                    }
159                            }
160                    });
161                    tree.setSelectionRow(0);
162            }
163            private void showPopup(final MouseEvent evt) {
164                    if (evt.isPopupTrigger()) {
165                            TreePath p = tree.getSelectionPath();
166                            if (p != null && p.getLastPathComponent() instanceof ProjectTreeNode) {
167                                    ProjectTreeNode node = (ProjectTreeNode) p.getLastPathComponent();
168                                    ProjectTreeNode orgNode = ProjectTreeUtil.navigateToOrganizationNode(node);
169                                    if (orgNode != null) {
170                                            if (ProjectNodeType.PROJECT_GLOBAL_CONFIG.equals(orgNode.getNodeType()))
171                                                    treePopupGlobalConfig.show(evt.getComponent(), evt.getX(), evt.getY());
172                                            else if (ProjectNodeType.PROJECT_MESSAGE_BROKER_CONFIG.equals(orgNode.getNodeType()))
173                                                    treePopupMessageBrokerConfig.show(evt.getComponent(), evt.getX(), evt.getY());
174                                            else if (ProjectNodeType.PROJECT_TEST.equals(orgNode.getNodeType()))
175                                                    treePopupTest.show(evt.getComponent(), evt.getX(), evt.getY());
176                                            else if (ProjectNodeType.PROJECT_MOCK_BATTERY.equals(orgNode.getNodeType()))
177                                                    treePopupMockBattery.show(evt.getComponent(), evt.getX(), evt.getY());
178                                            else if (ProjectNodeType.PROJECT_TEST_RUN.equals(orgNode.getNodeType()))
179                                                    treePopupTestRun.show(evt.getComponent(), evt.getX(), evt.getY());
180                                    }
181                            }
182                    }
183            }
184    }