1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hiveboard.drawing; |
16 | |
17 | import java.awt.Color; |
18 | import java.awt.event.ItemEvent; |
19 | import java.awt.event.ItemListener; |
20 | import java.awt.image.BufferedImage; |
21 | import java.beans.PropertyChangeEvent; |
22 | import java.beans.PropertyChangeListener; |
23 | import java.io.File; |
24 | import java.io.IOException; |
25 | import java.util.Hashtable; |
26 | |
27 | import javax.imageio.ImageIO; |
28 | import javax.swing.JCheckBox; |
29 | import javax.swing.JComponent; |
30 | import javax.swing.JLabel; |
31 | import javax.swing.JPanel; |
32 | import javax.swing.JSlider; |
33 | import javax.swing.JSpinner; |
34 | import javax.swing.border.CompoundBorder; |
35 | import javax.swing.border.EmptyBorder; |
36 | import javax.swing.border.LineBorder; |
37 | import javax.swing.event.ChangeEvent; |
38 | import javax.swing.event.ChangeListener; |
39 | |
40 | import org.apache.commons.logging.Log; |
41 | import org.apache.commons.logging.LogFactory; |
42 | import org.jdesktop.application.Resource; |
43 | import org.jdesktop.application.ResourceMap; |
44 | |
45 | import net.sourceforge.hiveboard.util.OutlineThumbnail; |
46 | import net.sourceforge.hivegui.application.HiveGuiApplicationMain; |
47 | |
48 | import zappini.designgridlayout.DesignGridLayout; |
49 | |
50 | /** |
51 | * Special accessory panel to be added to ImageToolMainPanel in order to allow |
52 | * user to select parameters of image insertion (transparency, size, scaling...) |
53 | */ |
54 | //CSOFF: ClassDataAbstractionCouplingCheck |
55 | public class ImageToolSettingsPanel extends JPanel |
56 | implements PropertyChangeListener |
57 | { |
58 | static final private Log _logger = LogFactory.getLog(ImageToolSettingsPanel.class); |
59 | |
60 | static final private String NAME = "image-tool-settings"; |
61 | |
62 | public ImageToolSettingsPanel( ImageCompositor compositor, |
63 | boolean realTimePreview) |
64 | { |
65 | super(); |
66 | _handler = new ImageToolSettingsPanelHandler( _displaySettings, |
67 | _thbPreview, |
68 | _txfX, |
69 | _txfY, |
70 | _txfWidth, |
71 | _txfHeight, |
72 | compositor, |
73 | realTimePreview); |
74 | setName(NAME); |
75 | setComponentNames(); |
76 | initComponents(); |
77 | layoutPanel(); |
78 | } |
79 | |
80 | // ======================== START OF PUBLIC API ======================== // |
81 | public void setApplication(HiveGuiApplicationMain application) |
82 | { |
83 | _application = application; |
84 | ResourceMap map = _application.getContext().getResourceMap(getClass()); |
85 | map.injectComponents(this); |
86 | map.injectFields(this); |
87 | _lblTransparent.setText(_transparent); |
88 | _lblOpaque.setText(_opaque); |
89 | Hashtable<Integer, JComponent> labels = new Hashtable<Integer, JComponent>(); |
90 | labels.put(MIN_ALPHA, _lblTransparent); |
91 | labels.put(MAX_ALPHA, _lblOpaque); |
92 | _alpha.setLabelTable(labels); |
93 | _alpha.setPaintLabels(true); |
94 | } |
95 | |
96 | public void setSourceImage(BufferedImage image) |
97 | { |
98 | _handler.setSourceImage(image); |
99 | updateState(); |
100 | } |
101 | |
102 | public void reset() |
103 | { |
104 | _handler.reset(); |
105 | } |
106 | |
107 | public ImageToolSettings getUserSettings() |
108 | { |
109 | initSettings(_settings); |
110 | _settings.setInsertArea(_handler.getSpinnersArea()); |
111 | return _settings; |
112 | } |
113 | // ======================== END OF PUBLIC API ======================== // |
114 | |
115 | // ======================== START OF INTERNAL EVENT CALLBACKS ======================== // |
116 | // Callback notifying a change of image file selection |
117 | public void propertyChange(PropertyChangeEvent e) |
118 | { |
119 | File selectedFile = (File) e.getNewValue(); |
120 | BufferedImage selection = null; |
121 | if (selectedFile != null) |
122 | { |
123 | try |
124 | { |
125 | // Read selected image |
126 | selection = ImageIO.read(selectedFile); |
127 | } |
128 | catch (IOException ex) |
129 | { |
130 | // Log error |
131 | _logger.error("propertyChange() on file " + selectedFile, ex); |
132 | // Show message to user |
133 | _application.showMessage( "io-error-when-reading", |
134 | selectedFile.getName(), |
135 | ex, |
136 | ex.getMessage()); |
137 | selection = null; |
138 | } |
139 | } |
140 | _handler.setSelectedImage(selection); |
141 | } |
142 | |
143 | private void updateState() |
144 | { |
145 | initSettings(_displaySettings); |
146 | _chkAspectRatio.setEnabled(_chkScale.isSelected()); |
147 | _alpha.setEnabled(_chkUserAlpha.isSelected()); |
148 | _handler.stateChanged(); |
149 | } |
150 | |
151 | private void initSettings(ImageToolSettings settings) |
152 | { |
153 | settings.setScaled(_chkScale.isSelected()); |
154 | settings.setKeepAspect(_chkAspectRatio.isSelected()); |
155 | settings.setUserAlpha(_chkUserAlpha.isSelected()); |
156 | settings.setAlpha(_alpha.getValue() / (float) MAX_ALPHA); |
157 | } |
158 | // ======================== END OF INTERNAL EVENT CALLBACKS ======================== // |
159 | |
160 | // ======================== START OF INTERNAL API FOR VIEW ======================== // |
161 | private void setComponentNames() |
162 | { |
163 | _thbPreview.setName(NAME + "-preview"); |
164 | _lblX.setName(NAME + "-x-label"); |
165 | _txfX.setName(NAME + "-x"); |
166 | _lblY.setName(NAME + "-y-label"); |
167 | _txfY.setName(NAME + "-y"); |
168 | _lblWidth.setName(NAME + "-width-label"); |
169 | _txfWidth.setName(NAME + "-width"); |
170 | _lblHeight.setName(NAME + "-height-label"); |
171 | _txfHeight.setName(NAME + "-height"); |
172 | _chkScale.setName(NAME + "-scale"); |
173 | _chkAspectRatio.setName(NAME + "-aspect-ratio"); |
174 | _chkUserAlpha.setName(NAME + "-user-alpha"); |
175 | _alpha.setName(NAME + "-alpha"); |
176 | } |
177 | |
178 | // Initializes components once (view-controller) |
179 | private void initComponents() |
180 | { |
181 | _alpha.setOrientation(JSlider.HORIZONTAL); |
182 | _alpha.setMinorTickSpacing(ALPHA_MIN_TICK); |
183 | _alpha.setMajorTickSpacing(ALPHA_MAX_TICK); |
184 | _alpha.setPaintTicks(true); |
185 | |
186 | _chkScale.setSelected(false); |
187 | _chkAspectRatio.setSelected(true); |
188 | _chkUserAlpha.setSelected(true); |
189 | _alpha.setValue(DEFAULT_ALPHA); |
190 | |
191 | // Add listeners for automatic enabling |
192 | // Add listeners for notifying the zone-drag listener on aspect ratio |
193 | ItemListener listener = new ItemListener() |
194 | { |
195 | public void itemStateChanged(ItemEvent e) |
196 | { |
197 | updateState(); |
198 | } |
199 | }; |
200 | _chkScale.addItemListener(listener); |
201 | _chkUserAlpha.addItemListener(listener); |
202 | _chkAspectRatio.addItemListener(listener); |
203 | |
204 | // Add listeners for real-time preview |
205 | ChangeListener updatePreviewListener = new ChangeListener() |
206 | { |
207 | public void stateChanged(ChangeEvent e) |
208 | { |
209 | updateState(); |
210 | } |
211 | }; |
212 | _alpha.addChangeListener(updatePreviewListener); |
213 | } |
214 | |
215 | private void layoutPanel() |
216 | { |
217 | setBorder(new CompoundBorder( new EmptyBorder(0, 2, 0, 2), |
218 | new LineBorder(Color.GRAY, 1))); |
219 | _thbPreview.setBorder(new LineBorder(Color.GRAY)); |
220 | |
221 | DesignGridLayout layout = new DesignGridLayout(this); |
222 | setLayout(layout); |
223 | |
224 | _lblWidth.setHorizontalAlignment(JLabel.RIGHT); |
225 | _lblHeight.setHorizontalAlignment(JLabel.RIGHT); |
226 | layout.row().center().add(_thbPreview); |
227 | layout.row().height(GAP_THUMBNAIL); |
228 | layout.row().label(_lblX).add(_txfX, 2).add(_lblWidth).add(_txfWidth, 2); |
229 | layout.row().label(_lblY).add(_txfY, 2).add(_lblHeight).add(_txfHeight, 2); |
230 | layout.row().add(_chkScale, _chkAspectRatio); |
231 | layout.row().add(_chkUserAlpha); |
232 | layout.row().center().add(_alpha); |
233 | } |
234 | |
235 | // ======================== END OF INTERNAL API FOR VIEW ======================== // |
236 | |
237 | static final private int PREVIEW_SIZE = 200; |
238 | static final private int GAP_THUMBNAIL = 18; |
239 | static final private int MIN_ALPHA = 0; |
240 | static final private int MAX_ALPHA = 255; |
241 | static final private int DEFAULT_ALPHA = 255; |
242 | static final private int ALPHA_MIN_TICK = 17; |
243 | static final private int ALPHA_MAX_TICK = 85; |
244 | |
245 | private HiveGuiApplicationMain _application; |
246 | private ImageToolSettingsPanelHandler _handler; |
247 | |
248 | // Settings are created only once on the heap |
249 | final private ImageToolSettings _settings = new ImageToolSettings(); |
250 | final private ImageToolSettings _displaySettings = new ImageToolSettings(); |
251 | |
252 | // All Swing components in the panel (besides labels) |
253 | final private OutlineThumbnail _thbPreview = new OutlineThumbnail(PREVIEW_SIZE); |
254 | final private JLabel _lblX = new JLabel(); |
255 | final private JSpinner _txfX = new JSpinner(); |
256 | final private JLabel _lblY = new JLabel(); |
257 | final private JSpinner _txfY = new JSpinner(); |
258 | final private JLabel _lblWidth = new JLabel(); |
259 | final private JSpinner _txfWidth = new JSpinner(); |
260 | final private JLabel _lblHeight = new JLabel(); |
261 | final private JSpinner _txfHeight = new JSpinner(); |
262 | final private JCheckBox _chkScale = new JCheckBox(); |
263 | final private JCheckBox _chkAspectRatio = new JCheckBox(); |
264 | final private JCheckBox _chkUserAlpha = new JCheckBox(); |
265 | final private JSlider _alpha = new JSlider(MIN_ALPHA, MAX_ALPHA); |
266 | final private JLabel _lblTransparent = new JLabel(); |
267 | @Resource(key = NAME + "-alpha-transparent-label") |
268 | private String _transparent; |
269 | final private JLabel _lblOpaque = new JLabel(); |
270 | @Resource(key = NAME + "-alpha-opaque-label") |
271 | private String _opaque; |
272 | } |
273 | //CSON: ClassDataAbstractionCouplingCheck |