KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > customizer > JavaPlatformComponentFactory


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.apisupport.project.ui.customizer;
21
22 import java.awt.Component JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.text.Collator JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.Comparator JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.SortedSet JavaDoc;
32 import java.util.TreeSet JavaDoc;
33 import javax.swing.ComboBoxModel JavaDoc;
34 import javax.swing.DefaultListCellRenderer JavaDoc;
35 import javax.swing.JLabel JavaDoc;
36 import javax.swing.JList JavaDoc;
37 import javax.swing.ListCellRenderer JavaDoc;
38 import javax.swing.event.ListDataEvent JavaDoc;
39 import javax.swing.event.ListDataListener JavaDoc;
40 import javax.swing.plaf.UIResource JavaDoc;
41 import org.netbeans.api.java.platform.JavaPlatform;
42 import org.netbeans.api.java.platform.JavaPlatformManager;
43 import org.openide.util.WeakListeners;
44
45 /**
46  * GUI tools for working with the Java platform.
47  * @author Jesse Glick
48  */

49 public class JavaPlatformComponentFactory {
50     
51     private JavaPlatformComponentFactory() {}
52     
53     public static ComboBoxModel JavaDoc/*<JavaPlatform>*/ javaPlatformListModel() {
54         return new Model JavaDoc();
55     }
56     
57     public static ListCellRenderer JavaDoc/*<JavaPlatform>*/ javaPlatformListCellRenderer() {
58         return new Renderer JavaDoc();
59     }
60     
61     private static final class Model implements ComboBoxModel JavaDoc/*<JavaPlatform>*/, PropertyChangeListener JavaDoc, Comparator JavaDoc<JavaPlatform> {
62         
63         private static final Collator JavaDoc COLL = Collator.getInstance();
64         private static final JavaPlatformManager mgr = JavaPlatformManager.getDefault();
65         private final SortedSet JavaDoc<JavaPlatform> platforms = new TreeSet JavaDoc(this);
66         private final List JavaDoc<ListDataListener JavaDoc> listeners = new ArrayList JavaDoc();
67         private JavaPlatform selected;
68         
69         public Model() {
70             refresh();
71             mgr.addPropertyChangeListener(WeakListeners.propertyChange(this, mgr));
72         }
73         
74         private void refresh() {
75             platforms.clear();
76             platforms.addAll(Arrays.asList(mgr.getInstalledPlatforms()));
77         }
78
79         public int getSize() {
80             return platforms.size();
81         }
82
83         public Object JavaDoc getElementAt(int index) {
84             return (JavaPlatform) new ArrayList JavaDoc(platforms).get(index);
85         }
86
87         public void addListDataListener(ListDataListener JavaDoc l) {
88             listeners.add(l);
89         }
90
91         public void removeListDataListener(ListDataListener JavaDoc l) {
92             listeners.remove(l);
93         }
94         
95         public void setSelectedItem(Object JavaDoc sel) {
96             if (sel != selected) {
97                 selected = (JavaPlatform) sel;
98                 fireChange();
99             }
100         }
101
102         public Object JavaDoc getSelectedItem() {
103             return selected;
104         }
105         
106         private void fireChange() {
107             ListDataEvent JavaDoc ev = new ListDataEvent JavaDoc(this, ListDataEvent.CONTENTS_CHANGED, 0, 0);
108             Iterator JavaDoc it = new ArrayList JavaDoc(listeners).iterator();
109             while (it.hasNext()) {
110                 ((ListDataListener JavaDoc) it.next()).contentsChanged(ev);
111             }
112         }
113
114         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
115             refresh();
116             fireChange();
117         }
118
119         public int compare(JavaPlatform p1, JavaPlatform p2) {
120             int res = COLL.compare(p1.getDisplayName(), p2.getDisplayName());
121             if (res != 0) {
122                 return res;
123             } else {
124                 String JavaDoc id1 = ModuleProperties.getPlatformID(p1);
125                 String JavaDoc id2 = ModuleProperties.getPlatformID(p2);
126                 if (id1 != null && id2 != null) {
127                     return id1.compareTo(id2);
128                 } else {
129                     return System.identityHashCode(p1) - System.identityHashCode(p2);
130                 }
131             }
132         }
133
134     }
135     
136     private static final class Renderer extends JLabel JavaDoc implements ListCellRenderer JavaDoc, UIResource JavaDoc /*<JavaPlatform>*/ {
137         
138         public Renderer() {
139             setOpaque(true);
140         }
141         
142         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
143             // #93658: GTK needs name to render cell renderer "natively"
144
setName("ComboBox.listRenderer"); // NOI18N
145

146             String JavaDoc name = value != null ? ((JavaPlatform) value).getDisplayName() : null;
147             setText(name);
148             
149             if ( isSelected ) {
150                 setBackground(list.getSelectionBackground());
151                 setForeground(list.getSelectionForeground());
152             }
153             else {
154                 setBackground(list.getBackground());
155                 setForeground(list.getForeground());
156             }
157             
158             return this;
159         }
160
161         // #93658: GTK needs name to render cell renderer "natively"
162
public String JavaDoc getName() {
163             String JavaDoc name = super.getName();
164             return name == null ? "ComboBox.renderer" : name; // NOI18N
165
}
166         
167     }
168     
169 }
170
Popular Tags