View Javadoc

1   // $Id: MasterListDetailList.java 165 2009-05-28 21:46:38Z agony $
2   /*
3    * xsAnalyzer - XML schema analyzing tool. Copyright (C) 2008 Michael Engelhardt
4    * 
5    * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
6    * License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
7    * version.
8    * 
9    * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
10   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
11   * 
12   * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
13   * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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   * @author Michael Engelhardt<me@mindcrime-ilab.de>
33   * @author $Author: agony $
34   * @version $Revision: 165 $
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       * @return the masterList
60       */
61      public JList getMasterList() {
62          return masterList;
63      }
64  
65      /**
66       * @return the detailList
67       */
68      public JList getDetailList() {
69          return detailList;
70      }
71  
72      /**
73       * @param listener
74       * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
75       */
76      public void addPropertyChangeListener(PropertyChangeListener listener) {
77          changeSupport.addPropertyChangeListener(listener);
78      }
79  
80      /**
81       * @param propertyName
82       * @param listener
83       * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.lang.String,
84       *      java.beans.PropertyChangeListener)
85       */
86      public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
87          changeSupport.addPropertyChangeListener(propertyName, listener);
88      }
89  
90      /**
91       * @param listener
92       * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
93       */
94      public void removePropertyChangeListener(PropertyChangeListener listener) {
95          changeSupport.removePropertyChangeListener(listener);
96      }
97  
98      /**
99       * @param propertyName
100      * @param listener
101      * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.lang.String,
102      *      java.beans.PropertyChangeListener)
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      * @return the listModelFactory
122      */
123     public ListModelFactory getListModelFactory() {
124         return listModelFactory;
125     }
126 
127     /**
128      * @param listModelFactory
129      *            the listModelFactory to set
130      */
131     public void setListModelFactory(ListModelFactory listModelFactory) {
132         this.listModelFactory = listModelFactory;
133     }
134 
135     /**
136      * Internal class triggers updates of the list selections of the master list to the detail list
137      * 
138      * @author Michael Engelhardt<me@mindcrime-ilab.de>
139      * @author $Author: agony $
140      * @version $Revision: 165 $
141      * 
142      */
143     private class MasterListListSelectionListener implements ListSelectionListener {
144 
145         private final ListModel NULL_MODEL = new DefaultListModel();
146 
147         /*
148          * (non-Javadoc)
149          * 
150          * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
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 }