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.Rectangle; |
18 | import java.awt.image.BufferedImage; |
19 | import java.io.File; |
20 | import java.io.IOException; |
21 | import java.util.Map; |
22 | |
23 | import javax.imageio.ImageIO; |
24 | import javax.swing.JComponent; |
25 | |
26 | import org.apache.commons.logging.Log; |
27 | import org.apache.commons.logging.LogFactory; |
28 | import org.apache.hivemind.ApplicationRuntimeException; |
29 | |
30 | import net.sourceforge.hiveboard.view.DrawingArea; |
31 | |
32 | public class ImageTool extends AbstractPushButtonTool implements OneShotTool |
33 | { |
34 | static final private Log _logger = LogFactory.getLog(ImageTool.class); |
35 | |
36 | // Used to pass special file chooser panel |
37 | public ImageTool(ImageToolMainPanel chooser, ImageCompositor compositor) |
38 | { |
39 | _compositor = compositor; |
40 | _chooser = chooser; |
41 | } |
42 | |
43 | // Called when user selects the editor |
44 | public DrawingAction execute(JComponent area) |
45 | { |
46 | // Extract board BufferedImage from area |
47 | BufferedImage board = ((DrawingArea) area).getBoardImageModel().getImage(); |
48 | // Initialize file chooser panel |
49 | _chooser.setSourceImage(board); |
50 | // Keep important data before releasing memory for board image |
51 | int width = board.getWidth(); |
52 | int height = board.getHeight(); |
53 | board = null; |
54 | // Display file chooser panel |
55 | if (!_application.showDialog(_chooser)) |
56 | { |
57 | // If user has cancelled |
58 | _chooser.forceReset(); |
59 | return null; |
60 | } |
61 | // Get all user selection: x,y,w,h,image,flags,transparency |
62 | try |
63 | { |
64 | _logger.debug("execute() #1"); |
65 | File input = _chooser.getSelectedFile(); |
66 | BufferedImage image = ImageIO.read(input); |
67 | _logger.debug("execute() #2"); |
68 | ImageToolSettings settings = _chooser.getUserSettings(); |
69 | _logger.debug("execute() #3"); |
70 | // Calculate useful image from user selection (also add alpha channel) |
71 | BufferedImage source = _compositor.computeUsefulSourceImage(image, |
72 | settings, |
73 | width, |
74 | height); |
75 | _logger.debug("execute() #4"); |
76 | // Display source somehow for debugging |
77 | if (_logger.isDebugEnabled()) |
78 | { |
79 | _logger.debug( "execute() useful image size=" + source.getWidth() + |
80 | " x " + source.getHeight()); |
81 | } |
82 | // Create ImageDrawingAction |
83 | Rectangle insertArea = settings.getInsertArea(); |
84 | _logger.debug("execute() #5"); |
85 | return new ImageDrawingAction(insertArea.x, insertArea.y, source, _compositor); |
86 | } |
87 | catch (IOException e) |
88 | { |
89 | // Log it! |
90 | _logger.error("execute()", e); |
91 | throw new ApplicationRuntimeException(e); |
92 | } |
93 | finally |
94 | { |
95 | _chooser.forceReset(); |
96 | _logger.debug("execute() end"); |
97 | } |
98 | } |
99 | |
100 | // This callback method is not useful for this tool which uses no property |
101 | @Override public void setPropertyMap(Map<String, Object> properties) |
102 | { |
103 | } |
104 | |
105 | private ImageCompositor _compositor; |
106 | private ImageToolMainPanel _chooser; |
107 | } |