1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package de.mindcrimeilab.xsanalyzer.ui;
19
20 import java.beans.PropertyChangeListener;
21 import java.beans.PropertyChangeSupport;
22
23 import javax.swing.DefaultListModel;
24 import javax.swing.JList;
25 import javax.swing.ListModel;
26 import javax.swing.ListSelectionModel;
27 import javax.swing.event.ListSelectionEvent;
28 import javax.swing.event.ListSelectionListener;
29
30
31
32
33
34
35
36
37 public class MasterListDetailList {
38
39 public final static String PC_MASTERLIST_LISTMODEL = "masterListListModel";
40
41 public final static String PC_DETAILLIST_LISTMODEL = "detailListListModel";
42
43 private final PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
44
45 private final JList masterList;
46
47 private final JList detailList;
48
49 private ListModelFactory listModelFactory;
50
51 public MasterListDetailList() {
52 masterList = new JList();
53 masterList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
54 masterList.addListSelectionListener(new MasterListListSelectionListener());
55 detailList = new JList();
56 }
57
58
59
60
61 public JList getMasterList() {
62 return masterList;
63 }
64
65
66
67
68 public JList getDetailList() {
69 return detailList;
70 }
71
72
73
74
75
76 public void addPropertyChangeListener(PropertyChangeListener listener) {
77 changeSupport.addPropertyChangeListener(listener);
78 }
79
80
81
82
83
84
85
86 public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
87 changeSupport.addPropertyChangeListener(propertyName, listener);
88 }
89
90
91
92
93
94 public void removePropertyChangeListener(PropertyChangeListener listener) {
95 changeSupport.removePropertyChangeListener(listener);
96 }
97
98
99
100
101
102
103
104 public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
105 changeSupport.removePropertyChangeListener(propertyName, listener);
106 }
107
108 public void setMasterListModel(ListModel model) {
109 ListModel oldModel = masterList.getModel();
110 masterList.setModel(model);
111 changeSupport.firePropertyChange(PC_MASTERLIST_LISTMODEL, oldModel, model);
112 }
113
114 private void setDetailListModel(ListModel model) {
115 ListModel oldModel = detailList.getModel();
116 detailList.setModel(model);
117 changeSupport.firePropertyChange(PC_DETAILLIST_LISTMODEL, oldModel, model);
118 }
119
120
121
122
123 public ListModelFactory getListModelFactory() {
124 return listModelFactory;
125 }
126
127
128
129
130
131 public void setListModelFactory(ListModelFactory listModelFactory) {
132 this.listModelFactory = listModelFactory;
133 }
134
135
136
137
138
139
140
141
142
143 private class MasterListListSelectionListener implements ListSelectionListener {
144
145 private final ListModel NULL_MODEL = new DefaultListModel();
146
147
148
149
150
151
152 @Override
153 public void valueChanged(ListSelectionEvent evt) {
154 JList list = (JList) evt.getSource();
155 Object value = list.getSelectedValue();
156 if (null != value) {
157 ListModel model = listModelFactory.createListModel(value);
158 setDetailListModel(model);
159 }
160 else {
161 setDetailListModel(NULL_MODEL);
162 }
163 }
164
165 }
166
167 }