1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.aequo.gui;
14
15 import java.awt.Color;
16 import java.awt.Component;
17 import java.awt.Dimension;
18 import java.awt.Graphics;
19 import java.awt.Graphics2D;
20 import java.awt.Paint;
21 import java.awt.Rectangle;
22 import java.awt.TexturePaint;
23 import java.awt.event.MouseEvent;
24 import java.awt.event.MouseListener;
25 import java.awt.image.BufferedImage;
26
27 import javax.swing.DefaultListCellRenderer;
28 import javax.swing.JComponent;
29 import javax.swing.JLabel;
30 import javax.swing.JList;
31 import javax.swing.JPanel;
32 import javax.swing.JPopupMenu;
33 import javax.swing.ListSelectionModel;
34
35 import org.abstracthorizon.aequo.CompareEntry;
36 import org.abstracthorizon.aequo.CompareModel;
37 import org.abstracthorizon.aequo.GlobalContext;
38
39
40
41
42
43
44
45
46 public class ListComparePanel<T, CompareEntryType extends CompareEntry<T>> extends AbstractComparePanel<T, CompareEntryType> {
47
48
49
50
51
52
53 public ListComparePanel(GlobalContext context, CompareModel<T, CompareEntryType> compareModel) {
54 super(context, compareModel);
55 }
56
57
58
59
60 public void initialise() {
61 super.initialise();
62 }
63
64
65
66
67 protected void createLists() {
68 super.createLists();
69 PopupListener popupListener = new PopupListener();
70
71 leftList.addMouseListener(popupListener);
72 rightList.addMouseListener(popupListener);
73 }
74
75
76
77
78 protected JComponent createLeftComponent() {
79 CompareList list = new CompareList(compareModel);
80
81 list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
82
83 list.setSelectionModel(compareModel.getSelectionModel());
84
85 list.setCellRenderer(new CellRenderer(0));
86
87 return list;
88 }
89
90
91
92
93 protected JComponent createRightComponent() {
94 CompareList list = new CompareList(compareModel);
95
96 list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
97
98 list.setSelectionModel(compareModel.getSelectionModel());
99
100 list.setCellRenderer(new CellRenderer(1));
101
102 return list;
103 }
104
105
106
107
108 protected class CellRenderer extends DefaultListCellRenderer {
109
110
111 protected int column;
112
113
114
115
116
117 public CellRenderer(int column) {
118 this.column = column;
119 }
120
121 @SuppressWarnings("unchecked")
122 public Component getListCellRendererComponent(
123 JList list,
124 Object value,
125 int index,
126 boolean isSelected,
127 boolean cellHasFocus)
128 {
129
130 CompareEntry<T> entry = (CompareEntry<T>)value;
131
132 Component component = super.getListCellRendererComponent(list, entry.getData(column), index, isSelected, cellHasFocus);
133 if (component instanceof JLabel) {
134 JLabel label = (JLabel)component;
135 if (label.getText().equals("")) {
136 label.setText(" ");
137 }
138 }
139
140 if (entry.getData(column) == null) {
141 EMPTY_LINE_COMPONENT.setPreferredSize(new Dimension(10, component.getPreferredSize().height));
142 component = EMPTY_LINE_COMPONENT;
143 }
144
145 byte result = compareModel.get(index).getStatus(column);
146
147
148
149 Color foreGround = foreGrounds.get(result);
150 Color backGround = backGrounds.get(result);
151
152 if (isSelected) {
153 backGround = backGround.darker();
154 }
155
156 component.setForeground(foreGround);
157 component.setBackground(backGround);
158
159
160 return component;
161 }
162 }
163
164
165
166
167
168 protected class PopupListener implements MouseListener {
169
170 public void mouseClicked(MouseEvent e) { }
171
172 public void mouseEntered(MouseEvent e) { }
173
174 public void mouseExited(MouseEvent e) { }
175
176 public void mousePressed(MouseEvent e) {
177 ListSelectionModel selModel = compareModel.getSelectionModel();
178 if (e.isPopupTrigger()) {
179
180 int index = ((CompareList)e.getComponent()).locationToIndex(e.getPoint());
181 if (index >= 0) {
182 if (e.isControlDown()) {
183 if (!selModel.isSelectedIndex(index)) {
184 selModel.addSelectionInterval(index, index);
185 }
186 } else {
187 if (!selModel.isSelectedIndex(index)) {
188 selModel.setSelectionInterval(index, index);
189 }
190 }
191 }
192
193 JPopupMenu popup = leftPopupMenu;
194 if (e.getComponent() == rightList) {
195 popup = rightPopupMenu;
196 updateCachedActionsColumn(1);
197 if (!rightList.hasFocus()) {
198 rightList.grabFocus();
199 }
200 } else {
201 updateCachedActionsColumn(0);
202 if (!leftList.hasFocus()) {
203 leftList.grabFocus();
204 }
205 }
206 updateMenu(popup.getComponents());
207 popup.show(e.getComponent(), e.getX(), e.getY());
208 }
209 }
210
211 public void mouseReleased(MouseEvent e) { }
212
213 }
214
215 public static Component EMPTY_LINE_COMPONENT = new JPanel() {
216
217 {
218 BufferedImage tile = new BufferedImage(12, 12, BufferedImage.TYPE_INT_ARGB);
219 Graphics2D g2 = tile.createGraphics();
220 Color backgroundColor = new Color(0, 0, 0, 0);
221 g2.setBackground(backgroundColor);
222 g2.clearRect(0, 0, 12, 12);
223
224 g2.setColor(Color.DARK_GRAY);
225 g2.drawLine(3, 0, 0, 3);
226 g2.drawLine(9, 0, 0, 9);
227 g2.drawLine(12, 3, 3, 12);
228 g2.drawLine(12, 9, 9, 12);
229
230 paint = new TexturePaint(tile, new Rectangle(0, 0, 12, 12));
231 }
232
233 TexturePaint paint;
234
235 public void paint(Graphics g) {
236 Graphics2D g2 = (Graphics2D)g;
237 Paint oldPaint = g2.getPaint();
238 g2.setBackground(getBackground());
239 g2.clearRect(0, 0, getWidth(), getHeight());
240 g2.setPaint(paint);
241 g.fillRect(0, 0, getWidth(), getHeight());
242 g2.setPaint(oldPaint);
243 }
244 };
245 }