1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package coras.client.ui;
22
23 import java.awt.Cursor;
24 import java.awt.Frame;
25 import java.awt.Rectangle;
26 import java.awt.event.KeyEvent;
27 import java.util.Collection;
28 import java.util.Iterator;
29
30 import javax.swing.DefaultListModel;
31 import javax.swing.JDialog;
32 import javax.swing.JOptionPane;
33
34 import coras.client.SwingWorker;
35 import coras.riskanalysis.RiskAnalysisProject;
36
37 /***
38 * @author fvr
39 *
40 * To change the template for this generated type comment go to
41 * Window - Preferences - Java - Code Generation - Code and Comments
42 */
43 public class OpenProjectDialog extends JDialog {
44 private javax.swing.JPanel jContentPane = null;
45
46 private javax.swing.JPanel buttonPanel = null;
47 private javax.swing.JButton openButton = null;
48 private javax.swing.JButton cancelButton = null;
49 private javax.swing.JList projectList = null;
50 private javax.swing.JScrollPane jScrollPane = null;
51
52 protected int result = -1;
53 /***
54 * This is the default constructor
55 */
56 public OpenProjectDialog() {
57 this(null);
58 }
59
60 public OpenProjectDialog(Frame owner) {
61 super(owner);
62 initialize();
63 }
64
65 public int getResult() {
66 return result;
67 }
68
69 /***
70 *
71 * @param projects
72 */
73 public void setProjects(Collection projects) {
74 DefaultListModel model = (DefaultListModel) projectList.getModel();
75 model.removeAllElements();
76 if (projects == null || projects.size() == 0) {
77 model.addElement("No projects found");
78 } else {
79 for (Iterator i = projects.iterator(); i.hasNext(); ) {
80 ProjectItem item = new ProjectItem((RiskAnalysisProject) i.next());
81 model.addElement(item);
82 }
83 }
84 projectList.repaint();
85 }
86
87 public RiskAnalysisProject getSelectedProject() {
88 Object o = projectList.getSelectedValue();
89 if (o instanceof ProjectItem) {
90 return ((ProjectItem)o).project;
91 } else {
92 return null;
93 }
94 }
95
96 private void retrieveProjectList() {
97 setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
98 getOwner().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
99
100 DefaultListModel model = (DefaultListModel) projectList.getModel();
101 model.removeAllElements();
102 model.addElement("Retrieving project list...");
103
104 new SwingWorker() {
105 public Object construct() {
106 return RiskAnalysisProject.getProjects();
107 }
108 public void finished() {
109 Collection c = (Collection) get();
110 setProjects(c);
111 setCursor(Cursor.getDefaultCursor());
112 getOwner().setCursor(Cursor.getDefaultCursor());
113 }
114 }.start();
115 }
116
117 /***
118 * This method initializes this
119 *
120 * @return void
121 */
122 private void initialize() {
123 this.setBounds(0, 0, 400, 200);
124 this.setContentPane(getJContentPane());
125 this.setTitle("Open Project");
126 this.setModal(true);
127 this.addWindowListener(new java.awt.event.WindowAdapter() {
128 public void windowOpened(java.awt.event.WindowEvent ev) {
129 retrieveProjectList();
130 }
131 });
132 }
133
134 /***
135 * This method initializes jContentPane
136 *
137 * @return javax.swing.JPanel
138 */
139 private javax.swing.JPanel getJContentPane() {
140 if(jContentPane == null) {
141 jContentPane = new javax.swing.JPanel();
142 jContentPane.setLayout(new java.awt.BorderLayout());
143 jContentPane.add(getButtonPanel(), java.awt.BorderLayout.SOUTH);
144 jContentPane.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
145 }
146 return jContentPane;
147 }
148 /***
149 * This method initializes jPanel
150 *
151 * @return javax.swing.JPanel
152 */
153 private javax.swing.JPanel getButtonPanel() {
154 if (buttonPanel == null) {
155 buttonPanel = new javax.swing.JPanel();
156 buttonPanel.add(getOpenButton(), null);
157 buttonPanel.add(getCancelButton(), null);
158 }
159 return buttonPanel;
160 }
161 /***
162 * This method initializes jButton
163 *
164 * @return javax.swing.JButton
165 */
166 private javax.swing.JButton getOpenButton() {
167 if (openButton == null) {
168 openButton = new javax.swing.JButton();
169 openButton.setText("Open");
170 openButton.setEnabled(false);
171 openButton.setMnemonic(java.awt.event.KeyEvent.VK_O);
172 openButton.addActionListener(new java.awt.event.ActionListener() {
173 public void actionPerformed(java.awt.event.ActionEvent e) {
174 result = JOptionPane.OK_OPTION;
175 dispose();
176 }
177 });
178 }
179 return openButton;
180 }
181 /***
182 * This method initializes jButton1
183 *
184 * @return javax.swing.JButton
185 */
186 private javax.swing.JButton getCancelButton() {
187 if (cancelButton == null) {
188 cancelButton = new javax.swing.JButton();
189 cancelButton.setText("Cancel");
190 cancelButton.setMnemonic(java.awt.event.KeyEvent.VK_C);
191 cancelButton.addActionListener(new java.awt.event.ActionListener() {
192 public void actionPerformed(java.awt.event.ActionEvent e) {
193 result = JOptionPane.CANCEL_OPTION;
194 dispose();
195 }
196 });
197 }
198 return cancelButton;
199 }
200 /***
201 * This method initializes jList
202 *
203 * @return javax.swing.JList
204 */
205 private javax.swing.JList getProjectList() {
206 if (projectList == null) {
207 projectList = new javax.swing.JList();
208 projectList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
209 projectList.addKeyListener(new java.awt.event.KeyAdapter() {
210 public void keyReleased(java.awt.event.KeyEvent e) {
211 if (e.getKeyCode() == KeyEvent.VK_ENTER) {
212 result = JOptionPane.OK_OPTION;
213 dispose();
214 }
215 }
216 });
217
218 projectList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
219 public void valueChanged(javax.swing.event.ListSelectionEvent e) {
220 openButton.setEnabled(projectList.getSelectedValue() instanceof ProjectItem);
221 }
222 });
223 projectList.addMouseListener(new java.awt.event.MouseAdapter() {
224 public void mouseClicked(java.awt.event.MouseEvent e) {
225 if (e.getClickCount() == 2) {
226 if (! (projectList.getSelectedValue() instanceof ProjectItem)) {
227 return;
228 }
229 int index = projectList.getSelectedIndex();
230 Rectangle bounds = projectList.getCellBounds(index, index);
231 if (bounds.contains(e.getPoint())) {
232 result = JOptionPane.OK_OPTION;
233 dispose();
234 }
235 }
236 }
237 });
238
239 projectList.setModel(new DefaultListModel());
240 }
241 return projectList;
242 }
243 /***
244 * This method initializes jScrollPane
245 *
246 * @return javax.swing.JScrollPane
247 */
248 private javax.swing.JScrollPane getJScrollPane() {
249 if (jScrollPane == null) {
250 jScrollPane = new javax.swing.JScrollPane();
251 jScrollPane.setViewportView(getProjectList());
252 }
253 return jScrollPane;
254 }
255
256 private class ProjectItem {
257 RiskAnalysisProject project;
258 ProjectItem(RiskAnalysisProject project) {
259 this.project = project;
260 }
261 public String toString() {
262 String name = null;
263 String description = null;
264 if (project != null) {
265 name = project.getName();
266 description = project.getShortDescription();
267 }
268 if (name == null && description == null) {
269 return "NoName";
270 } else if (description == null) {
271 return name;
272 } else if (name == null) {
273 return "NoName - " + description;
274 } else {
275 return name + " - " + description;
276 }
277 }
278 }
279
280 }