1 /*****************************************************************************
2 * Virtual Mockup for Machine Vision
3 * Copyright (C) 2001-2003 Fabio R. de Miranda, João E. Kogler Jr.,
4 * Carlos S. Santos.
5 * Virtual Mockup for Machine Vision Project funded by SENAC-SP
6 *
7 * Permission is granted to redistribute and/or modify this
8 * software under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License (http://www.gnu.org/copyleft/lesser.html)
16 * for more details.
17 *
18 *****************************************************************************/
19
20 package camera3d.gui;
21 /*
22 * Title: Câmera Virtual - LIVES
23 * Description: Câmera Virtual para Controle via LabVIEW
24 * Copyright: Copyright (c) 2001
25 * Company: Centro de Educação em Informática - SENAC - SP
26 */
27
28 import javax.swing.*;
29 import java.awt.*;
30 import java.awt.event.*;
31 import camera3d.*;
32 import camera3d.SelectionChangeListener;
33 import camera3d.SceneObjectsChangeListener;
34 import camera3d.event.*;
35 import java.util.*;
36
37 /***
38 * Pane where are shown the selection list entries.
39 *
40 * @author Fábio Roberto de Miranda
41 * @version 1.0
42 * @see camera3d.gui.SelectionListEntry
43 * @see camera3d.SelectionList
44 */
45 class SelectionListPane extends Box implements ActionListener, SelectionChangeListener, SceneObjectsChangeListener {
46
47 private Dimension minFillerSize = new Dimension(60, 15);
48 private Dimension maxFillerSize = new Dimension(60, 15);
49 private Dimension prefFillerSize = new Dimension(60,15);
50
51 private SelectionList list;
52
53 private int i;
54
55 int preferredWidth = 55;
56 int preferredHeight = 70;
57
58 int currentSize;
59 int initialSize = 50;
60
61 private GUIControl guiControl;
62
63 SelectionListEntry[] listEntry;
64 boolean singleObjectSelection = true;
65
66 public SelectionListPane(GUIControl guiControl) {
67 this();
68 this.guiControl = guiControl;
69 }
70
71 public SelectionListPane() {
72 super(BoxLayout.Y_AXIS);
73 listEntry = new SelectionListEntry[initialSize];
74 this.setBackground(Color.blue);
75 for (int i = 0; i < listEntry.length; i++) {
76 listEntry[i] = new SelectionListEntry();
77 }
78 // this.setBorder(BorderFactory.createEtchedBorder());
79 addKeyListener(new KeyAdapter() {
80 public void keyTyped(KeyEvent e) {}
81
82 public void keyPressed(KeyEvent e) {
83 debugln("__Got key event");
84 if (e.getKeyCode() == KeyEvent.VK_DELETE){
85 debugln("___Got delete key");
86 java.util.List v = list.getSelectedObjects();
87 Iterator iter = v.iterator();
88 int i = 0;
89 while(iter.hasNext()){
90 debugln("____Removing object: "+i);
91 i++;
92 guiControl.removeFromScene((VcObject)iter.next());
93 }
94 }
95 }
96
97 public void keyReleased(KeyEvent e) {}
98 });
99 }
100
101 void setSelectionList(SelectionList list){
102 this.list = list;
103 list.addSelectionChangeListener(this);
104 }
105
106 void setGUIControl(GUIControl guiControl){
107 this.guiControl = guiControl;
108 guiControl.getJ3DBase().addSceneObjectsChangeListener(this);
109 }
110
111 private void add(VcObject vcObject){
112 debugln("ListEntry add called");
113 boolean done = false;
114 for (int i=0; i < listEntry.length; i++){
115 if (!done){
116 if (listEntry[i]!=null){
117 } else {
118 //listEntry[i] = new SelectionListEntry();
119 //listEntry[i].setAlignmentX(0.5f);
120 }
121 if (listEntry[i].isEmpty()){
122 listEntry[i].setVcObject(vcObject);
123 done = true;
124 //add(new Box.Filler(minFillerSize, prefFillerSize, maxFillerSize));
125 add(listEntry[i]);
126 listEntry[i].addActionListener(this);
127 debugln("Adding listEntry in pane");
128 }
129 }
130 }
131 this.invalidate();
132 this.getParent().validate();
133 }
134
135 public Dimension getPreferredSize(){
136 Component[] comp = this.getComponents();
137 int height = 0;
138 for (int i=0; i<comp.length; i++){
139 height = height + comp[i].getPreferredSize().height;
140 }
141
142 preferredHeight = height;
143 return new Dimension(preferredWidth, height);
144 }
145
146 void remove(Vector v){
147 Iterator iter = v.iterator();
148 while (iter.hasNext()){
149 remove((VcObject)iter.next());
150 }
151 }
152
153 private void remove(VcObject vcObject){
154 debugln("ListEntry remove called");
155 for (int i=0; i < listEntry.length; i++){
156 if (listEntry[i]!=null){
157 if (listEntry[i].getVcObject()==vcObject){
158 listEntry[i].clear();
159 remove(listEntry[i]);
160 }
161 debugln("removing listEntry from pane");
162 }
163 }
164 this.invalidate();
165 this.getParent().validate();
166 }
167
168
169
170 void debugln(String s){
171 if (GUIControl.debugflag){
172 System.out.println(s);
173 }
174 }
175
176 /***
177 * Runs a simple test of this class.
178 */
179 public static void main(String[] args) {
180 JButton removeBtn = new JButton("remove");
181 JButton addBtn = new JButton("add");
182 final SelectionListPane selectionListPane = new SelectionListPane();
183 JFrame frame = new JFrame();
184 JScrollPane scrollPane = new JScrollPane(selectionListPane);
185 scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
186 frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
187 frame.getContentPane().add(removeBtn, BorderLayout.SOUTH);
188 frame.getContentPane().add(addBtn, BorderLayout.NORTH);
189 frame.setSize(400, 400);
190 frame.setVisible(true);
191 VcView cam1 = new VcView();
192 cam1.setLabel("camera1");
193 VcView cam2 = new VcView();
194 cam2.setLabel("camera2");
195 selectionListPane.add(cam1);
196 selectionListPane.add(cam2);
197 selectionListPane.add(new VcView());
198 selectionListPane.add(new VcView());
199 selectionListPane.add(new VcView());
200 selectionListPane.add(new VcView());
201 frame.pack();
202 frame.show();
203 removeBtn.addActionListener(new java.awt.event.ActionListener(){
204 public void actionPerformed(ActionEvent e){
205 selectionListPane.removeBtn_actionPerformed(e);
206 }
207 });
208 addBtn.addActionListener(new java.awt.event.ActionListener(){
209 public void actionPerformed(ActionEvent e){
210 selectionListPane.removeElementFromPane();
211 }
212 });
213 }
214
215 public void removeElementFromPane(){
216 SelectionListEntry[] listEntry = this.listEntry;
217 boolean cont = true;
218 for (int i=0; ((i<listEntry.length)&&(cont)); i++){
219 if (listEntry[i]!=null){
220 if (!listEntry[i].isEmpty()){
221 this.remove(listEntry[i].getVcObject());
222 cont = false;
223 break;
224 }
225 }
226 }
227 }
228
229
230 public void actionPerformed(ActionEvent e){
231 long begin = System.currentTimeMillis();
232 Object source = e.getSource();
233 /*
234 if (list!=null){
235 list.flushSelection();
236 }*/
237 SelectionListEntry entry = (SelectionListEntry)source;
238
239 boolean selected = entry.isSelected();
240 debugln("__SelListPane: selected = "+selected);
241 if (selected){
242 debugln("__Adding "+entry.getVcObject().getLabel()+" to selected Objects");
243 list.addToSelectedObjects(entry.getVcObject());
244 }else {
245 debugln("__Removing "+entry.getVcObject().getLabel()+" from selected Objects");
246 list.removeFromSelectedObjects(entry.getVcObject());
247 }
248 /* no longer necessary as each entry is a listener to its own objects state
249 if (singleObjectSelection){
250 for (int i = 0; i < listEntry.length; i++) {
251 listEntry[i].setSelected(false);
252 }
253 entry.setSelected(selected);
254 }
255 */
256 long end = System.currentTimeMillis();
257 long elapsed = end - begin;
258 debugln("SelectionListPane time:"+elapsed);
259 }
260
261 private void removeBtn_actionPerformed(java.awt.event.ActionEvent e){
262 i++;
263 this.add(new VcView());
264 }
265
266 /***
267 * Method from SelectionChangeListener interface.
268 *
269 */
270 public void selectionChanged(SelectionChangedEvent event){
271 // there might be a better way to do this, avoiding
272 // iterating through the complete list of entries
273 for(int i=0;i<listEntry.length;i++) {
274 SelectionListEntry entry = listEntry[i];
275 if(entry!=null){
276 VcObject obj = entry.getVcObject();
277 if(obj!=null){
278 entry.setSelected(obj.isSelected());
279 }
280 }
281 }
282 }
283
284 /***
285 * Method from SceneObjectsChangeListener interface.
286 *
287 */
288 public void sceneObjectsChanged(SceneObjectsChangedEvent event){
289 if(event instanceof InitialListEvent){
290 Iterator iter = ((InitialListEvent)event).getInitialList().iterator();
291 while(iter.hasNext()){
292 add((VcObject)iter.next());
293 }
294 }
295 if(event instanceof ObjectAddedEvent){
296 add((VcObject)((ObjectAddedEvent)event).getObject());
297 }
298 if(event instanceof ObjectRemovedEvent){
299 remove((VcObject)((ObjectRemovedEvent)event).getObject());
300 }
301 }
302
303
304 }
This page was automatically generated by Maven