Widget on SWT

| 0 comentarios

Acá en el mundo Java; Swing y SWT veces se consideran como tecnologías de estricta competencia. Ejemplos claros se encuentran en la web, donde abundan opiniones muy validas de cuando usar una u otra para la construcción de aplicaciones cliente (RCP). Sin embargo, en el mundo real, se requiere que ambas tecnologías convivan en una sola aplicación. Este mix resultante entre los dos conjuntos de herramientas no es una tarea sencilla. Por supuesto que se puede hacer, y se puede hacer de tal manera que los dos conjuntos de herramientas se integren sin problemas.

Como no había hablado de SWT en este blog, empezare haciendo una aplicación sencilla que muestre de manera rápida como se programan algunos controles y eventos en SWT. Dejando pendiente para otro post la integración de SWT/JFace con Swing + un control Flamingo etc., lo cual requiere de conocimientos más avanzados, aunque si ya has programado en SWing no deberías tener dificultades para aprender SWT, el debate de cuando usar cada uno lo puedes checar en [1].

Se me ha ocurrido empezar crear un widget para testear la conexion con algún DBMS

Config1


package vista;

//import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;


public class FormConnection{

//Variables
private Display display;
private Shell shell;
private Label lblInst;
//private Label lblSep;
private Label lblServer;
private Label lblBD;
private Label lblPort;
private Label lblUser;
private Label lblPass;
private Button btnAceptar;
private Text txtServer;
private Text txtBD;
private Text txtPort;
private Text txtUser;
private Text txtPass;
private Group grp;
private GridData gridDataH;
private GridData gridDataHVTop;
private GridData gridDataHV;
private Combo cmb;

public void initComponents(){

display = new Display();

GridLayout gly = new GridLayout();
gly.numColumns = 2;

shell = new Shell(display,SWT.MIN | SWT.CLOSE);
shell.setText("Configuración");
shell.addShellListener(new ShellAdapter(){
public void shellClosed(ShellEvent event){
int i = Mensajes.showMessageBoxYN(shell, "¿Desea salir de la aplicación?");
if(i == SWT.YES){
event.doit = true;
} else {
event.doit = false;
}
}
});
//x,y,width,height
shell.setBounds(300, 300, 290, 270);

shell.setLayout(gly);

gridDataHVTop = new GridData();
gridDataHVTop.horizontalSpan = 2;
gridDataHVTop.horizontalAlignment = GridData.FILL;
gridDataHVTop.grabExcessHorizontalSpace = true;

lblInst = new Label(shell, SWT.WRAP);
lblInst.setText("Especifique en cada uno de los campos, los parametros "+
"necesarios para establecer la conexión con el DBMS de su preferencia.");
lblInst.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
lblInst.setLayoutData(gridDataHVTop);

//lblSep = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);

gridDataHV = new GridData();
gridDataHV.horizontalSpan = 2;
gridDataHV.horizontalAlignment = GridData.FILL;
gridDataHV.grabExcessHorizontalSpace = true;

grp = new Group(shell, SWT.SHADOW_IN);
grp.setLayoutData(gridDataHV);
grp.setText("Datos de la conexión");
grp.setLayout(gly);
grp.setSize(200, 200);

lblServer = new Label(grp, SWT.LEFT);
lblServer.setText("Nombre del servidor:");

gridDataHV = new GridData();
gridDataHV.horizontalSpan = 1;
gridDataHV.horizontalAlignment = GridData.FILL;
gridDataHV.grabExcessHorizontalSpace = true;

txtServer = new Text(grp, SWT.BORDER | SWT.SINGLE);
txtServer.setLayoutData(gridDataHV);

lblBD = new Label(grp, SWT.LEFT);
lblBD.setText("Nombre de la BD:");

txtBD = new Text(grp, SWT.BORDER | SWT.SINGLE);
txtBD.setLayoutData(gridDataHV);

lblPort = new Label(grp, SWT.LEFT);
lblPort.setText("Numero de puerto:");

txtPort = new Text(grp, SWT.BORDER | SWT.SINGLE);
txtPort.setText("1521");
txtPort.addVerifyListener(new VerifyListener() {
public void verifyText(VerifyEvent event) {
event.doit = event.text.length() == 0
|| Character.isDigit(event.text.charAt(0));
}
});
txtPort.setToolTipText("Solo numeros en este campo");
txtPort.setLayoutData(gridDataHV);

lblUser = new Label(grp, SWT.LEFT);
lblUser.setText("Nombre de usuario:");

txtUser = new Text(grp, SWT.BORDER | SWT.SINGLE);
txtUser.setLayoutData(gridDataHV);

lblPass = new Label(grp, SWT.LEFT);
lblPass.setText("Password:");

txtPass = new Text(grp, SWT.BORDER | SWT.PASSWORD);
txtPass.setLayoutData(gridDataHV);

cmb = new Combo(shell,SWT.READ_ONLY);
cmb.setItems(new String[]{ "Oracle 10G Express", "MS SQL Server 05/08"});
cmb.select(0);
cmb.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent event) {
Mensajes.showMessageBoxOK(shell, "Se ha establecido la conexión con: "+
cmb.getText() );
}
});

gridDataH = new GridData();
gridDataH.horizontalSpan = 1;
gridDataH.horizontalAlignment = GridData.FILL;
gridDataH.grabExcessHorizontalSpace = true;

cmb.setLayoutData(gridDataH);

btnAceptar = new Button(shell, SWT.PUSH);
btnAceptar.setText(" Aceptar ");
btnAceptar.setToolTipText("Acepta los cambios realizados");
btnAceptar.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
Mensajes.showMessageBoxOK(shell, "Se presiono el " +
event.getSource().toString());
}
});

gridDataH = new GridData();
gridDataH.horizontalSpan = 1;
gridDataH.horizontalAlignment = GridData.FILL;
gridDataH.grabExcessHorizontalSpace = false;

btnAceptar.setLayoutData(gridDataH);
shell.setDefaultButton(btnAceptar);

//shell.pack(); //Ajusta todos los controles
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}

public static void main(String[] args) {
new FormConnection().initComponents();
}
}



Recomiendo lean la API org.eclipse.swt en la página de Eclipse para mas detalles. Por cierto no olviden agregar la biblioteca org.eclipse.swt.win32.win32.x86_3.n.n.vnnnn.jar (o su correspondiente en GNU/Linux u OSX)

Descargas
  • Descargar este demo y ejecutar el archivo JAR



Referencias

http://www.ibm.com/developerworks/opensource/library/os-swingswt/

Saludos y hasta la próxima.