1 /* Copyright (C) 2005-2011 Fabio Riccardi */
2 
3 package com.lightcrafts.ui.operation;
4 
5 import com.lightcrafts.model.LayerMode;
6 import com.lightcrafts.model.Operation;
7 import com.lightcrafts.ui.LightZoneSkin;
8 
9 import static com.lightcrafts.ui.operation.Locale.LOCALE;
10 import com.lightcrafts.utils.xml.XMLException;
11 import com.lightcrafts.utils.xml.XmlNode;
12 import org.jvnet.substance.color.ColorScheme;
13 import org.jvnet.substance.utils.SubstanceCoreUtilities;
14 import org.jvnet.substance.utils.SubstanceSizeUtils;
15 
16 import javax.swing.*;
17 
18 import java.awt.*;
19 import java.awt.image.BufferedImage;
20 import java.beans.PropertyChangeEvent;
21 import java.beans.PropertyChangeListener;
22 import java.beans.PropertyChangeSupport;
23 import java.util.List;
24 
25 class OpFooter extends Box implements PropertyChangeListener {
26     private LayerControls layerControls;
27     private InvertRegionCheckBox invertRegionSwitch;
28     private ColorSelectionControls colorControls;
29     private JTabbedPane tabPane;
30 
propertyChange(PropertyChangeEvent evt)31     public void propertyChange(PropertyChangeEvent evt) {
32         if (tabPane != null && evt.getPropertyName().equals(ColorSelectionControls.COLOR_SELECTION)) {
33             if (evt.getNewValue() == Boolean.TRUE)
34                 tabPane.setIconAt(1, getThemeIcon(null, false));
35             else
36                 tabPane.setIconAt(1, getThemeIcon(orangeScheme, false));
37         }
38         if (layerControls != null && evt.getPropertyName().equals(LayerControls.BLENDING_MODES)) {
39             if (evt.getNewValue() == Boolean.TRUE)
40                 tabPane.setIconAt(0, getThemeIcon(null, true));
41             else
42                 tabPane.setIconAt(0, getThemeIcon(orangeScheme, true));
43         }
44     }
45 
getThemeIcon(ColorScheme colorScheme, boolean square)46     private static Icon getThemeIcon(ColorScheme colorScheme, boolean square) {
47         int iSize = SubstanceSizeUtils.getTitlePaneIconSize();
48         BufferedImage result = SubstanceCoreUtilities.getBlankImage(iSize, iSize);
49         Graphics2D graphics = (Graphics2D) result.getGraphics().create();
50         graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
51                                   RenderingHints.VALUE_ANTIALIAS_ON);
52 
53         Color color1 = (colorScheme == null)
54                        ? Color.red
55                        : colorScheme.getUltraDarkColor();
56         Color color2 = (colorScheme == null)
57                        ? Color.green
58                        : colorScheme.getMidColor();
59         Color color3 = (colorScheme == null)
60                        ? Color.blue
61                        : colorScheme.getExtraLightColor();
62 
63         graphics.setColor(color1);
64         if (square)
65             graphics.fillRect(5, 2, 6, 6);
66         else
67             graphics.fillOval(5, 2, 6, 6);
68         graphics.setColor(color1.darker());
69         if (square)
70             graphics.drawRect(5, 2, 6, 6);
71         else
72             graphics.drawOval(5, 2, 6, 6);
73 
74         graphics.setColor(color2);
75         if (square)
76             graphics.fillRect(1, 9, 6, 6);
77         else
78             graphics.fillOval(1, 9, 6, 6);
79         graphics.setColor(color2.darker());
80         if (square)
81             graphics.drawRect(1, 9, 6, 6);
82         else
83             graphics.drawOval(1, 9, 6, 6);
84 
85         graphics.setColor(color3);
86         if (square)
87             graphics.fillRect(9, 9, 6, 6);
88         else
89             graphics.fillOval(9, 9, 6, 6);
90         graphics.setColor(color3.darker());
91         if (square)
92             graphics.drawRect(9, 9, 6, 6);
93         else
94             graphics.drawOval(9, 9, 6, 6);
95 
96         graphics.dispose();
97         return new ImageIcon(result);
98     }
99 
100     private final PropertyChangeSupport pcs = new PropertyChangeSupport( this );
101 
102     ColorScheme orangeScheme = new LightZoneSkin.CustomColorScheme(LightZoneSkin.Colors.LZOrange);
103 
OpFooter(OpControl control, List<LayerMode> layerModes)104     OpFooter(OpControl control, List<LayerMode> layerModes) {
105         super(BoxLayout.X_AXIS);
106 
107         layerControls = new LayerControls(control, layerModes, pcs);
108         invertRegionSwitch = new InvertRegionCheckBox(control, pcs);
109         colorControls = new ColorSelectionControls(control, pcs);
110 
111         Box blendBox = Box.createVerticalBox();
112         blendBox.add(Box.createVerticalStrut(5));
113         blendBox.add(layerControls);
114         blendBox.add(invertRegionSwitch);
115         blendBox.setBackground(LightZoneSkin.Colors.ToolPanesBackground);
116         layerControls.setAlignmentX( Component.LEFT_ALIGNMENT );
117         invertRegionSwitch.setAlignmentX( Component.LEFT_ALIGNMENT );
118 
119         tabPane = new JTabbedPane();
120         tabPane.setFont(LightZoneSkin.fontSet.getSmallFont());
121         tabPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
122         tabPane.add(LOCALE.get( "ToolSettingsTabName" ), blendBox);
123         tabPane.add(LOCALE.get( "ColorSelectionTabName"), colorControls);
124 
125         tabPane.setIconAt(0, getThemeIcon(orangeScheme, true));
126         tabPane.setIconAt(1, getThemeIcon(orangeScheme, false));
127 
128         add(tabPane, BorderLayout.NORTH);
129 
130         setBackground(LightZoneSkin.Colors.ToolPanesBackground);
131 
132         pcs.addPropertyChangeListener( this );
133     }
134 
isRegionsInverted()135     boolean isRegionsInverted() {
136         return invertRegionSwitch.isRegionsInverted();
137     }
138 
operationChanged(Operation op)139     void operationChanged(Operation op) {
140         layerControls.operationChanged(op);
141         colorControls.operationChanged(op);
142     }
143 
144     private final static String TabIndexTag = "layerControlsIndex";
145 
save(XmlNode node)146     void save(XmlNode node) {
147         layerControls.save(node);
148         invertRegionSwitch.save(node);
149         colorControls.save(node);
150         node.setAttribute(TabIndexTag, Integer.toString(tabPane.getSelectedIndex()));
151     }
152 
restore(XmlNode node)153     void restore(XmlNode node) throws XMLException {
154         layerControls.restore(node);
155         invertRegionSwitch.restore(node);
156         colorControls.restore(node);
157         if (node.hasAttribute(TabIndexTag)) {
158             tabPane.setSelectedIndex(Integer.parseInt(node.getAttribute(TabIndexTag)));
159         }
160     }
161 }
162