1 18 package org.apache.batik.util.gui; 19 20 import java.awt.BorderLayout ; 21 import java.awt.Component ; 22 import java.awt.FlowLayout ; 23 import java.awt.GridBagConstraints ; 24 import java.awt.GridBagLayout ; 25 import java.awt.Insets ; 26 import java.awt.event.ActionEvent ; 27 import java.util.ArrayList ; 28 import java.util.Enumeration ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Locale ; 33 import java.util.Map ; 34 import java.util.ResourceBundle ; 35 import java.util.StringTokenizer ; 36 37 import javax.swing.AbstractAction ; 38 import javax.swing.Action ; 39 import javax.swing.BorderFactory ; 40 import javax.swing.DefaultListModel ; 41 import javax.swing.JButton ; 42 import javax.swing.JComboBox ; 43 import javax.swing.JDialog ; 44 import javax.swing.JLabel ; 45 import javax.swing.JList ; 46 import javax.swing.JOptionPane ; 47 import javax.swing.JPanel ; 48 import javax.swing.JScrollPane ; 49 import javax.swing.ListSelectionModel ; 50 import javax.swing.event.ListDataEvent ; 51 import javax.swing.event.ListDataListener ; 52 import javax.swing.event.ListSelectionEvent ; 53 import javax.swing.event.ListSelectionListener ; 54 55 import org.apache.batik.util.gui.resource.ActionMap; 56 import org.apache.batik.util.gui.resource.ButtonFactory; 57 import org.apache.batik.util.gui.resource.MissingListenerException; 58 import org.apache.batik.util.gui.resource.ResourceManager; 59 60 66 public class CSSMediaPanel extends JPanel implements ActionMap { 67 68 71 protected final static String RESOURCES = 72 "org.apache.batik.util.gui.resources.CSSMediaPanel"; 73 74 77 protected static ResourceBundle bundle; 78 79 82 protected static ResourceManager resources; 83 84 static { 85 bundle = ResourceBundle.getBundle(RESOURCES, Locale.getDefault()); 86 resources = new ResourceManager(bundle); 87 } 88 89 92 protected JButton removeButton; 93 94 97 protected JButton addButton; 98 99 102 protected JButton clearButton; 103 104 107 protected DefaultListModel listModel = new DefaultListModel (); 108 109 112 protected JList mediaList; 113 114 117 public CSSMediaPanel() { 118 super(new GridBagLayout ()); 119 120 listeners.put("AddButtonAction", new AddButtonAction()); 121 listeners.put("RemoveButtonAction", new RemoveButtonAction()); 122 listeners.put("ClearButtonAction", new ClearButtonAction()); 123 124 setBorder(BorderFactory.createTitledBorder 125 (BorderFactory.createEtchedBorder(), 126 resources.getString("Panel.title"))); 127 128 ExtendedGridBagConstraints constraints = 129 new ExtendedGridBagConstraints(); 130 131 constraints.insets = new Insets (5, 5, 5, 5); 132 133 mediaList = new JList (); 134 mediaList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 135 mediaList.setModel(listModel); 136 mediaList.addListSelectionListener(new MediaListSelectionListener()); 137 listModel.addListDataListener(new MediaListDataListener()); 138 139 JScrollPane scrollPane = new JScrollPane (); 140 scrollPane.setBorder(BorderFactory.createLoweredBevelBorder()); 141 constraints.weightx = 1.0; 142 constraints.weighty = 1.0; 143 constraints.fill = GridBagConstraints.BOTH; 144 constraints.setGridBounds(0, 0, 1, 3); 145 scrollPane.getViewport().add(mediaList); 146 add(scrollPane, constraints); 147 148 ButtonFactory bf = new ButtonFactory(bundle, this); 149 constraints.weightx = 0; 150 constraints.weighty = 0; 151 constraints.fill = GridBagConstraints.HORIZONTAL; 152 constraints.anchor = GridBagConstraints.NORTH; 153 154 addButton = bf.createJButton("AddButton"); 155 constraints.setGridBounds(1, 0, 1, 1); 156 add(addButton, constraints); 157 158 removeButton = bf.createJButton("RemoveButton"); 159 constraints.setGridBounds(1, 1, 1, 1); 160 add(removeButton, constraints); 161 162 clearButton = bf.createJButton("ClearButton"); 163 constraints.setGridBounds(1, 2, 1, 1); 164 add(clearButton, constraints); 165 166 updateButtons(); 167 } 168 169 172 protected void updateButtons() { 173 removeButton.setEnabled(!mediaList.isSelectionEmpty()); 174 clearButton.setEnabled(!listModel.isEmpty()); 175 } 176 177 182 public void setMedia(List mediaList) { 183 listModel.removeAllElements(); 184 Iterator iter = mediaList.iterator(); 185 while (iter.hasNext()) { 186 listModel.addElement(iter.next()); 187 } 188 } 189 190 196 public void setMedia(String media) { 197 listModel.removeAllElements(); 198 StringTokenizer tokens = new StringTokenizer (media, " "); 199 while (tokens.hasMoreTokens()) { 200 listModel.addElement(tokens.nextToken()); 201 } 202 } 203 204 207 public List getMedia() { 208 List media = new ArrayList (listModel.size()); 209 Enumeration e = listModel.elements(); 210 while (e.hasMoreElements()) { 211 media.add(e.nextElement()); 212 } 213 return media; 214 } 215 216 219 public String getMediaAsString() { 220 StringBuffer buffer = new StringBuffer (); 221 Enumeration e = listModel.elements(); 222 while (e.hasMoreElements()) { 223 buffer.append((String )e.nextElement()); 224 buffer.append(" "); 225 } 226 return buffer.toString(); 227 } 228 229 235 public static int showDialog(Component parent, String title) { 236 return showDialog(parent, title, ""); 237 } 238 239 246 public static int showDialog(Component parent, 247 String title, 248 List mediaList) { 249 Dialog dialog = new Dialog (parent, title, mediaList); 250 dialog.setModal(true); 251 dialog.pack(); 252 dialog.show(); 253 return dialog.getReturnCode(); 254 } 255 256 263 public static int showDialog(Component parent, 264 String title, 265 String media) { 266 Dialog dialog = new Dialog (parent, title, media); 267 dialog.setModal(true); 268 dialog.pack(); 269 dialog.show(); 270 return dialog.getReturnCode(); 271 } 272 273 276 protected Map listeners = new HashMap (); 277 278 284 public Action getAction(String key) throws MissingListenerException { 285 return (Action )listeners.get(key); 286 } 287 288 291 protected class AddButtonAction extends AbstractAction { 292 public void actionPerformed(ActionEvent e) { 293 AddMediumDialog dialog = new AddMediumDialog(CSSMediaPanel.this); 294 dialog.pack(); 295 dialog.show(); 296 297 if ((dialog.getReturnCode() == AddMediumDialog.CANCEL_OPTION) || 298 (dialog.getMedium() == null)) { 299 return; 300 } 301 302 String medium = dialog.getMedium().trim(); 303 if (medium.length() == 0 || listModel.contains(medium)) { 304 return; 305 } 306 307 for (int i = 0; i < listModel.size() && medium != null; ++i) { 308 String s = (String )listModel.getElementAt(i); 309 int c = medium.compareTo(s); 310 if (c == 0) { 311 medium = null; 312 } else if (c < 0) { 313 listModel.insertElementAt(medium, i); 314 medium = null; 315 } 316 } 317 if (medium != null) { 318 listModel.addElement(medium); 319 } 320 } 321 } 322 323 326 protected class RemoveButtonAction extends AbstractAction { 327 public void actionPerformed(ActionEvent e) { 328 int index = mediaList.getSelectedIndex(); 329 mediaList.clearSelection(); 330 if (index >= 0) { 331 listModel.removeElementAt(index); 332 } 333 } 334 } 335 336 339 protected class ClearButtonAction extends AbstractAction { 340 public void actionPerformed(ActionEvent e) { 341 mediaList.clearSelection(); 342 listModel.removeAllElements(); 343 } 344 } 345 346 349 protected class MediaListSelectionListener 350 implements ListSelectionListener { 351 352 public void valueChanged(ListSelectionEvent e) { 353 updateButtons(); 354 } 355 } 356 357 360 protected class MediaListDataListener implements ListDataListener { 361 362 public void contentsChanged(ListDataEvent e) { 363 updateButtons(); 364 } 365 366 public void intervalAdded(ListDataEvent e) { 367 updateButtons(); 368 } 369 370 public void intervalRemoved(ListDataEvent e) { 371 updateButtons(); 372 } 373 } 374 375 377 380 public static class AddMediumDialog extends JDialog implements ActionMap { 381 382 385 public final static int OK_OPTION = 0; 386 387 390 public final static int CANCEL_OPTION = 1; 391 392 395 protected JComboBox medium; 396 397 400 protected int returnCode; 401 402 407 public AddMediumDialog(Component parent) { 408 super(JOptionPane.getFrameForComponent(parent), 409 resources.getString("AddMediumDialog.title")); 410 setModal(true); 411 412 listeners.put("OKButtonAction", new OKButtonAction()); 413 listeners.put("CancelButtonAction", new CancelButtonAction()); 414 415 getContentPane().add(createContentPanel(), BorderLayout.CENTER); 416 getContentPane().add(createButtonsPanel(), BorderLayout.SOUTH); 417 } 418 419 422 public String getMedium() { 423 return (String )medium.getSelectedItem(); 424 } 425 426 429 protected Component createContentPanel() { 430 JPanel panel = new JPanel (new BorderLayout ()); 431 panel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); 432 panel.add(new JLabel (resources.getString("AddMediumDialog.label")), 433 BorderLayout.WEST); 434 435 medium = new JComboBox (); 436 medium.setEditable(true); 437 String media = resources.getString("Media.list"); 438 StringTokenizer tokens = new StringTokenizer (media, " "); 439 while (tokens.hasMoreTokens()) { 440 medium.addItem(tokens.nextToken()); 441 } 442 panel.add(medium, BorderLayout.CENTER); 443 return panel; 444 } 445 446 449 protected Component createButtonsPanel() { 450 JPanel panel = new JPanel (new FlowLayout (FlowLayout.RIGHT)); 451 ButtonFactory bf = new ButtonFactory(bundle, this); 452 panel.add(bf.createJButton("OKButton")); 453 panel.add(bf.createJButton("CancelButton")); 454 return panel; 455 } 456 457 461 public int getReturnCode() { 462 return returnCode; 463 } 464 465 468 protected Map listeners = new HashMap (); 469 470 476 public Action getAction(String key) throws MissingListenerException { 477 return (Action )listeners.get(key); 478 } 479 480 483 protected class OKButtonAction extends AbstractAction { 484 public void actionPerformed(ActionEvent e) { 485 returnCode = OK_OPTION; 486 dispose(); 487 } 488 } 489 490 493 protected class CancelButtonAction extends AbstractAction { 494 public void actionPerformed(ActionEvent e) { 495 returnCode = CANCEL_OPTION; 496 dispose(); 497 } 498 } 499 } 500 501 503 506 public static class Dialog extends JDialog implements ActionMap { 507 508 511 public final static int OK_OPTION = 0; 512 513 516 public final static int CANCEL_OPTION = 1; 517 518 521 protected int returnCode; 522 523 526 public Dialog() { 527 this(null, "", ""); 528 } 529 530 537 public Dialog(Component parent, String title, List mediaList) { 538 super(JOptionPane.getFrameForComponent(parent), title); 539 540 listeners.put("OKButtonAction", new OKButtonAction()); 541 listeners.put("CancelButtonAction", new CancelButtonAction()); 542 543 CSSMediaPanel panel = new CSSMediaPanel(); 544 panel.setMedia(mediaList); 545 getContentPane().add(panel, BorderLayout.CENTER); 546 getContentPane().add(createButtonsPanel(), BorderLayout.SOUTH); 547 } 548 549 556 public Dialog(Component parent, String title, String media) { 557 super(JOptionPane.getFrameForComponent(parent), title); 558 559 listeners.put("OKButtonAction", new OKButtonAction()); 560 listeners.put("CancelButtonAction", new CancelButtonAction()); 561 562 CSSMediaPanel panel = new CSSMediaPanel(); 563 panel.setMedia(media); 564 getContentPane().add(panel, BorderLayout.CENTER); 565 getContentPane().add(createButtonsPanel(), BorderLayout.SOUTH); 566 } 567 568 572 public int getReturnCode() { 573 return returnCode; 574 } 575 576 579 protected JPanel createButtonsPanel() { 580 JPanel p = new JPanel (new FlowLayout (FlowLayout.RIGHT)); 581 ButtonFactory bf = new ButtonFactory(bundle, this); 582 p.add(bf.createJButton("OKButton")); 583 p.add(bf.createJButton("CancelButton")); 584 return p; 585 } 586 587 590 protected Map listeners = new HashMap (); 591 592 598 public Action getAction(String key) throws MissingListenerException { 599 return (Action )listeners.get(key); 600 } 601 602 605 protected class OKButtonAction extends AbstractAction { 606 public void actionPerformed(ActionEvent e) { 607 returnCode = OK_OPTION; 608 dispose(); 609 } 610 } 611 612 615 protected class CancelButtonAction extends AbstractAction { 616 public void actionPerformed(ActionEvent e) { 617 returnCode = CANCEL_OPTION; 618 dispose(); 619 } 620 } 621 } 622 623 626 public static void main(String [] args) { 627 String media = "all aural braille embossed handheld print projection screen tty tv"; 628 int code = CSSMediaPanel.showDialog(null, "Test", media); 629 System.out.println(code); 630 System.exit(0); 631 } 632 } 633 | Popular Tags |