View Javadoc

1   /*
2    * $Id$
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  
22  
23  package org.xmlhammer.gui.preferences;
24  
25  import java.awt.BorderLayout;
26  import java.awt.Font;
27  import java.awt.event.ItemEvent;
28  import java.awt.event.ItemListener;
29  import java.util.ArrayList;
30  
31  import javax.swing.Box;
32  import javax.swing.ButtonGroup;
33  import javax.swing.JLabel;
34  import javax.swing.JPanel;
35  import javax.swing.JRadioButton;
36  import javax.swing.JTextField;
37  import javax.swing.LookAndFeel;
38  import javax.swing.UIManager;
39  import javax.swing.UIManager.LookAndFeelInfo;
40  import javax.swing.border.EmptyBorder;
41  
42  import org.bounce.FormLayout;
43  
44  /***
45   * Put comment...
46   * 
47   * @version $Revision$, $Date$
48   * @author Edwin Dankert <edankert@gmail.com>
49   */
50  
51  public class LookAndFeelPanel extends PreferencesPanel {
52      private static final long serialVersionUID = -7714462525621553549L;
53  
54      private ArrayList<JRadioButton> buttons = null;
55      private JRadioButton otherButton = null;
56      private JTextField otherField = null;
57  
58  	/***
59  	 * @param name
60  	 */
61  	public LookAndFeelPanel() {
62  		super("Look and Feel", "preferences.laf");
63  		
64          JPanel preferences = new JPanel(new FormLayout( 5, 2));
65          preferences.setBorder(new EmptyBorder(0, 10, 0, 0));
66          
67          JLabel label = new JLabel("Restart the XML Hammer application for look and feel changes to take effect.");
68          label.setFont(label.getFont().deriveFont(Font.PLAIN));
69          
70          preferences.add(label, FormLayout.FULL);
71          preferences.add(Box.createVerticalStrut(10), FormLayout.FULL);
72  
73          ButtonGroup group = new ButtonGroup();
74          buttons = new ArrayList<JRadioButton>();
75          LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
76          
77          for (LookAndFeelInfo info : infos) {
78              JRadioButton button = new JRadioButton(info.getName());
79              buttons.add(button);
80              group.add(button);
81  
82              preferences.add(button, FormLayout.FULL);
83          }
84          
85          JPanel otherPanel = new JPanel(new BorderLayout());
86          otherField = new JTextField();
87          otherField.setEditable(false);
88  
89          otherButton = new JRadioButton("Other");
90          group.add(otherButton);
91          otherButton.addItemListener(new ItemListener() {
92              public void itemStateChanged(ItemEvent arg0) {
93                  otherField.setEditable(otherButton.isSelected());
94              }
95          });
96          
97          otherPanel.add(otherButton, BorderLayout.WEST);
98          otherPanel.add(otherField, BorderLayout.CENTER);
99  
100         preferences.add(otherPanel, FormLayout.FULL_FILL);
101 
102         setCenterPane( preferences);
103 	}
104 	
105 	public void setLookAndFeel(String lookandfeel) {
106         otherField.setText("");
107 
108         if (lookandfeel == null || lookandfeel.length() == 0) {
109             LookAndFeel laf = UIManager.getLookAndFeel();
110             lookandfeel = laf.getClass().getName();
111         }
112 
113         LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
114         
115         for (LookAndFeelInfo info : infos) {
116             if (info.getClassName().equals(lookandfeel)) {
117                 setSelectedButton(info.getName());
118                 return;
119             }
120         }
121         
122         otherField.setText(lookandfeel);
123         otherField.setCaretPosition(0);
124         otherButton.setSelected(true);
125 	}
126 	
127 	public String getLookAndFeel() {
128         LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
129         JRadioButton button = getSelectedButton();
130         
131         if (button != null) {
132             for (LookAndFeelInfo info : infos) {
133                 if (info.getName().equals(button.getText())) {
134                    return info.getClassName(); 
135                 }
136             }
137         } else if (otherButton.isSelected()) {
138             return otherField.getText();
139         }
140 
141         return null;
142 	}
143 	
144     private JRadioButton getSelectedButton() {
145         for (JRadioButton button : buttons) {
146             if (button.isSelected()) {
147                 return button;
148             }
149         }
150         
151         return null;
152     }
153 
154     private void setSelectedButton(String name) {
155         for (JRadioButton button : buttons) {
156             if (name.equals(button.getText())) {
157                 button.setSelected(true);
158                 return;
159             }
160         }
161     }
162 }