1 | /************************************************************************************ |
---|
2 | * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University |
---|
3 | * All rights reserved. |
---|
4 | * Refer to LICENSE for terms and conditions of use. |
---|
5 | ***********************************************************************************/ |
---|
6 | package vnsim.gui.selector; |
---|
7 | |
---|
8 | |
---|
9 | import java.awt.Dimension; |
---|
10 | import java.util.Vector; |
---|
11 | import javax.swing.*; |
---|
12 | import vnsim.vehicular.scenarios.Scenario; |
---|
13 | import vnsim.vehicular.scenarios.ScenarioMap; |
---|
14 | import java.awt.event.ActionEvent; |
---|
15 | import java.awt.event.ActionListener; |
---|
16 | import java.io.File; |
---|
17 | import java.io.FileInputStream; |
---|
18 | import java.io.ObjectInputStream; |
---|
19 | |
---|
20 | public class ScenarioSelection extends JFrame { |
---|
21 | /** |
---|
22 | * |
---|
23 | */ |
---|
24 | private static final long serialVersionUID = -90020853135669495L; |
---|
25 | JFrame fileSelectionFrame; |
---|
26 | Scenario outScenario=null; |
---|
27 | JPanel mapsPanel,buttonPanel; |
---|
28 | |
---|
29 | Vector<JButton> alButtons; |
---|
30 | |
---|
31 | JButton loadFile, cancel; |
---|
32 | JScrollPane scrollMaps; |
---|
33 | Integer mutex; |
---|
34 | |
---|
35 | int indexSelection = -1; |
---|
36 | |
---|
37 | File selectedFile; |
---|
38 | |
---|
39 | public class LocalActionListener implements ActionListener { |
---|
40 | public void actionPerformed(ActionEvent e) { |
---|
41 | if (e.getSource() == loadFile) { |
---|
42 | setVisible(false); |
---|
43 | fileSelectionFrame = new JFrame(); |
---|
44 | JFileChooser fc = new JFileChooser(); |
---|
45 | fc.setCurrentDirectory(new File(".")); |
---|
46 | // fileSelectionFrame.add(fc); |
---|
47 | fileSelectionFrame.setVisible(true); |
---|
48 | int returnVal = fc.showOpenDialog(fileSelectionFrame); |
---|
49 | if (returnVal == JFileChooser.APPROVE_OPTION) { |
---|
50 | selectedFile = fc.getSelectedFile(); |
---|
51 | System.out.println("File selected :" + selectedFile); |
---|
52 | try { |
---|
53 | ObjectInputStream ois = new ObjectInputStream( |
---|
54 | new FileInputStream(selectedFile)); |
---|
55 | outScenario = (Scenario) ois.readObject(); |
---|
56 | } catch (Exception ee) { |
---|
57 | ee.printStackTrace(); |
---|
58 | } |
---|
59 | synchronized (mutex) { |
---|
60 | mutex.notify(); |
---|
61 | } |
---|
62 | fileSelectionFrame.setVisible(false); |
---|
63 | } else { |
---|
64 | fileSelectionFrame.setVisible(false); |
---|
65 | setVisible(true); |
---|
66 | } |
---|
67 | } |
---|
68 | if (e.getSource() == cancel) { |
---|
69 | System.exit(0); |
---|
70 | } |
---|
71 | for (int i = 0; i < alButtons.size(); i++) { |
---|
72 | if (e.getSource() == alButtons.elementAt(i)) { |
---|
73 | indexSelection = i; |
---|
74 | setVisible(false); |
---|
75 | synchronized (mutex) { |
---|
76 | mutex.notify(); |
---|
77 | } |
---|
78 | break; |
---|
79 | } |
---|
80 | } |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | public ScenarioSelection(Vector<ScenarioMap> v, Integer m) |
---|
85 | { |
---|
86 | super("MapSelection"); |
---|
87 | this.mutex = m; |
---|
88 | SpringLayout l1, l2, l3; |
---|
89 | l1 = new SpringLayout(); |
---|
90 | l2 = new SpringLayout(); |
---|
91 | l3 = new SpringLayout(); |
---|
92 | |
---|
93 | this.setLayout(l1); |
---|
94 | LocalActionListener lal = new LocalActionListener(); |
---|
95 | |
---|
96 | mapsPanel=new JPanel(); |
---|
97 | mapsPanel.setLayout(l2); |
---|
98 | |
---|
99 | alButtons = new Vector<JButton>(); |
---|
100 | |
---|
101 | JButton b; |
---|
102 | JLabel l; |
---|
103 | ImageIcon icon; |
---|
104 | SpringLayout.Constraints constraint; |
---|
105 | for (int i = 0; i < v.size(); i++) { |
---|
106 | |
---|
107 | icon = new ImageIcon(".//images//"+v.elementAt(i).imageFileName); |
---|
108 | |
---|
109 | //System.out.println("imagName="+v.elementAt(i).imageFileName+ "icon="+icon.getImageLoadStatus()); |
---|
110 | l = new JLabel(icon); |
---|
111 | b = new JButton(v.elementAt(i).name); |
---|
112 | b.addActionListener(lal); |
---|
113 | alButtons.add(b); |
---|
114 | |
---|
115 | constraint = l2.getConstraints(l); |
---|
116 | constraint.setX(Spring.constant(1)); |
---|
117 | constraint.setY(Spring.constant(200 * i + 1)); |
---|
118 | constraint.setWidth(Spring.constant(300)); |
---|
119 | constraint.setHeight(Spring.constant(198)); |
---|
120 | |
---|
121 | constraint = l2.getConstraints(b); |
---|
122 | constraint.setX(Spring.constant(310)); |
---|
123 | constraint.setY(Spring.constant(200 * i + 101)); |
---|
124 | constraint.setWidth(Spring.constant(100)); |
---|
125 | constraint.setHeight(Spring.constant(50)); |
---|
126 | mapsPanel.add(l); |
---|
127 | mapsPanel.add(b); |
---|
128 | |
---|
129 | } |
---|
130 | mapsPanel.setPreferredSize(new Dimension(450, (200 * v.size() + 100))); |
---|
131 | scrollMaps = new JScrollPane(); |
---|
132 | scrollMaps.setPreferredSize(new Dimension(450, 600)); |
---|
133 | scrollMaps.setViewportView(mapsPanel); |
---|
134 | |
---|
135 | constraint = l1.getConstraints(scrollMaps); |
---|
136 | constraint.setX(Spring.constant(1)); |
---|
137 | constraint.setY(Spring.constant(1)); |
---|
138 | constraint.setWidth(Spring.constant(450)); |
---|
139 | constraint.setHeight(Spring.constant(600)); |
---|
140 | |
---|
141 | buttonPanel=new JPanel(); |
---|
142 | buttonPanel.setLayout(l3); |
---|
143 | |
---|
144 | loadFile = new JButton("LoadOldConfig"); |
---|
145 | cancel = new JButton("Cancel"); |
---|
146 | |
---|
147 | loadFile.addActionListener(lal); |
---|
148 | cancel.addActionListener(lal); |
---|
149 | |
---|
150 | constraint = l3.getConstraints(loadFile); |
---|
151 | constraint.setX(Spring.constant(50)); |
---|
152 | constraint.setY(Spring.constant(1)); |
---|
153 | constraint.setWidth(Spring.constant(150)); |
---|
154 | constraint.setHeight(Spring.constant(50)); |
---|
155 | |
---|
156 | constraint = l3.getConstraints(cancel); |
---|
157 | constraint.setX(Spring.constant(250)); |
---|
158 | constraint.setY(Spring.constant(1)); |
---|
159 | constraint.setWidth(Spring.constant(150)); |
---|
160 | constraint.setHeight(Spring.constant(50)); |
---|
161 | |
---|
162 | buttonPanel.add(loadFile); |
---|
163 | buttonPanel.add(cancel); |
---|
164 | |
---|
165 | constraint = l1.getConstraints(buttonPanel); |
---|
166 | constraint.setX(Spring.constant(1)); |
---|
167 | constraint.setY(Spring.constant(601)); |
---|
168 | constraint.setWidth(Spring.constant(450)); |
---|
169 | constraint.setHeight(Spring.constant(60)); |
---|
170 | |
---|
171 | this.add(scrollMaps); |
---|
172 | this.add(buttonPanel); |
---|
173 | this.setSize(480, 690); |
---|
174 | this.setLocation(10, 10); |
---|
175 | this.setResizable(false); |
---|
176 | this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
---|
177 | this.setVisible(true); |
---|
178 | |
---|
179 | } |
---|
180 | |
---|
181 | public Scenario getLoadedScenario() { |
---|
182 | return this.outScenario; |
---|
183 | } |
---|
184 | |
---|
185 | public int getSelection() { |
---|
186 | return this.indexSelection; |
---|
187 | } |
---|
188 | |
---|
189 | public File getSelectedFile() { |
---|
190 | return this.selectedFile; |
---|
191 | } |
---|
192 | } |
---|