1 /*
   2  * $Id$
   3  *
   4  * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.  Oracle designates this
  10  * particular file as subject to the "Classpath" exception as provided
  11  * by Oracle in the LICENSE file that accompanied this code.
  12  *
  13  * This code is distributed in the hope that it will be useful, but WITHOUT
  14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16  * version 2 for more details (a copy is included in the LICENSE file that
  17  * accompanied this code).
  18  *
  19  * You should have received a copy of the GNU General Public License version
  20  * 2 along with this work; if not, write to the Free Software Foundation,
  21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  22  *
  23  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  24  * or visit www.oracle.com if you need additional information or have any
  25  * questions.
  26  */
  27 package com.sun.javatest.tool;
  28 
  29 import com.sun.javatest.util.I18NResourceBundle;
  30 import java.awt.Color;
  31 import java.awt.Component;
  32 import java.awt.GridBagConstraints;
  33 import java.awt.GridBagLayout;
  34 import java.awt.Insets;
  35 import java.awt.event.ActionEvent;
  36 import java.awt.event.ActionListener;
  37 import java.util.Map;
  38 import javax.swing.JButton;
  39 import javax.swing.JColorChooser;
  40 import javax.swing.JLabel;
  41 import javax.swing.JPanel;
  42 
  43 public class ColorPrefsPane extends PreferencesPane {
  44         private JPanel inputColors;
  45         private UIFactory uif;
  46         private I18NResourceBundle i18n;
  47 
  48         public ColorPrefsPane(UIFactory uifactory) {
  49                 this.uif = uifactory;
  50                 i18n = I18NResourceBundle.getBundleForClass(ColorPrefsPane.class);
  51                 setLayout(new GridBagLayout());
  52                 Insets in = new Insets(2, 3, 2, 3);
  53                 GridBagConstraints c = new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, in, 3, 5);
  54 
  55                 inputColors = new JPanel();
  56                 inputColors.setBorder(uif.createTitledBorder("colorprefs.inputcolors")); // TODO i18n
  57                 inputColors.setLayout(new GridBagLayout());
  58 
  59                 GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE, in, 3, 5);
  60 
  61                 UIFactory.Colors[] colors = UIFactory.Colors.values();
  62                 for(int i = 0; i < colors.length; i++) {
  63                         if (!colors[i].isConfigurable())
  64                                 continue;
  65                         String colorName = colors[i].getPreferencesName();
  66                         JLabel label = uif.createLabel("colorprefs." + colorName);
  67                         JButton button = uif.createColorChooseButton(colorName, label, null);
  68                         button.addActionListener(new ActionListener() {
  69                                 private String title;
  70                                 private JButton button;
  71 
  72                                 public void actionPerformed(ActionEvent e) {
  73                                         Color newColor = JColorChooser.showDialog(ColorPrefsPane.this, title, button.getBackground());
  74                                         if(newColor != null)
  75                                                 button.setBackground(newColor);
  76                                 }
  77 
  78                                 public ActionListener init(String title, JButton button) {
  79                                         this.title = title;
  80                                         this.button = button;
  81                                         return this;
  82                                 }
  83                         }.init(i18n.getString("colorprefs." + colorName + ".cctitle"), button));
  84                         gbc.gridx = 0;
  85                         gbc.weightx = 0.3;
  86                         gbc.gridy = i;
  87                         inputColors.add(label, gbc);
  88                         gbc.gridx = 1;
  89                         gbc.weightx = 1;
  90                         gbc.gridy = i;
  91                         inputColors.add(button, gbc);
  92                 }
  93 
  94                 this.add(inputColors, c);
  95 
  96                 JButton defaults = uif.createButton("colorprefs.setdefaults", new ActionListener() {
  97                         public void actionPerformed(ActionEvent e) {
  98                                 Component[] components = inputColors.getComponents();
  99                                 for(Component c: components) {
 100                                         if(c instanceof JButton) {
 101                                                 JButton b = (JButton) c;
 102                                                 String name = b.getName();
 103                                                 b.setBackground(Color.decode(i18n.getString("colorprefs." + name + ".defvalue")));
 104                                         }
 105                                 }
 106                         }
 107                 });
 108 
 109                 c.anchor = GridBagConstraints.EAST;
 110                 c.fill = GridBagConstraints.NONE;
 111                 this.add(defaults, c);
 112 
 113         }
 114 
 115         @Override
 116         public String getText() {
 117                 return i18n.getString("colorprefs.name");
 118         }
 119 
 120         public void save(Map m) {
 121                 super.save(m);
 122                 Component[] components = inputColors.getComponents();
 123                 for(Component c: components) {
 124                         if(c instanceof JButton) {
 125                                 JButton b = (JButton) c;
 126                                 UIFactory.Colors.getColorByPreferencesName(c.getName());
 127                                 String colorCode = String.valueOf(b.getBackground().getRGB());
 128                                 UIFactory.setColorByName(b.getName(), b.getBackground());
 129                                 m.put(b.getName(),colorCode);
 130                         }
 131                 }
 132         }
 133 }