home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1998 November
/
Chip_1998-11_cd.bin
/
tema
/
Cafe
/
WDESAMPL.BIN
/
EmployeeDataManager.java
< prev
next >
Wrap
Text File
|
1998-02-26
|
12KB
|
407 lines
/*
This simple extension of the java.awt.Frame class
contains all the elements necessary to act as the
main window of an application.
*/
import java.awt.*;
import java.io.*;
import symantec.itools.awt.util.edit.SocialSecurityNumber;
public class EmployeeDataManager extends Frame
{
final static String employeeFileName = "employee.dat";
EmployeeFileAccessor efa;
void updateEmployeeDisplay(EmployeeList elist)
{
Employee emp = elist.first();
String textdata = "";
for (;;)
{
if (emp == null) break;
System.out.println("Appending " + emp.toString() + " to textarea");
textdata += emp.toString();
emp = elist.next();
}
ta.setText(textdata);
}
void miAbout_Action(java.awt.event.ActionEvent event)
{
//{{CONNECTION
// Action from About Create and show as modal
(new AboutDialog(this, true)).show();
//}}
}
void miExit_Action(java.awt.event.ActionEvent event)
{
//{{CONNECTION
// Action from Exit Create and show as modal
(new QuitDialog(this, true)).show();
//}}
}
void Frame1_WindowClosing(java.awt.event.WindowEvent event)
{
hide(); // hide the Frame
dispose(); // free the system resources
System.exit(0); // close the application
}
public EmployeeDataManager()
{
// This code is automatically generated by Visual Cafe when you add
// components to the visual environment. It instantiates and initializes
// the components. To modify the code, only use code syntax that matches
// what Visual Cafe can generate, or Visual Cafe may be unable to back
// parse your Java file into its visual environment.
//{{INIT_CONTROLS
setLayout(new BorderLayout(0,0));
setVisible(false);
setSize(751,309);
setFont(new Font("Dialog", Font.PLAIN, 12));
setForeground(new Color(0));
setBackground(new Color(12632256));
addPanel = new java.awt.Panel();
GridBagLayout gridBagLayout;
gridBagLayout = new GridBagLayout();
addPanel.setLayout(gridBagLayout);
addPanel.setBounds(0,0,751,0);
add("North", addPanel);
label1 = new java.awt.Label("Name:",Label.RIGHT);
label1.setBounds(39,0,51,23);
GridBagConstraints gbc;
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.anchor = GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(0,0,0,0);
((GridBagLayout)addPanel.getLayout()).setConstraints(label1, gbc);
addPanel.add(label1);
nfield = new java.awt.TextField();
nfield.setBounds(90,0,112,23);
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 2.0;
gbc.weighty = 1.0;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0,0,0,0);
((GridBagLayout)addPanel.getLayout()).setConstraints(nfield, gbc);
addPanel.add(nfield);
label2 = new java.awt.Label("Section#:",Label.RIGHT);
label2.setBounds(231,0,65,23);
gbc = new GridBagConstraints();
gbc.gridx = 2;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.anchor = GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(0,0,0,0);
((GridBagLayout)addPanel.getLayout()).setConstraints(label2, gbc);
addPanel.add(label2);
afield = new java.awt.TextField();
afield.setBounds(296,0,68,23);
gbc = new GridBagConstraints();
gbc.gridx = 3;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0,0,0,0);
((GridBagLayout)addPanel.getLayout()).setConstraints(afield, gbc);
addPanel.add(afield);
label3 = new java.awt.Label(" SSN:",Label.RIGHT);
label3.setBounds(398,0,45,23);
gbc = new GridBagConstraints();
gbc.gridx = 4;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.anchor = GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(0,0,0,0);
((GridBagLayout)addPanel.getLayout()).setConstraints(label3, gbc);
addPanel.add(label3);
sfield = new symantec.itools.awt.util.edit.SocialSecurityNumber();
sfield.setColumns(30);
sfield.setBounds(443,2,233,19);
sfield.setFont(new Font("Dialog", Font.PLAIN, 12));
sfield.setBackground(new Color(16777215));
gbc = new GridBagConstraints();
gbc.gridx = 5;
gbc.gridy = 0;
gbc.weightx = 2.0;
gbc.weighty = 1.0;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0,0,0,0);
gbc.ipadx = 30;
((GridBagLayout)addPanel.getLayout()).setConstraints(sfield, gbc);
addPanel.add(sfield);
sfield.setCursor(new Cursor(Cursor.TEXT_CURSOR));
addbutton = new java.awt.Button();
addbutton.setLabel("Add");
addbutton.setBounds(695,0,35,23);
gbc = new GridBagConstraints();
gbc.gridx = 6;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(0,0,0,0);
((GridBagLayout)addPanel.getLayout()).setConstraints(addbutton, gbc);
addPanel.add(addbutton);
sortPanel = new java.awt.Panel();
gridBagLayout = new GridBagLayout();
sortPanel.setLayout(gridBagLayout);
sortPanel.setBounds(0,23,751,286);
add("Center", sortPanel);
choice = new java.awt.Choice();
choice.addItem("Name");
choice.addItem("Section");
choice.addItem("SSN");
try {
choice.select(0);
}
catch (IllegalArgumentException e) { }
choice.setBounds(10,30,129,21);
choice.setBackground(new Color(16777215));
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.anchor = GridBagConstraints.NORTH;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(30,10,0,0);
((GridBagLayout)sortPanel.getLayout()).setConstraints(choice, gbc);
sortPanel.add(choice);
sortbutton = new java.awt.Button();
sortbutton.setLabel("Sort");
sortbutton.setBounds(169,30,36,23);
sortbutton.setFont(new Font("Dialog", Font.PLAIN, 12));
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.anchor = GridBagConstraints.NORTH;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(30,0,0,0);
((GridBagLayout)sortPanel.getLayout()).setConstraints(sortbutton, gbc);
sortPanel.add(sortbutton);
ta = new java.awt.TextArea(5,0);
ta.setBounds(235,30,500,216);
gbc = new GridBagConstraints();
gbc.gridx = 2;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.anchor = GridBagConstraints.NORTH;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(30,0,40,15);
((GridBagLayout)sortPanel.getLayout()).setConstraints(ta, gbc);
sortPanel.add(ta);
setTitle("Employee Data Manager");
//}}
//{{INIT_MENUS
mainMenuBar = new java.awt.MenuBar();
menu1 = new java.awt.Menu("File");
miExit = new java.awt.MenuItem("Exit");
menu1.add(miExit);
mainMenuBar.add(menu1);
menu3 = new java.awt.Menu("Help");
mainMenuBar.setHelpMenu(menu3);
miAbout = new java.awt.MenuItem("About..");
menu3.add(miAbout);
mainMenuBar.add(menu3);
setMenuBar(mainMenuBar);
//$$ mainMenuBar.move(4,277);
//}}
//{{REGISTER_LISTENERS
Window lWindow = new Window();
addWindowListener(lWindow);
Action lAction = new Action();
miAbout.addActionListener(lAction);
miExit.addActionListener(lAction);
addbutton.addActionListener(lAction);
sortbutton.addActionListener(lAction);
//}}
System.out.println("Opening file");
try
{
efa = new EmployeeFileAccessor(employeeFileName);
System.out.println("File opened");
}
catch (IOException e)
{
System.out.println("Unable to open file " + employeeFileName);
return;
}
// Fetch all employees from file, and display them in the text area
EmployeeList elist = efa.buildEmployeeList();
updateEmployeeDisplay(elist);
}
public EmployeeDataManager(String title)
{
this();
setTitle(title);
}
public synchronized void show()
{
move(50, 50);
super.show();
}
static public void main(String args[])
{
(new EmployeeDataManager()).show();
}
public void addNotify()
{
super.addNotify();
if (fComponentsAdjusted)
return;
// Adjust components according to the insets
setSize(insets().left + insets().right + getSize().width, insets().top + insets().bottom + getSize().height);
Component components[] = getComponents();
for (int i = 0; i < components.length; i++)
{
Point p = components[i].getLocation();
p.translate(insets().left, insets().top);
components[i].setLocation(p);
}
fComponentsAdjusted = true;
}
boolean fComponentsAdjusted = false;
//{{DECLARE_CONTROLS
java.awt.Panel addPanel;
java.awt.Label label1;
java.awt.TextField nfield;
java.awt.Label label2;
java.awt.TextField afield;
java.awt.Label label3;
symantec.itools.awt.util.edit.SocialSecurityNumber sfield;
java.awt.Button addbutton;
java.awt.Panel sortPanel;
java.awt.Choice choice;
java.awt.Button sortbutton;
java.awt.TextArea ta;
//}}
//{{DECLARE_MENUS
java.awt.MenuBar mainMenuBar;
java.awt.Menu menu1;
java.awt.MenuItem miExit;
java.awt.Menu menu3;
java.awt.MenuItem miAbout;
//}}
class Window extends java.awt.event.WindowAdapter
{
public void windowClosing(java.awt.event.WindowEvent event)
{
Object object = event.getSource();
if (object == EmployeeDataManager.this)
Frame1_WindowClosing(event);
}
}
class Action implements java.awt.event.ActionListener
{
public void actionPerformed(java.awt.event.ActionEvent event)
{
Object object = event.getSource();
if (object == miAbout)
miAbout_Action(event);
else if (object == miExit)
miExit_Action(event);
else if (object == addbutton)
addbutton_Action(event);
else if (object == sortbutton)
sortbutton_Action(event);
}
}
void addbutton_Action(java.awt.event.ActionEvent event)
{
Employee emp;
// The ADD button was pressed; add new employee
System.out.println("Add an employee");
// Create (and validate) an employee object
try {
// get the SS Number String and add dashes if needed
String ssn = sfield.getText();
if (ssn.charAt(3) != '-')
{
ssn = ssn.substring(0,3) + '-' + ssn.substring(3);
}
if (ssn.charAt(6) != '-')
{
ssn = ssn.substring(0,6) + '-' + ssn.substring(6);
}
emp = new Employee (nfield.getText(), afield.getText(), ssn);
} catch (Exception ex1) {
System.out.println("Invalid employee data");
System.out.println(ex1.getMessage());
//showStatus("Invalid employee data; please re-enter");
return;
}
// Add the employee to the file
try
{
efa.addEmployee(emp);
}
catch (IOException ex2)
{
System.out.println("Unable to add employee to file");
return;
}
System.out.println("Employee added");
// Redisplay the employee data
EmployeeList elist = efa.buildEmployeeList();
updateEmployeeDisplay(elist);
}
void sortbutton_Action(java.awt.event.ActionEvent event)
{
// The SORT button was pressed; perform sort
EmployeeList elist = efa.buildEmployeeList();
elist.sort(choice.getSelectedIndex());
System.out.println("Sort on field " + choice.getSelectedIndex());
// Redisplay the employee data
updateEmployeeDisplay(elist);
}
}