home *** CD-ROM | disk | FTP | other *** search
Java Source | 1999-02-11 | 21.1 KB | 667 lines |
- /* ****************************************************************
- ** @(#)namedb.java 1.5 0
- **
- ** Copyright 1997 Sun Microsystems, Inc. All Rights Reserved
- **
- ** ****************************************************************
- */
-
- import java.applet.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.util.Vector;
- import gjt.*;
-
- public class namedb extends Applet implements ActionListener {
-
- public intlStrings strings=new intlStrings(1);
-
- public Box box1,box2,box3;
- public Panel p1,p2,p3;
- public Label index;
-
- public Label title_label;
- public Panel button1_panel,button2_panel,data_panel,main_panel;
-
- public Frame theframe;
- private RoloApi rolo;
-
- public boolean UseSetName=true;
- public boolean ugly=false;
- public boolean right=false;
-
- public namePop nameDialog;
- public searchPop searchDialog;
- public FileDialog fileDialog;
- public messagebox errorDialog;
-
- public OpenlookButton forward_button,back_button,namelist_button;
- public OpenlookButton search_button,add_button,change_button,remove_button;
- public OpenlookButton clear_button;
- public Button open_button,save_button,saveas_button;
- public Button close_button,exit_button;
-
- 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() {
-
- try{
- String lang = getParameter("Lang");
- if (lang != null && lang.equals("fr")) {
- strings.setLang(2);
- }
- } catch(NullPointerException npe){
- //occurs when running as application
- }
-
- if (UseSetName)
- setName("namedbFrame");
- theframe=new Frame(strings.title());
- if (UseSetName)
- theframe.setName("namedb_frame");
- theframe.setBackground(Color.lightGray);
-
- rolo = new RoloApi();
-
- nameDialog = new namePop();
- nameDialog.setParent(this);
- nameDialog.init();
- if (UseSetName)
- nameDialog.setName("nameDialog");
-
- searchDialog = new searchPop();
- searchDialog.setParent(this);
- searchDialog.init();
- if (UseSetName)
- searchDialog.setName("searchDialog");
-
- fileDialog=new FileDialog(theframe);
- if (UseSetName)
- fileDialog.setName("fileDialog");
-
- errorDialog=new messagebox("Error","Must set name to add entry");
- errorDialog.pack();
- if (UseSetName)
- errorDialog.setName("errorDialog");
-
- main_panel=new Panel();
- if (UseSetName)
- main_panel.setName("centerPanel");
- add(main_panel);
-
- data_panel=new Panel();
- if (UseSetName)
- data_panel.setName("data_panel");
- add(data_panel);
-
- button1_panel=new Panel();
- if (UseSetName)
- button1_panel.setName("button1_panel");
- button1_panel.setLayout(new GridLayout(2,4));
-
- button2_panel=new Panel();
- if (UseSetName)
- button2_panel.setName("button2_panel");
- button2_panel.setLayout(new GridLayout(1,5));
-
- GridBagLayout main_grid = new GridBagLayout();
- GridBagLayout data_grid = new GridBagLayout();
-
- title_label=new Label(strings.title()+" - Untitled");
- if (UseSetName)
- title_label.setName("title_label");
- if (!ugly)
- title_label.setFont(new Font("Serif",Font.BOLD,20));
- main_panel.add(title_label);
-
- if (!ugly) {
- box1=new Box(button1_panel,"");
- main_panel.add(box1);
- box2=new Box(button2_panel,"");
- main_panel.add(box2);
- box3=new Box(data_panel," 0 of 0 ");
- if (UseSetName)
- box3.titleLabel.setName("status_label");
- box3.setFont(new Font("Serif",Font.BOLD+Font.ITALIC,14));
- main_panel.add(box3);
- } else {
- p1=new Panel();
- p1.add(button1_panel);
- main_panel.add(p1);
- p2=new Panel();
- p2.add(button2_panel);
- main_panel.add(p2);
- p3=new Panel();
- p3.add(data_panel);
- main_panel.add(p3);
- index=new Label(" 0 of 0 ");
- if (UseSetName)
- index.setName("status_label");
- main_panel.add(index);
- }
-
- back_button=new OpenlookButton("<<");
- if (UseSetName)
- back_button.setName("back_button");
- if (!ugly)
- back_button.setFont(new Font("Serif",Font.BOLD+Font.ITALIC,14));
- back_button.addActionListener(this);
- button1_panel.add(back_button,new Point(0,0));
-
- forward_button=new OpenlookButton(">>");
- if (UseSetName)
- forward_button.setName("forward_button");
- if (!ugly)
- forward_button.setFont(new Font("Serif",Font.BOLD+Font.ITALIC,14));
- forward_button.addActionListener(this);
- button1_panel.add(forward_button,new Point(0,1));
-
- namelist_button=new OpenlookButton(strings.nameb());
- if (UseSetName)
- namelist_button.setName("namelist_button");
- if (!ugly)
- namelist_button.setFont(new Font("Serif",Font.BOLD+Font.ITALIC,14));
- namelist_button.addActionListener(this);
- button1_panel.add(namelist_button,new Point(0,2));
-
- search_button=new OpenlookButton(strings.searchb());
- if (UseSetName)
- search_button.setName("search_button");
- if (!ugly)
- search_button.setFont(new Font("Serif",Font.BOLD+Font.ITALIC,14));
- search_button.addActionListener(this);
- button1_panel.add(search_button,new Point(0,3));
-
- add_button=new OpenlookButton(strings.addb());
- if (UseSetName)
- add_button.setName("add_button");
- if (!ugly)
- add_button.setFont(new Font("Serif",Font.BOLD+Font.ITALIC,14));
- add_button.addActionListener(this);
- button1_panel.add(add_button,new Point(1,0));
-
- change_button=new OpenlookButton(strings.changeb());
- if (UseSetName)
- change_button.setName("change_button");
- if (!ugly)
- change_button.setFont(new Font("Serif",Font.BOLD+Font.ITALIC,14));
- change_button.addActionListener(this);
- button1_panel.add(change_button,new Point(1,1));
-
- remove_button=new OpenlookButton(strings.removeb());
- if (UseSetName)
- remove_button.setName("remove_button");
- if (!ugly)
- remove_button.setFont(new Font("Serif",Font.BOLD+Font.ITALIC,14));
- remove_button.addActionListener(this);
- button1_panel.add(remove_button,new Point(1,2));
-
- clear_button=new OpenlookButton(strings.clearb());
- if (UseSetName)
- clear_button.setName("clear_button");
- if (!ugly)
- clear_button.setFont(new Font("Serif",Font.BOLD+Font.ITALIC,14));
- clear_button.addActionListener(this);
- button1_panel.add(clear_button,new Point(1,3));
-
- open_button=new Button(strings.openb());
- if (UseSetName)
- open_button.setName("open_button");
- if (!ugly)
- open_button.setFont(new Font("Serif",Font.BOLD+Font.ITALIC,14));
- open_button.addActionListener(this);
- button2_panel.add(open_button,new Point(0,0));
-
- save_button=new Button(strings.saveb());;
- if (UseSetName)
- save_button.setName("save_button");
- if (!ugly)
- save_button.setFont(new Font("Serif",Font.BOLD+Font.ITALIC,14));
- save_button.addActionListener(this);
- button2_panel.add(save_button,new Point(0,1));
-
- saveas_button=new Button(strings.saveasb());
- if (UseSetName)
- saveas_button.setName("saveas_button");
- if (!ugly)
- saveas_button.setFont(new Font("Serif",Font.BOLD+Font.ITALIC,14));
- saveas_button.addActionListener(this);
- button2_panel.add(saveas_button,new Point(0,2));
-
- close_button=new Button(strings.closeb());
- if (UseSetName)
- close_button.setName("close_button");
- if (!ugly)
- close_button.setFont(new Font("Serif",Font.BOLD+Font.ITALIC,14));
- close_button.addActionListener(this);
- button2_panel.add(close_button,new Point(0,3));
-
- exit_button=new Button(strings.exitb());
- if (UseSetName)
- exit_button.setName("exit_button");
- if (!ugly)
- exit_button.setFont(new Font("Serif",Font.BOLD+Font.ITALIC,14));
- exit_button.addActionListener(this);
- button2_panel.add(exit_button,new Point(0,4));
-
- name_label = new Label();
- name_label.setText(strings.namel());
- if (!ugly)
- name_label.setFont(new Font("Serif",Font.BOLD+Font.ITALIC,14));
- if (UseSetName)
- name_label.setName("name_label");
- data_panel.add(name_label);
-
- name_text = new TextField(40);
- if (UseSetName)
- name_text.setName("name_text");
- name_text.setBackground(Color.lightGray.darker());
- if (!ugly)
- name_text.setFont(new Font("TimesRoman",Font.BOLD,14));
- name_text.addActionListener(this);
- data_panel.add(name_text);
-
- address1_label = new Label();
- address1_label.setText(strings.addressl());
- if (!ugly)
- address1_label.setFont(new Font("Serif",Font.BOLD+Font.ITALIC,14));
- if (UseSetName)
- address1_label.setName("address1_button");
- data_panel.add(address1_label);
-
- address1_text = new TextField(20);
- if (UseSetName)
- address1_text.setName("address1_text");
- address1_text.setBackground(Color.lightGray.darker());
- if (!ugly)
- address1_text.setFont(new Font("TimesRoman",Font.BOLD,14));
- data_panel.add(address1_text);
-
- address2_text = new TextField(20);
- if (UseSetName)
- address2_text.setName("address2_text");
- address2_text.setBackground(Color.lightGray.darker());
- if (!ugly)
- address2_text.setFont(new Font("TimesRoman",Font.BOLD,14));
- data_panel.add(address2_text);
-
- phone_label = new Label();
- phone_label.setText(strings.phonel());
- if (UseSetName)
- phone_label.setName("phone_label");
- if (!ugly)
- phone_label.setFont(new Font("Serif",Font.BOLD+Font.ITALIC,14));
- data_panel.add(phone_label);
-
- phone_text = new TextField(20);
- if (UseSetName)
- phone_text.setName("phone_text");
- phone_text.setBackground(Color.lightGray.darker());
- if (!ugly)
- phone_text.setFont(new Font("TimesRoman",Font.BOLD,14));
- data_panel.add(phone_text);
-
- email_text = new TextField(20);
- if (UseSetName)
- email_text.setName("email_text");
- email_text.setBackground(Color.lightGray.darker());
- if (!ugly)
- email_text.setFont(new Font("TimesRoman",Font.BOLD,14));
- data_panel.add(email_text);
-
- other_label = new Label();
- other_label.setText(strings.salaryl());
- if (UseSetName)
- other_label.setName("other_label");
- if (!ugly)
- other_label.setFont(new Font("Serif",Font.BOLD+Font.ITALIC,14));
- data_panel.add(other_label);
-
- other_text = new TextField(20);
- if (UseSetName)
- other_text.setName("other_text");
- other_text.setBackground(Color.lightGray.darker());
- if (!ugly)
- other_text.setFont(new Font("TimesRoman",Font.BOLD,14));
- data_panel.add(other_text);
-
- // set the data grid, all data labels and tfs
- constrain( data_grid, name_label, 0,0,1,1,GridBagConstraints.WEST,GridBagConstraints.NONE,0,0,0,5,0,0,0,0 );
-
- constrain( data_grid, name_text, 1,0,2,1,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,0,0,0,0,0,0,0,0);
-
- constrain( data_grid, address1_label, 0,1,1,1,GridBagConstraints.WEST,GridBagConstraints.NONE,0,0,0,0,0,0,0,0 );
-
- constrain( data_grid, address1_text, 1,1,1,1,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,0,0,0,0,0,0,0,0 );
-
- constrain( data_grid, address2_text, 2,1,1,1,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,0,0,0,0,0,0,0,0 );
-
- constrain( data_grid, phone_label, 0,2,1,1,GridBagConstraints.WEST,GridBagConstraints.NONE,0,0,0,0,0,0,0,0 );
-
- constrain( data_grid, phone_text, 1,2,1,1,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,0,0,0,0,0,0,0,0 );
-
- constrain( data_grid, email_text, 2,2,1,1,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,0,0,0,0,0,0,0,0 );
-
- constrain( data_grid, other_label, 0,3,1,1,GridBagConstraints.WEST,GridBagConstraints.NONE,0,0,0,0,0,0,0,0 );
-
- constrain( data_grid, other_text, 1,3,1,1,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL,0,0,0,0,0,0,0,0 );
-
- data_panel.setLayout(data_grid);
-
- constrain( main_grid, title_label, 1,0,2,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,0,0,0,0,0,0,0,0 );
-
- if (ugly) {
- constrain( main_grid, index, 1,3,1,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,0,0,0,0,0,0,0,0 );
- constrain( main_grid, title_label,1,0,2,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,0,0,0,0,0,0,0,0 );
- constrain( main_grid, p1, 1,2,2,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,0,0,0,0,0,0,0,0 );
- constrain( main_grid, p2, 1,1,2,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,0,0,10,0,10,0,0,0 );
- constrain( main_grid, p3, 1,4,2,4, GridBagConstraints.CENTER,GridBagConstraints.NONE, 0,0,0,0,0,0,0,0 );
- } else {
- constrain( main_grid, box1, 1,5,2,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,0,0,0,0,0,0,0,0 );
- constrain( main_grid, box2, 1,7,2,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,0,0,10,0,10,0,0,0 );
- constrain( main_grid, box3, 1,1,2,4, GridBagConstraints.CENTER,GridBagConstraints.NONE, 0,0,0,0,0,0,0,0 );
- }
- main_panel.setLayout(main_grid);
-
- GridBagLayout framegrid = new GridBagLayout();
- constrain( framegrid, main_panel, 0,0,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH,40,40,0,0,0,30,0,0);
-
- setLayout(framegrid);
-
- change_button.setEnabled(false);
- // Give the application a chance to do its initialization
- super.init();
- }
-
- public void actionPerformed(ActionEvent event) {
- Component comp=(Component)event.getSource();
- if (comp == back_button) {
- rolo.current--;
- if (rolo.current<0)
- rolo.current=rolo.getSize()-1;
- updateCurrent();
- }
- if (comp == forward_button) {
- rolo.current++;
- if (rolo.getSize()==0)
- rolo.current= -1;
- if (rolo.current==rolo.getSize())
- rolo.current=0;
- updateCurrent();
- }
- if (comp == name_text ) {
- getEntry();
- }
- if (comp == namelist_button ) {
- goNameDialog();
- } else
- if (comp == search_button ) {
- goSearchDialog();
- } else
- if (comp == add_button ) {
- addEntry();
- } else
- if (comp == change_button ) {
- if (change_button.isEnabled()==true)
- changeEntry();
- } else
- if (comp == clear_button ) {
- clearEntry();
- } else
- if (comp == remove_button ) {
- removeEntry();
- } else
- if (comp == open_button ) {
- openFile();
- } else
- if (comp == save_button ) {
- saveFile();
- } else
- if (comp == saveas_button ) {
- saveasFile();
- } else
- if (comp == exit_button ) {
- System.exit(0);
- } else
- if (comp == close_button ) {
- clearAll();
- clearEntry();
- rolo.filename=null;
- }
- }
-
- public static void main(String[] args) {
-
- File file=null;
- Frame f=new Frame();
- namedb win=new namedb();
- int num;
- f.setTitle(win.strings.title());
- // support passing filename commandline as well as
- // -en,-fr,-nosetname,-help
- for (int i=0;i<args.length;i++) {
- if (args[i].startsWith("-")==false) {
- file=new File(args[i]);
- }
- if (args[i].equals("-help")) {
- System.out.println("namedb parameters:");
- System.out.println(" -v1 ugly gui with English");
- System.out.println(" -v2 pretty gui in French with components on right");
- System.out.println(" -en English");
- System.out.println(" -fr French");
- System.out.println(" -help this message");
- System.out.println(" -langN language where N is the number");
- System.out.println(" -nosetname will not use setName");
- System.out.println(" <filename> a db file to open");
- }
- if (args[i].equals("-nosetname")) {
- win.UseSetName=false;
- }
- if (args[i].equals("-rolo"))
- win.strings.setLang(0);
- if (args[i].equals("-en"))
- win.strings.setLang(1);
- if (args[i].equals("-fr"))
- win.strings.setLang(2);
- if (args[i].equals("-ugly")) {
- win.ugly=true;
- }
- if (args[i].equals("-right")) {
- win.right=true;
- }
- if (args[i].equals("-v1")) {
- win.ugly=true;
- }
- if (args[i].equals("-v2")) {
- win.right=true;
- win.strings.setLang(2);
- }
- if (args[i].startsWith("-lang")) {
- try {
- num=new Integer( args[i].substring(5) ).intValue();
- } catch (NumberFormatException e) {
- System.out.println("could not process number in: "+args[i]);
- num=1;
- }
- win.strings.setLang(num);
- }
- }
- win.init();
- if (file!=null && file.exists()) {
- win.rolo.open(file);
- win.title_label.setText(win.strings.title()+" - "+file.getName());
- win.main_panel.remove(win.title_label);
- win.main_panel.add(win.title_label);
- win.updateScreen();
- }
- win.theframe.add("Center",win);
- win.theframe.pack();
- win.theframe.show();
- }
-
- public void constrain(GridBagLayout grid,Component comp,int gridx,int gridy,int gridwidth,int gridheight,int anchor,int fill,int ipadx,int ipady,int top,int left,int bottom,int right,double weightx,double weighty) {
- GridBagConstraints con=new GridBagConstraints();
- con.gridx = gridx;
- con.gridy = gridy;
- con.gridwidth = gridwidth;
- con.gridheight = gridheight;
- con.anchor = anchor;
- con.fill = fill;
- con.ipadx = ipadx;
- con.ipady = ipady;
- con.weightx=weightx;
- con.weighty=weighty;
- con.insets = new Insets(top,left,bottom,right);
- grid.setConstraints(comp,con);
- }
-
- public void getEntry() {
- Vector res;
- res=rolo.searchIt(1,name_text.getText());
- if (res.size()==0)
- return;
- rolo.current=((Integer)res.elementAt(0)).intValue();
- rolo.showEntry(rolo.current);
- updateScreen();
- }
-
- public void addEntry() {
- if (name_text.getText().length()==0) {
- errorDialog.doit("Add Error","Must set name to add entry");
- return;
- }
- if (strings.getLang()>0) {
- try {
- rolo.processSalary( other_text.getText() );
- } catch (InvalidSalary e) {
- errorDialog.doit("Invalid Salary",e.toString());
- 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();
- updateScreen();
- change_button.setEnabled(true);
- }
-
- 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--;
- updateCurrent();
- }
- public void updateCurrent()
- {
- rolo.updateCurrent();
- updateScreen();
- nameDialog.updateData();
- if (rolo.current== -1)
- change_button.setEnabled(false);
- }
-
- public void clearEntry() {
- rolo.current= -1;
- rolo.updateCurrent();
- updateScreen();
- change_button.setEnabled(false);
- }
-
- 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(strings.title()+" - "+file.getName());
- main_panel.remove(title_label);
- main_panel.add(title_label);
- theframe.pack();
- 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(strings.title()+" - "+file.getName());
- main_panel.remove(title_label);
- main_panel.add(title_label);
- }
- }
- public void saveFile() {
- if (rolo.filename!=null)
- rolo.save();
- else
- saveasFile();
- }
-
- public void clearAll() {
- rolo=new RoloApi();
- nameDialog.updateData();
- searchDialog.clearsearch();
- title_label.setText(strings.title()+" - Untitled");
- main_panel.remove(title_label);
- main_panel.add(title_label);
- clearEntry();
- }
-
- public void goNameDialog() {
- nameDialog.showIt();
- }
-
- public void goSearchDialog() {
- searchDialog.clearsearch();
- searchDialog.showit();
- }
-
- public void updateScreen() {
- change_button.setEnabled(true);
- 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 );
- if (ugly)
- index.setText(" "+(rolo.current+1)+" of "+rolo.getSize()+" ");
- else
- box3.setText(" "+(rolo.current+1)+" of "+rolo.getSize()+" ");
- }
- public RoloApi getApi() { return rolo; }
- }
-