1 | package genius.gui.panels;
|
---|
2 |
|
---|
3 | import java.awt.Frame;
|
---|
4 | import java.awt.event.ActionEvent;
|
---|
5 | import java.awt.event.ActionListener;
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.List;
|
---|
8 |
|
---|
9 | import javax.swing.GroupLayout;
|
---|
10 | import javax.swing.JButton;
|
---|
11 | import javax.swing.JDialog;
|
---|
12 | import javax.swing.JList;
|
---|
13 | import javax.swing.JScrollPane;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Improved version of the ProfileVarUI and AgentVarUI classes.
|
---|
17 | *
|
---|
18 | * @author Mark Hendrikx (m.j.c.hendrikx@student.tudelft.nl)
|
---|
19 | * @version 04/12/11
|
---|
20 | * @param <A>
|
---|
21 | * type of the profile items
|
---|
22 | */
|
---|
23 | public class RepItemVarUI<A> extends JDialog {
|
---|
24 |
|
---|
25 | private static final long serialVersionUID = 1L;
|
---|
26 | private JButton okButton = new JButton("Ok");
|
---|
27 | private JButton clearButton = new JButton("Clear");
|
---|
28 | private JButton cancelButton = new JButton("Cancel");
|
---|
29 | private JList<A> profileList = new JList<A>();
|
---|
30 | private JScrollPane scrollPane = new JScrollPane();
|
---|
31 | private ArrayList<A> result;
|
---|
32 | private ExtendedListModel<A> model;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Creates the RepItem Selector.
|
---|
36 | *
|
---|
37 | * @param frame
|
---|
38 | * of the caller
|
---|
39 | */
|
---|
40 | public RepItemVarUI(Frame frame, String title) {
|
---|
41 | super(frame, title, true);
|
---|
42 | this.setLocation(frame.getLocation().x + frame.getWidth() / 2, frame.getLocation().y + frame.getHeight() / 4);
|
---|
43 | this.setSize(frame.getSize().width / 3, frame.getSize().height / 2);
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Initialize the GUI components. The GUI code is based on code created by
|
---|
48 | * using the Netbeans GUIbuilder.
|
---|
49 | *
|
---|
50 | * @return list of profiles
|
---|
51 | */
|
---|
52 | public List<A> getResult(ArrayList<A> items, ArrayList<A> selectedItems) {
|
---|
53 |
|
---|
54 | // Set the list model
|
---|
55 | model = new ExtendedListModel<A>();
|
---|
56 | profileList.setModel(model);
|
---|
57 |
|
---|
58 | model.setInitialContent(items);
|
---|
59 |
|
---|
60 | // Set a custom selection model (each click toggles a listitem on/off)
|
---|
61 | profileList.setSelectionModel(new MultiListSelectionModel());
|
---|
62 |
|
---|
63 | // Select previously selected items
|
---|
64 | for (A item : selectedItems) {
|
---|
65 | profileList.setSelectedValue(item, true);
|
---|
66 | }
|
---|
67 |
|
---|
68 | scrollPane.setViewportView(profileList);
|
---|
69 |
|
---|
70 | okButton.addActionListener(new ActionListener() {
|
---|
71 | public void actionPerformed(ActionEvent e) {
|
---|
72 | ArrayList<A> profiles = new ArrayList<A>();
|
---|
73 | for (int item : profileList.getSelectedIndices()) {
|
---|
74 | profiles.add((A) model.getElementAt(item));
|
---|
75 | }
|
---|
76 | result = profiles;
|
---|
77 | dispose();
|
---|
78 | }
|
---|
79 | });
|
---|
80 |
|
---|
81 | clearButton.addActionListener(new ActionListener() {
|
---|
82 | public void actionPerformed(ActionEvent e) {
|
---|
83 | profileList.clearSelection();
|
---|
84 | }
|
---|
85 | });
|
---|
86 |
|
---|
87 | cancelButton.addActionListener(new ActionListener() {
|
---|
88 | public void actionPerformed(ActionEvent e) {
|
---|
89 | dispose();
|
---|
90 | }
|
---|
91 | });
|
---|
92 |
|
---|
93 | // Set the layout of the GUI (autogenerated using Netbeans)
|
---|
94 | javax.swing.GroupLayout layout = new GroupLayout(getContentPane());
|
---|
95 | this.setLayout(layout);
|
---|
96 | layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
---|
97 | .addGroup(layout.createSequentialGroup().addContainerGap()
|
---|
98 | .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
---|
99 | .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
|
---|
100 | .addGroup(layout.createSequentialGroup()
|
---|
101 | .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 82,
|
---|
102 | javax.swing.GroupLayout.PREFERRED_SIZE)
|
---|
103 | .addGap(18, 18, 18)
|
---|
104 | .addComponent(clearButton, javax.swing.GroupLayout.PREFERRED_SIZE, 82,
|
---|
105 | javax.swing.GroupLayout.PREFERRED_SIZE)
|
---|
106 | .addGap(18, 18, 18).addComponent(cancelButton,
|
---|
107 | javax.swing.GroupLayout.PREFERRED_SIZE, 82,
|
---|
108 | javax.swing.GroupLayout.PREFERRED_SIZE)))
|
---|
109 | .addGap(18, 18, 18).addContainerGap()));
|
---|
110 | layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
|
---|
111 | javax.swing.GroupLayout.Alignment.TRAILING,
|
---|
112 | layout.createSequentialGroup().addContainerGap()
|
---|
113 | .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE)
|
---|
114 | .addGap(18, 18, 18)
|
---|
115 | .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
|
---|
116 | .addComponent(okButton).addComponent(clearButton).addComponent(cancelButton))
|
---|
117 | .addContainerGap()));
|
---|
118 | setVisible(true);
|
---|
119 | return result;
|
---|
120 | }
|
---|
121 | } |
---|