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    
005    package graphlab.plugins.main.ui;
006    
007    import graphlab.graph.graph.EdgeModel;
008    import graphlab.graph.graph.SubGraph;
009    import graphlab.graph.graph.VertexModel;
010    import graphlab.platform.Application;
011    import graphlab.plugins.main.GraphData;
012    import graphlab.ui.components.gpropertyeditor.GBasicCellRenderer;
013    import graphlab.ui.components.gpropertyeditor.GCellRenderer;
014    
015    import javax.swing.*;
016    import java.awt.*;
017    import java.awt.event.MouseAdapter;
018    import java.awt.event.MouseEvent;
019    import java.util.HashSet;
020    
021    /**
022     * @author Azin Azadi
023     */
024    public class SubGraphRenderer implements GBasicCellRenderer<SubGraph> {
025        public Component getRendererComponent(SubGraph sd) {
026            final SubGraph mysd = new SubGraph(sd.graph);
027            mysd.vertices = new HashSet<VertexModel>(sd.vertices);
028            mysd.edges = new HashSet<EdgeModel>(sd.edges);
029            mysd.label = sd.label;
030            String txt = "";
031            txt = "<HTML><BODY>";
032            if (mysd.label != null && !mysd.label.equals("")) {
033                txt += "<B>" + mysd.label + ": </B><BR>";
034            }
035    
036            if (mysd.vertices != null && mysd.vertices.size() > 0) {
037                txt = txt + "<B>V: </B> {";
038                for (VertexModel v : mysd.vertices) {
039                    txt = txt + v.getLabel() + ", ";
040                }
041                txt = txt.substring(0, txt.length() - 2) + "}";
042            }
043            if (mysd.edges != null && mysd.edges.size() > 0) {
044                txt += "<BR><B>E: </B> {";
045                for (EdgeModel e : mysd.edges) {
046                    txt = txt + e.getLabel() + ", ";
047                }
048                txt = txt.substring(0, txt.length() - 2) + "}";
049            }
050            txt = txt + "</BODY></HTML>";
051            JLabel l = new JLabel(txt) {
052                @Override
053                public void setForeground(Color fg) {
054                    super.setForeground(fg);
055                    if (fg== GCellRenderer.SELECTED_COLOR)
056                        showOnGraph(mysd);
057                }
058            };
059            l.addMouseListener(new MouseAdapter() {
060                public void mouseClicked(MouseEvent e) {
061                    showOnGraph(mysd);
062                }
063            }
064    
065            );
066            return l;
067        }
068    
069        private void showOnGraph(SubGraph mysd) {
070            GraphData gd = new GraphData(Application.getBlackBoard());
071            if (gd.getGraph() == null && mysd.graph == null)
072                return;
073            if (mysd.graph == null) {
074                gd.select.setSelected(mysd);
075                return;
076            }
077            if (gd.getGraph() == null) {
078                gd.core.showGraph(mysd.graph);
079                gd.select.setSelected(mysd);
080                return;
081            }
082            if (mysd.graph.equals(gd.getGraph())) {
083                gd.select.setSelected(mysd);
084            } else {
085                gd.core.showGraph(mysd.graph);
086                gd.select.setSelected(mysd);
087            }
088        }
089    }