Line | Hits | Source |
---|---|---|
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 | 0 | @SuppressWarnings("serial") //$NON-NLS-1$ |
44 | 0 | public class ImagePanel extends JPanel { |
45 | 0 | |
46 | 0 | private static final transient Logger logger = Logger |
47 | 0 | .getLogger(ImagePanel.class.getName()); |
48 | 0 | |
49 | 0 | private BufferedImage bufferedImage; |
50 | 0 | |
51 | 0 | private String imagePath; |
52 | 0 | |
53 | 0 | 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 | 0 | |
73 | 0 | private boolean scaleImage = false; |
74 | 0 | |
75 | 0 | public ImagePanel(String imagePath) { |
76 | 0 | this.imagePath = imagePath; |
77 | 0 | initialize(); |
78 | 0 | } |
79 | ||
80 | 0 | protected void initialize() { |
81 | 0 | this.setSize(new Dimension(350, 400)); |
82 | 0 | this.setPreferredSize(new Dimension(350, 400)); |
83 | 0 | this.setMinimumSize(new Dimension(350, 400)); |
84 | 0 | |
85 | 0 | this.setLayout(new BorderLayout()); |
86 | 0 | this.add(getImageScrollPanel(), BorderLayout.CENTER); |
87 | 0 | this.add(getButtonPanel(), BorderLayout.EAST); |
88 | 0 | } |
89 | ||
90 | /** | |
91 | * @return Returns the bufferedImage. | |
92 | */ | |
93 | 0 | protected BufferedImage getBufferedImage() { |
94 | 0 | if (bufferedImage == null) { |
95 | 0 | if (getImagePath() == "" || !new File(getImagePath()).exists()) //$NON-NLS-1$ |
96 | 0 | return null; |
97 | 0 | try { |
98 | 0 | if (!scaleImage) |
99 | 0 | bufferedImage = ImageIO.read(getImageFile()); |
100 | 0 | else { |
101 | 0 | tracker.addImage(bufferedImage = ImageIO |
102 | 0 | .read(getImageFile()), 0); |
103 | 0 | tracker.waitForID(0); |
104 | 0 | this.getDrawingPanel().setImage(bufferedImage); |
105 | 0 | } |
106 | 0 | this.getDrawingPanel().updateUI(); |
107 | 0 | } catch (Exception ex) { |
108 | 0 | logger.error(ex.getMessage()); |
109 | 0 | ex.printStackTrace(); |
110 | 0 | } |
111 | 0 | } |
112 | 0 | return bufferedImage; |
113 | } | |
114 | ||
115 | /** | |
116 | * @return Returns the imageFile. | |
117 | */ | |
118 | 0 | protected File getImageFile() { |
119 | 0 | return new File(getImagePath()); |
120 | } | |
121 | ||
122 | /** | |
123 | * @return Returns the imagePath. | |
124 | */ | |
125 | 0 | protected String getImagePath() { |
126 | 0 | if (imagePath == null) |
127 | 0 | imagePath = ""; //$NON-NLS-1$ |
128 | 0 | return imagePath; |
129 | } | |
130 | ||
131 | /** | |
132 | * @return Returns the buttonPanel. | |
133 | */ | |
134 | 0 | protected JPanel getButtonPanel() { |
135 | 0 | if (buttonPanel == null) { |
136 | 0 | buttonPanel = new JPanel(); |
137 | 0 | buttonPanel.setLayout(new GridLayout(6, 1)); |
138 | 0 | buttonPanel.add(getZoomOriginalImageButton(), null); |
139 | 0 | buttonPanel.add(getZoomBestFitImageButton(), null); |
140 | 0 | buttonPanel.add(getZoomInImageButton(), null); |
141 | 0 | buttonPanel.add(getZoomOutImageButton(), null); |
142 | 0 | buttonPanel.add(getOpenImageButton(), null); |
143 | 0 | buttonPanel.add(getDeleteImageButton(), null); |
144 | 0 | } |
145 | 0 | return buttonPanel; |
146 | } | |
147 | ||
148 | /** | |
149 | * @return Returns the imageScrollPanel. | |
150 | */ | |
151 | 0 | protected JScrollPane getImageScrollPanel() { |
152 | 0 | if (imageScrollPanel == null) { |
153 | 0 | imageScrollPanel = new JScrollPane(); |
154 | 0 | imageScrollPanel.setViewportView(getDrawingPanel()); |
155 | 0 | imageScrollPanel |
156 | 0 | .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); |
157 | 0 | imageScrollPanel |
158 | 0 | .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); |
159 | 0 | } |
160 | 0 | return imageScrollPanel; |
161 | } | |
162 | ||
163 | /** | |
164 | * @return Returns the openImageButton. | |
165 | */ | |
166 | 0 | public JButton getOpenImageButton() { |
167 | 0 | if (openImageButton == null) { |
168 | 0 | openImageButton = new JButton(); |
169 | 0 | openImageButton.setIcon(new ImageIcon(this.getClass().getResource( |
170 | 0 | "/icon/22x22/actions/document-open.png"))); //$NON-NLS-1$ |
171 | 0 | |
172 | 0 | openImageButton.addActionListener(new ActionListener() { |
173 | public void actionPerformed(ActionEvent arg0) { | |
174 | logger.debug("actionPerformed openImageButton"); //$NON-NLS-1$ | |
175 | final JFileChooser chooser = new JFileChooser(); | |
176 | chooser.setDialogTitle(Messages.getString("panel.imagepanel.openimage")); //$NON-NLS-1$ | |
177 | chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); | |
178 | chooser.setApproveButtonText(Messages.getString("button.open")); //$NON-NLS-1$ | |
179 | chooser.setFileFilter(new FileExtensionFilter(Messages.getString("common.filetype.jpg"), //$NON-NLS-1$ | |
180 | Messages.getString("common.file.extension.jpg"))); //$NON-NLS-1$ | |
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 | 0 | } |
192 | 0 | return openImageButton; |
193 | } | |
194 | ||
195 | /** | |
196 | * @return Returns the deleteImageButton. | |
197 | */ | |
198 | 0 | protected JButton getDeleteImageButton() { |
199 | 0 | if (deleteImageButton == null) { |
200 | 0 | deleteImageButton = new JButton(); |
201 | 0 | deleteImageButton.setIcon(new ImageIcon(this.getClass() |
202 | 0 | .getResource("/icon/22x22/actions/edit-delete.png"))); //$NON-NLS-1$ |
203 | 0 | deleteImageButton.addActionListener(new ActionListener() { |
204 | public void actionPerformed(ActionEvent arg0) { | |
205 | logger.debug("actionPerformed deleteImageButton"); //$NON-NLS-1$ | |
206 | imagePath = ""; //$NON-NLS-1$ | |
207 | bufferedImage = null; | |
208 | getDrawingPanel().setImage(null); | |
209 | getImageScrollPanel().updateUI(); | |
210 | } | |
211 | }); | |
212 | 0 | } |
213 | 0 | return deleteImageButton; |
214 | } | |
215 | ||
216 | /** | |
217 | * @return Returns the zoomOriginalImageButton. | |
218 | */ | |
219 | 0 | protected JButton getZoomOriginalImageButton() { |
220 | 0 | if (zoomOriginalImageButton == null) { |
221 | 0 | zoomOriginalImageButton = new JButton("="); //$NON-NLS-1$ |
222 | // zoomOriginalImageButton.setIcon(new | |
223 | 0 | // ImageIcon(this.getClass().getResource("/icon/22x22/actions/zoom-original.png"))); |
224 | 0 | zoomOriginalImageButton.addActionListener(new ActionListener() { |
225 | public void actionPerformed(ActionEvent arg0) { | |
226 | logger.debug("actionPerformed zoomOriginalImageButton"); //$NON-NLS-1$ | |
227 | getDrawingPanel().originalSize(); | |
228 | getImageScrollPanel().updateUI(); | |
229 | } | |
230 | }); | |
231 | 0 | } |
232 | 0 | return zoomOriginalImageButton; |
233 | } | |
234 | ||
235 | /** | |
236 | * @return Returns the zoomBestFitImageButton. | |
237 | */ | |
238 | 0 | protected JButton getZoomBestFitImageButton() { |
239 | 0 | if (zoomBestFitImageButton == null) { |
240 | 0 | zoomBestFitImageButton = new JButton("<>"); //$NON-NLS-1$ |
241 | // zoomBestFitImageButton.setIcon(new | |
242 | 0 | // ImageIcon(this.getClass().getResource("/icon/22x22/actions/zoom-best-fit.png"))); |
243 | 0 | zoomBestFitImageButton.addActionListener(new ActionListener() { |
244 | public void actionPerformed(ActionEvent arg0) { | |
245 | logger.debug("actionPerformed zoomBestFitImageButton"); //$NON-NLS-1$ | |
246 | // int width = (int) | |
247 | // getImageScrollPanel().getVisibleRect().getWidth(); | |
248 | // int height = (int) | |
249 | // getImageScrollPanel().getVisibleRect().getHeight(); | |
250 | // getDrawingPanel().setImage(getBufferedImage()..getScaledInstance(width,height,BufferedImage.SCALE_AREA_AVERAGING)); | |
251 | getImageScrollPanel().updateUI(); | |
252 | } | |
253 | }); | |
254 | 0 | } |
255 | 0 | return zoomBestFitImageButton; |
256 | } | |
257 | ||
258 | /** | |
259 | * @return Returns the zoomInImageButton. | |
260 | */ | |
261 | 0 | protected JButton getZoomInImageButton() { |
262 | 0 | if (zoomInImageButton == null) { |
263 | 0 | zoomInImageButton = new JButton("+"); //$NON-NLS-1$ |
264 | // zoomInImageButton.setIcon(new | |
265 | 0 | // ImageIcon(this.getClass().getResource("/icon/22x22/actions/zoom-in.png"))); |
266 | 0 | zoomInImageButton.addActionListener(new ActionListener() { |
267 | public void actionPerformed(ActionEvent arg0) { | |
268 | logger.debug("actionPerformed zoomInImageButton"); //$NON-NLS-1$ | |
269 | getDrawingPanel().zoomIn(); | |
270 | getImageScrollPanel().updateUI(); | |
271 | } | |
272 | }); | |
273 | 0 | } |
274 | 0 | return zoomInImageButton; |
275 | } | |
276 | ||
277 | /** | |
278 | * @return Returns the zoomOutImageButton. | |
279 | */ | |
280 | 0 | protected JButton getZoomOutImageButton() { |
281 | 0 | if (zoomOutImageButton == null) { |
282 | 0 | zoomOutImageButton = new JButton("-"); //$NON-NLS-1$ |
283 | // zoomOutImageButton.setIcon(new | |
284 | 0 | // ImageIcon(this.getClass().getResource("/icon/22x22/actions/zoom-out.png"))); |
285 | 0 | zoomOutImageButton.addActionListener(new ActionListener() { |
286 | public void actionPerformed(ActionEvent arg0) { | |
287 | logger.debug("actionPerformed zoomOutImageButton"); //$NON-NLS-1$ | |
288 | getDrawingPanel().zoomOut(); | |
289 | getImageScrollPanel().updateUI(); | |
290 | } | |
291 | }); | |
292 | 0 | } |
293 | 0 | return zoomOutImageButton; |
294 | } | |
295 | ||
296 | /** | |
297 | * @return Returns the drawingPanel. | |
298 | */ | |
299 | 0 | protected ImageDrawPanel getDrawingPanel() { |
300 | 0 | if (drawingPanel == null) { |
301 | 0 | drawingPanel = new ImageDrawPanel(getBufferedImage(), 10); |
302 | 0 | } |
303 | 0 | return drawingPanel; |
304 | } | |
305 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |