0% found this document useful (0 votes)
4 views2 pages

Service Grid

The ServiceGrid class is a JPanel that displays a list of ServiceItem objects in a JList, allowing for item selection and click handling. It supports custom rendering of items based on their properties, including price formatting and collaborator name resolution. The class provides methods to render a list of items, clear the list, and set a collaborator name resolver function.

Uploaded by

legginssammy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Service Grid

The ServiceGrid class is a JPanel that displays a list of ServiceItem objects in a JList, allowing for item selection and click handling. It supports custom rendering of items based on their properties, including price formatting and collaborator name resolution. The class provides methods to render a list of items, clear the list, and set a collaborator name resolver function.

Uploaded by

legginssammy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

package ui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.NumberFormat;
import java.util.List;
import java.util.Locale;
import java.util.function.Consumer;
import java.util.function.Function;

public class ServiceGrid extends JPanel {

private final DefaultListModel<ServiceItem> model = new DefaultListModel<>();


private final JList<ServiceItem> list = new JList<>(model);
private final Consumer<ServiceItem> onItemClick;

// Resolver opcional: dado un colaboradorId, devuelve el nombre a mostrar.


// Si es null o no resuelve, se muestra el ID tal cual.
private Function<String, String> colabNameResolver = null;

private final NumberFormat money = NumberFormat.getCurrencyInstance(new


Locale("es", "CO"));

/** Constructor habitual (compatible con el uso actual). */


public ServiceGrid(Consumer<ServiceItem> onItemClick) {
super(new BorderLayout());
this.onItemClick = onItemClick;

list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setFixedCellHeight(28);

list.setCellRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(
JList<?> l, Object value, int index, boolean isSelected,
boolean cellHasFocus) {
super.getListCellRendererComponent(l, value, index, isSelected,
cellHasFocus);

if (value instanceof ServiceItem it) {


String precioTxt;
try { precioTxt = money.format(it.getPrecio()); }
catch (Exception ex) { precioTxt = "$" + it.getPrecio(); }

StringBuilder sb = new StringBuilder();


sb.append(it.getNombre()).append(" — ").append(precioTxt);

if (it.isProducto()) {
sb.append(" · PROD");
if (!isSelected) setForeground(new Color(0, 60, 170)); //
azul oscuro para producto
} else if (it.isServicio()) {
// Mostrar el NOMBRE del colaborador (si hay), usando el
resolver
String colabId = it.getColaboradorId();
if (colabId != null && !colabId.isBlank()) {
String label = (colabNameResolver != null)
? safe(colabNameResolver.apply(colabId))
: null;
if (label == null || label.isBlank()) label = colabId;
sb.append(" · Colab: ").append(label);
} else {
sb.append(" · SERV");
}
if (!isSelected) setForeground(new Color(0, 128, 0)); //
verde oscuro para servicio
}

String cat = it.getCategoria();


if (cat != null && !cat.isBlank()) {
sb.append(" · Cat: ").append(cat);
}

setText(sb.toString());
setBorder(BorderFactory.createEmptyBorder(2, 6, 2, 6));
}
return this;
}
});

list.addMouseListener(new MouseAdapter() {
@Override public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
ServiceItem it = list.getSelectedValue();
if (it != null && onItemClick != null) onItemClick.accept(it);
}
}
});

add(new JScrollPane(list), BorderLayout.CENTER);


}

/** Permite inyectar el resolvedor (ID -> Nombre del colaborador). */


public void setColabNameResolver(Function<String, String> resolver) {
this.colabNameResolver = resolver;
// refrescar render de la lista
list.repaint();
}

public void render(List<ServiceItem> items) {


model.clear();
if (items == null) return;
for (ServiceItem it : items) model.addElement(it);
if (!model.isEmpty()) list.setSelectedIndex(0);
}

public void clear() {


model.clear();
}

private static String safe(String s) { return s == null ? "" : s; }


}

You might also like