001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    package graphlab.plugins.reports.ui;
005    
006    import graphlab.platform.core.BlackBoard;
007    import graphlab.platform.parameter.Parametrizable;
008    import graphlab.plugins.main.GraphData;
009    import graphlab.plugins.reports.extension.GraphReportExtension;
010    import graphlab.ui.AttributeSetView;
011    import graphlab.ui.ParameterShower;
012    import graphlab.ui.PortableNotifiableAttributeSetImpl;
013    import graphlab.ui.components.gpropertyeditor.GPropertyEditor;
014    import graphlab.ui.components.gpropertyeditor.GPropertyTableModel;
015    
016    import javax.swing.*;
017    import java.awt.*;
018    import java.awt.event.ActionEvent;
019    import java.awt.event.ActionListener;
020    import java.awt.event.MouseAdapter;
021    import java.awt.event.MouseEvent;
022    import java.util.ArrayList;
023    import java.util.HashMap;
024    
025    /**
026     * @author azin azadi
027    
028     */
029    public class ReportsUI implements ActionListener {
030        JButton recalculateBtn = new JButton("Recalculate");
031        GPropertyEditor propEd = new GPropertyEditor();
032        GraphData graphData;
033        ArrayList<GraphReportExtension> reports = new ArrayList<GraphReportExtension>();
034        HashMap<String, GraphReportExtension> reportByName = new HashMap<String, GraphReportExtension>();
035    
036        PortableNotifiableAttributeSetImpl reportResults = new PortableNotifiableAttributeSetImpl();
037    
038        //    private blackboard blackboard;
039        JFrame frm = new JFrame("Reports");
040    
041        public ReportsUI(BlackBoard b, boolean init) {
042            super();
043            if (init)
044                initComponents();
045            propEd.connect(reportResults);
046            BlackBoard blackboard = b;
047            graphData = new GraphData(blackboard);
048        }
049    
050        private void initComponents() {
051            BorderLayout lom = new BorderLayout(0, 0);
052            frm.setLayout(lom);
053            frm.add(initWrapper());
054            frm.pack();
055        }
056    
057        JPanel initWrapper() {
058            JPanel wrapper = new JPanel(new BorderLayout(0, 0));
059            wrapper.setPreferredSize(new Dimension(150, 100));
060            wrapper.add(propEd, BorderLayout.CENTER);
061            wrapper.add(recalculateBtn, BorderLayout.SOUTH);
062            recalculateBtn.addActionListener(this);
063    //        recalculateBtn.setSize(100, 20);
064    //        propEd.getTable().addColumn(new TableColumn(2,150));
065            propEd.getTable().addNotify();
066            propEd.getTable().addMouseListener(new MouseAdapter() {
067                public void mouseClicked(MouseEvent e) {
068                    if (e.getClickCount() == 2) {
069                        int row = propEd.getTable().rowAtPoint(e.getPoint());
070                        GPropertyTableModel gtm = (GPropertyTableModel) propEd.getTable().getModel();
071                        ParameterShower ps = new ParameterShower();
072                        String name = (String) gtm.getValueAt(row, 0);
073                        GraphReportExtension o = reportByName.get(name);
074                        if (o instanceof Parametrizable)
075                            if (ps.xshow(o)) {
076                                reCalculateReport(name);
077                            }
078                    }
079                }
080            });
081            return wrapper;
082        }
083    //    public Component getComponent(blackboard b) {
084    //        return ;
085    //    }
086    
087        public void addReport(GraphReportExtension gre) {
088            reports.add(gre);
089    //        reCalculateReports();
090        }
091    
092        public void show() {
093            frm.setVisible(true);
094            reCalculateReports();
095        }
096    
097        public void hide() {
098            frm.setVisible(false);
099        }
100    
101        //recalc button pressed
102        public void actionPerformed(ActionEvent e) {
103            new Thread() {
104                public void run() {
105                    reCalculateReports();
106                }
107            }.start();
108        }
109    
110        public void reCalculateReports() {
111            if (graphData.getGraph() == null)
112                return;
113            for (GraphReportExtension gre : reports) {
114                String name = gre.getName();
115                reportResults.put(name, gre.calculate(graphData));
116                reportByName.put(name, gre);
117                AttributeSetView view = reportResults.getView();
118                view.setEditable(name, false);
119                view.setDescription(name, gre.getDescription());
120            }
121            propEd.connect(reportResults);
122        }
123    
124        public void reCalculateReport(String reportName) {
125            GraphReportExtension gre = reportByName.get(reportName);
126            reportResults.put(reportName, gre.calculate(graphData));
127        }
128    }