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.GraphColoring;
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.HashMap;
020    import java.util.Map;
021    
022    /**
023     * @author Azin Azadi
024     */
025    public class GraphColoringRenderer implements GBasicCellRenderer<GraphColoring> {
026        public Component getRendererComponent(GraphColoring coloring) {
027            final GraphColoring myColoring = new GraphColoring(coloring.graph);
028            myColoring.vertexColors = new HashMap<VertexModel, Integer>(coloring.vertexColors);
029            myColoring.edgeColors = new HashMap<EdgeModel, Integer>(coloring.edgeColors);
030            myColoring.label = coloring.label;
031            String txt = "";
032            txt = "<HTML><BODY>";
033            if (myColoring.label != null && !myColoring.label.equals("")) {
034                txt = txt + "<B>" + myColoring.label + ":  </B>";
035            }
036            if (myColoring.vertexColors != null && myColoring.vertexColors.size() > 0) {
037                txt = txt + "<B>Vertex colors: </B> ";
038                for (Map.Entry<VertexModel, Integer> p : myColoring.vertexColors.entrySet()) {
039                    txt = txt + p.getKey().getLabel() + ":" + p.getValue() + " , ";
040                }
041            }
042            if (myColoring.edgeColors != null && myColoring.edgeColors.size() > 0) {
043                txt = txt + "<br/><B>Edge colors: </B> ";
044                for (Map.Entry<EdgeModel, Integer> p : myColoring.edgeColors.entrySet()) {
045                    txt = txt + p.getKey().getLabel() + ":" + p.getValue() + " , ";
046                }
047            }
048            txt = txt + "</BODY></HTML>";
049            
050            JLabel l = new JLabel(txt){
051                @Override
052                public void setForeground(Color fg) {
053                    super.setForeground(fg);
054                    if (fg== GCellRenderer.SELECTED_COLOR)
055                        showOnGraph(myColoring);
056                }
057            };
058            l.addMouseListener(new MouseAdapter() {
059                public void mouseClicked(MouseEvent e) {
060                    showOnGraph(myColoring);
061                }
062            });
063            return l;
064        }
065    
066        private void showOnGraph(GraphColoring myColoring) {
067            GraphData gd = new GraphData(Application.getBlackBoard());
068            if (gd.getGraph() == null && myColoring.graph == null)
069                return;
070            if (myColoring.graph == null) {
071                myColoring.applyColoring();
072                return;
073            }
074            if (gd.getGraph() == null) {
075                gd.core.showGraph(myColoring.graph);
076                myColoring.applyColoring();
077                return;
078            }
079            if (myColoring.graph.equals(gd.getGraph())) {
080                myColoring.applyColoring();
081            } else {
082                gd.core.showGraph(myColoring.graph);
083                myColoring.applyColoring();
084            }
085        }
086    }