KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > util > Models


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.source.util;
21
22 import java.util.List JavaDoc;
23 import javax.swing.ListModel JavaDoc;
24
25 /**
26  *
27  * @author Petr Hrebejk
28  */

29 public final class Models {
30
31     private Models() {
32     }
33
34
35     public static <T> ListModel JavaDoc fromList( List JavaDoc<? extends T> list ) {
36         return new ListListModel<T>( list );
37     }
38
39     /** Creates list model which translates the objects using a factory.
40      */

41     public static <T,P> ListModel JavaDoc translating( ListModel JavaDoc model, Factory<T,P> factory ) {
42         return new TranslatingListModel<T,P>( model, factory );
43     }
44  
45     // Private innerclasses ----------------------------------------------------
46

47     private static class ListListModel<T> implements ListModel JavaDoc {
48     
49         private List JavaDoc<? extends T> list;
50
51         /** Creates a new instance of IteratorList */
52         public ListListModel( List JavaDoc<? extends T> list ) {
53             this.list = list;
54         }
55
56         // List implementataion ------------------------------------------------
57

58         public T getElementAt(int index) {
59             // System.out.println("GE " + index );
60
return list.get( index );
61         }
62
63         public int getSize() {
64             return list.size();
65         }
66
67         public void removeListDataListener(javax.swing.event.ListDataListener JavaDoc l) {
68             // Does nothing - unmodifiable
69
}
70
71         public void addListDataListener(javax.swing.event.ListDataListener JavaDoc l) {
72             // Does nothing - unmodifiable
73
}
74
75     }
76     
77     private static class TranslatingListModel<T,P> implements ListModel JavaDoc {
78     
79         private Factory<T,P> factory;
80         private ListModel JavaDoc listModel;
81
82
83         /** Creates a new instance of IteratorList */
84         public TranslatingListModel( ListModel JavaDoc model, Factory<T,P> factory ) {
85             this.listModel = model;
86             this.factory = factory;
87         }
88
89         // List implementataion ----------------------------------------------------
90

91         //@SuppressWarnings("xlint")
92
public T getElementAt(int index) {
93             P original = (P)listModel.getElementAt( index );
94             return factory.create( original );
95         }
96
97         public int getSize() {
98             return listModel.getSize();
99         }
100
101         public void removeListDataListener(javax.swing.event.ListDataListener JavaDoc l) {
102             // Does nothing - unmodifiable
103
}
104
105         public void addListDataListener(javax.swing.event.ListDataListener JavaDoc l) {
106             // Does nothing - unmodifiable
107
}
108
109
110     }
111     
112 }
113
Popular Tags