1 10 package org.jgap.gui; 11 12 import java.util.*; 13 14 import java.awt.Dimension ; 15 import java.awt.event.*; 16 import javax.swing.*; 17 import javax.swing.event.*; 18 19 import org.jgap.data.config.*; 20 21 import info.clearthought.layout.*; 22 23 29 public class ConfigFrame 30 extends JFrame 31 implements IConfigInfo { 32 33 private final static String CVS_REVISION = "$Revision: 1.18 $"; 34 35 private Object m_conHandler; 37 38 private boolean m_isRoot; 39 40 private List m_panels; 42 43 private List m_listProps; 45 46 private List m_textProps; 48 49 private List m_listGroups; 51 52 private List m_textGroups; 54 55 private JPanel m_listPanel; 56 57 private JPanel m_textPanel; 58 59 private JPanel m_configPanel; 60 61 private JButton m_configButton; 62 63 private ConfigButtonListener m_cbl; 64 65 private JTextField m_fileName; 66 67 private JButton m_configureButton; 68 69 private JTextField m_configItem; 70 71 private Configurable m_conObj; 72 73 private ConfigFrame m_parent; 75 76 private static final String m_defaultConfigFile = "jgap.con"; 78 79 88 ConfigFrame(final ConfigFrame a_parent, final String a_title, 89 final boolean a_isRoot) { 90 super(a_title); 91 m_panels = Collections.synchronizedList(new ArrayList()); 92 m_textProps = Collections.synchronizedList(new ArrayList()); 93 m_listProps = Collections.synchronizedList(new ArrayList()); 94 m_listGroups = Collections.synchronizedList(new ArrayList()); 95 m_textGroups = Collections.synchronizedList(new ArrayList()); 96 m_cbl = new ConfigButtonListener(this); 97 m_isRoot = a_isRoot; 98 m_parent = a_parent; 99 } 100 101 109 public void createAndShowGUI(final Object a_conHandler) { 110 JFrame.setDefaultLookAndFeelDecorated(true); 111 m_conHandler = a_conHandler; 112 pack(); 114 setVisible(true); 115 setBounds(100, 100, 300, 300); 116 setSize(500, 300); 117 try { 118 MetaConfig mt = MetaConfig.getInstance(); 119 } 120 catch (MetaConfigException mcEx) { 121 JOptionPane.showMessageDialog(null, 122 "Exception while parsing JGAP Meta" 123 + " Config file " 124 + mcEx.getMessage(), 125 "Meta Config Exception", 126 JOptionPane.ERROR_MESSAGE); 127 } 128 catch (Exception ex) { 129 JOptionPane.showMessageDialog(null, 130 "Exception while parsing JGAP Meta Config" 131 + " file " 132 + ex.getMessage(), 133 "Meta Config Exception", 134 JOptionPane.ERROR_MESSAGE); 135 } 136 setup(); 137 show(); 138 if (m_isRoot) { 139 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 140 } 141 else { 142 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 143 } 144 } 145 146 154 public ConfigData getConfigData() { 155 ConfigData cd = new ConfigData(); 156 cd.setNS(m_conHandler.getClass().getName()); 157 List values; 159 try { 160 Iterator lIter = m_listGroups.iterator(); 161 while (lIter.hasNext()) { 162 ListGroup lg = (ListGroup) lIter.next(); 163 values = Collections.synchronizedList(new ArrayList()); 164 Enumeration e = lg.getOutListModel().elements(); 165 while (e.hasMoreElements()) { 166 String val = (String ) e.nextElement(); 167 values.add(val); 168 } 169 cd.addListData(lg.getProp().getName(), values); 170 } 171 TextGroup tg; 173 Iterator tIter = m_textGroups.iterator(); 174 while (tIter.hasNext()) { 175 tg = (TextGroup) tIter.next(); 176 cd.addTextData(tg.getProp().getName(), tg.getTextField().getText()); 177 } 178 } 179 catch (ClassCastException cex) { 180 JOptionPane.showMessageDialog(null, cex.getMessage(), 181 "ConfigFrame.getConfigData():Configuration" 182 + " Error", 183 JOptionPane.INFORMATION_MESSAGE); 184 } 185 return cd; 186 } 187 188 195 public String getFileName() { 196 if (m_isRoot) { 198 String fName = m_fileName.getText(); 199 if (fName.equals("")) { 201 fName = ConfigFrame.m_defaultConfigFile; 202 } 203 return fName; 204 } 205 else { 206 return m_parent.getFileName(); 207 } 208 } 209 210 220 private void setup() { 221 int numLists = 0, numTexts = 0; 222 List props = null; 223 try { 224 225 props = MetaConfig.getInstance().getConfigProperty(m_conHandler.getClass().getName()); 226 } 227 catch (Exception ex) { 228 JOptionPane.showMessageDialog(null, ex.getMessage(), 229 "Configuration Error: Could not get" 230 + " properties for class " 231 + m_conHandler.getClass().getName(), 232 JOptionPane.INFORMATION_MESSAGE); 233 } 234 if (props == null) { 235 JOptionPane.showMessageDialog(null, 236 "setup():No Configurable Properties in" 237 + " this Configuration", 238 "Configuration Message", 239 JOptionPane.INFORMATION_MESSAGE); 240 return; 241 } 242 Iterator iter = props.iterator(); 243 while (iter.hasNext()) { 244 try { 245 ConfigProperty prop = (ConfigProperty) iter.next(); 246 if (prop.getWidget().equals("JList")) { 247 numLists++; 248 m_listProps.add(prop); 249 } 250 else if (prop.getWidget().equals("JTextField")) { 251 numTexts++; 252 m_textProps.add(prop); 253 } 254 else { 255 JOptionPane.showMessageDialog(null, 257 "Unknown Widget " + prop.getWidget(), 258 "Configuration Error", 259 JOptionPane.INFORMATION_MESSAGE); 260 } 261 } 262 catch (ClassCastException cex) { 263 JOptionPane.showMessageDialog(null, 264 cex.getMessage(), 265 "ConfigError.setup():Configuration Error:" 266 + " Invalid cast", 267 JOptionPane.INFORMATION_MESSAGE); 268 } 269 } 270 if (numLists == 0 && numTexts == 0) { 272 JOptionPane.showMessageDialog(null, 273 "No Configurable Properties in this" 274 + " Configuration", 275 "Configuration Information", 276 JOptionPane.INFORMATION_MESSAGE); 277 return; 278 } 279 int numPanels = 2; 282 if (numLists > 0 && numTexts > 0) { 283 numPanels = 3; 284 } 285 addWidgets(numPanels, numLists, numTexts); 287 } 288 289 299 private void addWidgets(int a_numPanels, final int a_numLists, 300 final int a_numTexts) { 301 try { 302 a_numPanels = 3; 303 double[][] tableArray = new double[2][a_numPanels]; 305 double perPanel = (double) (1.0 / (double) a_numPanels); 306 int i = 0; 307 for (i = 0; i < a_numPanels - 1; i++) { 308 tableArray[1][i] = perPanel; 309 } 310 tableArray[1][i] = TableLayout.FILL; 312 tableArray[0][0] = TableLayout.FILL; 314 getContentPane().setLayout(new TableLayout(tableArray)); 315 int panelsAdded = 0; 317 if (a_numLists > 0) { 319 double[][] panelSize; 320 int numCols = 3 * a_numLists; 323 panelSize = new double[2][numCols]; 325 double space = (double) (1.0 / (double) a_numLists); 326 double listSpace = space * 0.4; 328 double buttonSpace = space * 0.2; 329 for (int itr = 0; itr < a_numLists; itr++) { 330 panelSize[0][3 * itr] = listSpace; 331 panelSize[0][3 * itr + 1] = buttonSpace; 332 panelSize[0][3 * itr + 2] = listSpace; 333 } 334 panelSize[1][0] = TableLayout.FILL; 336 m_listPanel = new JPanel(); 337 m_panels.add(m_listPanel); 338 m_listPanel.setLayout(new TableLayout(panelSize)); 339 getContentPane().add(m_listPanel, new TableLayoutConstraints( 340 0, panelsAdded, 0, panelsAdded, 341 TableLayout.FULL, TableLayout.FULL)); 342 panelsAdded++; 344 Iterator iter = m_listProps.iterator(), valIter; 346 ConfigProperty prop; 347 ListGroup lg; 348 for (int itr1 = 0; itr1 < a_numLists && iter.hasNext(); itr1++) { 349 lg = new ListGroup(this); 350 m_listGroups.add(lg); 351 prop = (ConfigProperty) iter.next(); 352 lg.setProp(prop); 353 m_listPanel.add(lg.getListScroller(), 354 new TableLayoutConstraints(3 * itr1, 0, 3 * itr1, 0, 355 TableLayout.CENTER, TableLayout.CENTER)); 356 m_listPanel.add(lg.getLButton(), 358 new TableLayoutConstraints(3 * itr1 + 1, 0, 359 3 * itr1 + 1, 0, 360 TableLayout.CENTER, TableLayout.TOP)); 361 m_listPanel.add(lg.getRButton(), 363 new TableLayoutConstraints(3 * itr1 + 1, 0, 364 3 * itr1 + 1, 0, 365 TableLayout.CENTER, TableLayout.BOTTOM)); 366 valIter = prop.getValuesIter(); 368 while (valIter.hasNext()) { 369 lg.getListModel().addElement(valIter.next()); 370 } 371 m_listPanel.add(lg.getOutListScroller(), 372 new TableLayoutConstraints(3 * itr1 + 2, 0, 373 3 * itr1 + 2, 0, 374 TableLayout.CENTER, TableLayout.CENTER)); 375 } 376 } 377 if (a_numTexts > 0) { 379 double[][] panelSize; 380 int numCols = a_numTexts * 2; 381 panelSize = new double[2][numCols]; 382 double perText = (double) (1.0 / (double) numCols); 384 int itr = 0; 385 for (itr = 0; itr < numCols - 1; itr++) { 387 panelSize[0][itr] = perText; 388 } 389 panelSize[0][itr] = TableLayout.FILL; 390 panelSize[1][0] = TableLayout.FILL; 392 m_textPanel = new JPanel(); 393 m_panels.add(m_textPanel); 394 m_textPanel.setLayout(new TableLayout(panelSize)); 395 getContentPane().add(m_textPanel, new TableLayoutConstraints( 396 0, panelsAdded, 0, panelsAdded, 397 TableLayout.FULL, TableLayout.FULL)); 398 panelsAdded++; 399 TextGroup tg; 401 Iterator iter = m_textProps.iterator(), valIter; 402 ConfigProperty prop; 403 for (int itr1 = 0; itr1 < a_numTexts && iter.hasNext(); itr1++) { 404 tg = new TextGroup(); 405 m_textGroups.add(tg); 406 prop = (ConfigProperty) iter.next(); 407 tg.setProp(prop); 408 JLabel label = tg.getLabel(); 409 label.setText(prop.getName()); 410 m_textPanel.add(label, 411 new TableLayoutConstraints(itr1, 0, itr1, 0, 412 TableLayout.RIGHT, TableLayout.CENTER)); 413 m_textPanel.add(tg.getTextField(), 414 new TableLayoutConstraints(itr1 + 1, 0, itr1 + 1, 0, 415 TableLayout.LEFT, TableLayout.CENTER)); 416 } 417 } 418 double[][] panelSize; 420 panelSize = new double[2][4]; 421 panelSize[0][0] = .25; 423 panelSize[0][1] = .25; 424 panelSize[0][2] = .25; 425 panelSize[0][3] = .25; 426 panelSize[1][0] = TableLayout.FILL; 428 m_configPanel = new JPanel(); 429 m_panels.add(m_configPanel); 430 m_configPanel.setLayout(new TableLayout(panelSize)); 431 getContentPane().add(m_configPanel, new TableLayoutConstraints( 432 0, panelsAdded, 0, panelsAdded, 433 TableLayout.FULL, TableLayout.FULL)); 434 m_configItem = new JTextField(50); 436 m_configPanel.add(m_configItem, 437 new TableLayoutConstraints(0, 0, 0, 0, 438 TableLayout.RIGHT, 439 TableLayout.CENTER)); 440 m_configureButton = new JButton("Configure"); 441 m_configureButton.addActionListener(m_cbl); 442 m_configPanel.add(m_configureButton, 443 new TableLayoutConstraints(1, 0, 1, 0, 444 TableLayout.LEFT, 445 TableLayout.CENTER)); 446 if (m_isRoot) { 447 m_fileName = new JTextField("jgap.con"); 448 m_configPanel.add(m_fileName, 449 new TableLayoutConstraints(2, 0, 2, 0, 450 TableLayout.RIGHT, TableLayout.CENTER)); 451 m_configButton = new JButton("Generate"); 452 m_configButton.addActionListener(m_cbl); 453 m_configPanel.add(m_configButton, 454 new TableLayoutConstraints(3, 0, 3, 0, 455 TableLayout.LEFT, TableLayout.CENTER)); 456 } 457 else { 458 m_configButton = new JButton("Save Configuration"); 459 m_configButton.addActionListener(m_cbl); 460 m_configPanel.add(m_configButton, 461 new TableLayoutConstraints(3, 0, 3, 0, 462 TableLayout.LEFT, TableLayout.CENTER)); 463 } 464 } 465 catch (Exception ex) { 466 JOptionPane.showMessageDialog(null, 467 "Exception" + ex.toString(), 468 "This is the title", 469 JOptionPane.INFORMATION_MESSAGE); 470 } 471 } 472 473 480 public class ListGroup { 481 private JList m_list; 483 484 private DefaultListModel m_listModel; 486 487 private JScrollPane m_listScroller; 488 489 private JList m_outList; 491 492 private DefaultListModel m_outListModel; 494 495 private JScrollPane m_outListScroller; 496 497 private ConfigListSelectionListener m_outListListener; 498 499 private JButton m_lButton; 501 502 private JButton m_rButton; 503 504 private ConfigProperty m_prop; 506 507 private ListButtonListener m_listBL; 508 509 private ConfigFrame m_frame; 510 511 518 ListGroup(final ConfigFrame a_frame) { 519 m_frame = a_frame; 520 m_listModel = new DefaultListModel(); 522 m_list = new JList(m_listModel); 523 m_list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); 524 m_list.setLayoutOrientation(JList.HORIZONTAL_WRAP); 525 m_list.setVisibleRowCount( -1); 526 m_listScroller = new JScrollPane(m_list); 527 m_listScroller.setPreferredSize(new Dimension (250, 80)); 528 m_outListModel = new DefaultListModel(); 530 m_outList = new JList(m_outListModel); 531 m_outList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 532 m_outList.setLayoutOrientation(JList.HORIZONTAL_WRAP); 533 m_outList.setVisibleRowCount( -1); 534 m_outListScroller = new JScrollPane(m_outList); 535 m_outListListener = new ConfigListSelectionListener(m_frame, m_outList); 536 m_outList.getSelectionModel().addListSelectionListener(m_outListListener); 537 m_outListScroller.setPreferredSize(new Dimension (250, 80)); 538 m_listBL = new ListButtonListener(this); 540 m_lButton = new JButton("<-"); 541 m_lButton.addActionListener(m_listBL); 542 m_rButton = new JButton("->"); 543 m_rButton.addActionListener(m_listBL); 544 } 545 546 553 public ConfigProperty getProp() { 554 return m_prop; 555 } 556 557 566 public void setProp(final ConfigProperty a_prop) { 567 m_prop = a_prop; 568 } 569 570 576 public JList getList() { 577 return m_list; 578 } 579 580 586 public DefaultListModel getListModel() { 587 return m_listModel; 588 } 589 590 596 public JScrollPane getListScroller() { 597 return m_listScroller; 598 } 599 600 606 public JList getOutList() { 607 return m_outList; 608 } 609 610 617 public DefaultListModel getOutListModel() { 618 return m_outListModel; 619 } 620 621 627 public JScrollPane getOutListScroller() { 628 return m_outListScroller; 629 } 630 631 637 public JButton getLButton() { 638 return m_lButton; 639 } 640 641 647 public JButton getRButton() { 648 return m_rButton; 649 } 650 651 657 public void leftButtonPressed() { 658 int[] indices = m_outList.getSelectedIndices(); 659 for (int i = 0; i < indices.length; i++) { 660 String removed = (String ) m_outListModel.remove(indices[0]); 661 m_listModel.addElement(removed); 662 } 663 } 664 665 671 public void rightButtonPressed() { 672 int[] indices = m_list.getSelectedIndices(); 673 for (int i = 0; i < indices.length; i++) { 674 String removed = (String ) m_listModel.remove(indices[0]); 675 m_outListModel.addElement(removed); 676 } 677 } 678 } 679 686 class TextGroup { 687 private JTextField m_textField; 688 689 private JLabel m_label; 690 691 private ConfigProperty m_prop; 692 693 TextGroup() { 694 m_textField = new JTextField(20); 695 m_label = new JLabel(); 696 } 697 698 public ConfigProperty getProp() { 699 return m_prop; 700 } 701 702 public void setProp(final ConfigProperty a_prop) { 703 m_prop = a_prop; 704 } 705 706 public JTextField getTextField() { 707 return m_textField; 708 } 709 710 public JLabel getLabel() { 711 return m_label; 712 } 713 } 714 720 class ConfigButtonListener 721 implements ActionListener { 722 private ConfigFrame m_frame; 723 724 ConfigButtonListener(final ConfigFrame a_frame) { 725 m_frame = a_frame; 726 } 727 728 public void actionPerformed(final ActionEvent a_e) { 729 if (a_e.getActionCommand().equals("Configure")) { 731 String conStr = m_configItem.getText(); 732 if (conStr.equals("")) { 733 JOptionPane.showMessageDialog(null, 734 "Configurable name is empty, cannot" 735 + " configure.", 736 "Configuration Error", 737 JOptionPane.INFORMATION_MESSAGE); 738 } 739 else { 740 try { 741 Class conClass; 742 m_conObj = null; 743 try { 744 conClass = Class.forName(conStr); 745 } 746 catch (ClassNotFoundException cnfEx) { 747 JOptionPane.showMessageDialog(null, 748 cnfEx.getMessage(), 749 "Configuration Error: Class not" 750 + " found", 751 JOptionPane.INFORMATION_MESSAGE); 752 return; 753 } 754 try { 755 m_conObj = (Configurable) conClass. 756 newInstance(); 757 } 758 catch (Exception ex) { 759 JOptionPane.showMessageDialog(null, 760 ex.getMessage(), 761 "Configuration Error:Could not" 762 + " create object", 763 JOptionPane.INFORMATION_MESSAGE); 764 return; 765 } 766 try { 767 SwingUtilities.invokeLater(new Runnable () { 768 public void run() { 769 try { 770 GUIManager.getInstance().showFrame(m_frame, m_conObj); 771 } 772 catch (Exception ex) { 773 JOptionPane.showMessageDialog(null, ex.getMessage(), 774 "Configuration Error:Could" 775 + " not create new Frame", 776 JOptionPane.ERROR_MESSAGE); 777 } 778 } 779 }); 780 } 781 catch (Exception ex) { 782 JOptionPane.showMessageDialog(null, ex.getMessage(), 783 "Configuration Error:Could not" 784 + " create new frame", 785 JOptionPane.ERROR_MESSAGE); 786 } 787 } 788 catch (Exception ex) { 789 JOptionPane.showMessageDialog(null, 790 ex.getMessage(), 791 "Configuration Error", 792 JOptionPane.INFORMATION_MESSAGE); 793 } 794 } 795 } 796 else { 797 ConfigWriter.getInstance().write(m_frame); 799 } 800 } 801 } 802 808 public class ListButtonListener 809 implements ActionListener { 810 private ListGroup m_lg; 811 812 ListButtonListener(final ListGroup a_lg) { 813 m_lg = a_lg; 814 } 815 816 public void actionPerformed(final ActionEvent a_e) { 817 if (a_e.getActionCommand().equals("<-")) { 819 m_lg.leftButtonPressed(); 821 } 822 else { 823 m_lg.rightButtonPressed(); 825 } 826 } 827 } 828 834 public class ConfigListSelectionListener 835 implements ListSelectionListener { 836 private JList m_list; 837 838 private ConfigFrame m_frame; 839 840 public ConfigListSelectionListener(final ConfigFrame a_frame, 841 final JList a_list) { 842 m_list = a_list; 843 m_frame = a_frame; 844 } 845 846 public void valueChanged(final ListSelectionEvent a_e) { 847 Object [] values = m_list.getSelectedValues(); 848 if (values.length > 0) { 849 String value = (String ) values[0]; 850 notifySelection(value); 851 } 852 } 853 } 854 861 private void notifySelection(final String a_value) { 862 m_configItem.setText(a_value); 863 } 864 } 865 | Popular Tags |