home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 25 / IOPROG_25.ISO / SOFT / JavaS / javastar-eval.exe / data1.cab / Program_Files / examples / namedb1 / namedb.java < prev    next >
Encoding:
Java Source  |  1999-02-11  |  14.4 KB  |  540 lines

  1. /* ****************************************************************
  2. ** @(#)namedb.java    1.6 97/05/23
  3. **
  4. ** Copyright 1997 Sun Microsystems, Inc. All Rights Reserved
  5. **
  6. ** ****************************************************************
  7. */
  8.  
  9. import java.applet.*;
  10. import java.awt.*;
  11. import java.io.*;
  12. import java.util.Vector;
  13.  
  14. public class namedb extends Applet {        
  15.   
  16.   private static final String title="Name Database";
  17.  
  18.   public Frame theframe;
  19.   private RoloApi rolo;
  20.  
  21.   private namePop nameDialog;
  22.   private searchPop searchDialog;
  23.   private FileDialog fileDialog;
  24.   private messagebox errorDialog;
  25.  
  26.   public Button namelist_button;
  27.   public Button search_button;
  28.   public Button add_button;
  29.   public Button change_button;
  30.   public Button remove_button;
  31.   public Button clear_button;
  32.   public Button open_button;
  33.   public Button save_button;
  34.   public Button saveas_button;
  35.   public Button close_button;
  36.   public Button exit_button;
  37.  
  38.   public Label title_label;
  39.   public Label name_label;
  40.   public Label address1_label;
  41.   public Label address2_label;
  42.   public Label phone_label;
  43.   public Label email_label;
  44.   public Label other_label;
  45.  
  46.   public TextField name_text;
  47.   public TextField address1_text;
  48.   public TextField address2_text;
  49.   public TextField phone_text;
  50.   public TextField email_text;
  51.   public TextField other_text;
  52.  
  53.   public void init() {
  54.     
  55.     theframe=new Frame(namedb.title);
  56.  
  57.     rolo = new RoloApi();
  58.  
  59.     nameDialog = new namePop();
  60.     nameDialog.init();
  61.     nameDialog.setParent(this);
  62.  
  63.     searchDialog = new searchPop();
  64.     searchDialog.init();
  65.     searchDialog.setParent(this);
  66.  
  67.     fileDialog=new FileDialog(theframe);
  68.     errorDialog=new messagebox(theframe,true,"error","Must set name to add entry");
  69.     errorDialog.init();
  70.  
  71.     GridBagLayout grid = new GridBagLayout();
  72.     int rowHeights[] = {30,30,30,30,30,30,30,30,30,30};
  73.     int columnWidths[] = {10,25,20,20,20,20,20,20,20};
  74.     double rowWeights[] = {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
  75.     double columnWeights[] = {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
  76.     grid.rowHeights = rowHeights;
  77.     grid.columnWidths = columnWidths;
  78.     grid.rowWeights = rowWeights;
  79.     grid.columnWeights = columnWeights;
  80.     
  81.     title_label = new Label(namedb.title+" - Untitled     ");
  82.     this.add(title_label);
  83.  
  84.     open_button=new Button("Open");
  85.     this.add(open_button);
  86.  
  87.     save_button=new Button("Save");;
  88.     this.add(save_button);
  89.  
  90.     saveas_button=new Button("Save as");
  91.     this.add(saveas_button);
  92.  
  93.     close_button=new Button("Close");
  94.     this.add(close_button);
  95.  
  96.     exit_button=new Button("Exit");
  97.     this.add(exit_button);
  98.  
  99.     add_button=new Button("Add");
  100.     this.add(add_button);
  101.  
  102.     change_button=new Button("Change");
  103.     this.add(change_button);
  104.  
  105.     remove_button=new Button("Remove");
  106.     this.add(remove_button);
  107.  
  108.     clear_button=new Button("Clear");
  109.     this.add(clear_button);
  110.  
  111.     namelist_button=new Button("Names");
  112.     this.add(namelist_button);
  113.  
  114.     search_button=new Button("Search");
  115.     this.add(search_button);
  116.  
  117.  
  118.     name_label = new Label();
  119.     name_label.setText("  Name");
  120.     this.add(name_label);
  121.     
  122.     name_text = new TextField(26);
  123.     this.add(name_text);
  124.     
  125.     address1_label = new Label();
  126.     address1_label.setText("  Address1");
  127.     this.add(address1_label);
  128.     
  129.     address1_text = new TextField(26);
  130.     this.add(address1_text);
  131.     
  132.     address2_label = new Label();
  133.     address2_label.setText("  Address2");
  134.     this.add(address2_label);
  135.     
  136.     address2_text = new TextField(26);
  137.     this.add(address2_text);
  138.  
  139.     phone_label = new Label();
  140.     phone_label.setText("  Phone");
  141.     this.add(phone_label);
  142.     
  143.     phone_text = new TextField(26);
  144.     this.add(phone_text);
  145.     
  146.     email_label = new Label();
  147.     email_label.setText("  Email");
  148.     this.add(email_label);
  149.     
  150.     email_text = new TextField(26);
  151.     this.add(email_text);
  152.     
  153.     other_label = new Label();
  154.     other_label.setText("  Other");
  155.     this.add(other_label);
  156.     
  157.     other_text = new TextField(26);
  158.     this.add(other_text);
  159.     
  160.     // Geometry management
  161.     GridBagConstraints con = new GridBagConstraints();
  162.  
  163.     reset(con);
  164.     con.gridx = 3;
  165.     con.gridy = 0;
  166.     con.gridwidth = 4;
  167.     con.anchor = GridBagConstraints.SOUTHWEST;
  168.     con.fill = GridBagConstraints.NONE;
  169.     grid.setConstraints(title_label, con);
  170.  
  171.     reset(con);
  172.     con.gridx = 3;
  173.     con.gridy = 1;
  174.     con.anchor = GridBagConstraints.WEST;
  175.     con.fill = GridBagConstraints.HORIZONTAL;
  176.     grid.setConstraints(namelist_button, con);
  177.  
  178.     reset(con);
  179.     con.gridx = 5;
  180.     con.gridy = 1;
  181.     con.anchor = GridBagConstraints.WEST;
  182.     con.fill = GridBagConstraints.HORIZONTAL;
  183.     grid.setConstraints(search_button, con);
  184.  
  185.     reset(con);
  186.     con.gridx = 3;
  187.     con.gridy = 8;
  188.     con.anchor = GridBagConstraints.WEST;
  189.     con.fill = GridBagConstraints.HORIZONTAL;
  190.     grid.setConstraints(add_button, con);
  191.  
  192.     reset(con);
  193.     con.gridx = 4;
  194.     con.gridy = 8;
  195.     con.anchor = GridBagConstraints.WEST;
  196.     con.fill = GridBagConstraints.HORIZONTAL;
  197.     grid.setConstraints(change_button, con);
  198.  
  199.     reset(con);
  200.     con.gridx = 5;
  201.     con.gridy = 8;
  202.     con.anchor = GridBagConstraints.WEST;
  203.     con.fill = GridBagConstraints.HORIZONTAL;
  204.     grid.setConstraints(remove_button, con);
  205.  
  206.     reset(con);
  207.     con.gridx = 6;
  208.     con.gridy = 8;
  209.     con.anchor = GridBagConstraints.WEST;
  210.     con.fill = GridBagConstraints.HORIZONTAL;
  211.     grid.setConstraints(clear_button, con);
  212.  
  213.     reset(con);
  214.     con.gridx = 1;
  215.     con.gridy = 2;
  216.     con.anchor = GridBagConstraints.WEST;
  217.     con.fill = GridBagConstraints.HORIZONTAL;
  218.     grid.setConstraints(open_button, con);
  219.  
  220.     reset(con);
  221.     con.gridx = 1;
  222.     con.gridy = 3;
  223.     con.anchor = GridBagConstraints.WEST;
  224.     con.fill = GridBagConstraints.HORIZONTAL;
  225.     grid.setConstraints(save_button, con);
  226.  
  227.     reset(con);
  228.     con.gridx = 1;
  229.     con.gridy = 4;
  230.     con.anchor = GridBagConstraints.WEST;
  231.     con.fill = GridBagConstraints.HORIZONTAL;
  232.     grid.setConstraints(saveas_button, con);
  233.  
  234.     reset(con);
  235.     con.gridx = 1;
  236.     con.gridy = 5;
  237.     con.anchor = GridBagConstraints.WEST;
  238.     con.fill = GridBagConstraints.HORIZONTAL;
  239.     grid.setConstraints(close_button, con);
  240.  
  241.     reset(con);
  242.     con.gridx = 1;
  243.     con.gridy = 7;
  244.     con.anchor = GridBagConstraints.WEST;
  245.     con.fill = GridBagConstraints.HORIZONTAL;
  246.     grid.setConstraints(exit_button, con);
  247.  
  248.     reset(con);
  249.     con.gridx = 2;
  250.     con.gridy = 2;
  251.     con.anchor = GridBagConstraints.WEST;
  252.     con.fill = GridBagConstraints.NONE;
  253.     grid.setConstraints(name_label, con);
  254.     
  255.     reset(con);
  256.     con.gridx = 3;
  257.     con.gridy = 2;
  258.     con.gridwidth = 4;
  259.     con.anchor = GridBagConstraints.WEST;
  260.     con.fill = GridBagConstraints.NONE;
  261.     grid.setConstraints(name_text, con);
  262.     
  263.     reset(con);
  264.     con.gridx = 2;
  265.     con.gridy = 3;
  266.     con.anchor = GridBagConstraints.WEST;
  267.     con.fill = GridBagConstraints.NONE;
  268.     grid.setConstraints(address1_label, con);
  269.     
  270.     reset(con);
  271.     con.gridx = 3;
  272.     con.gridy = 3;
  273.     con.gridwidth = 4;
  274.     con.anchor = GridBagConstraints.WEST;
  275.     con.fill = GridBagConstraints.NONE;
  276.     grid.setConstraints(address1_text, con);
  277.     
  278.     reset(con);
  279.     con.gridx = 2;
  280.     con.gridy = 4;
  281.     con.anchor = GridBagConstraints.WEST;
  282.     con.fill = GridBagConstraints.NONE;
  283.     grid.setConstraints(address2_label, con);
  284.     
  285.     reset(con);
  286.     con.gridx = 3;
  287.     con.gridy = 4;
  288.     con.gridwidth = 4;
  289.     con.anchor = GridBagConstraints.WEST;
  290.     con.fill = GridBagConstraints.NONE;
  291.     grid.setConstraints(address2_text, con);
  292.     
  293.     reset(con);
  294.     con.gridx = 2;
  295.     con.gridy = 5;
  296.     con.anchor = GridBagConstraints.WEST;
  297.     con.fill = GridBagConstraints.NONE;
  298.     grid.setConstraints(phone_label, con);
  299.     
  300.     reset(con);
  301.     con.gridx = 3;
  302.     con.gridy = 5;
  303.     con.gridwidth = 4;
  304.     con.anchor = GridBagConstraints.WEST;
  305.     con.fill = GridBagConstraints.NONE;
  306.     grid.setConstraints(phone_text, con);
  307.     
  308.     reset(con);
  309.     con.gridx = 2;
  310.     con.gridy = 6;
  311.     con.anchor = GridBagConstraints.WEST;
  312.     con.fill = GridBagConstraints.NONE;
  313.     grid.setConstraints(email_label, con);
  314.     
  315.     reset(con);
  316.     con.gridx = 3;
  317.     con.gridy = 6;
  318.     con.gridwidth = 4;
  319.     con.anchor = GridBagConstraints.WEST;
  320.     con.fill = GridBagConstraints.NONE;
  321.     grid.setConstraints(email_text, con);
  322.     
  323.     reset(con);
  324.     con.gridx = 2;
  325.     con.gridy = 7;
  326.     con.anchor = GridBagConstraints.WEST;
  327.     con.fill = GridBagConstraints.NONE;
  328.     grid.setConstraints(other_label, con);
  329.     
  330.     reset(con);
  331.     con.gridx = 3;
  332.     con.gridy = 7;
  333.     con.gridwidth = 4;
  334.     con.anchor = GridBagConstraints.WEST;
  335.     con.fill = GridBagConstraints.NONE;
  336.     grid.setConstraints(other_text, con);
  337.     
  338.     // Resize behavior management and parent heirarchy
  339.     setLayout(grid);
  340.     change_button.disable();
  341.     // Give the application a chance to do its initialization
  342.     super.init();
  343.     if (rolo.filename!=null) {
  344.       title_label.setText(namedb.title+" - "+rolo.filename.getName());
  345.     }
  346.   }
  347.   
  348.   public boolean handleEvent(Event event) {
  349.     if (event.target == name_text && event.id == event.ACTION_EVENT) {
  350.       getEntry();
  351.     } else
  352.     if (event.target == namelist_button    && event.id == event.ACTION_EVENT) {
  353.       goNameDialog();
  354.     } else
  355.     if (event.target == search_button && event.id == event.ACTION_EVENT) {
  356.       goSearchDialog();
  357.     } else
  358.     if (event.target == add_button && event.id == event.ACTION_EVENT) {
  359.       addEntry();
  360.     } else
  361.     if (event.target == change_button && event.id == event.ACTION_EVENT) {
  362.       changeEntry();
  363.     } else
  364.     if (event.target == clear_button && event.id == event.ACTION_EVENT) {
  365.       clearEntry();
  366.     } else
  367.     if (event.target == remove_button && event.id == event.ACTION_EVENT) {
  368.       removeEntry();
  369.     } else
  370.     if (event.target == open_button && event.id == event.ACTION_EVENT) {
  371.       openFile();
  372.     } else
  373.     if (event.target == save_button && event.id == event.ACTION_EVENT) {
  374.       saveFile();
  375.     } else
  376.     if (event.target == saveas_button && event.id == event.ACTION_EVENT) {
  377.       saveasFile();
  378.     } else
  379.     if (event.target == exit_button && event.id == event.ACTION_EVENT) {
  380.       System.exit(0);
  381.     } else 
  382.     if (event.target == close_button && event.id == event.ACTION_EVENT) {
  383.       clearAll();
  384.       clearEntry();
  385.       rolo.filename=null;
  386.     } else
  387.     return super.handleEvent(event);
  388.     return true;
  389.   }
  390.   
  391.   public static void main(String[] args) {
  392.     
  393.     File file;
  394.     if (args.length>0)
  395.       file=new File(args[0]);
  396.     else
  397.       file=null;
  398.  
  399.     Frame f=new Frame(namedb.title);
  400.     namedb win=new namedb();
  401.     win.init();
  402.     win.theframe.add("Center",win);
  403.     win.theframe.pack();
  404.     win.theframe.show();
  405.     if (file!=null && file.exists()) {
  406.       win.rolo.open(file);
  407.       win.title_label.setText(namedb.title+" - "+file.getName());
  408.     }
  409.   }
  410.   private void reset(GridBagConstraints con) {
  411.     con.gridx = GridBagConstraints.RELATIVE;
  412.     con.gridy = GridBagConstraints.RELATIVE;
  413.     con.gridwidth = 1;
  414.     con.gridheight = 1;
  415.     
  416.     con.weightx = 0;
  417.     con.weighty = 0;
  418.     con.anchor = GridBagConstraints.CENTER;
  419.     con.fill = GridBagConstraints.NONE;
  420.     
  421.     con.insets = new Insets(0, 0, 0, 0);
  422.     con.ipadx = 0;
  423.     con.ipady = 0;
  424.   }
  425.   
  426.   public void getEntry() {
  427.     Vector res;
  428.     res=rolo.searchIt(1,name_text.getText());
  429.     rolo.current=((Integer)res.elementAt(0)).intValue();
  430.     rolo.showEntry(rolo.current);
  431.   }
  432.   
  433.   public void addEntry() {
  434.     if (name_text.getText().length()==0) {
  435.       errorDialog.show();
  436.       return;
  437.     }
  438.     rolo.current =
  439.       rolo.addEntry( name_text.getText(), address1_text.getText(), 
  440.              address2_text.getText(), phone_text.getText(),
  441.              email_text.getText(), other_text.getText() );
  442.     nameDialog.updateData();
  443.     change_button.enable();
  444.   }
  445.   
  446.   public void changeEntry() {
  447.     rolo.removeEntry(rolo.current);
  448.     addEntry();
  449.   }
  450.  
  451.   public void removeEntry() {
  452.     if (rolo.current== -1) return;
  453.     rolo.removeEntry(rolo.current);
  454.     if (rolo.current == rolo.getSize())
  455.       rolo.current--;
  456.  
  457.     rolo.updateCurrent();
  458.     updateScreen();
  459.     nameDialog.updateData();
  460.     if (rolo.current== -1)
  461.       change_button.disable();
  462.   }
  463.  
  464.   public void clearEntry() {
  465.     rolo.current= -1;
  466.     rolo.updateCurrent();
  467.     updateScreen();
  468.     change_button.disable();
  469.   }
  470.   
  471.   public void openFile() {
  472.     fileDialog.setTitle("Open");
  473.     fileDialog.setMode(FileDialog.LOAD);
  474.     fileDialog.show();
  475.     String fdir=fileDialog.getDirectory();
  476.     String ffile=fileDialog.getFile();
  477.     if (fdir!=null && ffile!=null) {
  478.       File file=new File(fdir,ffile);
  479.       if (file.isFile()==false)
  480.     return;
  481.       clearEntry();
  482.       rolo.open(file);
  483.       nameDialog.updateData();
  484.       title_label.setText(namedb.title+" - "+file.getName());
  485.       updateScreen();
  486.     }
  487.   }
  488.   public void saveasFile() {
  489.     fileDialog.setTitle("Save As");
  490.     fileDialog.setMode(FileDialog.SAVE);
  491.     fileDialog.show();
  492.     String fdir=fileDialog.getDirectory();
  493.     String ffile=fileDialog.getFile();
  494.     if (fdir!=null && ffile!=null) {
  495.       File file=new File(fdir,ffile);
  496.       rolo.filename=file;
  497.       rolo.save();
  498.       title_label.setText(namedb.title+" - "+file.getName());
  499.     }
  500.   } 
  501.   public void saveFile() {
  502.     if (rolo.filename!=null)
  503.       rolo.save();
  504.     else
  505.       saveasFile();
  506.   }
  507.  
  508.   public void clearAll() {
  509.     rolo=new RoloApi();
  510.     nameDialog.updateData();
  511.     searchDialog.clearsearch();
  512.     title_label.setText(namedb.title+" - Untitled");
  513.     clearEntry();
  514.   }
  515.   
  516.   public void goNameDialog() {
  517.     nameDialog.showIt();
  518.   }
  519.   
  520.   public void goSearchDialog() {
  521.     searchDialog.clearsearch();
  522.     searchDialog.showit();
  523.   }
  524.  
  525.   public void updateScreen() {
  526.     change_button.enable();
  527.     name_text.setText( rolo.recurrent.name );
  528.     address1_text.setText( rolo.recurrent.address1 );
  529.     address2_text.setText( rolo.recurrent.address2 );
  530.     phone_text.setText( rolo.recurrent.phone );
  531.     email_text.setText( rolo.recurrent.email );
  532.     other_text.setText( rolo.recurrent.other );
  533.   }
  534.   public RoloApi getRoloApi() { return rolo; }
  535.  
  536. }
  537.  
  538.  
  539.  
  540.