1 18 package org.apache.batik.util.gui; 19 20 import java.awt.Component ; 21 import java.awt.FlowLayout ; 22 import java.awt.GridBagConstraints ; 23 import java.awt.GridBagLayout ; 24 import java.awt.GridLayout ; 25 import java.awt.Insets ; 26 import java.awt.event.ActionEvent ; 27 import java.net.URL ; 28 import java.util.HashMap ; 29 import java.util.Locale ; 30 import java.util.Map ; 31 import java.util.MissingResourceException ; 32 import java.util.ResourceBundle ; 33 import java.util.StringTokenizer ; 34 35 import javax.swing.AbstractAction ; 36 import javax.swing.Action ; 37 import javax.swing.BorderFactory ; 38 import javax.swing.DefaultListModel ; 39 import javax.swing.Icon ; 40 import javax.swing.ImageIcon ; 41 import javax.swing.JButton ; 42 import javax.swing.JDialog ; 43 import javax.swing.JFrame ; 44 import javax.swing.JLabel ; 45 import javax.swing.JList ; 46 import javax.swing.JPanel ; 47 import javax.swing.JScrollPane ; 48 import javax.swing.ListCellRenderer ; 49 import javax.swing.ListSelectionModel ; 50 import javax.swing.event.ListSelectionEvent ; 51 import javax.swing.event.ListSelectionListener ; 52 53 import org.apache.batik.util.gui.resource.ActionMap; 54 import org.apache.batik.util.gui.resource.ButtonFactory; 55 import org.apache.batik.util.gui.resource.MissingListenerException; 56 import org.apache.batik.util.gui.resource.ResourceManager; 57 58 65 public class LanguageDialog extends JDialog implements ActionMap { 66 67 70 public final static int OK_OPTION = 0; 71 72 75 public final static int CANCEL_OPTION = 1; 76 77 80 protected final static String RESOURCES = 81 "org.apache.batik.util.gui.resources.LanguageDialogMessages"; 82 83 86 protected static ResourceBundle bundle; 87 88 91 protected static ResourceManager resources; 92 93 static { 94 bundle = ResourceBundle.getBundle(RESOURCES, Locale.getDefault()); 95 resources = new ResourceManager(bundle); 96 } 97 98 101 protected Map listeners = new HashMap (); 102 103 106 protected Panel panel = new Panel (); 107 108 111 protected int returnCode; 112 113 116 public LanguageDialog(JFrame f) { 117 super(f); 118 setModal(true); 119 setTitle(resources.getString("Dialog.title")); 120 121 listeners.put("OKButtonAction", new OKButtonAction()); 122 listeners.put("CancelButtonAction", new CancelButtonAction()); 123 124 getContentPane().add(panel); 125 getContentPane().add("South", createButtonsPanel()); 126 127 pack(); 128 } 129 130 134 public int showDialog() { 135 show(); 136 return returnCode; 137 } 138 139 142 public void setLanguages(String s) { 143 panel.setLanguages(s); 144 } 145 146 149 public String getLanguages() { 150 return panel.getLanguages(); 151 } 152 153 155 161 public Action getAction(String key) throws MissingListenerException { 162 return (Action )listeners.get(key); 163 } 164 165 168 protected JPanel createButtonsPanel() { 169 JPanel p = new JPanel (new FlowLayout (FlowLayout.RIGHT)); 170 ButtonFactory bf = new ButtonFactory(bundle, this); 171 p.add(bf.createJButton("OKButton")); 172 p.add(bf.createJButton("CancelButton")); 173 174 return p; 175 } 176 177 180 public static class Panel extends JPanel implements ActionMap { 181 184 protected JList userList; 185 186 189 protected JList languageList; 190 191 194 protected DefaultListModel userListModel = new DefaultListModel (); 195 196 199 protected DefaultListModel languageListModel = new DefaultListModel (); 200 201 204 protected JButton addLanguageButton; 205 206 209 protected JButton removeLanguageButton; 210 211 214 protected JButton upLanguageButton; 215 216 219 protected JButton downLanguageButton; 220 221 224 protected JButton clearLanguageButton; 225 226 229 protected Map listeners = new HashMap (); 230 231 235 private static Map iconMap = null; 236 237 240 public Panel() { 241 super(new GridBagLayout ()); 242 243 initCountryIcons(); 244 245 setBorder(BorderFactory.createTitledBorder 246 (BorderFactory.createEtchedBorder(), 247 resources.getString("Panel.title"))); 248 249 listeners.put("AddLanguageButtonAction", 250 new AddLanguageButtonAction()); 251 listeners.put("RemoveLanguageButtonAction", 252 new RemoveLanguageButtonAction()); 253 listeners.put("UpLanguageButtonAction", 254 new UpLanguageButtonAction()); 255 listeners.put("DownLanguageButtonAction", 256 new DownLanguageButtonAction()); 257 listeners.put("ClearLanguageButtonAction", 258 new ClearLanguageButtonAction()); 259 260 userList = new JList (userListModel); 262 userList.setCellRenderer(new IconAndTextCellRenderer()); 263 264 languageList = new JList (languageListModel); 265 languageList.setCellRenderer(new IconAndTextCellRenderer()); 266 267 StringTokenizer st; 268 st = new StringTokenizer (resources.getString("Country.list"), " "); 269 while (st.hasMoreTokens()) { 270 languageListModel.addElement(st.nextToken()); 271 } 272 273 ExtendedGridBagConstraints constraints = 275 new ExtendedGridBagConstraints(); 276 constraints.insets = new Insets (5, 5, 5, 5); 277 278 constraints.weightx = 1.0; 279 constraints.weighty = 1.0; 280 constraints.fill = GridBagConstraints.BOTH; 281 282 constraints.setGridBounds(0, 0, 1, 1); 284 JScrollPane sp = new JScrollPane (); 285 sp.setBorder(BorderFactory.createCompoundBorder 286 (BorderFactory.createTitledBorder 287 (BorderFactory.createEmptyBorder(), 288 resources.getString("Languages.title")), 289 BorderFactory.createLoweredBevelBorder())); 290 sp.getViewport().add(languageList); 291 this.add(sp, constraints); 292 293 languageList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 294 languageList.addListSelectionListener 295 (new LanguageListSelectionListener()); 296 297 constraints.setGridBounds(2, 0, 1, 1); 299 300 JScrollPane sp2 = new JScrollPane (); 301 sp2.setBorder(BorderFactory.createCompoundBorder 302 (BorderFactory.createTitledBorder 303 (BorderFactory.createEmptyBorder(), 304 resources.getString("User.title")), 305 BorderFactory.createLoweredBevelBorder())); 306 sp2.getViewport().add(userList); 307 this.add(sp2, constraints); 308 309 userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 310 userList.addListSelectionListener(new UserListSelectionListener()); 311 312 constraints.setGridBounds(0, 1, 3, 1); 314 constraints.weightx = 0; 315 constraints.weighty = 0; 316 this.add(new JLabel (resources.getString("InfoLabel.text")), 317 constraints); 318 319 ButtonFactory bf = new ButtonFactory(bundle, this); 321 JPanel p = new JPanel (new GridLayout (5, 1, 0, 3)); 322 p.add(addLanguageButton = bf.createJButton("AddLanguageButton")); 323 addLanguageButton.setEnabled(false); 324 p.add(removeLanguageButton = 325 bf.createJButton("RemoveLanguageButton")); 326 removeLanguageButton.setEnabled(false); 327 p.add(upLanguageButton = bf.createJButton("UpLanguageButton")); 328 upLanguageButton.setEnabled(false); 329 p.add(downLanguageButton = bf.createJButton("DownLanguageButton")); 330 downLanguageButton.setEnabled(false); 331 p.add(clearLanguageButton = 332 bf.createJButton("ClearLanguageButton")); 333 clearLanguageButton.setEnabled(false); 334 335 JPanel t = new JPanel (new GridBagLayout ()); 336 constraints.setGridBounds(1, 0, 1, 1); 337 this.add(t, constraints); 338 339 constraints.fill = GridBagConstraints.HORIZONTAL; 340 constraints.setGridBounds(0, 0, 1, 1); 341 constraints.insets = new Insets (0, 0, 0, 0); 342 t.add(p, constraints); 343 344 sp2.setPreferredSize(sp.getPreferredSize()); 345 } 346 347 355 public synchronized static void initCountryIcons() 356 { 357 if (iconMap == null) { 359 iconMap = new HashMap (); 360 StringTokenizer st; 361 st = new StringTokenizer (resources.getString("Country.list"), 362 " "); 363 while (st.hasMoreTokens()) { 364 computeCountryIcon(LanguageDialog.Panel.class, 365 st.nextToken()); 366 } 367 } 368 } 369 370 373 public String getLanguages() { 374 StringBuffer result = new StringBuffer (); 375 if (userListModel.getSize() > 0) { 376 result.append(userListModel.getElementAt(0)); 377 378 for (int i = 1; i < userListModel.getSize(); i++) { 379 result.append("," + userListModel.getElementAt(i)); 380 } 381 } 382 return result.toString(); 383 } 384 385 388 public void setLanguages(String str) { 389 int len = userListModel.getSize(); 390 for (int i = 0; i < len; i++) { 391 Object o = userListModel.getElementAt(0); 392 userListModel.removeElementAt(0); 393 int size = languageListModel.getSize(); 394 int n = 0; 395 while (n < size) { 396 String s = (String )languageListModel.getElementAt(n); 397 if (s.compareTo(o) > 0) { 398 break; 399 } 400 n++; 401 } 402 languageListModel.insertElementAt(o, n); 403 } 404 405 StringTokenizer st; 406 st = new StringTokenizer (str, ","); 407 while (st.hasMoreTokens()) { 408 String s = st.nextToken(); 409 userListModel.addElement(s); 410 languageListModel.removeElement(s); 411 } 412 413 updateButtons(); 414 } 415 416 419 protected void updateButtons() { 420 int size = userListModel.size(); 421 int i = userList.getSelectedIndex(); 422 423 boolean empty = size == 0; 424 boolean selected = i != -1; 425 boolean zeroSelected = i == 0; 426 boolean lastSelected = i == size - 1; 427 428 removeLanguageButton.setEnabled(!empty && selected); 429 upLanguageButton.setEnabled(!empty && selected && !zeroSelected); 430 downLanguageButton.setEnabled(!empty && selected && !lastSelected); 431 clearLanguageButton.setEnabled(!empty); 432 433 size = languageListModel.size(); 434 i = languageList.getSelectedIndex(); 435 436 empty = size == 0; 437 selected = i != -1; 438 439 addLanguageButton.setEnabled(!empty && selected); 440 } 441 442 445 protected String getCountryText(String code) { 446 return resources.getString(code + ".text"); 447 } 448 449 452 protected Icon getCountryIcon(String code) { 453 return computeCountryIcon(getClass(), code); 454 } 455 456 private static Icon computeCountryIcon(Class ref, 457 String code) { 458 ImageIcon icon = null; 459 try { 460 if ((icon = (ImageIcon )iconMap.get(code)) != null) 461 return icon; 462 String s = resources.getString(code + ".icon"); 463 URL url = ref.getResource(s); 464 if (url != null) { 465 iconMap.put(code, icon = new ImageIcon (url)); 466 return icon; 467 } 468 } catch (MissingResourceException e) { 469 } 470 return new ImageIcon (ref.getResource("resources/blank.gif")); 471 } 472 473 475 481 public Action getAction(String key) throws MissingListenerException { 482 return (Action )listeners.get(key); 483 } 484 485 488 protected class AddLanguageButtonAction extends AbstractAction { 489 public void actionPerformed(ActionEvent e) { 490 int i = languageList.getSelectedIndex(); 491 Object o = languageListModel.getElementAt(i); 492 languageListModel.removeElementAt(i); 493 userListModel.addElement(o); 494 userList.setSelectedValue(o, true); 495 } 496 } 497 498 501 protected class RemoveLanguageButtonAction extends AbstractAction { 502 public void actionPerformed(ActionEvent e) { 503 int i = userList.getSelectedIndex(); 504 Object o = userListModel.getElementAt(i); 505 userListModel.removeElementAt(i); 506 507 int size = languageListModel.getSize(); 508 int n = 0; 509 while (n < size) { 510 String s = (String )languageListModel.getElementAt(n); 511 if (s.compareTo(o) > 0) { 512 break; 513 } 514 n++; 515 } 516 languageListModel.insertElementAt(o, n); 517 languageList.setSelectedValue(o, true); 518 updateButtons(); 519 } 520 } 521 522 525 protected class UpLanguageButtonAction extends AbstractAction { 526 public void actionPerformed(ActionEvent e) { 527 int i = userList.getSelectedIndex(); 528 Object o = userListModel.getElementAt(i); 529 userListModel.removeElementAt(i); 530 userListModel.insertElementAt(o, i - 1); 531 userList.setSelectedIndex(i - 1); 532 } 533 } 534 535 538 protected class DownLanguageButtonAction extends AbstractAction { 539 public void actionPerformed(ActionEvent e) { 540 int i = userList.getSelectedIndex(); 541 Object o = userListModel.getElementAt(i); 542 userListModel.removeElementAt(i); 543 userListModel.insertElementAt(o, i + 1); 544 userList.setSelectedIndex(i + 1); 545 } 546 } 547 548 551 protected class ClearLanguageButtonAction extends AbstractAction { 552 public void actionPerformed(ActionEvent e) { 553 int len = userListModel.getSize(); 554 for (int i = 0; i < len; i++) { 555 Object o = userListModel.getElementAt(0); 556 userListModel.removeElementAt(0); 557 int size = languageListModel.getSize(); 558 int n = 0; 559 while (n < size) { 560 String s = (String )languageListModel.getElementAt(n); 561 if (s.compareTo(o) > 0) { 562 break; 563 } 564 n++; 565 } 566 languageListModel.insertElementAt(o, n); 567 } 568 updateButtons(); 569 } 570 } 571 572 575 protected class LanguageListSelectionListener 576 implements ListSelectionListener { 577 public void valueChanged(ListSelectionEvent e) { 578 int i = languageList.getSelectedIndex(); 579 userList.getSelectionModel().clearSelection(); 580 languageList.setSelectedIndex(i); 581 updateButtons(); 582 } 583 } 584 585 588 protected class UserListSelectionListener 589 implements ListSelectionListener { 590 public void valueChanged(ListSelectionEvent e) { 591 int i = userList.getSelectedIndex(); 592 languageList.getSelectionModel().clearSelection(); 593 userList.setSelectedIndex(i); 594 updateButtons(); 595 } 596 } 597 598 601 protected class IconAndTextCellRenderer 602 extends JLabel 603 implements ListCellRenderer { 604 public IconAndTextCellRenderer() { 605 this.setOpaque(true); 606 this.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); 607 } 608 public Component getListCellRendererComponent(JList list, 609 Object value, 610 int index, 611 boolean isSelected, 612 boolean cellHasFocus){ 613 String s = (String )value; 614 this.setText(getCountryText(s)); 615 this.setIcon(getCountryIcon(s)); 616 this.setEnabled(list.isEnabled()); 617 this.setFont(list.getFont()); 618 if (isSelected) { 619 this.setBackground(list.getSelectionBackground()); 620 this.setForeground(list.getSelectionForeground()); 621 } else { 622 this.setBackground(list.getBackground()); 623 this.setForeground(list.getForeground()); 624 } 625 return this; 626 } 627 } 628 } 629 630 633 protected class OKButtonAction extends AbstractAction { 634 public void actionPerformed(ActionEvent e) { 635 returnCode = OK_OPTION; 636 dispose(); 637 } 638 } 639 640 643 protected class CancelButtonAction extends AbstractAction { 644 public void actionPerformed(ActionEvent e) { 645 returnCode = CANCEL_OPTION; 646 dispose(); 647 } 648 } 649 } 650 | Popular Tags |