root / trunk / code / projects / colonet / client / TaskAddWindow.java @ 559
History | View | Annotate | Download (3.47 KB)
1 |
|
---|---|
2 |
import javax.swing.*; |
3 |
import javax.swing.event.*; |
4 |
import java.awt.*; |
5 |
import java.awt.event.*; |
6 |
|
7 |
/**
|
8 |
* TaskAddWindow class
|
9 |
* A window that provides a simple way to add tasks to a task queue.
|
10 |
*/
|
11 |
public class TaskAddWindow extends JFrame implements ActionListener, ListSelectionListener { |
12 |
JPanel panelButtons;
|
13 |
JPanel panelParameters;
|
14 |
JPanel panelSouth;
|
15 |
JPanel panelSelection;
|
16 |
JButton btnSubmit;
|
17 |
JButton btnCancel;
|
18 |
DefaultListModel availableListModel;
|
19 |
JList availableList;
|
20 |
JScrollPane spAvailableTasks;
|
21 |
JTextArea txtDescription;
|
22 |
JTextField txtParameters;
|
23 |
|
24 |
public TaskAddWindow () {
|
25 |
super("Add a Task"); |
26 |
super.setSize(500,500); |
27 |
super.setLayout(new BorderLayout()); |
28 |
|
29 |
// set up buttons
|
30 |
btnSubmit = new JButton("Submit"); |
31 |
btnCancel = new JButton("Cancel"); |
32 |
panelButtons = new JPanel(); |
33 |
panelButtons.setLayout(new FlowLayout()); |
34 |
panelButtons.add(btnSubmit); |
35 |
panelButtons.add(btnCancel); |
36 |
this.getRootPane().setDefaultButton(btnSubmit);
|
37 |
|
38 |
// set up task list
|
39 |
availableListModel = new DefaultListModel(); |
40 |
availableListModel.addElement("Map the Environment");
|
41 |
availableListModel.addElement("Clean Up Chemical Spill");
|
42 |
availableListModel.addElement("Grow Plants");
|
43 |
availableListModel.addElement("Save the Cheerleader");
|
44 |
availableListModel.addElement("Save the World");
|
45 |
availableList = new JList(availableListModel); |
46 |
availableList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
47 |
availableList.setSelectedIndex(-1);
|
48 |
spAvailableTasks = new JScrollPane(availableList); |
49 |
spAvailableTasks.setBorder(BorderFactory.createTitledBorder("Select A Task")); |
50 |
txtDescription = new JTextArea(); |
51 |
txtDescription.setEditable(false);
|
52 |
txtDescription.setLineWrap(true);
|
53 |
txtDescription.setWrapStyleWord(true);
|
54 |
txtDescription.setBorder(BorderFactory.createTitledBorder("Description")); |
55 |
|
56 |
//set up parameter area
|
57 |
panelParameters = new JPanel(); |
58 |
panelParameters.setLayout(new BorderLayout()); |
59 |
txtParameters = new JTextField(); |
60 |
panelParameters.add(new JLabel("Optional parameters for this task: "), BorderLayout.WEST); |
61 |
panelParameters.add(txtParameters); |
62 |
|
63 |
// assemble objects
|
64 |
panelSelection = new JPanel(); |
65 |
panelSelection.setLayout(new GridLayout(1,2)); |
66 |
panelSelection.add(spAvailableTasks); |
67 |
panelSelection.add(txtDescription); |
68 |
|
69 |
panelSouth = new JPanel(); |
70 |
panelSouth.setLayout(new GridLayout(2,1)); |
71 |
panelSouth.add(panelParameters); |
72 |
panelSouth.add(panelButtons); |
73 |
|
74 |
this.getContentPane().add(panelSouth, BorderLayout.SOUTH); |
75 |
this.getContentPane().add(panelSelection, BorderLayout.CENTER); |
76 |
this.setLocationRelativeTo(null); |
77 |
|
78 |
// add listeners here
|
79 |
availableList.addListSelectionListener(this);
|
80 |
btnSubmit.addActionListener(this);
|
81 |
btnCancel.addActionListener(this);
|
82 |
} |
83 |
|
84 |
public void prompt () { |
85 |
this.setVisible(true); |
86 |
} |
87 |
|
88 |
private String getDescription (int index) { |
89 |
if (index < 0) |
90 |
return ""; |
91 |
switch (index) {
|
92 |
case 0: return "SLAM and junk"; |
93 |
case 1: return "I'm not sure this works"; |
94 |
case 2: return "Push them into the light"; |
95 |
case 3: return "..."; |
96 |
case 4: return "..."; |
97 |
|
98 |
default: return "Task not recognized"; |
99 |
} |
100 |
} |
101 |
|
102 |
public void actionPerformed (ActionEvent e) { |
103 |
Object source = e.getSource();
|
104 |
if (source == btnSubmit) {
|
105 |
txtParameters.setText(txtParameters.getText().trim()); |
106 |
|
107 |
|
108 |
this.setVisible(false); |
109 |
} else if (source == btnCancel) { |
110 |
this.setVisible(false); |
111 |
} |
112 |
} |
113 |
|
114 |
public void valueChanged (ListSelectionEvent e) { |
115 |
int index = availableList.getSelectedIndex();
|
116 |
if (index >= 0) |
117 |
txtDescription.setText(getDescription(index)); |
118 |
} |
119 |
|
120 |
} |