3
3
import javax .swing .DefaultListCellRenderer ;
4
4
import javax .swing .DefaultListModel ;
5
5
import javax .swing .JButton ;
6
+ import javax .swing .JCheckBox ;
7
+ import javax .swing .JLabel ;
6
8
import javax .swing .JList ;
7
9
import javax .swing .JPanel ;
8
10
import javax .swing .JScrollPane ;
9
11
import javax .swing .JViewport ;
10
- import javax .swing .SwingUtilities ;
12
+ import javax .swing .ListCellRenderer ;
11
13
import javax .swing .event .ListDataListener ;
12
14
import javax .swing .event .ListDataEvent ;
13
15
16
18
import pipe .gui .TAPAALGUI ;
17
19
import pipe .gui .swingcomponents .EscapableDialog ;
18
20
21
+ import java .awt .BorderLayout ;
19
22
import java .awt .Component ;
20
23
import java .awt .Dimension ;
21
24
import java .awt .FontMetrics ;
22
25
import java .awt .GridBagLayout ;
23
26
import java .awt .Insets ;
27
+ import java .awt .Rectangle ;
24
28
import java .awt .event .MouseAdapter ;
25
29
import java .awt .event .MouseEvent ;
26
30
import java .awt .GridBagConstraints ;
31
+ import java .util .HashMap ;
27
32
import java .util .List ;
33
+ import java .util .Map ;
28
34
29
35
public class ObservationListDialog extends EscapableDialog {
30
36
private final TimedArcPetriNetNetwork tapnNetwork ;
@@ -73,6 +79,74 @@ public Component getListCellRendererComponent(JList<?> list, Object value, int i
73
79
}
74
80
}
75
81
82
+ private static class CheckboxListRenderer extends JPanel implements ListCellRenderer <Observation > {
83
+ private final JCheckBox checkBox ;
84
+ private final JLabel label ;
85
+ private final EllipsisListCellRenderer ellipsisRenderer ;
86
+
87
+ public CheckboxListRenderer () {
88
+ setLayout (new BorderLayout ());
89
+ checkBox = new JCheckBox ();
90
+ label = new JLabel ();
91
+ ellipsisRenderer = new EllipsisListCellRenderer ();
92
+ add (checkBox , BorderLayout .WEST );
93
+ add (label , BorderLayout .CENTER );
94
+ }
95
+
96
+ @ Override
97
+ public Component getListCellRendererComponent (JList <? extends Observation > list , Observation value , int index , boolean isSelected , boolean cellHasFocus ) {
98
+ ellipsisRenderer .getListCellRendererComponent (list , value , index , isSelected , cellHasFocus );
99
+
100
+ boolean checked = ((CheckboxListModel )list .getModel ()).isChecked (value );
101
+ checkBox .setSelected (checked );
102
+
103
+ setToolTipText ("Select the observation to be monitored during SMC execution" );
104
+
105
+ label .setText (value .toString ());
106
+ label .setToolTipText (ellipsisRenderer .getToolTipText ());
107
+ label .setEnabled (checked );
108
+
109
+ setBackground (isSelected ? list .getSelectionBackground () : list .getBackground ());
110
+ setForeground (isSelected ? list .getSelectionForeground () : list .getForeground ());
111
+ checkBox .setBackground (getBackground ());
112
+ label .setBackground (getBackground ());
113
+
114
+ return this ;
115
+ }
116
+ }
117
+
118
+ private static class CheckboxListModel extends DefaultListModel <Observation > {
119
+ private final Map <Observation , Boolean > checkMap = new HashMap <>();
120
+
121
+ public boolean isChecked (Observation observation ) {
122
+ return checkMap .getOrDefault (observation , observation .isEnabled ());
123
+ }
124
+
125
+ public void toggleChecked (Observation observation ) {
126
+ boolean currentState = isChecked (observation );
127
+ boolean newState = !currentState ;
128
+ checkMap .put (observation , newState );
129
+ observation .setEnabled (newState );
130
+ int index = indexOf (observation );
131
+ if (index >= 0 ) {
132
+ fireContentsChanged (this , index , index );
133
+ }
134
+ }
135
+
136
+ @ Override
137
+ public void addElement (Observation observation ) {
138
+ super .addElement (observation );
139
+ checkMap .put (observation , observation .isEnabled ());
140
+ }
141
+
142
+ @ Override
143
+ public boolean removeElement (Object observation ) {
144
+ boolean removed = super .removeElement (observation );
145
+ checkMap .remove (observation );
146
+ return removed ;
147
+ }
148
+ }
149
+
76
150
private void init () {
77
151
setSize (500 , 350 );
78
152
setResizable (false );
@@ -81,7 +155,8 @@ private void init() {
81
155
82
156
JPanel observationPanel = new JPanel ();
83
157
observationPanel .setLayout (new GridBagLayout ());
84
- DefaultListModel <Observation > listModel = new DefaultListModel <>();
158
+
159
+ CheckboxListModel listModel = new CheckboxListModel ();
85
160
for (Observation observation : observations ) {
86
161
listModel .addElement (observation );
87
162
}
@@ -110,7 +185,7 @@ public void contentsChanged(ListDataEvent e) {
110
185
});
111
186
112
187
JList <Observation > observationList = new JList <>(listModel );
113
- observationList .setCellRenderer (new EllipsisListCellRenderer ());
188
+ observationList .setCellRenderer (new CheckboxListRenderer ());
114
189
115
190
JScrollPane observationScrollPane = new JScrollPane (observationList );
116
191
observationScrollPane .setHorizontalScrollBarPolicy (JScrollPane .HORIZONTAL_SCROLLBAR_NEVER );
@@ -125,8 +200,16 @@ public void contentsChanged(ListDataEvent e) {
125
200
observationList .addMouseListener (new MouseAdapter () {
126
201
@ Override
127
202
public void mouseClicked (MouseEvent e ) {
128
- if (e .getClickCount () == 2 ) {
129
- showEditObservationDialog (observationList .getSelectedIndex (), listModel );
203
+ int index = observationList .locationToIndex (e .getPoint ());
204
+ if (index >= 0 ) {
205
+ Rectangle bounds = observationList .getCellBounds (index , index );
206
+ if (bounds != null && e .getX () < bounds .x + 20 ) {
207
+ Observation observation = listModel .getElementAt (index );
208
+ listModel .toggleChecked (observation );
209
+ observationList .repaint ();
210
+ } else if (e .getClickCount () == 2 ) {
211
+ showEditObservationDialog (observationList .getSelectedIndex (), listModel );
212
+ }
130
213
}
131
214
}
132
215
});
0 commit comments