| 1 | /* |
| 2 | GNU Lesser General Public License |
| 3 | |
| 4 | ImageDialog |
| 5 | Copyright (C) 2003 Howard Kistler & other contributors |
| 6 | |
| 7 | This library is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation; either |
| 10 | version 2.1 of the License, or (at your option) any later version. |
| 11 | |
| 12 | This library is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with this library; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
| 22 | package com.hexidec.ekit.component; |
| 23 | |
| 24 | import com.hexidec.ekit.EkitCore; |
| 25 | import java.awt.Insets; |
| 26 | import java.awt.Container; |
| 27 | import java.awt.Cursor; |
| 28 | import java.awt.Dimension; |
| 29 | import java.awt.event.ActionListener; |
| 30 | import java.awt.FlowLayout; |
| 31 | import javax.swing.border.*; |
| 32 | import javax.swing.BorderFactory; |
| 33 | import javax.swing.Box; |
| 34 | import javax.swing.BoxLayout; |
| 35 | import javax.swing.event.ListSelectionListener; |
| 36 | import javax.swing.event.ListSelectionEvent; |
| 37 | import javax.swing.JButton; |
| 38 | import javax.swing.JDialog; |
| 39 | import javax.swing.JEditorPane; |
| 40 | import javax.swing.JLabel; |
| 41 | import javax.swing.JList; |
| 42 | import javax.swing.JPanel; |
| 43 | import javax.swing.JScrollPane; |
| 44 | import javax.swing.JTextField; |
| 45 | import javax.swing.JTextPane; |
| 46 | import javax.swing.ListSelectionModel; |
| 47 | import javax.swing.SwingConstants; |
| 48 | import javax.swing.text.html.HTML; |
| 49 | import javax.swing.text.html.HTMLDocument; |
| 50 | import javax.swing.text.html.HTMLEditorKit; |
| 51 | import javax.swing.WindowConstants; |
| 52 | |
| 53 | public class ImageDialog extends JDialog implements ActionListener |
| 54 | { |
| 55 | private final String[] Borders = new String[] |
| 56 | { |
| 57 | "none", |
| 58 | "solid", |
| 59 | "dotted", |
| 60 | "dashed", |
| 61 | "double", |
| 62 | "groove", |
| 63 | "ridge", |
| 64 | "inset", |
| 65 | "outset" |
| 66 | }; |
| 67 | private String[] BorderColors = new String[] |
| 68 | { |
| 69 | "none", |
| 70 | "aqua", |
| 71 | "black", |
| 72 | "blue", |
| 73 | "fuschia", |
| 74 | "gray", |
| 75 | "green", |
| 76 | "lime", |
| 77 | "maroon", |
| 78 | "navy", |
| 79 | "olive", |
| 80 | "purple", |
| 81 | "red", |
| 82 | "silver", |
| 83 | "teal", |
| 84 | "white", |
| 85 | "yellow" |
| 86 | }; |
| 87 | private String[]BorderSizes = new String[] |
| 88 | { |
| 89 | "none", |
| 90 | "1", |
| 91 | "2", |
| 92 | "3", |
| 93 | "4", |
| 94 | "5", |
| 95 | "6", |
| 96 | "7", |
| 97 | "8", |
| 98 | "9", |
| 99 | "10" |
| 100 | }; |
| 101 | private final String[]Wraps = new String[] |
| 102 | { |
| 103 | "none", |
| 104 | "left", |
| 105 | "right", |
| 106 | "top", |
| 107 | "middle", |
| 108 | "bottom" |
| 109 | }; |
| 110 | |
| 111 | private EkitCore ParentEkit; |
| 112 | private ExtendedHTMLEditorKit ImageHtmlKit; |
| 113 | private HTMLDocument ImageHtmlDoc; |
| 114 | private JList WrapList; |
| 115 | private JList BorderList; |
| 116 | private JList BorderSizeList; |
| 117 | private JList BorderColorList; |
| 118 | private JList ImageList; |
| 119 | private JTextField ImageAltText; |
| 120 | private JTextField ImageWidth; |
| 121 | private JTextField ImageHeight; |
| 122 | private JEditorPane PreviewPane; |
| 123 | |
| 124 | private String ImageDir; |
| 125 | private String[] Images; |
| 126 | private String PreviewImage; |
| 127 | private String SelectedImage; |
| 128 | |
| 129 | public ImageDialog(EkitCore parentEkit, String imageDir, String[] imageList, String title, boolean modal) |
| 130 | { |
| 131 | super(parentEkit.getFrame(), title, modal); |
| 132 | ImageDir = imageDir; |
| 133 | Images = imageList; |
| 134 | ParentEkit = parentEkit; |
| 135 | SelectedImage = null; |
| 136 | init(); |
| 137 | } |
| 138 | |
| 139 | public void actionPerformed(java.awt.event.ActionEvent e) |
| 140 | { |
| 141 | if(e.getActionCommand().equals("apply")) |
| 142 | { |
| 143 | ListSelectionModel sm = ImageList.getSelectionModel(); |
| 144 | if(sm.isSelectionEmpty()) |
| 145 | { |
| 146 | SimpleInfoDialog sidAbout = new SimpleInfoDialog(ParentEkit.getFrame(), "Error", true, "No image selected", SimpleInfoDialog.ERROR); |
| 147 | ImageList.requestFocus(); |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | if(validateControls()) |
| 152 | { |
| 153 | previewSelectedImage(); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | if(e.getActionCommand().equals("save")) |
| 158 | { |
| 159 | ListSelectionModel sm = ImageList.getSelectionModel(); |
| 160 | if(sm.isSelectionEmpty()) |
| 161 | { |
| 162 | SimpleInfoDialog sidAbout = new SimpleInfoDialog(ParentEkit.getFrame(), "Error", true, "No image selected", SimpleInfoDialog.ERROR); |
| 163 | ImageList.requestFocus(); |
| 164 | } |
| 165 | else |
| 166 | { |
| 167 | if(validateControls()) |
| 168 | { |
| 169 | previewSelectedImage(); |
| 170 | SelectedImage = PreviewImage; |
| 171 | hide(); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | else if(e.getActionCommand().equals("cancel")) |
| 176 | { |
| 177 | hide(); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | public void init() |
| 182 | { |
| 183 | SelectedImage=""; |
| 184 | Container contentPane = getContentPane(); |
| 185 | contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); |
| 186 | //setBounds(100,100,500,300); |
| 187 | setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE); |
| 188 | |
| 189 | ImageList = new JList(Images); |
| 190 | ImageList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
| 191 | ImageList.clearSelection(); |
| 192 | ListSelectionModel lsm = ImageList.getSelectionModel(); |
| 193 | |
| 194 | /* Create the editor kit, document, and stylesheet */ |
| 195 | PreviewPane = new JEditorPane(); |
| 196 | PreviewPane.setEditable(false); |
| 197 | ImageHtmlKit = new ExtendedHTMLEditorKit(); |
| 198 | ImageHtmlDoc = (HTMLDocument)(ImageHtmlKit.createDefaultDocument()); |
| 199 | ImageHtmlKit.setDefaultCursor(new Cursor(Cursor.TEXT_CURSOR)); |
| 200 | PreviewPane.setCaretPosition(0); |
| 201 | //PreviewPane.getDocument().addDocumentListener(this); |
| 202 | //StyleSheet styleSheet = ImageHtmlDoc.getStyleSheet(); |
| 203 | //ImageStyleSheet = styleSheet; |
| 204 | lsm.addListSelectionListener(new ListSelectionListener() |
| 205 | { |
| 206 | public void valueChanged(ListSelectionEvent e) |
| 207 | { |
| 208 | if(!e.getValueIsAdjusting() && validateControls()) |
| 209 | { |
| 210 | previewSelectedImage(); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | }); |
| 215 | |
| 216 | JScrollPane imageScrollPane = new JScrollPane(ImageList); |
| 217 | imageScrollPane.setPreferredSize(new Dimension(200,250)); |
| 218 | imageScrollPane.setMaximumSize(new Dimension(200,250)); |
| 219 | imageScrollPane.setAlignmentX(LEFT_ALIGNMENT); |
| 220 | JPanel centerPanel = new JPanel(); |
| 221 | centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.X_AXIS)); |
| 222 | centerPanel.add(imageScrollPane); |
| 223 | centerPanel.setBorder(BorderFactory.createTitledBorder("Server Images")); |
| 224 | |
| 225 | /* Set up the text pane */ |
| 226 | PreviewPane.setEditorKit(ImageHtmlKit); |
| 227 | PreviewPane.setDocument(ImageHtmlDoc); |
| 228 | PreviewPane.setMargin(new Insets(4, 4, 4, 4)); |
| 229 | JScrollPane previewViewport = new JScrollPane(PreviewPane); |
| 230 | previewViewport.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); |
| 231 | previewViewport.setPreferredSize(new Dimension(250,250)); |
| 232 | centerPanel.add(previewViewport); |
| 233 | |
| 234 | JPanel controlsPanel = new JPanel(); |
| 235 | controlsPanel.setLayout(new BoxLayout(controlsPanel, BoxLayout.Y_AXIS)); |
| 236 | JPanel altPanel = new JPanel(); |
| 237 | altPanel.setLayout(new BoxLayout(altPanel, BoxLayout.X_AXIS)); |
| 238 | altPanel.add(Box.createHorizontalStrut(10)); |
| 239 | JLabel imageAltTextLabel = new JLabel("Alternate Text:", SwingConstants.LEFT); |
| 240 | altPanel.add(imageAltTextLabel); |
| 241 | |
| 242 | ImageAltText = new JTextField(""); |
| 243 | ImageAltText.addActionListener(this); |
| 244 | ImageAltText.setPreferredSize(new Dimension(300,25)); |
| 245 | ImageAltText.setMaximumSize(new Dimension(600,25)); |
| 246 | altPanel.add(ImageAltText); |
| 247 | altPanel.add(Box.createHorizontalStrut(10)); |
| 248 | controlsPanel.add(altPanel); |
| 249 | controlsPanel.add(Box.createVerticalStrut(5)); |
| 250 | |
| 251 | JPanel dimPanel = new JPanel(); |
| 252 | dimPanel.setLayout(new BoxLayout(dimPanel, BoxLayout.X_AXIS)); |
| 253 | dimPanel.add(Box.createHorizontalStrut(10)); |
| 254 | JLabel imageWidthLabel = new JLabel("Width:", SwingConstants.LEFT); |
| 255 | dimPanel.add(imageWidthLabel); |
| 256 | ImageWidth = new JTextField(""); |
| 257 | ImageWidth.setPreferredSize(new Dimension(40,25)); |
| 258 | ImageWidth.setMaximumSize(new Dimension(40,25)); |
| 259 | dimPanel.add(ImageWidth); |
| 260 | JLabel imageWidthPixels = new JLabel("pix", SwingConstants.LEFT); |
| 261 | imageWidthPixels.setPreferredSize(new Dimension(20,10)); |
| 262 | dimPanel.add(imageWidthPixels); |
| 263 | dimPanel.add(Box.createHorizontalStrut(10)); |
| 264 | JLabel imageHeightLabel = new JLabel("Height:", SwingConstants.LEFT); |
| 265 | dimPanel.add(imageHeightLabel); |
| 266 | ImageHeight = new JTextField(""); |
| 267 | ImageHeight.setPreferredSize(new Dimension(40,25)); |
| 268 | ImageHeight.setMaximumSize(new Dimension(40,25)); |
| 269 | dimPanel.add(ImageHeight); |
| 270 | JLabel imageHeightPixels = new JLabel("pix", SwingConstants.LEFT); |
| 271 | imageHeightPixels.setPreferredSize(new Dimension(20,10)); |
| 272 | dimPanel.add(imageHeightPixels); |
| 273 | dimPanel.add(Box.createHorizontalStrut(10)); |
| 274 | |
| 275 | JLabel wrapLabel = new JLabel("Wrap:", SwingConstants.LEFT); |
| 276 | dimPanel.add(wrapLabel); |
| 277 | WrapList = new JList(Wraps); |
| 278 | WrapList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
| 279 | WrapList.getSelectionModel().setSelectionInterval(0,0); |
| 280 | JScrollPane wrapScrollPane = new JScrollPane(WrapList); |
| 281 | wrapScrollPane.setAlignmentX(LEFT_ALIGNMENT); |
| 282 | wrapScrollPane.setPreferredSize(new Dimension(80,40)); |
| 283 | wrapScrollPane.setMaximumSize(new Dimension(80,100)); |
| 284 | dimPanel.add(wrapScrollPane); |
| 285 | controlsPanel.add(dimPanel); |
| 286 | |
| 287 | /* |
| 288 | JPanel borderPanel = new JPanel(); |
| 289 | JLabel borderStyleLabel = new JLabel("Style:", SwingConstants.LEFT); |
| 290 | borderPanel.add(borderStyleLabel); |
| 291 | BorderList = new JList(Borders); |
| 292 | BorderList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
| 293 | BorderList.getSelectionModel().setSelectionInterval(0,0); |
| 294 | JScrollPane borderScrollPane = new JScrollPane(BorderList); |
| 295 | borderScrollPane.setAlignmentX(LEFT_ALIGNMENT); |
| 296 | borderScrollPane.setPreferredSize(new Dimension(80,40)); |
| 297 | borderScrollPane.setMaximumSize(new Dimension(80,100)); |
| 298 | borderPanel.add(borderScrollPane); |
| 299 | borderPanel.add(Box.createHorizontalStrut(5)); |
| 300 | */ |
| 301 | |
| 302 | dimPanel.add(Box.createHorizontalStrut(5)); |
| 303 | JLabel borderSizeLabel = new JLabel("Border Size:", SwingConstants.LEFT); |
| 304 | dimPanel.add(borderSizeLabel); |
| 305 | BorderSizeList = new JList(BorderSizes); |
| 306 | BorderSizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
| 307 | BorderSizeList.getSelectionModel().setSelectionInterval(0,0); |
| 308 | JScrollPane borderSizeScrollPane = new JScrollPane(BorderSizeList); |
| 309 | borderSizeScrollPane.setAlignmentX(LEFT_ALIGNMENT); |
| 310 | borderSizeScrollPane.setPreferredSize(new Dimension(80,40)); |
| 311 | borderSizeScrollPane.setMaximumSize(new Dimension(80,100)); |
| 312 | dimPanel.add(borderSizeScrollPane); |
| 313 | dimPanel.add(Box.createHorizontalStrut(10)); |
| 314 | dimPanel.add(Box.createVerticalStrut(10)); |
| 315 | |
| 316 | /* |
| 317 | JLabel borderColorLabel = new JLabel("Color:", SwingConstants.LEFT); |
| 318 | borderPanel.add(borderColorLabel); |
| 319 | BorderColorList = new JList(BorderColors); |
| 320 | BorderColorList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
| 321 | JScrollPane borderColorScrollPane = new JScrollPane(BorderColorList); |
| 322 | borderColorScrollPane.setAlignmentX(LEFT_ALIGNMENT); |
| 323 | borderColorScrollPane.setPreferredSize(new Dimension(80,40)); |
| 324 | borderPanel.add(borderColorScrollPane); |
| 325 | controlsPanel.add(borderPanel); |
| 326 | */ |
| 327 | |
| 328 | JPanel buttonPanel= new JPanel(); |
| 329 | buttonPanel.setBorder(new SoftBevelBorder(BevelBorder.LOWERED)); |
| 330 | JButton applyButton = new JButton("Apply"); |
| 331 | applyButton.setActionCommand("apply"); |
| 332 | applyButton.addActionListener(this); |
| 333 | |
| 334 | JButton saveButton = new JButton("Accept"); |
| 335 | saveButton.setActionCommand("save"); |
| 336 | saveButton.addActionListener(this); |
| 337 | JButton cancelButton = new JButton("Cancel"); |
| 338 | cancelButton.setActionCommand("cancel"); |
| 339 | cancelButton.addActionListener(this); |
| 340 | |
| 341 | buttonPanel.add(applyButton); |
| 342 | buttonPanel.add(saveButton); |
| 343 | buttonPanel.add(cancelButton); |
| 344 | |
| 345 | contentPane.add(centerPanel); |
| 346 | contentPane.add(controlsPanel); |
| 347 | contentPane.add(buttonPanel); |
| 348 | this.pack(); |
| 349 | setVisible(true); |
| 350 | } |
| 351 | |
| 352 | private void previewSelectedImage() |
| 353 | { |
| 354 | ListSelectionModel sm = ImageList.getSelectionModel(); |
| 355 | if(!sm.isSelectionEmpty()) |
| 356 | { |
| 357 | String theImage = Images[sm.getMinSelectionIndex()]; |
| 358 | try |
| 359 | { |
| 360 | // Clear the preview area |
| 361 | PreviewPane.setText(""); |
| 362 | StringBuffer attrString = new StringBuffer(); |
| 363 | if(!ImageHeight.getText().equals("")) |
| 364 | { |
| 365 | attrString.append("HEIGHT=\"" + ImageHeight.getText() + "\" "); |
| 366 | } |
| 367 | if(!ImageWidth.getText().equals("")) |
| 368 | { |
| 369 | attrString.append("WIDTH=\"" + ImageWidth.getText() + "\" "); |
| 370 | } |
| 371 | if(!ImageAltText.getText().equals("")) |
| 372 | { |
| 373 | attrString.append("ALT=\"" + ImageAltText.getText() + "\" "); |
| 374 | } |
| 375 | if(!WrapList.getSelectionModel().isSelectionEmpty()) |
| 376 | { |
| 377 | String theWrap = Wraps[WrapList.getSelectionModel().getMinSelectionIndex()]; |
| 378 | if(!theWrap.equals("none")) |
| 379 | { |
| 380 | attrString.append("ALIGN=\"" + theWrap + "\" "); |
| 381 | } |
| 382 | } |
| 383 | /* |
| 384 | if(!BorderList.getSelectionModel().isSelectionEmpty())\r |
| 385 | {\r |
| 386 | String theBorder = Borders[BorderList.getSelectionModel().getMinSelectionIndex()];\r |
| 387 | if(!theBorder.equals("none"))\r |
| 388 | { |
| 389 | */\r |
| 390 | String borderSize = null; |
| 391 | String borderColor = null; |
| 392 | if(!BorderSizeList.getSelectionModel().isSelectionEmpty()) |
| 393 | { |
| 394 | borderSize = BorderSizes[BorderSizeList.getSelectionModel().getMinSelectionIndex()]; |
| 395 | if(!borderSize.equals("none")) |
| 396 | { |
| 397 | attrString.append("BORDER=" + borderSize); |
| 398 | } |
| 399 | } |
| 400 | else |
| 401 | { |
| 402 | borderSize = BorderSizes[0]; |
| 403 | } |
| 404 | /* |
| 405 | if(!BorderColorList.getSelectionModel().isSelectionEmpty()) |
| 406 | { |
| 407 | borderColor = BorderColors[BorderColorList.getSelectionModel().getMinSelectionIndex()]; |
| 408 | } |
| 409 | else |
| 410 | { |
| 411 | borderColor = "gray"; |
| 412 | } |
| 413 | attrString.append("STYLE=\"border: " + borderColor + " " + borderSize + "px " + theBorder + "\""); |
| 414 | } |
| 415 | } |
| 416 | */ |
| 417 | PreviewImage = "<IMG SRC=" + ImageDir + "/" + theImage + " " + attrString.toString() + ">"; |
| 418 | ImageHtmlKit.insertHTML(ImageHtmlDoc, 0, PreviewImage, 0, 0, HTML.Tag.IMG); |
| 419 | repaint(); |
| 420 | } |
| 421 | catch(Exception ex) |
| 422 | { |
| 423 | System.err.println("Exception previewing image"); |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | private boolean validateControls() |
| 429 | { |
| 430 | boolean result = true; |
| 431 | if(!ImageWidth.getText().equals("")) |
| 432 | { |
| 433 | try |
| 434 | { |
| 435 | Integer.parseInt(ImageWidth.getText()); |
| 436 | } |
| 437 | catch (NumberFormatException e) |
| 438 | { |
| 439 | result = false; |
| 440 | SimpleInfoDialog sidAbout = new SimpleInfoDialog(ParentEkit.getFrame(), "Error", true, "Image Width is not an integer", SimpleInfoDialog.ERROR); |
| 441 | ImageWidth.requestFocus(); |
| 442 | } |
| 443 | } |
| 444 | if( result && !ImageHeight.getText().equals("")) |
| 445 | { |
| 446 | try |
| 447 | { |
| 448 | Integer.parseInt(ImageHeight.getText()); |
| 449 | } |
| 450 | catch (NumberFormatException e) |
| 451 | { |
| 452 | result = false; |
| 453 | SimpleInfoDialog sidAbout = new SimpleInfoDialog(ParentEkit.getFrame(), "Error", true, "Image Height is not an integer", SimpleInfoDialog.ERROR); |
| 454 | ImageHeight.requestFocus(); |
| 455 | } |
| 456 | } |
| 457 | return result; |
| 458 | } |
| 459 | |
| 460 | public String getSelectedImage() |
| 461 | { |
| 462 | return SelectedImage; |
| 463 | } |
| 464 | } |