1 package de.desy.acop.displayers.chart;
2
3 import java.awt.Color;
4 import java.awt.Component;
5 import java.awt.Graphics;
6 import java.awt.GridBagConstraints;
7 import java.awt.GridBagLayout;
8 import java.awt.Insets;
9 import java.awt.event.ActionEvent;
10 import java.awt.event.ActionListener;
11 import java.beans.PropertyChangeEvent;
12 import java.beans.PropertyChangeListener;
13 import java.util.HashMap;
14
15 import javax.swing.DefaultListCellRenderer;
16 import javax.swing.DefaultListModel;
17 import javax.swing.Icon;
18 import javax.swing.JButton;
19 import javax.swing.JDialog;
20 import javax.swing.JLabel;
21 import javax.swing.JList;
22 import javax.swing.JPanel;
23 import javax.swing.JScrollPane;
24 import javax.swing.SwingUtilities;
25
26 import com.cosylab.gui.displayers.DataConsumer;
27
28 import de.desy.acop.displayers.AcopChartReorg;
29
30
31
32
33
34
35
36
37
38
39 public class LegendDialog extends JDialog {
40
41 private class CellRenderer extends DefaultListCellRenderer {
42
43 private class DotIcon implements Icon {
44 private Color color;
45 private DotIcon(Color color) {
46 this.color = color;
47 }
48 public int getIconHeight() {
49 return 16;
50 }
51 public int getIconWidth() {
52 return 16;
53 }
54 public void paintIcon(Component c, Graphics g, int x, int y) {
55 Color col = g.getColor();
56 g.setColor(color);
57 g.fillRect(x, y, 16, 16);
58 g.setColor(col);
59 }
60 }
61 private static final long serialVersionUID = 1L;
62 private HashMap<AcopChartConsumer, Icon> cache = new HashMap<AcopChartConsumer, Icon>();
63 @Override
64 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
65 Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
66 if (value instanceof AcopChartConsumer && c instanceof JLabel) {
67 AcopChartConsumer consumer = (AcopChartConsumer) value;
68 JLabel l = (JLabel)c;
69
70 l.setText(consumer.getName());
71 l.setIcon(getCachedIcon(consumer));
72 }
73 return c;
74
75 }
76
77 private Icon getCachedIcon(AcopChartConsumer consumer) {
78 Icon icon = cache.get(consumer);
79 if (icon == null) {
80 icon = new DotIcon(consumer.getColor());
81 cache.put(consumer, icon);
82 }
83 return icon;
84 }
85 }
86
87
88 private static final long serialVersionUID = 1L;
89 private JPanel contentPanel;
90 private JList legendList;
91 private DefaultListModel model;
92 private JButton closeButton;
93 private AcopChartReorg chart;
94
95
96
97
98
99
100 public LegendDialog(AcopChartReorg chart) {
101 super();
102 this.chart = chart;
103 chart.addPropertyChangeListener(AcopChartReorg.CONSUMERS, new PropertyChangeListener(){
104 public void propertyChange(PropertyChangeEvent evt) {
105 fillList(LegendDialog.this.chart.getConsumers());
106
107 }
108 });
109 initialize();
110 fillList(LegendDialog.this.chart.getConsumers());
111 }
112
113 private void initialize() {
114 setSize(500,500);
115 setLocationRelativeTo(chart);
116 setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
117 setContentPane(getContentPanel());
118 }
119
120 private JPanel getContentPanel() {
121 if (contentPanel == null) {
122 contentPanel = new JPanel(new GridBagLayout());
123 JScrollPane scroll = new JScrollPane(getLegendList());
124 contentPanel.add(scroll, new GridBagConstraints(0,0,1,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH, new Insets(5,5,5,5),0,0));
125 closeButton = new JButton("Close");
126 closeButton.addActionListener(new ActionListener(){
127 public void actionPerformed(ActionEvent e) {
128 dispose();
129 }
130 });
131 contentPanel.add(closeButton, new GridBagConstraints(0,1,1,1,1,0,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5,5,5,5),0,0));
132 }
133 return contentPanel;
134 }
135
136 private JList getLegendList() {
137 if (legendList == null) {
138 legendList = new JList(getListModel());
139 legendList.setCellRenderer(new CellRenderer());
140 }
141 return legendList;
142 }
143
144 private DefaultListModel getListModel() {
145 if (model == null) {
146 model = new DefaultListModel();
147 }
148 return model;
149 }
150
151 private void fillList(final DataConsumer[] consumers) {
152 SwingUtilities.invokeLater(new Runnable(){
153 public void run() {
154 getListModel().clear();
155 for (DataConsumer c : consumers) {
156 getListModel().addElement(c);
157 }
158 }
159 });
160 }
161 }