1 package com
.hexidec
.ekit
.component
;
3 import com
.hexidec
.ekit
.EkitCore
;
4 import java
.awt
.Insets
;
5 import java
.awt
.Container
;
6 import java
.awt
.Cursor
;
7 import java
.awt
.Dimension
;
8 import java
.awt
.event
.ActionListener
;
9 import java
.awt
.FlowLayout
;
10 import javax
.swing
.border
.*;
11 import javax
.swing
.BorderFactory
;
12 import javax
.swing
.Box
;
13 import javax
.swing
.BoxLayout
;
14 import javax
.swing
.event
.ListSelectionListener
;
15 import javax
.swing
.event
.ListSelectionEvent
;
16 import javax
.swing
.JButton
;
17 import javax
.swing
.JDialog
;
18 import javax
.swing
.JEditorPane
;
19 import javax
.swing
.JLabel
;
20 import javax
.swing
.JList
;
21 import javax
.swing
.JPanel
;
22 import javax
.swing
.JScrollPane
;
23 import javax
.swing
.JTextField
;
24 import javax
.swing
.JTextPane
;
25 import javax
.swing
.ListSelectionModel
;
26 import javax
.swing
.SwingConstants
;
27 import javax
.swing
.text
.html
.HTML
;
28 import javax
.swing
.text
.html
.HTMLDocument
;
29 import javax
.swing
.text
.html
.HTMLEditorKit
;
30 import javax
.swing
.WindowConstants
;
32 public class ImageDialog
extends JDialog
implements ActionListener
34 private final String
[]Borders
= new String
[] { "none", "solid", "dotted", "dashed", "double", "groove",
35 "ridge", "inset", "outset" };
36 private String
[]BorderColors
= new String
[] { "aqua", "black", "blue", "fuschia", "gray",
37 "green", "lime", "maroon", "navy", "olive",
38 "purple", "red", "silver", "teal", "white", "yellow" };
39 private String
[]BorderSizes
= new String
[] { "none", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
40 private final String
[]Wraps
= new String
[] { "none", "left", "right", "top", "middle", "bottom" };
41 private EkitCore ParentEkit
;
42 private ExtendedHTMLEditorKit ImageHtmlKit
;
43 private HTMLDocument ImageHtmlDoc
;
44 private JList WrapList
;
45 private JList BorderList
;
46 private JList BorderSizeList
;
47 private JList BorderColorList
;
48 private JList ImageList
;
49 private JTextField ImageAltText
;
50 private JTextField ImageWidth
;
51 private JTextField ImageHeight
;
52 private JEditorPane PreviewPane
;
53 private String ImageDir
;
54 private String
[]Images
;
55 private String PreviewImage
;
56 private String SelectedImage
;
58 public ImageDialog(EkitCore parentEkit
, String imageDir
, String
[] imageList
, String title
, boolean modal
)
60 super(parentEkit
.getFrame(), title
, modal
);
63 ParentEkit
= parentEkit
;
68 public void actionPerformed(java
.awt
.event
.ActionEvent e
)
70 if(e
.getActionCommand().equals("apply"))
72 ListSelectionModel sm
= ImageList
.getSelectionModel();
73 if(sm
.isSelectionEmpty())
75 SimpleInfoDialog sidAbout
= new SimpleInfoDialog(ParentEkit
.getFrame(), "Error", true
, "No image selected", SimpleInfoDialog
.ERROR
);
76 ImageList
.requestFocus();
80 if(validateControls())
82 previewSelectedImage();
86 if(e
.getActionCommand().equals("save"))
88 ListSelectionModel sm
= ImageList
.getSelectionModel();
89 if(sm
.isSelectionEmpty())
91 SimpleInfoDialog sidAbout
= new SimpleInfoDialog(ParentEkit
.getFrame(), "Error", true
, "No image selected", SimpleInfoDialog
.ERROR
);
92 ImageList
.requestFocus();
96 if(validateControls())
98 previewSelectedImage();
99 SelectedImage
= PreviewImage
;
104 else if(e
.getActionCommand().equals("cancel"))
113 Container contentPane
= getContentPane();
114 contentPane
.setLayout(new BoxLayout(contentPane
, BoxLayout
.Y_AXIS
));
115 //setBounds(100,100,500,300);
116 setDefaultCloseOperation(WindowConstants
.HIDE_ON_CLOSE
);
118 ImageList
= new JList(Images
);
119 ImageList
.setSelectionMode(ListSelectionModel
.SINGLE_SELECTION
);
120 ImageList
.clearSelection();
121 ListSelectionModel lsm
= ImageList
.getSelectionModel();
123 /* Create the editor kit, document, and stylesheet */
124 PreviewPane
= new JEditorPane();
125 PreviewPane
.setEditable(false
);
126 ImageHtmlKit
= new ExtendedHTMLEditorKit();
127 ImageHtmlDoc
= (HTMLDocument
)(ImageHtmlKit
.createDefaultDocument());
128 ImageHtmlKit
.setDefaultCursor(new Cursor(Cursor
.TEXT_CURSOR
));
129 PreviewPane
.setCaretPosition(0);
130 //PreviewPane.getDocument().addDocumentListener(this);
131 //StyleSheet styleSheet = ImageHtmlDoc.getStyleSheet();
132 //ImageStyleSheet = styleSheet;
133 lsm
.addListSelectionListener(new ListSelectionListener()
135 public void valueChanged(ListSelectionEvent e
)
137 if(!e
.getValueIsAdjusting() && validateControls())
139 previewSelectedImage();
145 JScrollPane imageScrollPane
= new JScrollPane(ImageList
);
146 imageScrollPane
.setPreferredSize(new Dimension(200,250));
147 imageScrollPane
.setMaximumSize(new Dimension(200,250));
148 imageScrollPane
.setAlignmentX(LEFT_ALIGNMENT
);
149 JPanel centerPanel
= new JPanel();
150 centerPanel
.setLayout(new BoxLayout(centerPanel
, BoxLayout
.X_AXIS
));
151 centerPanel
.add(imageScrollPane
);
152 centerPanel
.setBorder(BorderFactory
.createTitledBorder("Server Images"));
154 /* Set up the text pane */
155 PreviewPane
.setEditorKit(ImageHtmlKit
);
156 PreviewPane
.setDocument(ImageHtmlDoc
);
157 PreviewPane
.setMargin(new Insets(4, 4, 4, 4));
158 JScrollPane previewViewport
= new JScrollPane(PreviewPane
);
159 previewViewport
.setVerticalScrollBarPolicy(JScrollPane
.VERTICAL_SCROLLBAR_ALWAYS
);
160 previewViewport
.setPreferredSize(new Dimension(250,250));
161 centerPanel
.add(previewViewport
);
163 JPanel controlsPanel
= new JPanel();
164 controlsPanel
.setLayout(new BoxLayout(controlsPanel
, BoxLayout
.Y_AXIS
));
165 JPanel altPanel
= new JPanel();
166 altPanel
.setLayout(new BoxLayout(altPanel
, BoxLayout
.X_AXIS
));
167 altPanel
.add(Box
.createHorizontalStrut(10));
168 JLabel imageAltTextLabel
= new JLabel("Alternate Text:", SwingConstants
.LEFT
);
169 altPanel
.add(imageAltTextLabel
);
171 ImageAltText
= new JTextField("");
172 ImageAltText
.addActionListener(this);
173 ImageAltText
.setPreferredSize(new Dimension(300,25));
174 ImageAltText
.setMaximumSize(new Dimension(600,25));
175 altPanel
.add(ImageAltText
);
176 altPanel
.add(Box
.createHorizontalStrut(10));
177 controlsPanel
.add(altPanel
);
179 controlsPanel
.add(Box
.createVerticalStrut(5));
181 JPanel dimPanel
= new JPanel();
182 dimPanel
.setLayout(new BoxLayout(dimPanel
, BoxLayout
.X_AXIS
));
183 dimPanel
.add(Box
.createHorizontalStrut(10));
184 JLabel imageWidthLabel
= new JLabel("Width:", SwingConstants
.LEFT
);
185 dimPanel
.add(imageWidthLabel
);
186 ImageWidth
= new JTextField("");
187 ImageWidth
.setPreferredSize(new Dimension(40,25));
188 ImageWidth
.setMaximumSize(new Dimension(40,25));
189 dimPanel
.add(ImageWidth
);
190 JLabel imageWidthPixels
= new JLabel("pix", SwingConstants
.LEFT
);
191 imageWidthPixels
.setPreferredSize(new Dimension(20,10));
192 dimPanel
.add(imageWidthPixels
);
193 dimPanel
.add(Box
.createHorizontalStrut(10));
194 JLabel imageHeightLabel
= new JLabel("Height:", SwingConstants
.LEFT
);
195 dimPanel
.add(imageHeightLabel
);
196 ImageHeight
= new JTextField("");
197 ImageHeight
.setPreferredSize(new Dimension(40,25));
198 ImageHeight
.setMaximumSize(new Dimension(40,25));
199 dimPanel
.add(ImageHeight
);
200 JLabel imageHeightPixels
= new JLabel("pix", SwingConstants
.LEFT
);
201 imageHeightPixels
.setPreferredSize(new Dimension(20,10));
202 dimPanel
.add(imageHeightPixels
);
203 dimPanel
.add(Box
.createHorizontalStrut(10));
205 JLabel wrapLabel
= new JLabel("Wrap:", SwingConstants
.LEFT
);
206 dimPanel
.add(wrapLabel
);
207 WrapList
= new JList(Wraps
);
208 WrapList
.setSelectionMode(ListSelectionModel
.SINGLE_SELECTION
);
209 WrapList
.getSelectionModel().setSelectionInterval(0,0);
210 JScrollPane wrapScrollPane
= new JScrollPane(WrapList
);
211 wrapScrollPane
.setAlignmentX(LEFT_ALIGNMENT
);
212 wrapScrollPane
.setPreferredSize(new Dimension(80,40));
213 wrapScrollPane
.setMaximumSize(new Dimension(80,100));
214 dimPanel
.add(wrapScrollPane
);
215 controlsPanel
.add(dimPanel
);
217 //JPanel borderPanel = new JPanel();
218 //JLabel borderStyleLabel = new JLabel("Style:", SwingConstants.LEFT);
219 //borderPanel.add(borderStyleLabel);
220 //BorderList = new JList(Borders);
221 //BorderList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
222 //BorderList.getSelectionModel().setSelectionInterval(0,0);
223 //JScrollPane borderScrollPane = new JScrollPane(BorderList);
224 //borderScrollPane.setAlignmentX(LEFT_ALIGNMENT);
225 //borderScrollPane.setPreferredSize(new Dimension(80,40));
226 //borderScrollPane.setMaximumSize(new Dimension(80,100));
227 //borderPanel.add(borderScrollPane);
228 //borderPanel.add(Box.createHorizontalStrut(5));
230 dimPanel
.add(Box
.createHorizontalStrut(5));
231 JLabel borderSizeLabel
= new JLabel("Border Size:", SwingConstants
.LEFT
);
232 dimPanel
.add(borderSizeLabel
);
233 BorderSizeList
= new JList(BorderSizes
);
234 BorderSizeList
.setSelectionMode(ListSelectionModel
.SINGLE_SELECTION
);
235 BorderSizeList
.getSelectionModel().setSelectionInterval(0,0);
236 JScrollPane borderSizeScrollPane
= new JScrollPane(BorderSizeList
);
237 borderSizeScrollPane
.setAlignmentX(LEFT_ALIGNMENT
);
238 borderSizeScrollPane
.setPreferredSize(new Dimension(80,40));
239 borderSizeScrollPane
.setMaximumSize(new Dimension(80,100));
240 dimPanel
.add(borderSizeScrollPane
);
241 dimPanel
.add(Box
.createHorizontalStrut(10));
242 dimPanel
.add(Box
.createVerticalStrut(10));
244 //JLabel borderColorLabel = new JLabel("Color:", SwingConstants.LEFT);
245 //borderPanel.add(borderColorLabel);
246 //BorderColorList = new JList(BorderColors);
247 //BorderColorList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
248 //JScrollPane borderColorScrollPane = new JScrollPane(BorderColorList);
249 //borderColorScrollPane.setAlignmentX(LEFT_ALIGNMENT);
250 //borderColorScrollPane.setPreferredSize(new Dimension(80,40));
251 //borderPanel.add(borderColorScrollPane);
252 //controlsPanel.add(borderPanel);
254 JPanel buttonPanel
= new JPanel();
255 buttonPanel
.setBorder(new SoftBevelBorder(BevelBorder
.LOWERED
));
256 JButton applyButton
= new JButton("Apply");
257 applyButton
.setActionCommand("apply");
258 applyButton
.addActionListener(this);
260 JButton saveButton
= new JButton("Accept");
261 saveButton
.setActionCommand("save");
262 saveButton
.addActionListener(this);
263 JButton cancelButton
= new JButton("Cancel");
264 cancelButton
.setActionCommand("cancel");
265 cancelButton
.addActionListener(this);
267 buttonPanel
.add(applyButton
);
268 buttonPanel
.add(saveButton
);
269 buttonPanel
.add(cancelButton
);
271 contentPane
.add(centerPanel
);
272 contentPane
.add(controlsPanel
);
273 contentPane
.add(buttonPanel
);
278 private void previewSelectedImage()
280 ListSelectionModel sm
= ImageList
.getSelectionModel();
281 if(!sm
.isSelectionEmpty())
283 String theImage
= Images
[sm
.getMinSelectionIndex()];
286 // Clear the preview area
287 PreviewPane
.setText("");
288 StringBuffer attrString
= new StringBuffer();
289 if(!ImageHeight
.getText().equals(""))
291 attrString
.append("HEIGHT=\"" + ImageHeight
.getText() + "\" ");
293 if(!ImageWidth
.getText().equals(""))
295 attrString
.append("WIDTH=\"" + ImageWidth
.getText() + "\" ");
297 if(!ImageAltText
.getText().equals(""))
299 attrString
.append("ALT=\"" + ImageAltText
.getText() + "\" ");
301 if(!WrapList
.getSelectionModel().isSelectionEmpty())
303 String theWrap
= Wraps
[WrapList
.getSelectionModel().getMinSelectionIndex()];
304 if(!theWrap
.equals("none"))
306 attrString
.append("ALIGN=\"" + theWrap
+ "\" ");
309 //if(!BorderList.getSelectionModel().isSelectionEmpty())
311 //String theBorder = Borders[BorderList.getSelectionModel().getMinSelectionIndex()];
312 //if(!theBorder.equals("none"))
314 String borderSize
= null
;
315 String borderColor
= null
;
316 if(!BorderSizeList
.getSelectionModel().isSelectionEmpty())
318 borderSize
= BorderSizes
[BorderSizeList
.getSelectionModel().getMinSelectionIndex()];
319 if(!borderSize
.equals("none"))
320 attrString
.append("BORDER=" + borderSize
);
324 borderSize
= BorderSizes
[0];
326 //if(!BorderColorList.getSelectionModel().isSelectionEmpty())
328 // borderColor = BorderColors[BorderColorList.getSelectionModel().getMinSelectionIndex()];
332 // borderColor = "gray";
334 //attrString.append("STYLE=\"border: " + borderColor + " " + borderSize + "px " + theBorder + "\"");
339 PreviewImage
= "<IMG SRC=" + ImageDir
+ "/" + theImage
+ " " + attrString
.toString() + ">";
340 ImageHtmlKit
.insertHTML(ImageHtmlDoc
, 0, PreviewImage
, 0, 0, HTML
.Tag
.IMG
);
345 System
.err
.println("Exception previewing image");
350 private boolean validateControls()
352 boolean result
= true
;
354 if(!ImageWidth
.getText().equals(""))
358 Integer
.parseInt(ImageWidth
.getText());
360 catch (NumberFormatException e
)
363 SimpleInfoDialog sidAbout
= new SimpleInfoDialog(ParentEkit
.getFrame(), "Error", true
, "Image Width is not an integer", SimpleInfoDialog
.ERROR
);
364 ImageWidth
.requestFocus();
367 if( result
&& !ImageHeight
.getText().equals(""))
371 Integer
.parseInt(ImageHeight
.getText());
373 catch (NumberFormatException e
)
376 SimpleInfoDialog sidAbout
= new SimpleInfoDialog(ParentEkit
.getFrame(), "Error", true
, "Image Height is not an integer", SimpleInfoDialog
.ERROR
);
377 ImageHeight
.requestFocus();
384 public String
getSelectedImage()
386 return SelectedImage
;