1 /*
2  * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 package com.sun.hotspot.igv.filter;
25 
26 import com.sun.hotspot.igv.data.Properties;
27 import com.sun.hotspot.igv.graph.Connection.ConnectionStyle;
28 import com.sun.hotspot.igv.graph.*;
29 import java.awt.Color;
30 import java.util.ArrayList;
31 import java.util.List;
32 
33 /**
34  *
35  * @author Thomas Wuerthinger
36  */
37 public class ColorFilter extends AbstractFilter {
38 
39     private List<ColorRule> colorRules;
40     private String name;
41 
ColorFilter(String name)42     public ColorFilter(String name) {
43         this.name = name;
44         colorRules = new ArrayList<>();
45     }
46 
47     @Override
getName()48     public String getName() {
49         return name;
50     }
51 
52     @Override
apply(Diagram diagram)53     public void apply(Diagram diagram) {
54 
55         Properties.PropertySelector<Figure> selector = new Properties.PropertySelector<>(diagram.getFigures());
56         for (ColorRule rule : colorRules) {
57             if (rule.getSelector() != null) {
58                 List<Figure> figures = rule.getSelector().selected(diagram);
59                 for (Figure f : figures) {
60                     applyRule(rule, f);
61                     if (rule.getColor() != null) {
62                         f.setColor(rule.getColor());
63                     }
64                 }
65             } else {
66                 for (Figure f : diagram.getFigures()) {
67                     applyRule(rule, f);
68                 }
69             }
70         }
71     }
72 
applyRule(ColorRule rule, Figure f)73     private void applyRule(ColorRule rule, Figure f) {
74         if (rule.getColor() != null) {
75             f.setColor(rule.getColor());
76         }
77         Color color = rule.getLineColor();
78         ConnectionStyle style = rule.getLineStyle();
79 
80         for (OutputSlot s : f.getOutputSlots()) {
81             for (Connection c : s.getConnections()) {
82                 if (color != null) {
83                     c.setColor(color);
84                 }
85 
86                 if (style != null) {
87                     c.setStyle(style);
88                 }
89             }
90         }
91     }
92 
addRule(ColorRule r)93     public void addRule(ColorRule r) {
94         colorRules.add(r);
95     }
96 
97     public static class ColorRule {
98 
99         private Color color;
100         private Color lineColor;
101         private Connection.ConnectionStyle lineStyle;
102         private Selector selector;
103 
ColorRule(Selector selector, Color c)104         public ColorRule(Selector selector, Color c) {
105             this(selector, c, null, null);
106         }
107 
ColorRule(Selector selector, Color c, Color lineColor, Connection.ConnectionStyle lineStyle)108         public ColorRule(Selector selector, Color c, Color lineColor, Connection.ConnectionStyle lineStyle) {
109             this.selector = selector;
110             this.color = c;
111             this.lineColor = lineColor;
112             this.lineStyle = lineStyle;
113 
114         }
115 
ColorRule(Color c)116         public ColorRule(Color c) {
117             this(null, c);
118         }
119 
getColor()120         public Color getColor() {
121             return color;
122         }
123 
getSelector()124         public Selector getSelector() {
125             return selector;
126         }
127 
getLineColor()128         public Color getLineColor() {
129             return lineColor;
130         }
131 
getLineStyle()132         public Connection.ConnectionStyle getLineStyle() {
133             return lineStyle;
134         }
135     }
136 }
137