001: package net.sf.memoranda.ui.htmleditor;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Color;
005: import java.awt.Dimension;
006: import java.awt.FlowLayout;
007: import java.awt.Frame;
008: import java.awt.GridBagConstraints;
009: import java.awt.GridBagLayout;
010: import java.awt.Image;
011: import java.awt.Insets;
012: import java.awt.event.ActionEvent;
013: import java.awt.event.WindowEvent;
014: import java.awt.event.WindowListener;
015:
016: import javax.swing.ImageIcon;
017: import javax.swing.JButton;
018: import javax.swing.JComboBox;
019: import javax.swing.JDialog;
020: import javax.swing.JFileChooser;
021: import javax.swing.JLabel;
022: import javax.swing.JPanel;
023: import javax.swing.JTextField;
024: import javax.swing.UIManager;
025: import javax.swing.border.EmptyBorder;
026: import javax.swing.border.EtchedBorder;
027:
028: import net.sf.memoranda.ui.htmleditor.util.Local;
029:
030: /**
031: * <p>Title: </p>
032: * <p>Description: </p>
033: * <p>Copyright: Copyright (c) 2002</p>
034: * <p>Company: </p>
035: * @author unascribed
036: * @version 1.0
037: */
038: public class ImageDialog extends JDialog implements WindowListener {
039: JPanel headerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
040: JLabel header = new JLabel();
041: JPanel areaPanel = new JPanel(new GridBagLayout());
042: GridBagConstraints gbc;
043: JLabel jLabel1 = new JLabel();
044: public JTextField fileField = new JTextField();
045: JButton browseB = new JButton();
046: JLabel jLabel2 = new JLabel();
047: public JTextField altField = new JTextField();
048: JLabel jLabel3 = new JLabel();
049: public JTextField widthField = new JTextField();
050: JLabel jLabel4 = new JLabel();
051: public JTextField heightField = new JTextField();
052: JLabel jLabel5 = new JLabel();
053: public JTextField hspaceField = new JTextField();
054: JLabel jLabel6 = new JLabel();
055: public JTextField vspaceField = new JTextField();
056: JLabel jLabel7 = new JLabel();
057: public JTextField borderField = new JTextField();
058: JLabel jLabel8 = new JLabel();
059: String[] aligns = { "left", "right", "top", "middle", "bottom",
060: "absmiddle", "texttop", "baseline" };
061: // Note: align values are not localized because they are HTML keywords
062: public JComboBox alignCB = new JComboBox(aligns);
063: JLabel jLabel9 = new JLabel();
064: public JTextField urlField = new JTextField();
065: JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT,
066: 10, 10));
067: JButton okB = new JButton();
068: JButton cancelB = new JButton();
069: public boolean CANCELLED = false;
070:
071: public ImageDialog(Frame frame) {
072: super (frame, Local.getString("Image"), true);
073: try {
074: jbInit();
075: pack();
076: } catch (Exception ex) {
077: ex.printStackTrace();
078: }
079: super .addWindowListener(this );
080: }
081:
082: public ImageDialog() {
083: this (null);
084: }
085:
086: void jbInit() throws Exception {
087: this .setResizable(false);
088: // three Panels, so used BorderLayout for this dialog.
089: headerPanel.setBorder(new EmptyBorder(new Insets(0, 5, 0, 5)));
090: headerPanel.setBackground(Color.WHITE);
091: header.setFont(new java.awt.Font("Dialog", 0, 20));
092: header.setForeground(new Color(0, 0, 124));
093: header.setText(Local.getString("Image"));
094: header.setIcon(new ImageIcon(
095: net.sf.memoranda.ui.htmleditor.ImageDialog.class
096: .getResource("resources/icons/imgbig.png")));
097: headerPanel.add(header);
098: this .getContentPane().add(headerPanel, BorderLayout.NORTH);
099:
100: areaPanel.setBorder(new EtchedBorder(Color.white, new Color(
101: 142, 142, 142)));
102: jLabel1.setText(Local.getString("Image file"));
103: gbc = new GridBagConstraints();
104: gbc.gridx = 0;
105: gbc.gridy = 0;
106: gbc.insets = new Insets(10, 10, 5, 5);
107: gbc.anchor = GridBagConstraints.WEST;
108: areaPanel.add(jLabel1, gbc);
109: fileField.setMinimumSize(new Dimension(200, 25));
110: fileField.setPreferredSize(new Dimension(285, 25));
111: gbc = new GridBagConstraints();
112: gbc.gridx = 1;
113: gbc.gridy = 0;
114: gbc.gridwidth = 5;
115: gbc.insets = new Insets(10, 5, 5, 5);
116: gbc.anchor = GridBagConstraints.WEST;
117: gbc.fill = GridBagConstraints.HORIZONTAL;
118: areaPanel.add(fileField, gbc);
119: browseB.setMinimumSize(new Dimension(25, 25));
120: browseB.setPreferredSize(new Dimension(25, 25));
121: browseB
122: .setIcon(new ImageIcon(
123: net.sf.memoranda.ui.htmleditor.ImageDialog.class
124: .getResource("resources/icons/fileopen16.png")));
125: browseB.addActionListener(new java.awt.event.ActionListener() {
126: public void actionPerformed(ActionEvent e) {
127: browseB_actionPerformed(e);
128: }
129: });
130: gbc = new GridBagConstraints();
131: gbc.gridx = 6;
132: gbc.gridy = 0;
133: gbc.insets = new Insets(10, 5, 5, 10);
134: gbc.anchor = GridBagConstraints.WEST;
135: areaPanel.add(browseB, gbc);
136: jLabel2.setText(Local.getString("ALT text"));
137: gbc = new GridBagConstraints();
138: gbc.gridx = 0;
139: gbc.gridy = 1;
140: gbc.insets = new Insets(5, 10, 5, 5);
141: gbc.anchor = GridBagConstraints.WEST;
142: areaPanel.add(jLabel2, gbc);
143: altField.setPreferredSize(new Dimension(315, 25));
144: altField.setMinimumSize(new Dimension(200, 25));
145: gbc = new GridBagConstraints();
146: gbc.gridx = 1;
147: gbc.gridy = 1;
148: gbc.gridwidth = 6;
149: gbc.insets = new Insets(5, 5, 5, 10);
150: gbc.anchor = GridBagConstraints.WEST;
151: gbc.fill = GridBagConstraints.HORIZONTAL;
152: areaPanel.add(altField, gbc);
153: jLabel3.setText(Local.getString("Width"));
154: gbc = new GridBagConstraints();
155: gbc.gridx = 0;
156: gbc.gridy = 2;
157: gbc.insets = new Insets(5, 10, 5, 5);
158: gbc.anchor = GridBagConstraints.WEST;
159: areaPanel.add(jLabel3, gbc);
160: widthField.setPreferredSize(new Dimension(30, 25));
161: widthField.setMinimumSize(new Dimension(30, 25));
162: gbc = new GridBagConstraints();
163: gbc.gridx = 1;
164: gbc.gridy = 2;
165: gbc.insets = new Insets(5, 5, 5, 5);
166: gbc.anchor = GridBagConstraints.WEST;
167: areaPanel.add(widthField, gbc);
168: jLabel4.setText(Local.getString("Height"));
169: gbc = new GridBagConstraints();
170: gbc.gridx = 2;
171: gbc.gridy = 2;
172: gbc.insets = new Insets(5, 50, 5, 5);
173: gbc.anchor = GridBagConstraints.WEST;
174: areaPanel.add(jLabel4, gbc);
175: heightField.setMinimumSize(new Dimension(30, 25));
176: heightField.setPreferredSize(new Dimension(30, 25));
177: gbc = new GridBagConstraints();
178: gbc.gridx = 3;
179: gbc.gridy = 2;
180: gbc.insets = new Insets(5, 5, 5, 5);
181: gbc.anchor = GridBagConstraints.WEST;
182: areaPanel.add(heightField, gbc);
183: jLabel5.setText(Local.getString("H. space"));
184: gbc = new GridBagConstraints();
185: gbc.gridx = 0;
186: gbc.gridy = 3;
187: gbc.insets = new Insets(5, 10, 5, 5);
188: gbc.anchor = GridBagConstraints.WEST;
189: areaPanel.add(jLabel5, gbc);
190: hspaceField.setMinimumSize(new Dimension(30, 25));
191: hspaceField.setPreferredSize(new Dimension(30, 25));
192: hspaceField.setText("0");
193: gbc = new GridBagConstraints();
194: gbc.gridx = 1;
195: gbc.gridy = 3;
196: gbc.insets = new Insets(5, 5, 5, 5);
197: gbc.anchor = GridBagConstraints.WEST;
198: areaPanel.add(hspaceField, gbc);
199: jLabel6.setText(Local.getString("V. space"));
200: gbc = new GridBagConstraints();
201: gbc.gridx = 2;
202: gbc.gridy = 3;
203: gbc.insets = new Insets(5, 50, 5, 5);
204: gbc.anchor = GridBagConstraints.WEST;
205: areaPanel.add(jLabel6, gbc);
206: vspaceField.setMinimumSize(new Dimension(30, 25));
207: vspaceField.setPreferredSize(new Dimension(30, 25));
208: vspaceField.setText("0");
209: gbc = new GridBagConstraints();
210: gbc.gridx = 3;
211: gbc.gridy = 3;
212: gbc.insets = new Insets(5, 5, 5, 5);
213: gbc.anchor = GridBagConstraints.WEST;
214: areaPanel.add(vspaceField, gbc);
215: jLabel7.setText(Local.getString("Border"));
216: gbc = new GridBagConstraints();
217: gbc.gridx = 0;
218: gbc.gridy = 4;
219: gbc.insets = new Insets(5, 10, 5, 5);
220: gbc.anchor = GridBagConstraints.WEST;
221: areaPanel.add(jLabel7, gbc);
222: borderField.setMinimumSize(new Dimension(30, 25));
223: borderField.setPreferredSize(new Dimension(30, 25));
224: borderField.setText("0");
225: gbc = new GridBagConstraints();
226: gbc.gridx = 1;
227: gbc.gridy = 4;
228: gbc.insets = new Insets(5, 5, 5, 5);
229: gbc.anchor = GridBagConstraints.WEST;
230: areaPanel.add(borderField, gbc);
231: jLabel8.setText(Local.getString("Align"));
232: gbc = new GridBagConstraints();
233: gbc.gridx = 2;
234: gbc.gridy = 4;
235: gbc.insets = new Insets(5, 50, 5, 5);
236: gbc.anchor = GridBagConstraints.WEST;
237: areaPanel.add(jLabel8, gbc);
238: alignCB.setBackground(new Color(230, 230, 230));
239: alignCB.setFont(new java.awt.Font("Dialog", 1, 10));
240: alignCB.setPreferredSize(new Dimension(100, 25));
241: alignCB.setSelectedIndex(0);
242: gbc = new GridBagConstraints();
243: gbc.gridx = 3;
244: gbc.gridy = 4;
245: gbc.gridwidth = 2;
246: gbc.insets = new Insets(5, 5, 5, 5);
247: gbc.anchor = GridBagConstraints.WEST;
248: areaPanel.add(alignCB, gbc);
249: jLabel9.setText(Local.getString("Hyperlink"));
250: gbc = new GridBagConstraints();
251: gbc.gridx = 0;
252: gbc.gridy = 5;
253: gbc.insets = new Insets(5, 10, 10, 5);
254: gbc.anchor = GridBagConstraints.WEST;
255: areaPanel.add(jLabel9, gbc);
256: urlField.setPreferredSize(new Dimension(315, 25));
257: urlField.setMinimumSize(new Dimension(200, 25));
258: gbc = new GridBagConstraints();
259: gbc.gridx = 1;
260: gbc.gridy = 5;
261: gbc.gridwidth = 6;
262: gbc.insets = new Insets(5, 5, 10, 10);
263: gbc.fill = GridBagConstraints.HORIZONTAL;
264: gbc.anchor = GridBagConstraints.WEST;
265: areaPanel.add(urlField, gbc);
266: this .getContentPane().add(areaPanel, BorderLayout.CENTER);
267:
268: okB.setMaximumSize(new Dimension(100, 26));
269: okB.setMinimumSize(new Dimension(100, 26));
270: okB.setPreferredSize(new Dimension(100, 26));
271: okB.setText(Local.getString("Ok"));
272: okB.addActionListener(new java.awt.event.ActionListener() {
273: public void actionPerformed(ActionEvent e) {
274: okB_actionPerformed(e);
275: }
276: });
277: this .getRootPane().setDefaultButton(okB);
278: cancelB.setMaximumSize(new Dimension(100, 26));
279: cancelB.setMinimumSize(new Dimension(100, 26));
280: cancelB.setPreferredSize(new Dimension(100, 26));
281: cancelB.setText(Local.getString("Cancel"));
282: cancelB.addActionListener(new java.awt.event.ActionListener() {
283: public void actionPerformed(ActionEvent e) {
284: cancelB_actionPerformed(e);
285: }
286: });
287: buttonsPanel.add(okB, null);
288: buttonsPanel.add(cancelB, null);
289: this .getContentPane().add(buttonsPanel, BorderLayout.SOUTH);
290: }
291:
292: void okB_actionPerformed(ActionEvent e) {
293: this .dispose();
294: }
295:
296: void cancelB_actionPerformed(ActionEvent e) {
297: CANCELLED = true;
298: this .dispose();
299: }
300:
301: private ImageIcon getPreviewIcon(java.io.File file) {
302: ImageIcon tmpIcon = new ImageIcon(file.getPath());
303: ImageIcon thmb = null;
304: if (tmpIcon.getIconHeight() > 48) {
305: thmb = new ImageIcon(tmpIcon.getImage().getScaledInstance(
306: -1, 48, Image.SCALE_DEFAULT));
307: } else {
308: thmb = tmpIcon;
309: }
310: if (thmb.getIconWidth() > 350) {
311: return new ImageIcon(thmb.getImage().getScaledInstance(350,
312: -1, Image.SCALE_DEFAULT));
313: } else {
314: return thmb;
315: }
316: }
317:
318: //java.io.File selectedFile = null;
319: public void updatePreview() {
320: try {
321: if (new java.net.URL(fileField.getText()).getPath() != "")
322: header
323: .setIcon(getPreviewIcon(new java.io.File(
324: new java.net.URL(fileField.getText())
325: .getPath())));
326: } catch (Exception ex) {
327: ex.printStackTrace();
328: }
329: }
330:
331: public void windowOpened(WindowEvent e) {
332: }
333:
334: public void windowClosing(WindowEvent e) {
335: CANCELLED = true;
336: this .dispose();
337: }
338:
339: public void windowClosed(WindowEvent e) {
340: }
341:
342: public void windowIconified(WindowEvent e) {
343: }
344:
345: public void windowDeiconified(WindowEvent e) {
346: }
347:
348: public void windowActivated(WindowEvent e) {
349: }
350:
351: public void windowDeactivated(WindowEvent e) {
352: }
353:
354: void browseB_actionPerformed(ActionEvent e) {
355: // Fix until Sun's JVM supports more locales...
356: UIManager.put("FileChooser.lookInLabelText", Local
357: .getString("Look in:"));
358: UIManager.put("FileChooser.upFolderToolTipText", Local
359: .getString("Up One Level"));
360: UIManager.put("FileChooser.newFolderToolTipText", Local
361: .getString("Create New Folder"));
362: UIManager.put("FileChooser.listViewButtonToolTipText", Local
363: .getString("List"));
364: UIManager.put("FileChooser.detailsViewButtonToolTipText", Local
365: .getString("Details"));
366: UIManager.put("FileChooser.fileNameLabelText", Local
367: .getString("File Name:"));
368: UIManager.put("FileChooser.filesOfTypeLabelText", Local
369: .getString("Files of Type:"));
370: UIManager.put("FileChooser.openButtonText", Local
371: .getString("Open"));
372: UIManager.put("FileChooser.openButtonToolTipText", Local
373: .getString("Open selected file"));
374: UIManager.put("FileChooser.cancelButtonText", Local
375: .getString("Cancel"));
376: UIManager.put("FileChooser.cancelButtonToolTipText", Local
377: .getString("Cancel"));
378:
379: JFileChooser chooser = new JFileChooser();
380: chooser.setFileHidingEnabled(false);
381: chooser.setDialogTitle(Local.getString("Choose an image file"));
382: chooser.setAcceptAllFileFilterUsed(false);
383: chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
384: chooser
385: .addChoosableFileFilter(new net.sf.memoranda.ui.htmleditor.filechooser.ImageFilter());
386: chooser
387: .setAccessory(new net.sf.memoranda.ui.htmleditor.filechooser.ImagePreview(
388: chooser));
389: chooser.setPreferredSize(new Dimension(550, 375));
390: java.io.File lastSel = (java.io.File) Context
391: .get("LAST_SELECTED_IMG_FILE");
392: if (lastSel != null)
393: chooser.setCurrentDirectory(lastSel);
394: if (chooser.showOpenDialog(this ) == JFileChooser.APPROVE_OPTION) {
395: try {
396: fileField.setText(chooser.getSelectedFile().toURL()
397: .toString());
398: header
399: .setIcon(getPreviewIcon(chooser
400: .getSelectedFile()));
401: Context.put("LAST_SELECTED_IMG_FILE", chooser
402: .getSelectedFile());
403: } catch (Exception ex) {
404: fileField.setText(chooser.getSelectedFile().getPath());
405: }
406: try {
407: ImageIcon img = new ImageIcon(chooser.getSelectedFile()
408: .getPath());
409: widthField.setText(new Integer(img.getIconWidth())
410: .toString());
411: heightField.setText(new Integer(img.getIconHeight())
412: .toString());
413: } catch (Exception ex) {
414: ex.printStackTrace();
415: }
416: }
417: }
418: }
|