Coverage Report - org.xmlhammer.gui.preferences.LookAndFeelPanel
 
Classes in this File Line Coverage Branch Coverage Complexity
LookAndFeelPanel
79% 
92% 
0
 
 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  0
 public class LookAndFeelPanel extends PreferencesPanel {
 52  
     private static final long serialVersionUID = -7714462525621553549L;
 53  
 
 54  22
     private ArrayList<JRadioButton> buttons = null;
 55  22
     private JRadioButton otherButton = null;
 56  22
     private JTextField otherField = null;
 57  
 
 58  
         /**
 59  
          * @param name
 60  
          */
 61  
         public LookAndFeelPanel() {
 62  22
                 super("Look and Feel", "preferences.laf");
 63  
                 
 64  22
         JPanel preferences = new JPanel(new FormLayout( 5, 2));
 65  22
         preferences.setBorder(new EmptyBorder(0, 10, 0, 0));
 66  
         
 67  22
         JLabel label = new JLabel("Restart the XML Hammer application for look and feel changes to take effect.");
 68  22
         label.setFont(label.getFont().deriveFont(Font.PLAIN));
 69  
         
 70  22
         preferences.add(label, FormLayout.FULL);
 71  22
         preferences.add(Box.createVerticalStrut(10), FormLayout.FULL);
 72  
 
 73  22
         ButtonGroup group = new ButtonGroup();
 74  22
         buttons = new ArrayList<JRadioButton>();
 75  22
         LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
 76  
         
 77  110
         for (LookAndFeelInfo info : infos) {
 78  88
             JRadioButton button = new JRadioButton(info.getName());
 79  88
             buttons.add(button);
 80  88
             group.add(button);
 81  
 
 82  88
             preferences.add(button, FormLayout.FULL);
 83  
         }
 84  
         
 85  22
         JPanel otherPanel = new JPanel(new BorderLayout());
 86  22
         otherField = new JTextField();
 87  22
         otherField.setEditable(false);
 88  
 
 89  22
         otherButton = new JRadioButton("Other");
 90  22
         group.add(otherButton);
 91  22
         otherButton.addItemListener(new ItemListener() {
 92  22
             public void itemStateChanged(ItemEvent arg0) {
 93  0
                 otherField.setEditable(otherButton.isSelected());
 94  0
             }
 95  
         });
 96  
         
 97  22
         otherPanel.add(otherButton, BorderLayout.WEST);
 98  22
         otherPanel.add(otherField, BorderLayout.CENTER);
 99  
 
 100  22
         preferences.add(otherPanel, FormLayout.FULL_FILL);
 101  
 
 102  22
         setCenterPane( preferences);
 103  22
         }
 104  
         
 105  
         public void setLookAndFeel(String lookandfeel) {
 106  22
         otherField.setText("");
 107  
 
 108  22
         if (lookandfeel == null || lookandfeel.length() == 0) {
 109  22
             LookAndFeel laf = UIManager.getLookAndFeel();
 110  22
             lookandfeel = laf.getClass().getName();
 111  
         }
 112  
 
 113  22
         LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
 114  
         
 115  22
         for (LookAndFeelInfo info : infos) {
 116  22
             if (info.getClassName().equals(lookandfeel)) {
 117  22
                 setSelectedButton(info.getName());
 118  22
                 return;
 119  
             }
 120  
         }
 121  
         
 122  0
         otherField.setText(lookandfeel);
 123  0
         otherField.setCaretPosition(0);
 124  0
         otherButton.setSelected(true);
 125  0
         }
 126  
         
 127  
         public String getLookAndFeel() {
 128  22
         LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
 129  22
         JRadioButton button = getSelectedButton();
 130  
         
 131  22
         if (button != null) {
 132  22
             for (LookAndFeelInfo info : infos) {
 133  22
                 if (info.getName().equals(button.getText())) {
 134  22
                    return info.getClassName(); 
 135  
                 }
 136  
             }
 137  0
         } else if (otherButton.isSelected()) {
 138  0
             return otherField.getText();
 139  
         }
 140  
 
 141  0
         return null;
 142  
         }
 143  
         
 144  
     private JRadioButton getSelectedButton() {
 145  22
         for (JRadioButton button : buttons) {
 146  22
             if (button.isSelected()) {
 147  22
                 return button;
 148  
             }
 149  0
         }
 150  
         
 151  0
         return null;
 152  
     }
 153  
 
 154  
     private void setSelectedButton(String name) {
 155  22
         for (JRadioButton button : buttons) {
 156  22
             if (name.equals(button.getText())) {
 157  22
                 button.setSelected(true);
 158  22
                 return;
 159  
             }
 160  0
         }
 161  0
     }
 162  
 }