Skip to content
This repository has been archived by the owner on Aug 9, 2024. It is now read-only.

Commit

Permalink
stage changes - App + Visual Output
Browse files Browse the repository at this point in the history
  • Loading branch information
Laifsyn committed Jun 15, 2024
1 parent c97f84e commit c7887c0
Show file tree
Hide file tree
Showing 2 changed files with 280 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.utp.clsEstructuraDatos.Estructuras.colas;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JLabel;
import javax.swing.JPanel;

public abstract class AbstractCola<T> {
/**
* Indice donde se puede extraer el siguiente elemento
Expand Down Expand Up @@ -28,6 +35,10 @@ public AbstractCola(int capacidad) {
this.inner = (T[]) new Object[capacidad];
}

public int capacity() {
return this.capacidad;
}

/**
* Evalua si la cola está vacía al comparar la longitud con 0
*/
Expand Down Expand Up @@ -82,6 +93,119 @@ public String toString() {
return sb.toString();
}

public JPanel as_Jpanel() {
return new PanelDrawer(this).as_panel();
}

private class PanelDrawer {
final JPanel panel = new JPanel(new GridBagLayout());
final AbstractCola<T> cola;
final JLabel front_surplus = new JLabel();
final JLabel frente_item = new JLabel();
final JLabel middle = new JLabel();
final JLabel cola_item = new JLabel();
final JLabel cola_surplus = new JLabel();

/**
* ?["{frente-1}"]
* ["{this[frente]}"]
* [..."{longitud}"...]
* ["...{this[cola-1]}]
* ?[cola-1 - capacidad]
*/
PanelDrawer(AbstractCola<T> cola) {
this.cola = cola;
var c = new GridBagConstraints(0, 0, 1, 5, 0, 0, GridBagConstraints.CENTER, 0, new Insets(5, 5, 5, 5), 0,
0);
panel.add(front_surplus, c);
c.gridy = 1;
panel.add(frente_item, c);
c.gridy = 2;
panel.add(middle, c);
c.gridy = 3;
panel.add(cola_item, c);
c.gridy = 4;
panel.add(cola_surplus, c);
}

JPanel as_panel() {
int frente = this.cola.frente;
int cola = this.cola.cola;
int capacidad = this.cola.capacity();
int longitud = this.cola.longitud;

// ******************************************

if (frente > cola)
wrapped(frente, cola, capacidad, longitud);
else
aligned(frente, cola, capacidad, longitud);

// ******************************************
return this.panel;
}

/**
* llamado cuando frente (donde se retira de la fila) es menor o igual que el
* indice de la cola
*/
void aligned(int frente, int cola, int capacidad, int longitud) {

if (frente > 0)
this.front_surplus.setText("[" + (frente - 1) + "]");
else
this.front_surplus.setText("");

// ******************************************

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("[" + (cola + 1 - capacidad) + "]");
else
this.cola_surplus.setText("");
}

/**
* llamado cuando frente (donde se retira de la fila) es mayor que el indice de
* la cola
*/
void wrapped(int frente, int cola, int capacidad, int longitud) {

this.front_surplus.setText("(" + frente + ")");

// ******************************************

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] + "]");

// ******************************************

this.cola_surplus.setText("[" + (cola + 1 - capacidad) + "]");
}

}

// @formatter:off
abstract public Result insertar(T elemento);
abstract public Option<T> quitar();
Expand Down
156 changes: 156 additions & 0 deletions src/main/java/com/utp/clsEstructuraDatos/laboratorio_3/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package com.utp.clsEstructuraDatos.laboratorio_3;

import javax.management.RuntimeErrorException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.utp.clsEstructuraDatos.Estructuras.colas.AbstractCola;
import com.utp.clsEstructuraDatos.Estructuras.colas.ColaCircular;
import com.utp.clsEstructuraDatos.Estructuras.colas.ColaSimple;

import static com.utp.clsEstructuraDatos.laboratorio_3.ColasApp.CMD.*;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

public class Main {
public static void main(String[] args) {
ColasApp app = new ColasApp();
app.run_app();
}
}

class ColasApp {
static public String LABORATORIO = "Laboratorio 3";
final JButton btn_crear_cola = new JButton("Crear Cola");
final JButton btn_insertar = new JButton("Insertar");
final JButton btn_quitar = new JButton("Quitar");
final JButton btn_limpiar = new JButton("Limpiar");
final JButton btn_mostrar = new JButton("Mostrar");
final JFrame frame = new JFrame(LABORATORIO);
AbstractCola<Integer> cola;

static int COLA_CIRCULAR = 0;
static int COLA_SIMPLE = 1;

ColasApp() {}

public void run_app() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setLayout(null);
frame.add(content());
this.send_command(null);
}

JPanel content() {
JPanel content = new JPanel();
content.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints(0, 0, 2, 3, 0, 0, GridBagConstraints.CENTER, 0,
new Insets(5, 5, 5, 5), 0, 0);

c.gridwidth = 2;
content.add(this.btn_crear_cola, c);

c.gridy = 1;
c.gridwidth = 1;
content.add(this.btn_insertar, c);
c.gridx = 1;
content.add(this.btn_quitar, c);

c.gridy = 2;
c.gridx = 0;
content.add(this.btn_limpiar, c);
c.gridx = 1;
content.add(this.btn_mostrar, c);

c.gridy = 3;
c.gridx = 0;


return content;
}

// Packs the frame to the preferred size, updating if undersized
public void try_pack() {
Dimension current_size = frame.getSize();
Dimension preferred_size = frame.getPreferredSize();
int new_width = Math.max(current_size.width, preferred_size.width);
int new_height = Math.max(current_size.height, preferred_size.height);
if (new_width != current_size.width || new_height != current_size.height)
frame.setSize(new_width, new_height);
}

public int prompt_colas_type() {
int num = JOptionPane.showOptionDialog(frame, "Seleccione el tipo de cola a utilizar",
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;
}

void switch_buttons(boolean value) {
this.btn_insertar.setEnabled(value);
this.btn_quitar.setEnabled(value);
this.btn_limpiar.setEnabled(value);
this.btn_mostrar.setEnabled(value);
}

// Event Manager
public void send_command(CMD command) {
switch (command) {
case Pack() -> {
this.try_pack();
}
case CrearCola(int capacidad_cola) -> {
int cola = prompt_colas_type();
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);
}
case Insertar<Integer>(Integer elemento) -> {
this.cola.insertar(elemento);
}
case Quitar() -> {
this.cola.quitar();
}
case Limpiar() -> {
this.cola.limpiar();
}
case Mostrar() -> {
throw new RuntimeErrorException(null, "Not Implemented");
}
case null -> {
}
default -> {
throw new RuntimeErrorException(null, "Unsopported Command " + command.getClass().getName());
}
}
if (this.cola == null) {
this.btn_crear_cola.setText("Crear Cola");
switch_buttons(false);
}
}

public static sealed interface CMD {
// @formatter:off
public static record Pack() implements CMD {}
public static record CrearCola(int capacidad_cola) implements CMD {}
public static record Insertar<T>(T elemento) implements CMD {}
public static record Quitar() implements CMD {}
public static record Limpiar() implements CMD {}
public static record Mostrar() implements CMD {}
// @formatter:on
}
}

0 comments on commit c7887c0

Please sign in to comment.