00001 package GuiComponents.SctConf;
00002
00003 import javax.swing.*;
00004 import javax.swing.event.*;
00005 import java.awt.*;
00006 import java.awt.event.*;
00007
00008 import java.util.Vector;
00009
00010 public class ModuleListView extends JPanel {
00011 sctConf.Configuration config;
00012
00013
00014 JList moduleList;
00015
00016 boolean unused;
00017
00018 public ModuleListView(sctConf.Configuration conf) {
00019
00020 config = conf;
00021
00022 unused = true;
00023
00024 setLayout(new BorderLayout());
00025 JPanel buttons = new JPanel();
00026
00027 JCheckBox unusedCheck = new JCheckBox("Unused", unused);
00028 unusedCheck.addChangeListener(new ChangeListener() {
00029 public void stateChanged(ChangeEvent e) {
00030 System.out.println("CheckBox state changed");
00031
00032 boolean tempUnused = ((ButtonModel)((JCheckBox)(e.getSource())).getModel()).isSelected();
00033 System.out.println("To " + tempUnused);
00034
00035 if(tempUnused != unused) {
00036 System.out.println("Updating");
00037 unused = tempUnused;
00038 updateList();
00039 }
00040 }
00041 });
00042 buttons.add(unusedCheck);
00043
00044 JButton addButton = new JButton("Add");
00045 addButton.addActionListener(new ActionListener() {
00046 public void actionPerformed(ActionEvent e) {
00047 String fileNames[] = getFileNames();
00048
00049 if(fileNames != null) {
00050 for(int f=0; f<fileNames.length; f++) {
00051 System.out.println("Add a module to the list using the configuration in file " + fileNames[f]);
00052
00053 try {
00054 config.configureModuleFromFile(fileNames[f]);
00055 } catch(sctConf.ConfigurationException ex) {
00056 JOptionPane.showMessageDialog(null, "Failed to add " + fileNames[f] + " due to " + ex.detail);
00057 }
00058 }
00059 updateList();
00060 }
00061 }
00062 });
00063 buttons.add(addButton);
00064
00065 JButton close = new JButton("Close");
00066 close.addActionListener(new ActionListener() {
00067 public void actionPerformed(ActionEvent e) {
00068 ((Window)getTopLevelAncestor()).dispose();
00069 }
00070 });
00071 buttons.add(close);
00072
00073 add(buttons, BorderLayout.SOUTH);
00074
00075 moduleList = new JList(getList());
00076 moduleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
00077 moduleList.setSelectedIndex(0);
00078
00079 JScrollPane listScrollPane = new JScrollPane(moduleList);
00080 add(listScrollPane, BorderLayout.CENTER);
00081 validate();
00082
00083 updateList();
00084 }
00085
00086 void updateList() {
00087 moduleList.setListData(getList());
00088 }
00089
00090 public Vector getList() {
00091 Vector result = new Vector();
00092
00093 try {
00094 String[] modules;
00095
00096 if(unused) modules = config.listUnusedModules();
00097 else modules = config.listAllModules();
00098
00099 for(int i=0; i<modules.length; i++) {
00100 result.addElement(modules[i]);
00101 System.out.println(modules[i]);
00102 }
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 } catch(sctConf.ConfigurationException e) {
00121 System.out.println("Java caught an exception!: " + e.detail);
00122 }
00123
00124 System.out.println("Returned list of " + result.size() + " modules");
00125
00126 return result;
00127 }
00128
00130 public String getSelected() {
00131 return (String)(moduleList.getSelectedValue());
00132 }
00133
00134 static JFileChooser fileChooser;
00135
00136 { fileChooser = null; }
00137
00138 static String[] getFileNames() {
00139 if(fileChooser == null) {
00140 fileChooser = new JFileChooser();
00141 }
00142
00143 fileChooser.setMultiSelectionEnabled(true);
00144
00145 if(fileChooser.showDialog(null, "Pick module configuration file(s)") == JFileChooser.APPROVE_OPTION) {
00146 java.io.File files[] = fileChooser.getSelectedFiles();
00147 String [] result = new String[files.length];
00148
00149 for(int i=0; i<files.length; i++) {
00150 result[i] = files[i].toString();
00151 }
00152 return result;
00153 } else {
00154 return null;
00155 }
00156 }
00157 }
00158