[127] | 1 | package genius.gui.boaframework;
|
---|
| 2 |
|
---|
| 3 | import java.awt.Frame;
|
---|
| 4 | import java.awt.event.ActionEvent;
|
---|
| 5 | import java.awt.event.ActionListener;
|
---|
| 6 | import java.io.File;
|
---|
| 7 | import java.io.IOException;
|
---|
| 8 |
|
---|
| 9 | import javax.swing.JButton;
|
---|
| 10 | import javax.swing.JDialog;
|
---|
| 11 | import javax.swing.JFileChooser;
|
---|
| 12 | import javax.swing.JLabel;
|
---|
| 13 | import javax.swing.JOptionPane;
|
---|
| 14 | import javax.swing.JSeparator;
|
---|
| 15 | import javax.swing.JTextField;
|
---|
| 16 | import javax.swing.filechooser.FileFilter;
|
---|
| 17 |
|
---|
| 18 | import genius.core.Global;
|
---|
| 19 | import genius.core.boaframework.AcceptanceStrategy;
|
---|
| 20 | import genius.core.boaframework.BoaType;
|
---|
| 21 | import genius.core.boaframework.OMStrategy;
|
---|
| 22 | import genius.core.boaframework.OfferingStrategy;
|
---|
| 23 | import genius.core.boaframework.OpponentModel;
|
---|
| 24 | import genius.core.boaframework.repository.BOAagentRepository;
|
---|
| 25 | import genius.core.boaframework.repository.BOArepItem;
|
---|
| 26 | import genius.gui.panels.GenericFileFilter;
|
---|
| 27 |
|
---|
| 28 | /**
|
---|
| 29 | * GUI to add or edit a BOA component to the BOA repository.
|
---|
| 30 | */
|
---|
| 31 | public class BOAComponentEditor extends JDialog {
|
---|
| 32 |
|
---|
| 33 | private static final long serialVersionUID = -7204112461104285605L;
|
---|
| 34 | private JLabel componentNameLabel;
|
---|
| 35 | private JTextField componentNameTextField;
|
---|
| 36 | private JLabel componentClassLabel;
|
---|
| 37 | private JTextField componentClassTextField;
|
---|
| 38 | private JSeparator lowerSeparator;
|
---|
| 39 | private JButton addComponent;
|
---|
| 40 | private JButton openButton;
|
---|
| 41 | private JSeparator upperSeparator;
|
---|
| 42 | private BOArepItem result = null;
|
---|
| 43 | private BoaType type;
|
---|
| 44 |
|
---|
| 45 | public BOAComponentEditor(Frame frame, String title) {
|
---|
| 46 | super(frame, title, true);
|
---|
| 47 | this.setLocation(frame.getLocation().x + frame.getWidth() / 2,
|
---|
| 48 | frame.getLocation().y + frame.getHeight() / 4);
|
---|
| 49 | this.setSize(frame.getSize().width / 3, frame.getSize().height / 2);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public BOArepItem getResult(BOArepItem item) {
|
---|
| 53 | componentNameLabel = new JLabel("Component name");
|
---|
| 54 | componentNameTextField = new JTextField();
|
---|
| 55 |
|
---|
| 56 | componentClassLabel = new JLabel("Component class");
|
---|
| 57 | componentClassTextField = new javax.swing.JTextField();
|
---|
| 58 | componentClassTextField.setEditable(false);
|
---|
| 59 |
|
---|
| 60 | addComponent = new JButton("Add component");
|
---|
| 61 | addComponent.addActionListener(new ActionListener() {
|
---|
| 62 | @Override
|
---|
| 63 | public void actionPerformed(ActionEvent e) {
|
---|
| 64 | addComponent();
|
---|
| 65 | }
|
---|
| 66 | });
|
---|
| 67 |
|
---|
| 68 | openButton = new javax.swing.JButton("Open");
|
---|
| 69 | openButton.addActionListener(new ActionListener() {
|
---|
| 70 | @Override
|
---|
| 71 | public void actionPerformed(ActionEvent e) {
|
---|
| 72 | openAction();
|
---|
| 73 | }
|
---|
| 74 | });
|
---|
| 75 |
|
---|
| 76 | upperSeparator = new javax.swing.JSeparator();
|
---|
| 77 | lowerSeparator = new javax.swing.JSeparator();
|
---|
| 78 | setResizable(false);
|
---|
| 79 |
|
---|
| 80 | javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
|
---|
| 81 | getContentPane());
|
---|
| 82 | getContentPane().setLayout(layout);
|
---|
| 83 | layout.setHorizontalGroup(layout
|
---|
| 84 | .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
---|
| 85 | .addComponent(upperSeparator)
|
---|
| 86 | .addGroup(layout.createSequentialGroup().addGap(12, 12, 12)
|
---|
| 87 | .addGroup(layout
|
---|
| 88 | .createParallelGroup(
|
---|
| 89 | javax.swing.GroupLayout.Alignment.LEADING)
|
---|
| 90 | .addGroup(layout.createSequentialGroup()
|
---|
| 91 | .addComponent(componentNameLabel)
|
---|
| 92 | .addGap(14, 14, 14)
|
---|
| 93 | .addComponent(componentNameTextField))
|
---|
| 94 | .addGroup(layout.createSequentialGroup()
|
---|
| 95 | .addComponent(componentClassLabel)
|
---|
| 96 | .addGap(18, 18, 18)
|
---|
| 97 | .addComponent(componentClassTextField)
|
---|
| 98 | .addPreferredGap(
|
---|
| 99 | javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
---|
| 100 | .addComponent(openButton))
|
---|
| 101 | .addGroup(layout.createSequentialGroup()
|
---|
| 102 | .addGroup(layout.createParallelGroup(
|
---|
| 103 | javax.swing.GroupLayout.Alignment.LEADING,
|
---|
| 104 | false)
|
---|
| 105 |
|
---|
| 106 | .addGroup(
|
---|
| 107 | layout.createSequentialGroup()
|
---|
| 108 | .addComponent(
|
---|
| 109 | addComponent)
|
---|
| 110 | .addPreferredGap(
|
---|
| 111 | javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
|
---|
| 112 | .addGap(0, 0, Short.MAX_VALUE)))
|
---|
| 113 | .addContainerGap())
|
---|
| 114 | .addComponent(lowerSeparator));
|
---|
| 115 | layout.setVerticalGroup(layout
|
---|
| 116 | .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
---|
| 117 | .addGroup(layout.createSequentialGroup().addContainerGap()
|
---|
| 118 | .addGroup(layout
|
---|
| 119 | .createParallelGroup(
|
---|
| 120 | javax.swing.GroupLayout.Alignment.BASELINE)
|
---|
| 121 | .addComponent(componentNameLabel)
|
---|
| 122 | .addComponent(componentNameTextField,
|
---|
| 123 | javax.swing.GroupLayout.PREFERRED_SIZE,
|
---|
| 124 | javax.swing.GroupLayout.DEFAULT_SIZE,
|
---|
| 125 | javax.swing.GroupLayout.PREFERRED_SIZE))
|
---|
| 126 | .addPreferredGap(
|
---|
| 127 | javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
---|
| 128 |
|
---|
| 129 | .addGroup(layout
|
---|
| 130 | .createParallelGroup(
|
---|
| 131 | javax.swing.GroupLayout.Alignment.BASELINE)
|
---|
| 132 | .addComponent(componentClassLabel)
|
---|
| 133 | .addComponent(componentClassTextField,
|
---|
| 134 | javax.swing.GroupLayout.PREFERRED_SIZE,
|
---|
| 135 | javax.swing.GroupLayout.DEFAULT_SIZE,
|
---|
| 136 | javax.swing.GroupLayout.PREFERRED_SIZE)
|
---|
| 137 | .addComponent(openButton))
|
---|
| 138 | .addPreferredGap(
|
---|
| 139 | javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
---|
| 140 | .addComponent(upperSeparator,
|
---|
| 141 | javax.swing.GroupLayout.PREFERRED_SIZE, 10,
|
---|
| 142 | javax.swing.GroupLayout.PREFERRED_SIZE)
|
---|
| 143 | .addPreferredGap(
|
---|
| 144 | javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
---|
| 145 | .addComponent(lowerSeparator,
|
---|
| 146 | javax.swing.GroupLayout.PREFERRED_SIZE, 10,
|
---|
| 147 | javax.swing.GroupLayout.PREFERRED_SIZE)
|
---|
| 148 | .addPreferredGap(
|
---|
| 149 | javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
---|
| 150 | .addGroup(layout
|
---|
| 151 | .createParallelGroup(
|
---|
| 152 | javax.swing.GroupLayout.Alignment.LEADING)
|
---|
| 153 | .addComponent(addComponent))
|
---|
| 154 | .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
|
---|
| 155 | Short.MAX_VALUE)));
|
---|
| 156 |
|
---|
| 157 | if (item != null) {
|
---|
| 158 | componentNameTextField.setText(item.getName());
|
---|
| 159 | componentClassTextField.setText(item.getClassPath());
|
---|
| 160 | type = item.getType();
|
---|
| 161 | // for (BOAparameter param : item.getParameters()) {
|
---|
| 162 | // parameterListModel.addElement(param);
|
---|
| 163 | // }
|
---|
| 164 | addComponent.setText("Save");
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | pack();
|
---|
| 168 | setVisible(true);
|
---|
| 169 | return result;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | private void addComponent() {
|
---|
| 173 | boolean valid = true;
|
---|
| 174 | if (componentNameTextField.getText().length() == 0
|
---|
| 175 | || componentNameTextField.getText().length() > 35) {
|
---|
| 176 | valid = false;
|
---|
| 177 | JOptionPane.showMessageDialog(null,
|
---|
| 178 | "Component name should be non-empty and at most 35 characters.",
|
---|
| 179 | "Invalid parameter input", 0);
|
---|
| 180 | }
|
---|
| 181 | if (componentClassTextField.getText().length() == 0) {
|
---|
| 182 | valid = false;
|
---|
| 183 | JOptionPane.showMessageDialog(null, "Please specify a class.",
|
---|
| 184 | "Invalid parameter input", 0);
|
---|
| 185 | }
|
---|
| 186 | if (valid) {
|
---|
| 187 | String name = componentNameTextField.getText();
|
---|
| 188 | String classPath = componentClassTextField.getText();
|
---|
| 189 | BOArepItem newComponent = new BOArepItem(name, classPath, type);
|
---|
| 190 | // for (int i = 0; i < parameterListModel.getSize(); i++) {
|
---|
| 191 | // BOAparameter item = (BOAparameter) parameterListModel
|
---|
| 192 | // .getElementAt(i);
|
---|
| 193 | // newComponent.addParameter(item);
|
---|
| 194 | // }
|
---|
| 195 | BOAagentRepository.getInstance().addComponent(newComponent);
|
---|
| 196 | result = newComponent;
|
---|
| 197 | dispose();
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | private void openAction() {
|
---|
| 202 |
|
---|
| 203 | JFileChooser fc = new JFileChooser(System.getProperty("user.dir"));
|
---|
| 204 |
|
---|
| 205 | // Filter such that only directories and .class files are shown.
|
---|
| 206 | FileFilter filter = new GenericFileFilter("class",
|
---|
| 207 | "Java class files (.class)");
|
---|
| 208 | fc.setFileFilter(filter);
|
---|
| 209 |
|
---|
| 210 | // Open the file picker
|
---|
| 211 | int returnVal = fc.showOpenDialog(null);
|
---|
| 212 |
|
---|
| 213 | // If file selected
|
---|
| 214 | if (returnVal == JFileChooser.APPROVE_OPTION) {
|
---|
| 215 | File file = fc.getSelectedFile();
|
---|
| 216 |
|
---|
| 217 | try {
|
---|
| 218 | Object object = Global.loadClassFromFile(file);
|
---|
| 219 | if (object instanceof OfferingStrategy) {
|
---|
| 220 | type = BoaType.BIDDINGSTRATEGY;
|
---|
| 221 | } else if (object instanceof AcceptanceStrategy) {
|
---|
| 222 | type = BoaType.ACCEPTANCESTRATEGY;
|
---|
| 223 | } else if (object instanceof OpponentModel) {
|
---|
| 224 | type = BoaType.OPPONENTMODEL;
|
---|
| 225 | } else if (object instanceof OMStrategy) {
|
---|
| 226 | type = BoaType.OMSTRATEGY;
|
---|
| 227 | } else {
|
---|
| 228 | throw new IllegalArgumentException("File " + file
|
---|
| 229 | + " does not extend OfferingStrategy, AcceptanceStrategy, \n"
|
---|
| 230 | + "OpponentModel, or OMStrategy.");
|
---|
| 231 | }
|
---|
| 232 | } catch (Throwable e) {
|
---|
| 233 | Global.showLoadError(file, e);
|
---|
| 234 | return;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | try {
|
---|
| 238 | componentClassTextField.setText(file.getCanonicalPath());
|
---|
| 239 | if (componentNameTextField.getText().isEmpty()) {
|
---|
| 240 | componentNameTextField.setText(file.getName());
|
---|
| 241 | }
|
---|
| 242 | } catch (IOException e) {
|
---|
| 243 | // TODO Auto-generated catch block
|
---|
| 244 | e.printStackTrace();
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | }
|
---|
| 248 | }
|
---|
[1] | 249 | } |
---|