Você está na página 1de 6

UNIVERSIDAD DE LAS FUERZAS

ARMADAS ESPE-L

ASIGNATURA:
PATRONES DE DISEÑO

DOCENTE:

ING. RUBÉN LÓPEZ

NOMBRE:

ISRAEL TENEDA
TAREA:

PATRÓN DE DISEÑO SINGLETON


Diagrama

Capturas de Pantalla de la implementación

Nos permite crear solo una instacia de un JPanel mediante el patrón Singleton
Código del Patrón

Clase VPnael

package singleton;

import java.awt.Dimension;
import java.beans.PropertyVetoException;
import java.util.LinkedHashMap;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
public abstract class VPanel extends JPanel {

public static JDesktopPane desktopPane;

public static void installDesktopPane(JDesktopPane desktopPane) {


VPanel.desktopPane = desktopPane;
}

public VPanel(String name) {


this.name = name;
if(desktopPane == null)
throw new IllegalStateException("VPanel is being used with a null
desktop pane.");
}
static LinkedHashMap<Class, VPanel> self_panel_map;

JInternalFrame self_jif;
protected VPanel self_panel;
boolean loading;
boolean showing;

public final String name;


public abstract void init();

public static VPanel showPanel(VPanel newInstance) {


if(self_panel_map == null)
self_panel_map = new LinkedHashMap<>();
Class newInstanceClass = newInstance.getClass();
if(self_panel_map.containsKey(newInstanceClass)) {
VPanel oldInstance = self_panel_map.get(newInstanceClass);
oldInstance.showing = oldInstance.self_jif.isVisible();
if(!oldInstance.loading && !oldInstance.showing) {
newInstance.loading = true;
newInstance.self_panel = newInstance;
newInstance.self_jif = new JInternalFrame(newInstance.name, true,
true, true, true);
newInstance.self_panel.init();
self_panel_map.put(newInstanceClass, newInstance);
return newInstance;
} else if(oldInstance.showing) {
try {
oldInstance.self_jif.setSelected(true);
} catch (PropertyVetoException e) {
handleError(e);
}
}
return oldInstance;
} else {
newInstance.loading = true;
newInstance.self_panel = newInstance;
newInstance.self_jif = new JInternalFrame(newInstance.name, true,
true, true, true);
newInstance.self_panel.init();
self_panel_map.put(newInstanceClass, newInstance);
return newInstance;
}
}

public void setVisible() {

self_jif.add(self_panel);
self_jif.pack();
self_jif.setVisible(true);
desktopPane.add(self_jif);
centerJIF();
try {
self_jif.setSelected(true);
} catch (PropertyVetoException e) {
handleError(e);
}
loading = false;
}

private static void handleError(Exception e) {


e.printStackTrace();
}

public void centerJIF() {


Dimension desktopSize = desktopPane.getSize();
Dimension JInternalFrameSize = self_jif.getSize();
int width = (desktopSize.width - JInternalFrameSize.width) / 2;
int height = (desktopSize.height - JInternalFrameSize.height) / 2;
self_jif.setLocation(width, height);
}

/* private Class getClass() {


throw new UnsupportedOperationException("Not supported yet."); //To
change body of generated methods, choose Tools | Templates.
}*/
}
Clase VPanel_Test implementación de Vpanel

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package singleton;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextField;

/**
*
* @author israt
*/
public class Test_Vpanel {
public static void main(String[] args) {
JFrame jf = new JFrame("MainFrame");
JDesktopPane jdp = new JDesktopPane();
jf.setExtendedState( jf.getExtendedState()|JFrame.MAXIMIZED_BOTH );

VPanel.installDesktopPane(jdp); // This only needs to happen once throughout


the entire application lifecycle.

JMenuBar menuBar = new JMenuBar();


JMenu menu = new JMenu("Panels");
JMenuItem menuItem = new JMenuItem("Open Test Panel");
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Test_VPanel.showPanel(new Test_VPanel()); // Every time you show the
panel, you create a new instance.
// But this new instance is only used if it is needed. The init()
method is only called if it is going
// To show a new instance.
}
});
menu.add(menuItem);
menuBar.add(menu);
jf.setJMenuBar(menuBar);

jf.setContentPane(jdp);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

static class Test_VPanel extends VPanel {

public Test_VPanel() {
super("Test Panel");
}

@Override
public void init() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();

JLabel label = new JLabel("JLabel");


JTextField textField = new JTextField();

gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
gbc.gridwidth = 1;
gbc.gridy = 0;
gbc.insets = new Insets(4,4,4,4);
add(label, gbc);

gbc.gridy = 1;
add(textField, gbc);

setVisible(); // This needs to be called at the end of init()


}
}
}

Você também pode gostar