|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ImageDialog.java | 34% | 63.3% | 43.5% | 57% |
|
1 |
/*
|
|
2 |
* SimplyHTML, a word processor based on Java, HTML and CSS
|
|
3 |
* Copyright (C) 2002 Ulrich Hilger
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or
|
|
6 |
* modify it under the terms of the GNU General Public License
|
|
7 |
* as published by the Free Software Foundation; either version 2
|
|
8 |
* of the License, or (at your option) any later version.
|
|
9 |
*
|
|
10 |
* This program is distributed in the hope that it will be useful,
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
* GNU General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License
|
|
16 |
* along with this program; if not, write to the Free Software
|
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
18 |
*/
|
|
19 |
|
|
20 |
import java.awt.Frame;
|
|
21 |
import java.awt.Dialog;
|
|
22 |
import java.awt.Container;
|
|
23 |
import java.awt.Dimension;
|
|
24 |
import javax.swing.JPanel;
|
|
25 |
import javax.swing.JLabel;
|
|
26 |
import javax.swing.JList;
|
|
27 |
import javax.swing.JButton;
|
|
28 |
import javax.swing.JTextField;
|
|
29 |
import java.awt.BorderLayout;
|
|
30 |
import java.awt.GridBagLayout;
|
|
31 |
import java.awt.GridBagConstraints;
|
|
32 |
import javax.swing.border.TitledBorder;
|
|
33 |
import javax.swing.border.EtchedBorder;
|
|
34 |
import javax.swing.text.html.HTML;
|
|
35 |
import javax.swing.SwingConstants;
|
|
36 |
import java.awt.event.ActionListener;
|
|
37 |
import java.awt.event.ActionEvent;
|
|
38 |
import javax.swing.JFileChooser;
|
|
39 |
//import com.sun.demo.ExampleFileFilter;
|
|
40 |
import java.io.File;
|
|
41 |
import javax.swing.event.ListSelectionListener;
|
|
42 |
import javax.swing.event.ListSelectionEvent;
|
|
43 |
import javax.swing.ImageIcon;
|
|
44 |
import javax.swing.text.SimpleAttributeSet;
|
|
45 |
import javax.swing.JScrollPane;
|
|
46 |
import java.awt.event.ComponentAdapter;
|
|
47 |
import java.awt.event.ComponentEvent;
|
|
48 |
import java.awt.event.KeyAdapter;
|
|
49 |
import java.awt.event.KeyEvent;
|
|
50 |
import java.awt.event.FocusAdapter;
|
|
51 |
import java.awt.event.FocusEvent;
|
|
52 |
import javax.swing.event.ChangeListener;
|
|
53 |
import javax.swing.event.ChangeEvent;
|
|
54 |
import javax.swing.JSpinner;
|
|
55 |
import java.util.Vector;
|
|
56 |
import java.io.StringWriter;
|
|
57 |
import javax.swing.text.AttributeSet;
|
|
58 |
import javax.swing.JOptionPane;
|
|
59 |
|
|
60 |
/**
|
|
61 |
* A dialog providing an image repository and a way to edit display options
|
|
62 |
* for images from the repository.
|
|
63 |
*
|
|
64 |
* @author Ulrich Hilger
|
|
65 |
* @author Light Development
|
|
66 |
* @author <a href="http://www.lightdev.com">http://www.lightdev.com</a>
|
|
67 |
* @author <a href="mailto:info@lightdev.com">info@lightdev.com</a>
|
|
68 |
* @author published under the terms and conditions of the
|
|
69 |
* GNU General Public License,
|
|
70 |
* for details see file gpl.txt in the distribution
|
|
71 |
* package of this software
|
|
72 |
*
|
|
73 |
* @version stage 11, April 27, 2003
|
|
74 |
*/
|
|
75 |
|
|
76 |
public class ImageDialog extends DialogShell |
|
77 |
implements ActionListener, ListSelectionListener, ChangeListener
|
|
78 |
{ |
|
79 |
/** directory this ImageDialog maintains */
|
|
80 |
public File imgDir;
|
|
81 |
|
|
82 |
/** KeyListener for watching changes in the scale text field */
|
|
83 |
private KeyHandler keyHandler = new KeyHandler(); |
|
84 |
|
|
85 |
/** FocusListener for watching changes in the scale text field */
|
|
86 |
private FocusHandler focusHandler = new FocusHandler(); |
|
87 |
|
|
88 |
private SimpleAttributeSet originalAttributes = new SimpleAttributeSet(); |
|
89 |
|
|
90 |
/**
|
|
91 |
* indicates whether or not changes in a SizeSelectorPanel are
|
|
92 |
* to be processed. Usually, changes caused by a method of this
|
|
93 |
* class are to be ignored
|
|
94 |
*/
|
|
95 |
private boolean ignoreChangeEvents = false; |
|
96 |
|
|
97 |
/** list with images in this image repository */
|
|
98 |
private JList imgFileList;
|
|
99 |
|
|
100 |
/** button to add an image file to the repository */
|
|
101 |
private JButton addImgBtn;
|
|
102 |
|
|
103 |
/** button to delete an image file from the repository */
|
|
104 |
private JButton delImgBtn;
|
|
105 |
|
|
106 |
/** text field for manipulating the scale of an image */
|
|
107 |
private JTextField scale;
|
|
108 |
|
|
109 |
/** component to manipulate the image width */
|
|
110 |
private SizeSelectorPanel imgWidth;
|
|
111 |
|
|
112 |
/** component to manipulate the image height */
|
|
113 |
private SizeSelectorPanel imgHeight;
|
|
114 |
|
|
115 |
/** component to display the original width of an image */
|
|
116 |
private JLabel oWidth;
|
|
117 |
|
|
118 |
/** component to display the original height of an image */
|
|
119 |
private JLabel oHeight;
|
|
120 |
|
|
121 |
/** component to preview an image */
|
|
122 |
private ImagePreview preview;
|
|
123 |
|
|
124 |
/** component to scroll an image inside the preview */
|
|
125 |
private JScrollPane scPrev;
|
|
126 |
|
|
127 |
/**
|
|
128 |
* contains all components having attributes for the image represented
|
|
129 |
* in this <code>ImageDialog</code>
|
|
130 |
*/
|
|
131 |
private Vector attributeComponents = new Vector(); |
|
132 |
|
|
133 |
/** the help id for this dialog */
|
|
134 |
private static final String helpTopicId = "item166"; |
|
135 |
|
|
136 |
/** the document the image came from, if any */
|
|
137 |
private SHTMLDocument doc;
|
|
138 |
|
|
139 |
/**
|
|
140 |
* construct a new ImageDialog
|
|
141 |
*
|
|
142 |
* @param parent the parent frame of this ImageDialog
|
|
143 |
* @param title the title of this ImageDialog
|
|
144 |
* @param imgDir the directory of the image repository
|
|
145 |
*/
|
|
146 | 0 |
public ImageDialog(Dialog parent, String title, File imgDir) {
|
147 | 0 |
super(parent, title, helpTopicId);
|
148 | 0 |
initDialog(title, imgDir); |
149 |
} |
|
150 |
|
|
151 |
/**
|
|
152 |
* construct a new ImageDialog
|
|
153 |
*
|
|
154 |
* @param parent the parent frame of this ImageDialog
|
|
155 |
* @param title the title of this ImageDialog
|
|
156 |
* @param imgDir the directory of the image repository
|
|
157 |
*/
|
|
158 | 1 |
public ImageDialog(Frame parent, String title, File imgDir) {
|
159 | 1 |
super(parent, title, helpTopicId);
|
160 | 1 |
initDialog(title, imgDir); |
161 |
} |
|
162 |
|
|
163 | 0 |
public ImageDialog(Frame parent, String title, File imgDir, SHTMLDocument sourceDoc) {
|
164 | 0 |
super(parent, title, helpTopicId);
|
165 | 0 |
this.doc = sourceDoc;
|
166 | 0 |
initDialog(title, imgDir); |
167 |
} |
|
168 |
|
|
169 |
/**
|
|
170 |
* build the dialog contents after construction
|
|
171 |
*
|
|
172 |
* @param title the title of this ImageDialog
|
|
173 |
* @param imgDir the directory of the image repository
|
|
174 |
*/
|
|
175 | 1 |
private void initDialog(String title, File imgDir) { |
176 |
|
|
177 |
//System.out.println("ImageDialog.initDialog imgDir=" + imgDir.getAbsolutePath());
|
|
178 |
|
|
179 | 1 |
this.imgDir = imgDir;
|
180 |
|
|
181 | 1 |
Dimension dim; |
182 |
|
|
183 |
// create an image directory panel
|
|
184 | 1 |
JPanel dirPanel = new JPanel(new BorderLayout()); |
185 | 1 |
dirPanel.setBorder(new TitledBorder(new EtchedBorder( |
186 |
EtchedBorder.LOWERED), |
|
187 |
FrmMain.dynRes.getResourceString( |
|
188 |
FrmMain.resources, "imgDirPanelTitle")));
|
|
189 |
|
|
190 |
// create a list to disply image files in
|
|
191 | 1 |
imgFileList = new JList();
|
192 | 1 |
dim = new Dimension(100, 100);
|
193 | 1 |
imgFileList.setMinimumSize(dim); |
194 | 1 |
imgFileList.setPreferredSize(dim); |
195 | 1 |
imgFileList.addListSelectionListener(this);
|
196 | 1 |
updateFileList(); |
197 |
|
|
198 |
// create a panel with action buttons for image files
|
|
199 | 1 |
JPanel dirBtnPanel = new JPanel();
|
200 |
|
|
201 |
// create image directory action buttons
|
|
202 | 1 |
addImgBtn = new JButton(FrmMain.dynRes.getResourceString(
|
203 |
FrmMain.resources, "addImgBtnTitle"));
|
|
204 | 1 |
addImgBtn.addActionListener(this);
|
205 | 1 |
delImgBtn = new JButton(FrmMain.dynRes.getResourceString(
|
206 |
FrmMain.resources, "delImgBtnTitle"));
|
|
207 | 1 |
delImgBtn.addActionListener(this);
|
208 |
|
|
209 |
// add action buttons to button panel
|
|
210 | 1 |
dirBtnPanel.add(addImgBtn); |
211 | 1 |
dirBtnPanel.add(delImgBtn); |
212 |
|
|
213 |
// add components to image directory panel
|
|
214 | 1 |
dirPanel.add(imgFileList, BorderLayout.CENTER); |
215 | 1 |
dirPanel.add(dirBtnPanel, BorderLayout.SOUTH); |
216 |
|
|
217 |
// create an image preview panel
|
|
218 | 1 |
JPanel previewPanel = new JPanel(new BorderLayout()); |
219 | 1 |
previewPanel.setBorder(new TitledBorder(new EtchedBorder( |
220 |
EtchedBorder.LOWERED), |
|
221 |
FrmMain.dynRes.getResourceString( |
|
222 |
FrmMain.resources, "imgPreviewPanelTitle")));
|
|
223 |
|
|
224 |
// add a new ImagePreview object to the preview panel
|
|
225 | 1 |
preview = new ImagePreview();
|
226 | 1 |
dim = new Dimension(250, 250);
|
227 | 1 |
preview.setMinimumSize(dim); |
228 | 1 |
preview.setPreferredSize(dim); |
229 | 1 |
scPrev = new JScrollPane(preview);
|
230 | 1 |
previewPanel.add(scPrev, BorderLayout.CENTER); |
231 |
|
|
232 |
// layout and constraints to use later on
|
|
233 | 1 |
GridBagLayout g = new GridBagLayout();
|
234 | 1 |
GridBagConstraints c = new GridBagConstraints();
|
235 |
|
|
236 |
// create an image properties panel
|
|
237 | 1 |
JPanel eastPanel = new JPanel(new BorderLayout()); |
238 | 1 |
JPanel propertiesPanel = new JPanel(g);
|
239 | 1 |
eastPanel.add(propertiesPanel, BorderLayout.NORTH); |
240 | 1 |
eastPanel.setBorder(new TitledBorder(new EtchedBorder( |
241 |
EtchedBorder.LOWERED), |
|
242 |
FrmMain.dynRes.getResourceString( |
|
243 |
FrmMain.resources, "imgPropertiesPanelTitle")));
|
|
244 |
|
|
245 |
// add scale component
|
|
246 | 1 |
Util.addGridBagComponent(propertiesPanel, |
247 |
new JLabel(
|
|
248 |
FrmMain.dynRes.getResourceString( |
|
249 |
FrmMain.resources, "imgScaleLabel")),
|
|
250 |
g, c, 0, 0, |
|
251 |
GridBagConstraints.EAST); |
|
252 | 1 |
scale = new JTextField();
|
253 | 1 |
scale.addKeyListener(keyHandler); |
254 | 1 |
scale.addFocusListener(focusHandler); |
255 | 1 |
dim = new Dimension(50, 20);
|
256 | 1 |
scale.setMinimumSize(dim); |
257 | 1 |
scale.setPreferredSize(dim); |
258 | 1 |
JPanel helperPanel = new JPanel();
|
259 | 1 |
helperPanel.add(scale); |
260 | 1 |
helperPanel.add(new JLabel(SizeSelectorPanel.UNIT_PERCENT, SwingConstants.LEFT));
|
261 | 1 |
Util.addGridBagComponent(propertiesPanel, helperPanel, g, c, 1, 0, |
262 |
GridBagConstraints.WEST); |
|
263 |
|
|
264 |
// add width component
|
|
265 | 1 |
Util.addGridBagComponent(propertiesPanel, |
266 |
new JLabel(
|
|
267 |
FrmMain.dynRes.getResourceString( |
|
268 |
FrmMain.resources, "imgWidthLabel")),
|
|
269 |
g, c, 0, 1, |
|
270 |
GridBagConstraints.EAST); |
|
271 | 1 |
imgWidth = new SizeSelectorPanel(HTML.Attribute.WIDTH, null, false, SizeSelectorPanel.TYPE_LABEL); |
272 | 1 |
attributeComponents.addElement(imgWidth); |
273 | 1 |
imgWidth.getValueSelector().addChangeListener(this);
|
274 | 1 |
Util.addGridBagComponent(propertiesPanel, imgWidth, g, c, 1, 1, |
275 |
GridBagConstraints.WEST); |
|
276 |
|
|
277 |
// add height component
|
|
278 | 1 |
Util.addGridBagComponent(propertiesPanel, |
279 |
new JLabel(
|
|
280 |
FrmMain.dynRes.getResourceString( |
|
281 |
FrmMain.resources, "imgHeightLabel")),
|
|
282 |
g, c, 0, 2, |
|
283 |
GridBagConstraints.EAST); |
|
284 | 1 |
imgHeight = new SizeSelectorPanel(HTML.Attribute.HEIGHT, null, false, SizeSelectorPanel.TYPE_LABEL); |
285 | 1 |
attributeComponents.addElement(imgHeight); |
286 | 1 |
imgHeight.getValueSelector().addChangeListener(this);
|
287 | 1 |
Util.addGridBagComponent(propertiesPanel, imgHeight, g, c, 1, 2, |
288 |
GridBagConstraints.WEST); |
|
289 |
|
|
290 |
// add hspace component
|
|
291 | 1 |
Util.addGridBagComponent(propertiesPanel, |
292 |
new JLabel(
|
|
293 |
FrmMain.dynRes.getResourceString( |
|
294 |
FrmMain.resources, "imgHSpaceLabel")),
|
|
295 |
g, c, 0, 3, |
|
296 |
GridBagConstraints.EAST); |
|
297 | 1 |
SizeSelectorPanel hSpace = new SizeSelectorPanel(
|
298 |
HTML.Attribute.HSPACE, null, false, SizeSelectorPanel.TYPE_LABEL); |
|
299 | 1 |
attributeComponents.addElement(hSpace); |
300 | 1 |
Util.addGridBagComponent(propertiesPanel, hSpace, g, c, 1, 3, |
301 |
GridBagConstraints.WEST); |
|
302 |
|
|
303 |
// add vspace component
|
|
304 | 1 |
Util.addGridBagComponent(propertiesPanel, |
305 |
new JLabel(
|
|
306 |
FrmMain.dynRes.getResourceString( |
|
307 |
FrmMain.resources, "imgVSpaceLabel")),
|
|
308 |
g, c, 0, 4, |
|
309 |
GridBagConstraints.EAST); |
|
310 | 1 |
SizeSelectorPanel vSpace = new SizeSelectorPanel(
|
311 |
HTML.Attribute.VSPACE, null, false, SizeSelectorPanel.TYPE_LABEL); |
|
312 | 1 |
attributeComponents.addElement(vSpace); |
313 | 1 |
Util.addGridBagComponent(propertiesPanel, vSpace, g, c, 1, 4, |
314 |
GridBagConstraints.WEST); |
|
315 |
|
|
316 |
// add alignment component
|
|
317 | 1 |
Util.addGridBagComponent(propertiesPanel, new JLabel(
|
318 |
FrmMain.dynRes.getResourceString( |
|
319 |
FrmMain.resources, "imgAlignLabel")),
|
|
320 |
g, c, 0, 5, GridBagConstraints.EAST); |
|
321 | 1 |
String[] items = new String[] {
|
322 |
FrmMain.dynRes.getResourceString(FrmMain.resources, "imgAlignTop"),
|
|
323 |
FrmMain.dynRes.getResourceString(FrmMain.resources, "imgAlignMiddle"),
|
|
324 |
FrmMain.dynRes.getResourceString(FrmMain.resources, "imgAlignBottom"),
|
|
325 |
FrmMain.dynRes.getResourceString(FrmMain.resources, "imgAlignLeft"),
|
|
326 |
FrmMain.dynRes.getResourceString(FrmMain.resources, "imgAlignCenter"),
|
|
327 |
FrmMain.dynRes.getResourceString(FrmMain.resources, "imgAlignRight")};
|
|
328 | 1 |
String[] names = new String[] {"top", "middle", "bottom", "left", |
329 |
"center", "right"}; |
|
330 | 1 |
AttributeComboBox imgAlign = new AttributeComboBox(
|
331 |
items, names, null, HTML.Attribute.ALIGN);
|
|
332 | 1 |
attributeComponents.addElement(imgAlign); |
333 | 1 |
Util.addGridBagComponent(propertiesPanel, imgAlign, g, c, 1, 5, |
334 |
GridBagConstraints.WEST); |
|
335 |
|
|
336 |
// add original width component
|
|
337 | 1 |
Util.addGridBagComponent(propertiesPanel, |
338 |
new JLabel(
|
|
339 |
FrmMain.dynRes.getResourceString( |
|
340 |
FrmMain.resources, "oWidthLabel")),
|
|
341 |
g, c, 0, 6, |
|
342 |
GridBagConstraints.EAST); |
|
343 | 1 |
oWidth = new JLabel(""); |
344 | 1 |
Util.addGridBagComponent(propertiesPanel, oWidth, g, c, 1, 6, |
345 |
GridBagConstraints.WEST); |
|
346 |
|
|
347 |
// add original height component
|
|
348 | 1 |
Util.addGridBagComponent(propertiesPanel, |
349 |
new JLabel(
|
|
350 |
FrmMain.dynRes.getResourceString( |
|
351 |
FrmMain.resources, "oHeightLabel")),
|
|
352 |
g, c, 0, 7, |
|
353 |
GridBagConstraints.EAST); |
|
354 | 1 |
oHeight = new JLabel(""); |
355 | 1 |
Util.addGridBagComponent(propertiesPanel, oHeight, g, c, 1, 7, |
356 |
GridBagConstraints.WEST); |
|
357 |
|
|
358 |
// add border component
|
|
359 | 1 |
Util.addGridBagComponent(propertiesPanel, |
360 |
new JLabel(
|
|
361 |
FrmMain.dynRes.getResourceString( |
|
362 |
FrmMain.resources, "imgBorderLabel")),
|
|
363 |
g, c, 0, 8, |
|
364 |
GridBagConstraints.EAST); |
|
365 | 1 |
SizeSelectorPanel imgBorder = new SizeSelectorPanel(
|
366 |
HTML.Attribute.BORDER, null, false, SizeSelectorPanel.TYPE_LABEL); |
|
367 | 1 |
attributeComponents.addElement(imgBorder); |
368 | 1 |
Util.addGridBagComponent(propertiesPanel, imgBorder, g, c, 1, 8, |
369 |
GridBagConstraints.WEST); |
|
370 |
|
|
371 |
// add to content pane of DialogShell
|
|
372 | 1 |
Container contentPane = super.getContentPane();
|
373 | 1 |
contentPane.add(dirPanel, BorderLayout.WEST); |
374 | 1 |
contentPane.add(previewPanel, BorderLayout.CENTER); |
375 | 1 |
contentPane.add(eastPanel, BorderLayout.EAST); |
376 |
|
|
377 |
// cause optimal placement of all elements
|
|
378 | 1 |
pack(); |
379 |
|
|
380 | 1 |
scPrev.addComponentListener(new ResizeListener());
|
381 |
} |
|
382 |
|
|
383 | 0 |
public Integer getImgWidth() {
|
384 | 0 |
return imgWidth.getIntValue();
|
385 |
} |
|
386 |
|
|
387 | 0 |
public Integer getImgHeight() {
|
388 | 0 |
return imgHeight.getIntValue();
|
389 |
} |
|
390 |
|
|
391 |
/**
|
|
392 |
* set dialog content from a given set of image attributes
|
|
393 |
*
|
|
394 |
* @param a the set of attributes to set dialog contents from
|
|
395 |
*/
|
|
396 | 0 |
public void setImageAttributes(AttributeSet a) { |
397 |
//System.out.println("ImageDialog.setImageAttributes");
|
|
398 | 0 |
ignoreChangeEvents = true;
|
399 | 0 |
originalAttributes.addAttributes(a); |
400 | 0 |
if(a.isDefined(HTML.Attribute.SRC)) {
|
401 | 0 |
File imgFile = null;
|
402 | 0 |
if(doc != null) { |
403 | 0 |
imgFile = new File(Util.resolveRelativePath(a.getAttribute(HTML.Attribute.SRC).toString(), doc.getBase().getFile()));
|
404 |
} |
|
405 |
else {
|
|
406 | 0 |
imgFile = new File(a.getAttribute(HTML.Attribute.SRC).toString());
|
407 |
} |
|
408 |
//System.out.println("ImageDialog.setImageAttribute imgFile=" + imgFile.getAbsolutePath());
|
|
409 | 0 |
imgFileList.setSelectedValue(imgFile.getName().toLowerCase(), true);
|
410 |
} |
|
411 | 0 |
for(int i = 0; i < attributeComponents.size(); i++) { |
412 | 0 |
((AttributeComponent) attributeComponents.get(i)).setValue(a); |
413 |
} |
|
414 | 0 |
if(a.isDefined(HTML.Attribute.WIDTH)) {
|
415 | 0 |
preview.setPreviewWidth( |
416 |
Integer.parseInt(a.getAttribute(HTML.Attribute.WIDTH).toString())); |
|
417 |
} |
|
418 | 0 |
if(a.isDefined(HTML.Attribute.HEIGHT)) {
|
419 | 0 |
preview.setPreviewHeight( |
420 |
Integer.parseInt(a.getAttribute(HTML.Attribute.HEIGHT).toString())); |
|
421 |
} |
|
422 | 0 |
int scalePct = preview.getScale();
|
423 | 0 |
scale.setText(Integer.toString(scalePct)); |
424 | 0 |
ignoreChangeEvents = false;
|
425 |
} |
|
426 |
|
|
427 | 0 |
public void setImage(String fName, String w, String h) { |
428 |
//System.out.println("ImageDialog.setImage fName=" + fName);
|
|
429 | 0 |
imgFileList.setSelectedValue(new File(fName).getName(), true); |
430 | 0 |
preview.setImage(new ImageIcon(fName));
|
431 | 0 |
try {
|
432 | 0 |
if(w != null && w.length() > 0) { |
433 | 0 |
preview.setPreviewWidth(Integer.parseInt(w)); |
434 |
} |
|
435 | 0 |
if(h != null && h.length() > 0) { |
436 | 0 |
preview.setPreviewHeight(Integer.parseInt(h)); |
437 |
} |
|
438 |
} |
|
439 |
catch(Exception e) {
|
|
440 | 0 |
Util.errMsg(this, null, e); |
441 |
} |
|
442 |
} |
|
443 |
|
|
444 |
/**
|
|
445 |
* get the HTML representing the image selected in this
|
|
446 |
* <code>ImageDialog</code>
|
|
447 |
*/
|
|
448 | 1 |
public String getImageHTML() {
|
449 | 1 |
SimpleAttributeSet set = new SimpleAttributeSet(originalAttributes);
|
450 | 1 |
StringWriter sw = new StringWriter();
|
451 | 1 |
SHTMLWriter w = new SHTMLWriter(sw);
|
452 | 1 |
for(int i = 0; i < attributeComponents.size(); i++) { |
453 | 6 |
set.addAttributes(((AttributeComponent) attributeComponents.get(i)).getValue()); |
454 |
} |
|
455 | 1 |
set.addAttribute(HTML.Attribute.SRC, getImageSrc()); |
456 | 1 |
try {
|
457 | 1 |
w.startTag(HTML.Tag.IMG.toString(), set); |
458 |
} |
|
459 |
catch(Exception e) {
|
|
460 | 0 |
Util.errMsg(this, e.getMessage(), e);
|
461 |
} |
|
462 | 1 |
return sw.getBuffer().toString();
|
463 |
} |
|
464 |
|
|
465 |
/**
|
|
466 |
* get the value for the SRC attribute of an image tag
|
|
467 |
*
|
|
468 |
* @return the value of the SRC attribute of an image tag
|
|
469 |
*/
|
|
470 | 1 |
public String getImageSrc() {
|
471 | 1 |
StringBuffer buf = new StringBuffer();
|
472 | 1 |
Object value = imgFileList.getSelectedValue(); |
473 | 1 |
if(value != null) { |
474 | 1 |
buf.append(FrmMain.IMAGE_DIR); |
475 | 1 |
buf.append(Util.URL_SEPARATOR); |
476 | 1 |
buf.append(value.toString()); |
477 |
} |
|
478 | 1 |
return buf.toString();
|
479 |
} |
|
480 |
|
|
481 |
/**
|
|
482 |
* handle the event when the user pressed the 'Add...' button
|
|
483 |
* to add a new image to the repository
|
|
484 |
*/
|
|
485 | 1 |
private void handleAddImage() { |
486 | 1 |
try {
|
487 | 1 |
JFileChooser chooser = new JFileChooser();
|
488 | 1 |
chooser.setMultiSelectionEnabled(true);
|
489 | 1 |
ExampleFileFilter filter = new ExampleFileFilter();
|
490 | 1 |
filter.addExtension("gif");
|
491 | 1 |
filter.addExtension("jpg");
|
492 | 1 |
filter.addExtension("jpeg");
|
493 | 1 |
filter.setDescription(FrmMain.dynRes.getResourceString( |
494 |
FrmMain.resources, "imageFileDesc"));
|
|
495 | 1 |
chooser.setFileFilter(filter); |
496 | 1 |
if(chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { |
497 | 1 |
File[] sFiles = chooser.getSelectedFiles(); |
498 | 1 |
if(!imgDir.exists()) {
|
499 | 1 |
imgDir.mkdirs(); |
500 |
} |
|
501 | 1 |
String imgDirName = imgDir.getAbsolutePath(); |
502 | 1 |
for(int i = 0; i < sFiles.length; i++) { |
503 |
//System.out.println("file selected: " + sFiles[i] + " new name= " + imgDirName + File.separator + sFiles[i].getName());
|
|
504 | 1 |
Util.copyFile(sFiles[i], |
505 |
new File(imgDirName + File.separator + sFiles[i].getName()));
|
|
506 | 1 |
updateFileList(); |
507 |
} |
|
508 |
} |
|
509 |
} |
|
510 |
catch(Exception e) {
|
|
511 | 0 |
Util.errMsg(this, e.getMessage(), e);
|
512 |
} |
|
513 |
} |
|
514 |
|
|
515 |
/**
|
|
516 |
* handle the event occurring when the user pressed the 'Delete' button
|
|
517 |
* to remove an image from the repository
|
|
518 |
*/
|
|
519 | 0 |
private void handleDeleteImage() { |
520 | 0 |
String fName = imgFileList.getSelectedValue().toString(); |
521 | 0 |
if(Util.msg(JOptionPane.YES_NO_OPTION, "confirmDelete", "deleteFileQuery", fName, "\r\n")) { |
522 | 0 |
File delFile = new File(imgDir.getAbsolutePath() + File.separator + fName);
|
523 | 0 |
delFile.delete(); |
524 | 0 |
updateFileList(); |
525 |
} |
|
526 |
} |
|
527 |
|
|
528 |
/**
|
|
529 |
* display all files found in the image directory
|
|
530 |
*/
|
|
531 | 2 |
private void updateFileList() { |
532 | 2 |
if(imgDir != null && imgFileList != null) { |
533 | 2 |
String[] files = imgDir.list(); |
534 | 2 |
if(files != null && files.length > 0) { |
535 | 1 |
for(int i = 0; i < files.length; i++) { |
536 | 1 |
files[i] = files[i].toLowerCase(); |
537 |
} |
|
538 | 1 |
imgFileList.setListData(files); |
539 |
} |
|
540 |
} |
|
541 |
} |
|
542 |
|
|
543 |
/**
|
|
544 |
* update all image property displays to the current setting
|
|
545 |
*/
|
|
546 | 2 |
private void updateControls() { |
547 | 2 |
ignoreChangeEvents = true;
|
548 | 2 |
int scalePct = preview.getScale();
|
549 | 2 |
SimpleAttributeSet set = new SimpleAttributeSet();
|
550 | 2 |
oWidth.setText(Integer.toString(preview.getOriginalWidth())); |
551 | 2 |
oHeight.setText(Integer.toString(preview.getOriginalHeight())); |
552 |
//System.out.println("updateControls origW=" + preview.getOriginalWidth());
|
|
553 |
//System.out.println("updateControls add WIDTH attr as " + Integer.toString(
|
|
554 |
// preview.getOriginalWidth() * scalePct / 100) + SizeSelectorPanel.UNIT_PT);
|
|
555 | 2 |
set.addAttribute(HTML.Attribute.WIDTH, Integer.toString( |
556 |
preview.getOriginalWidth() * scalePct / 100) + SizeSelectorPanel.UNIT_PT); |
|
557 | 2 |
set.addAttribute(HTML.Attribute.HEIGHT, Integer.toString( |
558 |
preview.getOriginalHeight() * scalePct / 100) + SizeSelectorPanel.UNIT_PT); |
|
559 | 2 |
imgWidth.setValue(set); |
560 | 2 |
imgHeight.setValue(set); |
561 | 2 |
scale.setText(Integer.toString(scalePct)); |
562 | 2 |
ignoreChangeEvents = false;
|
563 |
} |
|
564 |
|
|
565 |
/**
|
|
566 |
* apply a scale set by the user through respective text field and
|
|
567 |
* update all related image property displays
|
|
568 |
*/
|
|
569 | 0 |
private void applyPreviewScale() { |
570 |
//System.out.println("applyPreviewScale scale=" + scale.getText());
|
|
571 | 0 |
ignoreChangeEvents = true;
|
572 | 0 |
try {
|
573 | 0 |
preview.setScale(Integer.parseInt(scale.getText())); |
574 | 0 |
updateControls(); |
575 |
} |
|
576 |
catch(Exception e) {}
|
|
577 | 0 |
ignoreChangeEvents = false;
|
578 |
} |
|
579 |
|
|
580 |
/**
|
|
581 |
* apply a new width set by the user and update
|
|
582 |
* all related image property displays
|
|
583 |
*/
|
|
584 | 0 |
private void applyPreviewWidth() { |
585 |
//System.out.println("applyPreviewWidth width=" + imgWidth.getIntValue().intValue());
|
|
586 | 0 |
ignoreChangeEvents = true;
|
587 | 0 |
preview.setPreviewWidth(imgWidth.getIntValue().intValue()); |
588 | 0 |
int scalePct = preview.getScale();
|
589 |
//System.out.println("applyPreviewWidth scale now " + scalePct);
|
|
590 | 0 |
SimpleAttributeSet set = new SimpleAttributeSet();
|
591 | 0 |
scale.setText(Integer.toString(scalePct)); |
592 | 0 |
set.addAttribute(HTML.Attribute.HEIGHT, Integer.toString( |
593 |
preview.getOriginalHeight() * scalePct / 100) + SizeSelectorPanel.UNIT_PT); |
|
594 |
//System.out.println("applyPreviewWidth, changing height to " + Integer.toString(
|
|
595 |
// preview.getOriginalHeight() * scalePct / 100) + SizeSelectorPanel.UNIT_PT);
|
|
596 | 0 |
imgHeight.setValue(set); |
597 | 0 |
ignoreChangeEvents = false;
|
598 |
} |
|
599 |
|
|
600 |
/**
|
|
601 |
* apply a new height set by the user and update
|
|
602 |
* all related image property displays
|
|
603 |
*/
|
|
604 | 0 |
private void applyPreviewHeight() { |
605 |
//System.out.println("applyPreviewHeight height=" + imgHeight.getIntValue().intValue());
|
|
606 | 0 |
ignoreChangeEvents = true;
|
607 | 0 |
preview.setPreviewHeight(imgHeight.getIntValue().intValue()); |
608 | 0 |
int scalePct = preview.getScale();
|
609 |
//System.out.println("applyPreviewHeight scale now " + scalePct);
|
|
610 | 0 |
SimpleAttributeSet set = new SimpleAttributeSet();
|
611 | 0 |
scale.setText(Integer.toString(scalePct)); |
612 | 0 |
set.addAttribute(HTML.Attribute.WIDTH, Integer.toString( |
613 |
preview.getOriginalWidth() * scalePct / 100) + SizeSelectorPanel.UNIT_PT); |
|
614 |
//System.out.println("applyPreviewHeight, changing width to " + Integer.toString(
|
|
615 |
// preview.getOriginalWidth() * scalePct / 100) + SizeSelectorPanel.UNIT_PT);
|
|
616 | 0 |
imgWidth.setValue(set); |
617 | 0 |
ignoreChangeEvents = false;
|
618 |
} |
|
619 |
|
|
620 |
/* ---------------- event handling start ------------------------- */
|
|
621 |
|
|
622 |
/**
|
|
623 |
* implements the ActionListener interface to be notified of
|
|
624 |
* clicks onto the file repository buttons.
|
|
625 |
*/
|
|
626 | 2 |
public void actionPerformed(ActionEvent e) { |
627 | 2 |
Object src = e.getSource(); |
628 | 2 |
if(src == addImgBtn) {
|
629 | 1 |
handleAddImage(); |
630 |
} |
|
631 | 1 |
else if(src == delImgBtn) { |
632 | 0 |
handleDeleteImage(); |
633 |
} |
|
634 |
else {
|
|
635 | 1 |
super.actionPerformed(e);
|
636 |
} |
|
637 |
} |
|
638 |
|
|
639 |
/**
|
|
640 |
* Listener for changes in the image list.
|
|
641 |
*
|
|
642 |
* <p>updates the image preview and property displays according
|
|
643 |
* to the current selection (if any)</p>
|
|
644 |
*/
|
|
645 | 2 |
public void valueChanged(ListSelectionEvent e) { |
646 | 2 |
if(!imgFileList.isSelectionEmpty()) {
|
647 |
/*System.out.println("ImageDialog.valueChanged setting preview image to " + imgDir.getAbsolutePath() +
|
|
648 |
File.separator +
|
|
649 |
imgFileList.getSelectedValue().toString());*/
|
|
650 | 2 |
preview.setImage(new ImageIcon(imgDir.getAbsolutePath() +
|
651 |
File.separator + |
|
652 |
imgFileList.getSelectedValue().toString())); |
|
653 | 2 |
updateControls(); |
654 |
} |
|
655 |
else {
|
|
656 | 0 |
preview.setImage(null);
|
657 | 0 |
int vWidth = scPrev.getWidth() - 5;
|
658 | 0 |
int vHeight = scPrev.getHeight() - 5;
|
659 | 0 |
preview.setPreferredSize(new Dimension(vWidth, vHeight));
|
660 | 0 |
preview.revalidate(); |
661 |
} |
|
662 |
} |
|
663 |
|
|
664 |
/**
|
|
665 |
* Listener for resize events.
|
|
666 |
*
|
|
667 |
* <p>used on the JScrollPane holding the image preview
|
|
668 |
* to adjust the preview to size changes and to synchronize
|
|
669 |
* property displays accordingly.</p>
|
|
670 |
*/
|
|
671 |
private class ResizeListener extends ComponentAdapter { |
|
672 | 0 |
public void componentResized(ComponentEvent e) { |
673 | 0 |
int vWidth = scPrev.getWidth() - 5;
|
674 | 0 |
int vHeight = scPrev.getHeight() - 5;
|
675 | 0 |
preview.setPreferredSize(new Dimension(vWidth, vHeight));
|
676 | 0 |
preview.revalidate(); |
677 | 0 |
updateControls(); |
678 |
} |
|
679 |
} |
|
680 |
|
|
681 |
/**
|
|
682 |
* Listener for key events
|
|
683 |
*
|
|
684 |
* <p>Used to adjust preview properties according to
|
|
685 |
* user settings in the scale text field</p>
|
|
686 |
*/
|
|
687 |
private class KeyHandler extends KeyAdapter { |
|
688 | 0 |
public void keyReleased(KeyEvent e) { |
689 | 0 |
Object source = e.getSource(); |
690 | 0 |
int keyCode = e.getKeyCode();
|
691 | 0 |
if(source.equals(scale)) {
|
692 | 0 |
if(keyCode == KeyEvent.VK_ENTER)
|
693 |
{ |
|
694 | 0 |
applyPreviewScale(); |
695 |
} |
|
696 |
} |
|
697 |
} |
|
698 |
} |
|
699 |
|
|
700 |
/**
|
|
701 |
* Listener for focus events
|
|
702 |
*
|
|
703 |
* <p>Used to adjust preview properties according to
|
|
704 |
* user settings in the scale text field</p>
|
|
705 |
*/
|
|
706 |
private class FocusHandler extends FocusAdapter { |
|
707 | 0 |
public void focusLost(FocusEvent e) { |
708 | 0 |
Object source = e.getSource(); |
709 | 0 |
if(source.equals(scale)) {
|
710 | 0 |
applyPreviewScale(); |
711 |
} |
|
712 |
} |
|
713 |
} |
|
714 |
|
|
715 |
/**
|
|
716 |
* Listener for change events
|
|
717 |
*
|
|
718 |
* <p>Used to adjust preview properties according to
|
|
719 |
* user settings in SizeSelectorPanels</p>
|
|
720 |
*/
|
|
721 | 6 |
public void stateChanged(ChangeEvent e) { |
722 | 6 |
if(!ignoreChangeEvents) {
|
723 | 0 |
Object source = e.getSource(); |
724 | 0 |
if(source.equals(imgWidth.getValueSelector())) {
|
725 | 0 |
applyPreviewWidth(); |
726 |
} |
|
727 | 0 |
else if(source.equals(imgHeight.getValueSelector())) { |
728 | 0 |
applyPreviewHeight(); |
729 |
} |
|
730 |
} |
|
731 |
} |
|
732 |
|
|
733 |
/* ---------------- event handling end ------------------------- */
|
|
734 |
|
|
735 |
} |
|