View Javadoc

1   // Copyright 2003, FreeHEP.
2   package org.freehep.graphicsio.exportchooser;
3   
4   import java.awt.Color;
5   import java.awt.event.ActionEvent;
6   import java.awt.event.ActionListener;
7   import java.util.Properties;
8   
9   import javax.swing.JColorChooser;
10  import javax.swing.JDialog;
11  import javax.swing.JLabel;
12  
13  import org.freehep.graphics2d.PrintColor;
14  import org.freehep.graphicsio.PageConstants;
15  import org.freehep.swing.layout.TableLayout;
16  import org.freehep.util.UserProperties;
17  
18  /**
19   * 
20   * @author Mark Donszelmann
21   * @version $Id: BackgroundPanel.java 8584 2006-08-10 23:06:37Z duns $
22   */
23  public class BackgroundPanel extends OptionPanel {
24  
25      private Color initialBackground;
26  
27      private Color background;
28  
29      private JColorChooser colorChooser;
30  
31      private OptionButton colorButton;
32  
33      private String key;
34  
35      public BackgroundPanel(Properties options, String rootKey,
36              boolean hasTransparency) {
37          this(options, rootKey, hasTransparency, "Background");
38      }
39  
40      public BackgroundPanel(Properties options, String rootKey,
41              boolean hasTransparency, String title) {
42          super(title);
43  
44          key = rootKey + "." + PageConstants.BACKGROUND_COLOR;
45  
46          UserProperties user = new UserProperties(options);
47          initialBackground = user.getPropertyColor(key);
48          if (initialBackground == null)
49              initialBackground = Color.WHITE;
50          background = initialBackground;
51  
52          OptionCheckBox backgroundCheck = new OptionCheckBox(options, rootKey
53                  + "." + PageConstants.BACKGROUND, "Background");
54  
55          colorChooser = new JColorChooser(initialBackground);
56          JDialog dialog = JColorChooser.createDialog(this,
57                  "Choose Background Color", true, colorChooser,
58                  new ChangeColorListener(), null);
59  
60          colorButton = new OptionButton(options, rootKey, "Select Color", dialog);
61  
62          backgroundCheck.enables(colorButton);
63  
64          String left = title == null ? TableLayout.VERY_LEFT : TableLayout.LEFT;
65          String right = title == null ? TableLayout.VERY_RIGHT
66                  : TableLayout.RIGHT;
67          if (hasTransparency) {
68              OptionCheckBox transparentCheck = new OptionCheckBox(options,
69                      rootKey + "." + PageConstants.TRANSPARENT, "Transparent");
70              add(left, transparentCheck);
71              add(right, new JLabel());
72              transparentCheck.disables(backgroundCheck);
73          }
74  
75          add(left, backgroundCheck);
76          add(right, colorButton);
77  
78          // trigger initial setting
79          new ChangeColorListener().actionPerformed(null);
80      }
81  
82      public boolean applyChangedOptions(Properties options) {
83          boolean changed = super.applyChangedOptions(options);
84  
85          if (!background.equals(initialBackground)) {
86              UserProperties.setProperty(options, key, background);
87              changed = true;
88          }
89          return changed;
90      }
91  
92      class ChangeColorListener implements ActionListener {
93          public void actionPerformed(ActionEvent event) {
94              background = colorChooser.getColor();
95              colorButton.setBackground(background);
96              colorButton.setForeground(PrintColor.invert(background));
97          }
98      }
99  }