1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package zeus.visualiser.report;
25
26 import java.awt.*;
27 import java.awt.event.*;
28 import java.util.*;
29 import javax.swing.*;
30 import javax.swing.event.*;
31
32 import zeus.gui.*;
33 import zeus.util.Assert;
34 import zeus.util.Misc;
35
36
37 public class DeleteReportDialog extends JDialog
38 implements ActionListener,
39 ListSelectionListener,
40 ChangeListener {
41
42 protected DefaultListModel agentListModel, taskListModel;
43 protected JList agentList, taskList;
44 protected JButton cancelButton;
45 protected JButton delButton;
46 protected JButton selectButton;
47 protected JButton clearButton;
48 protected JButton invertButton;
49 protected ReportModel graphModel = null;
50
51 public DeleteReportDialog(JFrame frame, String title) {
52 super(frame, title, false);
53
54 JLabel agentLabel = new JLabel("Agents");
55 JLabel taskLabel = new JLabel("Tasks");
56 JLabel blankLabel = new JLabel("");
57
58 agentListModel = new DefaultListModel();
59 taskListModel = new DefaultListModel();
60 agentList = new JList(agentListModel);
61 taskList = new JList(taskListModel);
62
63 cancelButton = new JButton("Cancel");
64
65 GridBagLayout gb = new GridBagLayout();
66 GridBagConstraints gbc = new GridBagConstraints();
67 Container pane = getContentPane();
68 pane.setLayout(gb);
69
70 gbc.insets = new Insets(10,10,0,0);
71 gbc.anchor = GridBagConstraints.NORTHWEST;
72 gbc.fill = GridBagConstraints.NONE;
73 gbc.gridwidth = 1;
74 gb.setConstraints(agentLabel, gbc);
75 pane.add(agentLabel);
76
77 gbc.insets = new Insets(10,10,0,0);
78 gb.setConstraints(taskLabel, gbc);
79 pane.add(taskLabel);
80
81 gbc.insets = new Insets(10,10,0,10);
82 gbc.gridwidth = GridBagConstraints.REMAINDER;
83 gb.setConstraints(blankLabel,gbc);
84 pane.add(blankLabel);
85
86 JScrollPane scrollpane = new JScrollPane(agentList);
87 scrollpane.setPreferredSize(new Dimension(100,150));
88 gbc.weightx = gbc.weighty = 1.0;
89 gbc.insets = new Insets(5,10,10,0);
90 gbc.anchor = GridBagConstraints.NORTHWEST;
91 gbc.fill = GridBagConstraints.BOTH;
92 gbc.gridwidth = 1;
93 gb.setConstraints(scrollpane,gbc);
94 pane.add(scrollpane);
95
96 scrollpane = new JScrollPane(taskList);
97 scrollpane.setPreferredSize(new Dimension(100,150));
98 gbc.insets = new Insets(5,10,10,0);
99 gb.setConstraints(scrollpane,gbc);
100 pane.add(scrollpane);
101
102 selectButton = new JButton("Select All");
103 clearButton = new JButton("Clear All");
104 invertButton = new JButton("Invert Selection");
105 delButton = new JButton("Delete");
106
107 JPanel panel = new JPanel();
108 panel.setLayout(new GridLayout(4,1,2,2));
109 panel.add(selectButton);
110 panel.add(clearButton);
111 panel.add(invertButton);
112 panel.add(delButton);
113 gbc.insets = new Insets(5,10,10,10);
114 gbc.weightx = gbc.weighty = 0;
115 gbc.gridwidth = GridBagConstraints.REMAINDER;
116 gbc.fill = GridBagConstraints.NONE;
117 gb.setConstraints(panel, gbc);
118 pane.add(panel);
119
120 JSeparator separator = new JSeparator();
121 gbc.insets = new Insets(0,0,0,0);
122 gbc.anchor = GridBagConstraints.NORTHWEST;
123 gbc.fill = GridBagConstraints.HORIZONTAL;
124 gbc.gridwidth = GridBagConstraints.REMAINDER;
125 gb.setConstraints(separator,gbc);
126 pane.add(separator);
127
128 gbc.insets = new Insets(10,0,10,0);
129 gbc.anchor = GridBagConstraints.CENTER;
130 gbc.fill = GridBagConstraints.NONE;
131 gbc.gridwidth = GridBagConstraints.REMAINDER;
132 gb.setConstraints(cancelButton,gbc);
133 pane.add(cancelButton);
134
135 delButton.addActionListener(this);
136 selectButton.addActionListener(this);
137 clearButton.addActionListener(this);
138 invertButton.addActionListener(this);
139 cancelButton.addActionListener(this);
140 agentList.addListSelectionListener(this);
141 taskList.addListSelectionListener(this);
142
143 addWindowListener(new WindowAdapter() {
144 public void windowClosing(WindowEvent evt) {
145 cancelBtnFn();
146 }
147 });
148
149 agentList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
150 taskList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
151 agentList.addListSelectionListener(this);
152
153 }
154
155 public void actionPerformed(ActionEvent evt) {
156 Object source = evt.getSource();
157 int num;
158
159 if ( source == selectButton ) {
160 num = taskListModel.getSize();
161 taskList.setSelectionInterval(0,num-1);
162 }
163 else if ( source == clearButton )
164 taskList.clearSelection();
165 else if ( source == invertButton ) {
166 num = taskListModel.getSize();
167 int[] indices = taskList.getSelectedIndices();
168 taskList.clearSelection();
169 for(int i = 0; i < num; i++ ) {
170 boolean status = false;
171 for(int j = 0; !status && j < indices.length; j++ )
172 status = (indices[j] == i);
173 if ( !status )
174 taskList.addSelectionInterval(i,i);
175 }
176 }
177 else if ( source == delButton )
178 deleteBtnFn();
179 else if ( source == cancelButton )
180 cancelBtnFn();
181 }
182
183 private boolean agentSelected() {
184 ListSelectionModel selmodel = agentList.getSelectionModel();
185 if ( selmodel.isSelectionEmpty() ) {
186 JOptionPane.showMessageDialog(this,
187 "Select an agent before calling this operation", "Error",
188 JOptionPane.ERROR_MESSAGE);
189 return false;
190 }
191 return true;
192 }
193
194 private boolean taskSelected() {
195 ListSelectionModel selmodel = taskList.getSelectionModel();
196 if( selmodel.isSelectionEmpty() ) {
197 JOptionPane.showMessageDialog(this,
198 "Select one or more task entries before\ncalling this operation",
199 "Error", JOptionPane.ERROR_MESSAGE);
200 return false;
201 }
202 return true;
203 }
204
205 protected void cancelBtnFn() {
206 setVisible(false);
207 if ( graphModel != null )
208 graphModel.removeChangeListener(this);
209 }
210
211 protected void deleteBtnFn() {
212 if ( !agentSelected() || !taskSelected() )
213 return;
214
215 String agent = (String)agentList.getSelectedValue();
216 Object obj[] = taskList.getSelectedValues();
217
218 if ( graphModel != null )
219 graphModel.removeTasks(agent,Misc.stringArray(obj));
220 }
221
222 public void display(ReportModel graphModel) {
223 this.graphModel = graphModel;
224 reset();
225 graphModel.addChangeListener(this);
226 pack();
227 setVisible(true);
228 }
229
230 public void valueChanged(ListSelectionEvent evt) {
231 JList list = (JList)evt.getSource();
232 String value = (String)list.getSelectedValue();
233 if ( list == agentList && graphModel != null && value != null ) {
234 String[] tasks = graphModel.getTasks(value);
235 taskList.clearSelection();
236 taskListModel.removeAllElements();
237 for(int i = 0; i < tasks.length; i++ )
238 taskListModel.addElement(tasks[i]);
239 }
240 }
241
242 public void stateChanged(ChangeEvent evt) {
243 if ( graphModel != null && evt.getSource() == graphModel ) {
244 String selection = (String)agentList.getSelectedValue();
245 Object[] tasks = taskList.getSelectedValues();
246 agentList.clearSelection();
247 agentListModel.removeAllElements();
248 String[] agents = graphModel.getAgents();
249 for(int i = 0; i < agents.length; i++ )
250 agentListModel.addElement(agents[i]);
251 if ( selection != null )
252 agentList.setSelectedValue(selection,true);
253
254 int num = taskListModel.getSize();
255 for(int i = 0; i < tasks.length; i++ )
256 for(int j = 0; j < num; j++ )
257 if ( tasks[i] == taskListModel.getElementAt(j) )
258 taskList.addSelectionInterval(j,j);
259 }
260 }
261
262 public void reset() {
263 agentList.clearSelection();
264 taskList.clearSelection();
265 agentListModel.removeAllElements();
266 if ( graphModel != null ) {
267 String[] agents = graphModel.getAgents();
268 for(int i = 0; i < agents.length; i++ )
269 agentListModel.addElement(agents[i]);
270 }
271 }
272 }