Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c12c255

Browse files
committed
Nuevas funcionalidades
1 parent 15be56a commit c12c255

File tree

5 files changed

+178
-1
lines changed

5 files changed

+178
-1
lines changed

‎HolaDb4o/grancasas.db4o

233 Bytes
Binary file not shown.

‎HolaDb4o/src/org/sfaci/holadb4o/gui/HolaDb4o.java

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import com.db4o.Db4oEmbedded;
1616
import com.db4o.ObjectContainer;
17+
import com.db4o.ObjectSet;
1718

1819
import java.awt.event.WindowAdapter;
1920
import java.awt.event.WindowEvent;
@@ -22,6 +23,7 @@
2223

2324
import java.awt.BorderLayout;
2425

26+
import javax.swing.DefaultComboBoxModel;
2527
import javax.swing.JLabel;
2628
import javax.swing.JMenuBar;
2729
import javax.swing.JToolBar;
@@ -35,6 +37,14 @@
3537
import java.awt.event.ActionListener;
3638
import java.awt.event.ActionEvent;
3739

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+
3848
/**
3949
* Ejemplo que pruebas las principales características de db4o
4050
* @author Santiago Faci
@@ -61,6 +71,9 @@ public class HolaDb4o {
6171
private JScrollPane scrollPane_1;
6272
private JTablaTiendas tablaTiendas;
6373
private JTable tablaCentros;
74+
private JButton tfBuscar;
75+
private JTextField tfFiltro;
76+
private JComboBox<String> cbCampos;
6477

6578
/**
6679
* Launch the application.
@@ -82,6 +95,7 @@ public void run() {
8295
* Create the application.
8396
*/
8497
public HolaDb4o() {
98+
8599
initialize();
86100
inicializar();
87101
}
@@ -143,7 +157,66 @@ private void modificar() {
143157
* TODO Elimina Tiendas o Centros Comercial
144158
*/
145159
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() {
146198

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() {
147220
}
148221

149222
/**
@@ -189,12 +262,20 @@ public JToolBar getToolBar() {
189262
toolBar.add(getBtAlta());
190263
toolBar.add(getBtModificar());
191264
toolBar.add(getBtEliminar());
265+
toolBar.add(getTfFiltro());
266+
toolBar.add(getCbCampos());
267+
toolBar.add(getTfBuscar());
192268
}
193269
return toolBar;
194270
}
195271
public JTabbedPane getTab() {
196272
if (tab == null) {
197273
tab = new JTabbedPane(JTabbedPane.TOP);
274+
tab.addChangeListener(new ChangeListener() {
275+
public void stateChanged(ChangeEvent arg0) {
276+
cambiarPestana();
277+
}
278+
});
198279
tab.addTab("Tiendas", null, getPanelTiendas(), null);
199280
tab.addTab("Centros Comerciales", null, getPanelCentros(), null);
200281
}
@@ -284,6 +365,12 @@ public JScrollPane getScrollPane_1() {
284365
public JTable getTablaTiendas() {
285366
if (tablaTiendas == null) {
286367
tablaTiendas = new JTablaTiendas();
368+
tablaTiendas.addMouseListener(new MouseAdapter() {
369+
@Override
370+
public void mouseClicked(MouseEvent e) {
371+
seleccionarTiendas();
372+
}
373+
});
287374
}
288375
return tablaTiendas;
289376
}
@@ -293,4 +380,29 @@ public JTable getTablaCentros() {
293380
}
294381
return tablaCentros;
295382
}
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+
}
296408
}

‎HolaDb4o/src/org/sfaci/holadb4o/gui/JTablaTiendas.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.sfaci.holadb4o.gui;
22

3+
import java.text.DateFormat;
4+
import java.text.ParseException;
5+
import java.text.SimpleDateFormat;
36
import java.util.List;
47

58
import javax.swing.JTable;
@@ -9,6 +12,9 @@
912
import org.sfaci.holadb4o.util.Constantes;
1013
import org.sfaci.holadb4o.util.Util;
1114

15+
import com.db4o.query.Predicate;
16+
import com.db4o.query.Query;
17+
1218
/**
1319
* Tabla que lista datos de Tiendas
1420
* @author Santiago Faci
@@ -50,9 +56,62 @@ public boolean isCellEditable(int fila, int columna) {
5056
*/
5157
public void listar() {
5258

59+
// Lista todos los objetos del tipo que se pasa como parámetro
5360
List<Tienda> tiendas = Util.db.query(Tienda.class);
5461
cargarFilas(tiendas);
5562
}
63+
64+
public void listar(final String filtro, int campo) {
65+
66+
Tienda tienda = null;
67+
List<Tienda> tiendas = null;
68+
69+
switch (campo) {
70+
case Constantes.C_TODOS:
71+
tiendas = Util.db.query(new Predicate<Tienda>() {
72+
@Override
73+
public boolean match(Tienda tienda) {
74+
75+
if (tienda.getNombre().contains(filtro))
76+
return true;
77+
if (tienda.getDescripcion().contains(filtro))
78+
return true;
79+
if (String.valueOf(tienda.getNumeroLocal()).contains(filtro))
80+
return true;
81+
82+
return false;
83+
}
84+
});
85+
break;
86+
case Constantes.C_NOMBRE:
87+
tienda = new Tienda();
88+
tienda.setNombre(filtro);
89+
tiendas = Util.db.queryByExample(tienda);
90+
break;
91+
case Constantes.C_DESCRIPCION:
92+
tienda = new Tienda();
93+
tienda.setDescripcion(filtro);
94+
tiendas = Util.db.queryByExample(tienda);
95+
break;
96+
case Constantes.C_NUMERO_LOCAL:
97+
tienda = new Tienda();
98+
tienda.setNumeroLocal(Integer.parseInt(filtro));
99+
tiendas = Util.db.queryByExample(tienda);
100+
break;
101+
case Constantes.C_FECHA_APERTURA:
102+
try {
103+
tienda = new Tienda();
104+
tienda.setFechaApertura(new SimpleDateFormat().parse(filtro));
105+
tiendas = Util.db.queryByExample(tienda);
106+
} catch (ParseException pe) {
107+
pe.printStackTrace();
108+
}
109+
break;
110+
default:
111+
}
112+
113+
cargarFilas(tiendas);
114+
}
56115

57116
/*
58117
* 'Pinta' los datos en el JTable

‎HolaDb4o/src/org/sfaci/holadb4o/gui/JTienda.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public void actionPerformed(ActionEvent e) {
119119
getRootPane().setDefaultButton(okButton);
120120
}
121121
{
122-
JButton cancelButton = new JButton("Cancel");
122+
JButton cancelButton = new JButton("Cancelar");
123123
cancelButton.addActionListener(new ActionListener() {
124124
public void actionPerformed(ActionEvent e) {
125125
cancelar();

‎HolaDb4o/src/org/sfaci/holadb4o/util/Constantes.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,10 @@ public class Constantes {
1717
public static final String DESCRIPCION = "Descripción";
1818
public static final String NUMERO_LOCAL = "Núm. Local";
1919
public static final String FECHA_APERTURA = "Fecha Apertura";
20+
21+
public static final int C_TODOS = 0;
22+
public static final int C_NOMBRE = 1;
23+
public static final int C_DESCRIPCION = 2;
24+
public static final int C_NUMERO_LOCAL = 3;
25+
public static final int C_FECHA_APERTURA = 4;
2026
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /