diff --git a/src/main/java/com/utp/clsEstructuraDatos/Estructuras/colas/AbstractCola.java b/src/main/java/com/utp/clsEstructuraDatos/Estructuras/colas/AbstractCola.java index f94696b..a809f2d 100644 --- a/src/main/java/com/utp/clsEstructuraDatos/Estructuras/colas/AbstractCola.java +++ b/src/main/java/com/utp/clsEstructuraDatos/Estructuras/colas/AbstractCola.java @@ -1,5 +1,6 @@ package com.utp.clsEstructuraDatos.Estructuras.colas; +import java.awt.Color; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; @@ -73,6 +74,11 @@ public void reset() { this.longitud = 0; } + /** Cantidad de elementos accesibles en la cola */ + public int len() { + return this.longitud; + } + /** * Devuelve una representación opinionada de la cola en forma de cadena */ @@ -134,8 +140,12 @@ public int capacity() { public JPanel as_panel() { JPanel panel = new JPanel(new GridBagLayout()); - var c = new GridBagConstraints(0, 0, 1, 5, 0, 0, GridBagConstraints.CENTER, 0, new Insets(5, 5, 5, 5), 0, - 0); + var c = new GridBagConstraints(); + c.insets = new Insets(2, 5, 5, 5); + c.fill = GridBagConstraints.HORIZONTAL; + c.gridx = 0; + c.gridy = 0; + panel.add(front_surplus, c); c.gridy = 1; panel.add(frente_item, c); @@ -166,34 +176,26 @@ public JPanel as_panel() { */ public void aligned() { int frente = this.frente(); - int cola = this.cola(); - int capacidad = this.capacity(); - int longitud = this.longitud(); - - if (frente > 0) - this.front_surplus.setText("[" + (frente - 1) + "]"); - else + if (frente > 0) { + this.front_surplus.setText("[+" + (frente) + "...]"); + this.front_surplus.setForeground(Color.BLACK); + this.front_surplus.setToolTipText("Espacio libre antes del frente"); + } else { this.front_surplus.setText(""); + this.front_surplus.setToolTipText(null); + } + update_content(); - // ****************************************** - - this.frente_item.setText("[" + this.cola.inner[frente] + "]"); - // ****************************************** - - if (longitud > 0) - this.middle.setText("[..." + longitud + "...]"); - else - this.middle.setText("[0]"); - - // ****************************************** - this.cola_item.setText("[" + this.cola.inner[cola] + "]"); - - // ****************************************** - - if (cola < capacidad - 1) - this.cola_surplus.setText("[+" + (capacidad - (cola + 1)) + "...]"); - else - this.cola_surplus.setText(""); + int capacidad = this.capacity(); + if (this.longitud() < capacidad) { + this.cola_surplus.setText("[...+" + (capacidad - this.cola()) + "]"); + this.cola_surplus.setForeground(Color.BLACK); + this.cola_surplus.setToolTipText("Espacio libre para inserción"); + } else { + this.cola_surplus.setText("[Full]"); + this.cola_surplus.setForeground(Color.RED); + this.cola_surplus.setToolTipText(null); + } } /** @@ -201,31 +203,63 @@ public void aligned() { * la cola */ public void wrapped() { - int frente = this.frente(); - int cola = this.cola(); - int capacidad = this.capacity(); - int longitud = this.longitud(); + this.front_surplus.setText("Frente idx: (" + frente + ")"); + this.front_surplus.setToolTipText("Indice de frente"); + update_content(); + + if (this.longitud() < this.capacity()) { + this.cola_surplus.setText("[...+" + (frente - this.cola()) + "]"); + this.cola_surplus.setForeground(Color.BLACK); + this.cola_surplus.setToolTipText("Espacio libre para inserción"); + } else { + this.cola_surplus.setText("[Full]"); + this.cola_surplus.setForeground(Color.RED); + this.cola_surplus.setToolTipText(null); + } + } - this.front_surplus.setText("(" + frente + ")"); + void update_content() { // ****************************************** - this.frente_item.setText("[" + this.cola.inner[frente] + "]"); - + if (longitud > 0) { + this.frente_item.setText("Frente: [" + this.cola.inner[frente] + "]"); + this.frente_item.setForeground(Color.BLACK); + this.frente_item.setToolTipText("Elemento en la posición de frente"); + } else { + this.frente_item.setText("Frente: [_EMPTY_]"); + this.frente_item.setForeground(Color.RED); + this.frente_item.setToolTipText("La cola está vacía"); + } // ****************************************** - if (longitud > 0) - this.middle.setText("[..." + longitud + "...]"); - else - this.middle.setText("[0]"); + if (longitud > 0) { + this.middle.setText("Longitud: [..." + longitud + "...]"); + this.middle.setForeground(Color.BLACK); + this.middle.setToolTipText("Cantidad de elementos en la cola"); + } else { + this.middle.setText("Longitud: [0]"); + this.middle.setForeground(Color.RED); + this.middle.setToolTipText("No hay elementos en la cola"); + } // ****************************************** - this.cola_item.setText("[" + this.cola.inner[cola] + "]"); + int cola_index_item = (this.cola() - 1) % capacidad; + if (cola_index_item < 0) { + cola_index_item += capacidad; + } + if (longitud > 0) { + this.cola_item.setText("Cola: [" + this.cola.inner[cola_index_item] + "]"); + this.cola_item.setForeground(Color.BLACK); + this.cola_item.setToolTipText("Elemento en la posición de cola"); + } else { + this.cola_item.setText("Cola: [_EMPTY_]"); + this.cola_item.setForeground(Color.RED); + this.cola_item.setToolTipText("La cola está vacía"); + } // ****************************************** - - this.cola_surplus.setText("[" + (capacidad - (cola + 1)) + "]"); } } diff --git a/src/main/java/com/utp/clsEstructuraDatos/Estructuras/colas/ColaSimple.java b/src/main/java/com/utp/clsEstructuraDatos/Estructuras/colas/ColaSimple.java index 81ec928..63f0314 100644 --- a/src/main/java/com/utp/clsEstructuraDatos/Estructuras/colas/ColaSimple.java +++ b/src/main/java/com/utp/clsEstructuraDatos/Estructuras/colas/ColaSimple.java @@ -46,6 +46,7 @@ public Option quitar() { return new Option.None<>(); T elemento = this.inner[frente]; + this.inner[frente] = null; frente = (frente + 1) % capacidad; if (frente == cola) this.reset(); diff --git a/src/main/java/com/utp/clsEstructuraDatos/laboratorio_3/Main.java b/src/main/java/com/utp/clsEstructuraDatos/laboratorio_3/Main.java index db300df..c683b38 100644 --- a/src/main/java/com/utp/clsEstructuraDatos/laboratorio_3/Main.java +++ b/src/main/java/com/utp/clsEstructuraDatos/laboratorio_3/Main.java @@ -3,7 +3,6 @@ import javax.management.RuntimeErrorException; import javax.swing.JButton; import javax.swing.JFrame; -import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; @@ -31,7 +30,7 @@ class ColasApp { final JButton btn_quitar = new JButton("Quitar"); final JButton btn_limpiar = new JButton("Limpiar"); final JButton btn_mostrar = new JButton("Mostrar"); - final JPanel panel_cola = new JPanel(); + final JPanel display_area = new JPanel(); final JFrame frame = new JFrame(LABORATORIO); AbstractCola.PanelDrawer drawer; AbstractCola cola; @@ -45,26 +44,26 @@ public void run_app() { frame.add(content()); frame.setVisible(true); frame.pack(); + var pref_size = frame.getPreferredSize(); + int pref_width = Math.max(pref_size.width, 275); + frame.setSize(pref_width, pref_size.height); this.send_command(null); } ColasApp() { this.btn_crear_cola.addActionListener(e -> { - // TODO: Descomentar - - // String input = JOptionPane.showInputDialog(frame, "Ingrese la capacidad de la - // cola", "Crear Cola", - // JOptionPane.QUESTION_MESSAGE); - // try { - // Integer.parseInt(input); - // } catch (NumberFormatException e1) { - // JOptionPane.showMessageDialog(frame, "Debe ingresar un número entero", - // "Error", - // JOptionPane.ERROR_MESSAGE); - // return; - // } - // int capacidad_cola = Integer.parseInt(input); - var capacidad_cola = 10; + + String input = JOptionPane.showInputDialog(frame, "Ingrese la capacidad de la cola", "Crear Cola", + JOptionPane.QUESTION_MESSAGE); + try { + Integer.parseInt(input); + } catch (NumberFormatException e1) { + JOptionPane.showMessageDialog(frame, "Debe ingresar un número entero", + "Error", + JOptionPane.ERROR_MESSAGE); + return; + } + int capacidad_cola = Integer.parseInt(input); this.send_command(new CrearCola(capacidad_cola)); }); this.btn_insertar.addActionListener(e -> { @@ -102,7 +101,7 @@ JPanel content() { content.add(buttons_panel(), c); c.gridy = 1; - content.add(panel_cola, c); + content.add(display_area, c); return content; } @@ -149,7 +148,6 @@ public int prompt_colas_type() { LABORATORIO + ": Tipo de Cola", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] { "Cola Circular", "Cola Simple" }, "Cola Circular"); - System.out.println("Seleccion del tipo: " + num); return num; } @@ -167,25 +165,34 @@ case Pack() -> { this.try_pack(); } case Redraw() -> { - + if (this.cola == null) + return; + else if (this.drawer.cola() < this.drawer.frente() || (this.drawer.longitud() == this.drawer.capacity() + && this.drawer.cola() <= this.drawer.frente())) + this.drawer.wrapped(); + else + this.drawer.aligned(); this.try_pack(); } case CrearCola(int capacidad_cola) -> { - // TODO: Descomentar - // int cola = prompt_colas_type(); - // if (cola == COLA_CIRCULAR) { - // this.cola = new ColaCircular<>(capacidad_cola); - // } else { - // this.cola = new ColaSimple<>(capacidad_cola); - // } - this.cola = new ColaCircular<>(10); + int cola = prompt_colas_type(); + if (this.cola != null) { + this.cola.limpiar(); + this.drawer.aligned(); + } + if (cola == COLA_CIRCULAR) { + this.cola = new ColaCircular<>(capacidad_cola); + } else { + this.cola = new ColaSimple<>(capacidad_cola); + } String new_text = String.format("[%s (c:%d)]Recrear Cola", this.cola.getClass().getSimpleName(), this.cola.capacity()); this.btn_crear_cola.setText(new_text); switch_buttons(true); this.drawer = this.cola.as_drawer(); - this.panel_cola.removeAll(); - this.panel_cola.add(drawer.as_panel()); + this.display_area.removeAll(); + this.display_area.add(drawer.as_panel()); + drawer.aligned(); this.send_command(new Redraw()); this.try_pack(); } @@ -213,11 +220,34 @@ case Mostrar() -> { if (this.cola == null) { this.btn_crear_cola.setText("Crear Cola"); switch_buttons(false); + } else { // Only operations that can be done on a non-null queue + if (this.cola.len() <= 0) { + this.btn_quitar.setEnabled(false); + this.btn_quitar.setToolTipText("No hay elementos para quitar"); + this.btn_limpiar.setEnabled(false); + this.btn_limpiar.setToolTipText("No hay elementos para limpiar"); + } else { + this.btn_quitar.setEnabled(true); + this.btn_quitar.setToolTipText(null); + this.btn_limpiar.setEnabled(true); + this.btn_limpiar.setToolTipText(null); + } + + if (this.cola.len() == this.cola.capacity()) { + this.btn_insertar.setEnabled(false); + this.btn_insertar.setToolTipText("La cola está llena"); + } else { + this.btn_insertar.setEnabled(true); + this.btn_insertar.setToolTipText(null); + } } } public static sealed interface CMD { // @formatter:off + /** + * Agranda la ventana al tamaño preferido, actualizando si está subdimensionada. + */ public static record Pack() implements CMD {} public static record CrearCola(int capacidad_cola) implements CMD {} public static record Insertar(T elemento) implements CMD {}