14
14
15
15
import com .db4o .Db4oEmbedded ;
16
16
import com .db4o .ObjectContainer ;
17
+ import com .db4o .ObjectSet ;
17
18
18
19
import java .awt .event .WindowAdapter ;
19
20
import java .awt .event .WindowEvent ;
22
23
23
24
import java .awt .BorderLayout ;
24
25
26
+ import javax .swing .DefaultComboBoxModel ;
25
27
import javax .swing .JLabel ;
26
28
import javax .swing .JMenuBar ;
27
29
import javax .swing .JToolBar ;
35
37
import java .awt .event .ActionListener ;
36
38
import java .awt .event .ActionEvent ;
37
39
40
+ import javax .swing .JComboBox ;
41
+ import javax .swing .event .ChangeListener ;
42
+ import javax .swing .event .ChangeEvent ;
43
+
44
+ import java .awt .Dimension ;
45
+ import java .awt .event .MouseAdapter ;
46
+ import java .awt .event .MouseEvent ;
47
+
38
48
/**
39
49
* Ejemplo que pruebas las principales características de db4o
40
50
* @author Santiago Faci
@@ -61,6 +71,9 @@ public class HolaDb4o {
61
71
private JScrollPane scrollPane_1 ;
62
72
private JTablaTiendas tablaTiendas ;
63
73
private JTable tablaCentros ;
74
+ private JButton tfBuscar ;
75
+ private JTextField tfFiltro ;
76
+ private JComboBox <String > cbCampos ;
64
77
65
78
/**
66
79
* Launch the application.
@@ -82,6 +95,7 @@ public void run() {
82
95
* Create the application.
83
96
*/
84
97
public HolaDb4o () {
98
+
85
99
initialize ();
86
100
inicializar ();
87
101
}
@@ -143,7 +157,66 @@ private void modificar() {
143
157
* TODO Elimina Tiendas o Centros Comercial
144
158
*/
145
159
private void eliminar () {
160
+
161
+ switch (tab .getSelectedIndex ()) {
162
+ case TIENDA :
163
+ int filaSeleccionada = 0 ;
164
+
165
+ filaSeleccionada = tablaTiendas .getSelectedRow ();
166
+ if (filaSeleccionada == -1 )
167
+ return ;
168
+
169
+ String nombre = (String ) tablaTiendas .getValueAt (filaSeleccionada , 0 );
170
+ Tienda tienda = new Tienda ();
171
+ tienda .setNombre (nombre );
172
+ // Se asume que no existen dos tiendas con el mismo nombre.
173
+ // Así se puede contar con que la consulta sólo devuelve un resultado
174
+ ObjectSet <Tienda > resultado = Util .db .queryByExample (tienda );
175
+ tienda = resultado .next ();
176
+ Util .db .delete (tienda );
177
+
178
+ tablaTiendas .listar ();
179
+ case CENTRO_COMERCIAL :
180
+ // TODO
181
+ default :
182
+ }
183
+ }
184
+
185
+ /**
186
+ * TODO Busca en Tiendas o Centros Comerciales
187
+ */
188
+ private void buscar () {
189
+
190
+ int campo = cbCampos .getSelectedIndex ();
191
+ tablaTiendas .listar (tfFiltro .getText (), campo );
192
+ }
193
+
194
+ /**
195
+ * TODO Cambia de pestaña. Hay que recargar el combo de campos
196
+ */
197
+ private void cambiarPestana () {
146
198
199
+ switch (tab .getSelectedIndex ()) {
200
+ case TIENDA :
201
+ cbCampos .removeAllItems ();
202
+ cbCampos .addItem ("<Todos>" );
203
+ cbCampos .addItem (Constantes .NOMBRE );
204
+ cbCampos .addItem (Constantes .DESCRIPCION );
205
+ cbCampos .addItem (Constantes .NUMERO_LOCAL );
206
+ cbCampos .addItem (Constantes .FECHA_APERTURA );
207
+ break ;
208
+ case CENTRO_COMERCIAL :
209
+ cbCampos .removeAllItems ();
210
+ // TODO
211
+ break ;
212
+ default :
213
+ }
214
+ }
215
+
216
+ /**
217
+ * TODO Selecciona alguna tienda de la tabla
218
+ */
219
+ private void seleccionarTiendas () {
147
220
}
148
221
149
222
/**
@@ -189,12 +262,20 @@ public JToolBar getToolBar() {
189
262
toolBar .add (getBtAlta ());
190
263
toolBar .add (getBtModificar ());
191
264
toolBar .add (getBtEliminar ());
265
+ toolBar .add (getTfFiltro ());
266
+ toolBar .add (getCbCampos ());
267
+ toolBar .add (getTfBuscar ());
192
268
}
193
269
return toolBar ;
194
270
}
195
271
public JTabbedPane getTab () {
196
272
if (tab == null ) {
197
273
tab = new JTabbedPane (JTabbedPane .TOP );
274
+ tab .addChangeListener (new ChangeListener () {
275
+ public void stateChanged (ChangeEvent arg0 ) {
276
+ cambiarPestana ();
277
+ }
278
+ });
198
279
tab .addTab ("Tiendas" , null , getPanelTiendas (), null );
199
280
tab .addTab ("Centros Comerciales" , null , getPanelCentros (), null );
200
281
}
@@ -284,6 +365,12 @@ public JScrollPane getScrollPane_1() {
284
365
public JTable getTablaTiendas () {
285
366
if (tablaTiendas == null ) {
286
367
tablaTiendas = new JTablaTiendas ();
368
+ tablaTiendas .addMouseListener (new MouseAdapter () {
369
+ @ Override
370
+ public void mouseClicked (MouseEvent e ) {
371
+ seleccionarTiendas ();
372
+ }
373
+ });
287
374
}
288
375
return tablaTiendas ;
289
376
}
@@ -293,4 +380,29 @@ public JTable getTablaCentros() {
293
380
}
294
381
return tablaCentros ;
295
382
}
383
+ public JButton getTfBuscar () {
384
+ if (tfBuscar == null ) {
385
+ tfBuscar = new JButton ("Buscar" );
386
+ tfBuscar .addActionListener (new ActionListener () {
387
+ public void actionPerformed (ActionEvent e ) {
388
+ buscar ();
389
+ }
390
+ });
391
+ }
392
+ return tfBuscar ;
393
+ }
394
+ public JTextField getTfFiltro () {
395
+ if (tfFiltro == null ) {
396
+ tfFiltro = new JTextField ();
397
+ tfFiltro .setColumns (10 );
398
+ }
399
+ return tfFiltro ;
400
+ }
401
+ public JComboBox <String > getCbCampos () {
402
+ if (cbCampos == null ) {
403
+ cbCampos = new JComboBox <String >();
404
+ cbCampos .setPreferredSize (new Dimension (90 , 20 ));
405
+ }
406
+ return cbCampos ;
407
+ }
296
408
}
0 commit comments