home *** CD-ROM | disk | FTP | other *** search
Java Source | 1999-02-11 | 14.4 KB | 540 lines |
- /* ****************************************************************
- ** @(#)namedb.java 1.6 97/05/23
- **
- ** Copyright 1997 Sun Microsystems, Inc. All Rights Reserved
- **
- ** ****************************************************************
- */
-
- import java.applet.*;
- import java.awt.*;
- import java.io.*;
- import java.util.Vector;
-
- public class namedb extends Applet {
-
- private static final String title="Name Database";
-
- public Frame theframe;
- private RoloApi rolo;
-
- private namePop nameDialog;
- private searchPop searchDialog;
- private FileDialog fileDialog;
- private messagebox errorDialog;
-
- public Button namelist_button;
- public Button search_button;
- public Button add_button;
- public Button change_button;
- public Button remove_button;
- public Button clear_button;
- public Button open_button;
- public Button save_button;
- public Button saveas_button;
- public Button close_button;
- public Button exit_button;
-
- public Label title_label;
- public Label name_label;
- public Label address1_label;
- public Label address2_label;
- public Label phone_label;
- public Label email_label;
- public Label other_label;
-
- public TextField name_text;
- public TextField address1_text;
- public TextField address2_text;
- public TextField phone_text;
- public TextField email_text;
- public TextField other_text;
-
- public void init() {
-
- theframe=new Frame(namedb.title);
-
- rolo = new RoloApi();
-
- nameDialog = new namePop();
- nameDialog.init();
- nameDialog.setParent(this);
-
- searchDialog = new searchPop();
- searchDialog.init();
- searchDialog.setParent(this);
-
- fileDialog=new FileDialog(theframe);
- errorDialog=new messagebox(theframe,true,"error","Must set name to add entry");
- errorDialog.init();
-
- GridBagLayout grid = new GridBagLayout();
- int rowHeights[] = {30,30,30,30,30,30,30,30,30,30};
- int columnWidths[] = {10,25,20,20,20,20,20,20,20};
- double rowWeights[] = {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
- double columnWeights[] = {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
- grid.rowHeights = rowHeights;
- grid.columnWidths = columnWidths;
- grid.rowWeights = rowWeights;
- grid.columnWeights = columnWeights;
-
- title_label = new Label(namedb.title+" - Untitled ");
- this.add(title_label);
-
- open_button=new Button("Open");
- this.add(open_button);
-
- save_button=new Button("Save");;
- this.add(save_button);
-
- saveas_button=new Button("Save as");
- this.add(saveas_button);
-
- close_button=new Button("Close");
- this.add(close_button);
-
- exit_button=new Button("Exit");
- this.add(exit_button);
-
- add_button=new Button("Add");
- this.add(add_button);
-
- change_button=new Button("Change");
- this.add(change_button);
-
- remove_button=new Button("Remove");
- this.add(remove_button);
-
- clear_button=new Button("Clear");
- this.add(clear_button);
-
- namelist_button=new Button("Names");
- this.add(namelist_button);
-
- search_button=new Button("Search");
- this.add(search_button);
-
-
- name_label = new Label();
- name_label.setText(" Name");
- this.add(name_label);
-
- name_text = new TextField(26);
- this.add(name_text);
-
- address1_label = new Label();
- address1_label.setText(" Address1");
- this.add(address1_label);
-
- address1_text = new TextField(26);
- this.add(address1_text);
-
- address2_label = new Label();
- address2_label.setText(" Address2");
- this.add(address2_label);
-
- address2_text = new TextField(26);
- this.add(address2_text);
-
- phone_label = new Label();
- phone_label.setText(" Phone");
- this.add(phone_label);
-
- phone_text = new TextField(26);
- this.add(phone_text);
-
- email_label = new Label();
- email_label.setText(" Email");
- this.add(email_label);
-
- email_text = new TextField(26);
- this.add(email_text);
-
- other_label = new Label();
- other_label.setText(" Other");
- this.add(other_label);
-
- other_text = new TextField(26);
- this.add(other_text);
-
- // Geometry management
- GridBagConstraints con = new GridBagConstraints();
-
- reset(con);
- con.gridx = 3;
- con.gridy = 0;
- con.gridwidth = 4;
- con.anchor = GridBagConstraints.SOUTHWEST;
- con.fill = GridBagConstraints.NONE;
- grid.setConstraints(title_label, con);
-
- reset(con);
- con.gridx = 3;
- con.gridy = 1;
- con.anchor = GridBagConstraints.WEST;
- con.fill = GridBagConstraints.HORIZONTAL;
- grid.setConstraints(namelist_button, con);
-
- reset(con);
- con.gridx = 5;
- con.gridy = 1;
- con.anchor = GridBagConstraints.WEST;
- con.fill = GridBagConstraints.HORIZONTAL;
- grid.setConstraints(search_button, con);
-
- reset(con);
- con.gridx = 3;
- con.gridy = 8;
- con.anchor = GridBagConstraints.WEST;
- con.fill = GridBagConstraints.HORIZONTAL;
- grid.setConstraints(add_button, con);
-
- reset(con);
- con.gridx = 4;
- con.gridy = 8;
- con.anchor = GridBagConstraints.WEST;
- con.fill = GridBagConstraints.HORIZONTAL;
- grid.setConstraints(change_button, con);
-
- reset(con);
- con.gridx = 5;
- con.gridy = 8;
- con.anchor = GridBagConstraints.WEST;
- con.fill = GridBagConstraints.HORIZONTAL;
- grid.setConstraints(remove_button, con);
-
- reset(con);
- con.gridx = 6;
- con.gridy = 8;
- con.anchor = GridBagConstraints.WEST;
- con.fill = GridBagConstraints.HORIZONTAL;
- grid.setConstraints(clear_button, con);
-
- reset(con);
- con.gridx = 1;
- con.gridy = 2;
- con.anchor = GridBagConstraints.WEST;
- con.fill = GridBagConstraints.HORIZONTAL;
- grid.setConstraints(open_button, con);
-
- reset(con);
- con.gridx = 1;
- con.gridy = 3;
- con.anchor = GridBagConstraints.WEST;
- con.fill = GridBagConstraints.HORIZONTAL;
- grid.setConstraints(save_button, con);
-
- reset(con);
- con.gridx = 1;
- con.gridy = 4;
- con.anchor = GridBagConstraints.WEST;
- con.fill = GridBagConstraints.HORIZONTAL;
- grid.setConstraints(saveas_button, con);
-
- reset(con);
- con.gridx = 1;
- con.gridy = 5;
- con.anchor = GridBagConstraints.WEST;
- con.fill = GridBagConstraints.HORIZONTAL;
- grid.setConstraints(close_button, con);
-
- reset(con);
- con.gridx = 1;
- con.gridy = 7;
- con.anchor = GridBagConstraints.WEST;
- con.fill = GridBagConstraints.HORIZONTAL;
- grid.setConstraints(exit_button, con);
-
- reset(con);
- con.gridx = 2;
- con.gridy = 2;
- con.anchor = GridBagConstraints.WEST;
- con.fill = GridBagConstraints.NONE;
- grid.setConstraints(name_label, con);
-
- reset(con);
- con.gridx = 3;
- con.gridy = 2;
- con.gridwidth = 4;
- con.anchor = GridBagConstraints.WEST;
- con.fill = GridBagConstraints.NONE;
- grid.setConstraints(name_text, con);
-
- reset(con);
- con.gridx = 2;
- con.gridy = 3;
- con.anchor = GridBagConstraints.WEST;
- con.fill = GridBagConstraints.NONE;
- grid.setConstraints(address1_label, con);
-
- reset(con);
- con.gridx = 3;
- con.gridy = 3;
- con.gridwidth = 4;
- con.anchor = GridBagConstraints.WEST;
- con.fill = GridBagConstraints.NONE;
- grid.setConstraints(address1_text, con);
-
- reset(con);
- con.gridx = 2;
- con.gridy = 4;
- con.anchor = GridBagConstraints.WEST;
- con.fill = GridBagConstraints.NONE;
- grid.setConstraints(address2_label, con);
-
- reset(con);
- con.gridx = 3;
- con.gridy = 4;
- con.gridwidth = 4;
- con.anchor = GridBagConstraints.WEST;
- con.fill = GridBagConstraints.NONE;
- grid.setConstraints(address2_text, con);
-
- reset(con);
- con.gridx = 2;
- con.gridy = 5;
- con.anchor = GridBagConstraints.WEST;
- con.fill = GridBagConstraints.NONE;
- grid.setConstraints(phone_label, con);
-
- reset(con);
- con.gridx = 3;
- con.gridy = 5;
- con.gridwidth = 4;
- con.anchor = GridBagConstraints.WEST;
- con.fill = GridBagConstraints.NONE;
- grid.setConstraints(phone_text, con);
-
- reset(con);
- con.gridx = 2;
- con.gridy = 6;
- con.anchor = GridBagConstraints.WEST;
- con.fill = GridBagConstraints.NONE;
- grid.setConstraints(email_label, con);
-
- reset(con);
- con.gridx = 3;
- con.gridy = 6;
- con.gridwidth = 4;
- con.anchor = GridBagConstraints.WEST;
- con.fill = GridBagConstraints.NONE;
- grid.setConstraints(email_text, con);
-
- reset(con);
- con.gridx = 2;
- con.gridy = 7;
- con.anchor = GridBagConstraints.WEST;
- con.fill = GridBagConstraints.NONE;
- grid.setConstraints(other_label, con);
-
- reset(con);
- con.gridx = 3;
- con.gridy = 7;
- con.gridwidth = 4;
- con.anchor = GridBagConstraints.WEST;
- con.fill = GridBagConstraints.NONE;
- grid.setConstraints(other_text, con);
-
- // Resize behavior management and parent heirarchy
- setLayout(grid);
- change_button.disable();
- // Give the application a chance to do its initialization
- super.init();
- if (rolo.filename!=null) {
- title_label.setText(namedb.title+" - "+rolo.filename.getName());
- }
- }
-
- public boolean handleEvent(Event event) {
- if (event.target == name_text && event.id == event.ACTION_EVENT) {
- getEntry();
- } else
- if (event.target == namelist_button && event.id == event.ACTION_EVENT) {
- goNameDialog();
- } else
- if (event.target == search_button && event.id == event.ACTION_EVENT) {
- goSearchDialog();
- } else
- if (event.target == add_button && event.id == event.ACTION_EVENT) {
- addEntry();
- } else
- if (event.target == change_button && event.id == event.ACTION_EVENT) {
- changeEntry();
- } else
- if (event.target == clear_button && event.id == event.ACTION_EVENT) {
- clearEntry();
- } else
- if (event.target == remove_button && event.id == event.ACTION_EVENT) {
- removeEntry();
- } else
- if (event.target == open_button && event.id == event.ACTION_EVENT) {
- openFile();
- } else
- if (event.target == save_button && event.id == event.ACTION_EVENT) {
- saveFile();
- } else
- if (event.target == saveas_button && event.id == event.ACTION_EVENT) {
- saveasFile();
- } else
- if (event.target == exit_button && event.id == event.ACTION_EVENT) {
- System.exit(0);
- } else
- if (event.target == close_button && event.id == event.ACTION_EVENT) {
- clearAll();
- clearEntry();
- rolo.filename=null;
- } else
- return super.handleEvent(event);
- return true;
- }
-
- public static void main(String[] args) {
-
- File file;
- if (args.length>0)
- file=new File(args[0]);
- else
- file=null;
-
- Frame f=new Frame(namedb.title);
- namedb win=new namedb();
- win.init();
- win.theframe.add("Center",win);
- win.theframe.pack();
- win.theframe.show();
- if (file!=null && file.exists()) {
- win.rolo.open(file);
- win.title_label.setText(namedb.title+" - "+file.getName());
- }
- }
- private void reset(GridBagConstraints con) {
- con.gridx = GridBagConstraints.RELATIVE;
- con.gridy = GridBagConstraints.RELATIVE;
- con.gridwidth = 1;
- con.gridheight = 1;
-
- con.weightx = 0;
- con.weighty = 0;
- con.anchor = GridBagConstraints.CENTER;
- con.fill = GridBagConstraints.NONE;
-
- con.insets = new Insets(0, 0, 0, 0);
- con.ipadx = 0;
- con.ipady = 0;
- }
-
- public void getEntry() {
- Vector res;
- res=rolo.searchIt(1,name_text.getText());
- rolo.current=((Integer)res.elementAt(0)).intValue();
- rolo.showEntry(rolo.current);
- }
-
- public void addEntry() {
- if (name_text.getText().length()==0) {
- errorDialog.show();
- return;
- }
- rolo.current =
- rolo.addEntry( name_text.getText(), address1_text.getText(),
- address2_text.getText(), phone_text.getText(),
- email_text.getText(), other_text.getText() );
- nameDialog.updateData();
- change_button.enable();
- }
-
- public void changeEntry() {
- rolo.removeEntry(rolo.current);
- addEntry();
- }
-
- public void removeEntry() {
- if (rolo.current== -1) return;
- rolo.removeEntry(rolo.current);
- if (rolo.current == rolo.getSize())
- rolo.current--;
-
- rolo.updateCurrent();
- updateScreen();
- nameDialog.updateData();
- if (rolo.current== -1)
- change_button.disable();
- }
-
- public void clearEntry() {
- rolo.current= -1;
- rolo.updateCurrent();
- updateScreen();
- change_button.disable();
- }
-
- public void openFile() {
- fileDialog.setTitle("Open");
- fileDialog.setMode(FileDialog.LOAD);
- fileDialog.show();
- String fdir=fileDialog.getDirectory();
- String ffile=fileDialog.getFile();
- if (fdir!=null && ffile!=null) {
- File file=new File(fdir,ffile);
- if (file.isFile()==false)
- return;
- clearEntry();
- rolo.open(file);
- nameDialog.updateData();
- title_label.setText(namedb.title+" - "+file.getName());
- updateScreen();
- }
- }
- public void saveasFile() {
- fileDialog.setTitle("Save As");
- fileDialog.setMode(FileDialog.SAVE);
- fileDialog.show();
- String fdir=fileDialog.getDirectory();
- String ffile=fileDialog.getFile();
- if (fdir!=null && ffile!=null) {
- File file=new File(fdir,ffile);
- rolo.filename=file;
- rolo.save();
- title_label.setText(namedb.title+" - "+file.getName());
- }
- }
- public void saveFile() {
- if (rolo.filename!=null)
- rolo.save();
- else
- saveasFile();
- }
-
- public void clearAll() {
- rolo=new RoloApi();
- nameDialog.updateData();
- searchDialog.clearsearch();
- title_label.setText(namedb.title+" - Untitled");
- clearEntry();
- }
-
- public void goNameDialog() {
- nameDialog.showIt();
- }
-
- public void goSearchDialog() {
- searchDialog.clearsearch();
- searchDialog.showit();
- }
-
- public void updateScreen() {
- change_button.enable();
- name_text.setText( rolo.recurrent.name );
- address1_text.setText( rolo.recurrent.address1 );
- address2_text.setText( rolo.recurrent.address2 );
- phone_text.setText( rolo.recurrent.phone );
- email_text.setText( rolo.recurrent.email );
- other_text.setText( rolo.recurrent.other );
- }
- public RoloApi getRoloApi() { return rolo; }
-
- }
-
-
-
-