/**************************************************************************
/* This class shows a simple GUI created with Swt
/*
/* Copyright (c) 2002 by Bernhard Bablok (
[email protected])
/*
/* This program is free software; you can redistribute it and/or modify
/* it under the terms of the GNU General Public License as published
/* by the Free Software Foundation; either version 2 of the License or
/* (at your option) any later version.
/*
/* This program is distributed in the hope that it will be useful, but
/* WITHOUT ANY WARRANTY; without even the implied warranty of
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/* GNU General Public License for more details.
/*
/* You should have received a copy of the GNU General Public License
/* along with this program; see the file COPYING. If not, write to
/* the Free Software Foundation Inc., 59 Temple Place - Suite 330,
/* Boston, MA 02111-1307 USA
/**************************************************************************/
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.graphics.*;
/**
This class shows a simple GUI created with Swt
@version $Revision: 1.1 $
@author $Author: Bablokb $
*/
public class SwtGui {
private final int WINDOW_WIDTH = 300, WINDOW_HEIGHT = 200;
private Display iDisplay;
private Shell iMainWindow;
private Composite iInputPanel;
private Label iLabel;
private Text iTextArea;
private Text iEntryField;
private Button iButton;
private Color iColorWhite;
//////////////////////////////////////////////////////////////////////////////
/**
Constructor.
*/
public SwtGui() {
iDisplay = new Display();
iMainWindow = new Shell(iDisplay);
iMainWindow.setText("SWT-Example");
iColorWhite = new Color(iDisplay,255,255,255);
}
//////////////////////////////////////////////////////////////////////////////
/**
Create GUI using Swt.
*/
private void createGUI() {
FillLayout layout = new FillLayout();
// layout.justify = true;
// layout.pack = true;
// layout.wrap = false;
layout.type = SWT.VERTICAL;
iMainWindow.setLayout(layout);
iTextArea = new Text(iMainWindow,SWT.MULTI);
iTextArea.setBackground(iColorWhite);
createPanel();
iMainWindow.setMenuBar(createMenuBar());
iMainWindow.setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
Rectangle screenSize = iDisplay.getClientArea();
iMainWindow.setLocation((screenSize.width - WINDOW_WIDTH)/2,
(screenSize.height - WINDOW_HEIGHT)/2);
iEntryField.setFocus();
}
//////////////////////////////////////////////////////////////////////////////
/**
Create input panel using Swt.
*/
private void createPanel() {
iInputPanel = new Composite(iMainWindow,SWT.NULL);
RowLayout layout = new RowLayout();
layout.justify = true;
layout.wrap = false;
layout.type = SWT.HORIZONTAL;
iInputPanel.setLayout(new RowLayout());
iLabel = new Label(iInputPanel,SWT.LEFT);
iLabel.setText("Text: ");
iEntryField = new Text(iInputPanel,SWT.SINGLE);
iEntryField.setBackground(iColorWhite);
iButton = new Button(iInputPanel,SWT.PUSH | SWT.CENTER);
iButton.setText("Add");
iButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
iTextArea.append(iEntryField.getText());
iTextArea.append("\n");
}
}
);
}
//////////////////////////////////////////////////////////////////////////////
/**
Create menu using Swt.
*/
private Menu createMenuBar() {
Menu mb = new Menu(iMainWindow,SWT.BAR);
Menu fileMenu = new Menu(mb);
MenuItem fileMenuItem = new MenuItem(mb,SWT.CASCADE);
fileMenuItem.setText("File");
fileMenuItem.setMenu(fileMenu);
MenuItem exitItem = new MenuItem(fileMenu,SWT.PUSH);
exitItem.setText("Exit");
exitItem.addSelectionListener(new SelectionAdapter () {
public void widgetSelected(SelectionEvent e) {
exit();
}
});
Menu helpMenu = new Menu(mb);
MenuItem helpMenuItem = new MenuItem(mb,SWT.CASCADE);
helpMenuItem.setText("Help");
helpMenuItem.setMenu(helpMenu);
MenuItem aboutItem = new MenuItem(helpMenu,SWT.PUSH);
aboutItem.setText("About...");
aboutItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
about();
}
}
);
return mb;
}
//////////////////////////////////////////////////////////////////////////////
/**
Close window and leave application.
*/
void exit() {
iMainWindow.setVisible(false);
iMainWindow.dispose();
}
//////////////////////////////////////////////////////////////////////////////
/**
Show about-dialog.
*/
void about() {
MessageBox box = new MessageBox(iMainWindow,SWT.ICON_INFORMATION|SWT.OK);
box.setText("About SWT-Example");
box.setMessage("SWT-Example class: " + SwtGui.class.getName());
box.open();
}
//////////////////////////////////////////////////////////////////////////////
/**
Main event loop
*/
private void processLoop() {
iMainWindow.open();
while (!iMainWindow.isDisposed()) {
if (!iDisplay.readAndDispatch())
iDisplay.sleep();
}
iDisplay.dispose();
}
//////////////////////////////////////////////////////////////////////////////
/**
Main entry point
*/
public static void main(String[] args) {
SwtGui gui = new SwtGui();
gui.createGUI();
gui.processLoop();
}
}