KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > propedit > LookAndFeelEditor


1 package net.suberic.pooka.gui.propedit;
2
3 import net.suberic.util.gui.propedit.*;
4 import javax.swing.*;
5 import net.suberic.util.*;
6 import java.awt.FlowLayout JavaDoc;
7 import java.awt.event.*;
8 import java.util.*;
9
10 /**
11  * A property editor that gives a choice for each available look and feel.
12  */

13 public class LookAndFeelEditor extends ListEditorPane {
14
15   /**
16    * Creates the JComboBox with the appropriate options.
17    */

18   protected JComboBox createComboBox() {
19     originalValue = manager.getProperty(property, "");
20
21     Vector items = loadLnF();
22
23     JComboBox jcb = new JComboBox(items);
24
25     if (debug)
26       System.out.println("setting to original index " + originalIndex);
27
28     jcb.setSelectedIndex(originalIndex);
29
30     jcb.addItemListener(new ItemListener() {
31     public void itemStateChanged(ItemEvent e) {
32       int newIndex = inputField.getSelectedIndex();
33       if (newIndex != currentIndex) {
34         String JavaDoc newValue = (String JavaDoc)labelToValueMap.get(inputField.getSelectedItem());
35         try {
36           firePropertyChangingEvent(newValue);
37           firePropertyChangedEvent(newValue);
38           currentIndex = newIndex;
39         } catch (PropertyValueVetoException pvve) {
40           manager.getFactory().showError(inputField, "Error changing value " + label.getText() + " to " + newValue + ": " + pvve.getReason());
41           inputField.setSelectedIndex(currentIndex);
42         }
43       }
44     }
45       });
46
47     return jcb;
48   }
49
50   /**
51    * Creates the list of available Look and Feels.
52    *
53    * This goes through and adds each registered look and feel, and then
54    * goes through again and adds any in our custom list that might have
55    * been missed, if available.
56    */

57   public Vector loadLnF() {
58     HashMap foundLnfs = new HashMap();
59     Vector items = new Vector();
60
61     // first add registered look-and-feels
62
UIManager.LookAndFeelInfo[] feels = UIManager.getInstalledLookAndFeels();
63
64     for (int i = 0; i < feels.length; i++) {
65       String JavaDoc itemLabel = feels[i].getName();
66       String JavaDoc itemValue = feels[i].getClassName();
67       
68       try {
69
70     LookAndFeel currentLnf = (LookAndFeel) Class.forName(itemValue).newInstance();
71     if (currentLnf.isSupportedLookAndFeel()) {
72       if (debug)
73         System.out.println("instantiated " + itemValue + "; adding " + itemLabel);
74
75       if (itemValue.equals(originalValue)) {
76         if (debug)
77           System.out.println("matching " + itemValue + "; settingin originalIndex to " + i);
78         originalIndex=items.size();
79         currentIndex=items.size();
80       }
81       items.add(itemLabel);
82       labelToValueMap.put(itemLabel, itemValue);
83       
84       foundLnfs.put(itemValue, null);
85     } else {
86       if (debug)
87         System.out.println("not adding " + itemLabel + "; not supported look and feel.");
88     }
89       } catch (Exception JavaDoc e) {
90     // assume it couldn't be instantiated.
91
if (debug)
92       System.out.println("error instantiating " + itemLabel + "; not adding.");
93       }
94     }
95     
96     // now add configured ones.
97
StringTokenizer tokens;
98     
99     String JavaDoc allowedValuesString = manager.getProperty(editorTemplate + ".allowedValues", "");
100     if (manager.getProperty(allowedValuesString, "") != "") {
101       tokens = new StringTokenizer(manager.getProperty(allowedValuesString, ""), ":");
102       manager.addPropertyEditorListener(allowedValuesString, new ListEditorListener());
103     } else {
104       tokens = new StringTokenizer(manager.getProperty(editorTemplate + ".allowedValues", ""), ":");
105     }
106     
107     while (tokens.hasMoreTokens()) {
108       String JavaDoc currentItem = tokens.nextToken();
109       
110       String JavaDoc itemLabel = manager.getProperty(editorTemplate + ".listMapping." + currentItem.toString() + ".label", "");
111       if (itemLabel.equals(""))
112     itemLabel = currentItem.toString();
113       
114       String JavaDoc itemValue = manager.getProperty(editorTemplate + ".listMapping." + currentItem.toString() + ".value", "");
115       if (itemValue.equals(""))
116     itemValue = currentItem.toString();
117
118       if (! foundLnfs.containsKey(itemValue)) {
119     // try instantiating this.
120
try {
121       LookAndFeel currentLnf = (LookAndFeel) Class.forName(itemValue).newInstance();
122       if (currentLnf.isSupportedLookAndFeel()) {
123         if (debug)
124           System.out.println("instantiated; adding " + itemLabel);
125         if (itemValue.equals(originalValue)) {
126           if (debug)
127         System.out.println("setting originalIndex to " + items.size());
128           originalIndex=items.size();
129           currentIndex=items.size();
130         }
131         items.add(itemLabel);
132         labelToValueMap.put(itemLabel, itemValue);
133       } else {
134         if (debug)
135           System.out.println("not adding " + itemLabel + "; not supported look and feel.");
136       }
137     } catch (Exception JavaDoc e) {
138       // assume it didn't work.
139
if (debug)
140         System.out.println("error instantiating " + itemLabel + "; not adding.");
141
142     }
143       }
144     }
145     
146     if (originalIndex == -1) {
147       items.add(originalValue);
148       labelToValueMap.put(originalValue, originalValue);
149       originalIndex = items.size() - 1;
150     }
151
152     return items;
153   }
154 }
155
Popular Tags