1 /********************************************************************************
2 * InternetCafe is a software solution that helps the management of Cybercafes
3 * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4 * Copyright (C) 2006 Guido Angelo Ingenito
5
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *******************************************************************************/
20 package ui.panel;
21
22 import java.awt.BorderLayout;
23 import java.awt.Dimension;
24 import java.awt.GridLayout;
25 import java.awt.MediaTracker;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import java.awt.image.BufferedImage;
29 import java.io.File;
30
31 import javax.imageio.ImageIO;
32 import javax.swing.ImageIcon;
33 import javax.swing.JButton;
34 import javax.swing.JFileChooser;
35 import javax.swing.JPanel;
36 import javax.swing.JScrollPane;
37
38 import org.apache.log4j.Logger;
39
40 import ui.Messages;
41 import ui.util.FileExtensionFilter;
42
43 @SuppressWarnings("serial")
44 public class ImagePanel extends JPanel {
45
46 private static final transient Logger logger = Logger
47 .getLogger(ImagePanel.class.getName());
48
49 private BufferedImage bufferedImage;
50
51 private String imagePath;
52
53 private MediaTracker tracker = new MediaTracker(this);
54
55 private JScrollPane imageScrollPanel;
56
57 private JPanel buttonPanel;
58
59 private ImageDrawPanel drawingPanel;
60
61 private JButton openImageButton;
62
63 private JButton deleteImageButton;
64
65 private JButton zoomOriginalImageButton;
66
67 private JButton zoomBestFitImageButton;
68
69 private JButton zoomInImageButton;
70
71 private JButton zoomOutImageButton;
72
73 private boolean scaleImage = false;
74
75 public ImagePanel(String imagePath) {
76 this.imagePath = imagePath;
77 initialize();
78 }
79
80 protected void initialize() {
81 this.setSize(new Dimension(350, 400));
82 this.setPreferredSize(new Dimension(350, 400));
83 this.setMinimumSize(new Dimension(350, 400));
84
85 this.setLayout(new BorderLayout());
86 this.add(getImageScrollPanel(), BorderLayout.CENTER);
87 this.add(getButtonPanel(), BorderLayout.EAST);
88 }
89
90 /***
91 * @return Returns the bufferedImage.
92 */
93 protected BufferedImage getBufferedImage() {
94 if (bufferedImage == null) {
95 if (getImagePath() == "" || !new File(getImagePath()).exists())
96 return null;
97 try {
98 if (!scaleImage)
99 bufferedImage = ImageIO.read(getImageFile());
100 else {
101 tracker.addImage(bufferedImage = ImageIO
102 .read(getImageFile()), 0);
103 tracker.waitForID(0);
104 this.getDrawingPanel().setImage(bufferedImage);
105 }
106 this.getDrawingPanel().updateUI();
107 } catch (Exception ex) {
108 logger.error(ex.getMessage());
109 ex.printStackTrace();
110 }
111 }
112 return bufferedImage;
113 }
114
115 /***
116 * @return Returns the imageFile.
117 */
118 protected File getImageFile() {
119 return new File(getImagePath());
120 }
121
122 /***
123 * @return Returns the imagePath.
124 */
125 protected String getImagePath() {
126 if (imagePath == null)
127 imagePath = "";
128 return imagePath;
129 }
130
131 /***
132 * @return Returns the buttonPanel.
133 */
134 protected JPanel getButtonPanel() {
135 if (buttonPanel == null) {
136 buttonPanel = new JPanel();
137 buttonPanel.setLayout(new GridLayout(6, 1));
138 buttonPanel.add(getZoomOriginalImageButton(), null);
139 buttonPanel.add(getZoomBestFitImageButton(), null);
140 buttonPanel.add(getZoomInImageButton(), null);
141 buttonPanel.add(getZoomOutImageButton(), null);
142 buttonPanel.add(getOpenImageButton(), null);
143 buttonPanel.add(getDeleteImageButton(), null);
144 }
145 return buttonPanel;
146 }
147
148 /***
149 * @return Returns the imageScrollPanel.
150 */
151 protected JScrollPane getImageScrollPanel() {
152 if (imageScrollPanel == null) {
153 imageScrollPanel = new JScrollPane();
154 imageScrollPanel.setViewportView(getDrawingPanel());
155 imageScrollPanel
156 .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
157 imageScrollPanel
158 .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
159 }
160 return imageScrollPanel;
161 }
162
163 /***
164 * @return Returns the openImageButton.
165 */
166 public JButton getOpenImageButton() {
167 if (openImageButton == null) {
168 openImageButton = new JButton();
169 openImageButton.setIcon(new ImageIcon(this.getClass().getResource(
170 "/icon/22x22/actions/document-open.png")));
171
172 openImageButton.addActionListener(new ActionListener() {
173 public void actionPerformed(ActionEvent arg0) {
174 logger.debug("actionPerformed openImageButton");
175 final JFileChooser chooser = new JFileChooser();
176 chooser.setDialogTitle(Messages.getString("panel.imagepanel.openimage"));
177 chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
178 chooser.setApproveButtonText(Messages.getString("button.open"));
179 chooser.setFileFilter(new FileExtensionFilter(Messages.getString("common.filetype.jpg"),
180 Messages.getString("common.file.extension.jpg")));
181 if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
182 imagePath = chooser.getSelectedFile().getAbsolutePath();
183 bufferedImage = null;
184 getDrawingPanel().setImage(
185 bufferedImage = getBufferedImage());
186 getImageScrollPanel().updateUI();
187
188 }
189 }
190 });
191 }
192 return openImageButton;
193 }
194
195 /***
196 * @return Returns the deleteImageButton.
197 */
198 protected JButton getDeleteImageButton() {
199 if (deleteImageButton == null) {
200 deleteImageButton = new JButton();
201 deleteImageButton.setIcon(new ImageIcon(this.getClass()
202 .getResource("/icon/22x22/actions/edit-delete.png")));
203 deleteImageButton.addActionListener(new ActionListener() {
204 public void actionPerformed(ActionEvent arg0) {
205 logger.debug("actionPerformed deleteImageButton");
206 imagePath = "";
207 bufferedImage = null;
208 getDrawingPanel().setImage(null);
209 getImageScrollPanel().updateUI();
210 }
211 });
212 }
213 return deleteImageButton;
214 }
215
216 /***
217 * @return Returns the zoomOriginalImageButton.
218 */
219 protected JButton getZoomOriginalImageButton() {
220 if (zoomOriginalImageButton == null) {
221 zoomOriginalImageButton = new JButton("=");
222
223
224 zoomOriginalImageButton.addActionListener(new ActionListener() {
225 public void actionPerformed(ActionEvent arg0) {
226 logger.debug("actionPerformed zoomOriginalImageButton");
227 getDrawingPanel().originalSize();
228 getImageScrollPanel().updateUI();
229 }
230 });
231 }
232 return zoomOriginalImageButton;
233 }
234
235 /***
236 * @return Returns the zoomBestFitImageButton.
237 */
238 protected JButton getZoomBestFitImageButton() {
239 if (zoomBestFitImageButton == null) {
240 zoomBestFitImageButton = new JButton("<>");
241
242
243 zoomBestFitImageButton.addActionListener(new ActionListener() {
244 public void actionPerformed(ActionEvent arg0) {
245 logger.debug("actionPerformed zoomBestFitImageButton");
246
247
248
249
250
251 getImageScrollPanel().updateUI();
252 }
253 });
254 }
255 return zoomBestFitImageButton;
256 }
257
258 /***
259 * @return Returns the zoomInImageButton.
260 */
261 protected JButton getZoomInImageButton() {
262 if (zoomInImageButton == null) {
263 zoomInImageButton = new JButton("+");
264
265
266 zoomInImageButton.addActionListener(new ActionListener() {
267 public void actionPerformed(ActionEvent arg0) {
268 logger.debug("actionPerformed zoomInImageButton");
269 getDrawingPanel().zoomIn();
270 getImageScrollPanel().updateUI();
271 }
272 });
273 }
274 return zoomInImageButton;
275 }
276
277 /***
278 * @return Returns the zoomOutImageButton.
279 */
280 protected JButton getZoomOutImageButton() {
281 if (zoomOutImageButton == null) {
282 zoomOutImageButton = new JButton("-");
283
284
285 zoomOutImageButton.addActionListener(new ActionListener() {
286 public void actionPerformed(ActionEvent arg0) {
287 logger.debug("actionPerformed zoomOutImageButton");
288 getDrawingPanel().zoomOut();
289 getImageScrollPanel().updateUI();
290 }
291 });
292 }
293 return zoomOutImageButton;
294 }
295
296 /***
297 * @return Returns the drawingPanel.
298 */
299 protected ImageDrawPanel getDrawingPanel() {
300 if (drawingPanel == null) {
301 drawingPanel = new ImageDrawPanel(getBufferedImage(), 10);
302 }
303 return drawingPanel;
304 }
305 }