Электронная библиотека книг Александра Фролова и Григория Фролова.
Shop2You.ru Создайте свой интернет-магазин
Библиотека
Братьев
Фроловых
[Назад]

Исходный текст программы SaveProperty.java

Оглавление

Назад

// ==========================================
// SaveProperty.java
// (C) Alexandr Frolov, 1998
// E-mail: frolov@glasnet.ru
// Web:    http://www.glasnet.ru/~frolov 
// ==========================================
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

public class SaveProperty
{
  public static void main(String args[])
  {
    FrameWindow frame;
    frame =  new FrameWindow(
      "Save and restore properties");
    frame.setVisible(true);
  }
}

// =======================================
// Class FrameWindow
// =======================================
class FrameWindow extends Frame
  implements ActionListener, WindowListener
{
  TextArea ta;
  MenuBar mb;
  Menu mFile;
  Menu mProperties;
  
  MenuItem miLoad;
  MenuItem miAddItem;
  MenuItem miSave;
  MenuItem miExit;
  
  FileDialog fdlg;
  
  // ============================================
  // FrameWindow
  // ============================================
  public FrameWindow(String szTitle)
  {
    super(szTitle);
    setSize(400, 300);
    
    mb = new MenuBar();
    mFile = new Menu("File");
    mProperties = new Menu("Properties");
    
    miLoad = new MenuItem("Load...");
    mProperties.add(miLoad);
    
    miSave = new MenuItem("Save ...");
    mProperties.add(miSave);
    
    mProperties.add("-");
    
    miAddItem = new MenuItem("Add Item...");
    mProperties.add(miAddItem);
    
    miExit = new MenuItem("Exit");
    mFile.add(miExit);
    
    mb.add(mFile);
    mb.add(mProperties);
    setMenuBar(mb);
    
    miLoad.addActionListener(this);
    miAddItem.addActionListener(this);
    miSave.addActionListener(this);
    miExit.addActionListener(this);
    
    this.addWindowListener(this);
    
    ta = new TextArea(10, 30);

    setLayout(new BorderLayout());
    add("Center", ta);
    
    viewCurrentProperties();
  }

  // ============================================
  // actionPerformed
  // ============================================
  public void actionPerformed(ActionEvent e)
  {
    if(e.getSource().equals(miSave))
    {
      fdlg = new FileDialog(this, 
	"Save property file as...", 
	FileDialog.SAVE);
	
      fdlg.setFile("!JavaProperties.jpr");	
      fdlg.show();
      
      try
      {  
        Properties p = System.getProperties();
	
	FileOutputStream fos = 
	  new FileOutputStream(
	    fdlg.getDirectory() + fdlg.getFile());
	    
	p.save(fos, "Current properties");
	fos.close();
      }
      catch(Exception ex)
      {
	System.out.println(ex.toString());
      }
    }
    
    else if(e.getSource().equals(miLoad))
    {
      fdlg = new FileDialog(this, 
	"Open property file", 
	FileDialog.LOAD);
	
      fdlg.setFile("*.jpr");	
      fdlg.show();
      
      try
      {  
        Properties p = System.getProperties();
	
	FileInputStream fis = 
	  new FileInputStream(
	    fdlg.getDirectory() + fdlg.getFile());
	    
	p.load(fis);
	fis.close();
      }
      catch(Exception ex)
      {
	System.out.println(ex.toString());
      }
      
      viewCurrentProperties();
    }
    
    else if(e.getSource().equals(miAddItem))
    {
      NewItemDialog nid = 
	new NewItemDialog("Add new item", this);
	
      nid.show();
      String szNewItem = nid.getItem();
      
      if(szNewItem != null)
      {
        try
	{  
	  Properties p = System.getProperties();
      
          ByteArrayOutputStream os = 
	    new ByteArrayOutputStream();
          DataOutputStream dos = 
	    new DataOutputStream(os);
	
	  dos.writeBytes(szNewItem);
	
          byte[] buf;
	  buf = os.toByteArray();
	  os.close();
      
	  ByteArrayInputStream is = 
	    new ByteArrayInputStream(buf);
	
	  p.load(is);	
        }
        catch(Exception ex)
	{
	  System.out.println(ex.toString());
	}
	
        viewCurrentProperties();
      }	 
    }
    
    else if(e.getSource().equals(miExit))
    {
      setVisible(false);
      System.exit(0);
    }
  }
  
  // ============================================
  // viewCurrentProperties
  // ============================================
  void viewCurrentProperties()
  {
    ta.selectAll();
    ta.replaceRange("", 0, ta.getSelectionEnd());
      
    try
    {  
      Properties p = System.getProperties();
      Enumeration en = p.propertyNames();
      String s;
	
      while(en.hasMoreElements())
      {
	s = (String)en.nextElement();
        ta.append(s + "=" +
	  p.getProperty(s) + "\n");
      }	
    }
    catch(Exception ex)
    {
      System.out.println(ex.toString());
    }
  }
  
  // ============================================
  // windowClosing
  // ============================================
  public void windowClosing(WindowEvent e)
  {
    setVisible(false);
    System.exit(0);
  }
  
  public void windowOpened(WindowEvent e) {}
  public void windowClosed(WindowEvent e) {}
  public void windowIconified(WindowEvent e) {}
  public void windowDeiconified(WindowEvent e) {}
  public void windowActivated(WindowEvent e) {}
  public void windowDeactivated(WindowEvent e) {}
}

// ============================================
// Class NewItemDialog
// ============================================
class NewItemDialog extends Dialog
  implements ActionListener, WindowListener
{
  TextField tfName;
  TextField tfValue;
  Label lbName;
  Label lbValue;
  
  Button bOK;
  Button bCancel;
  
  String szNewItem = null;
  
  // ============================================
  // NewItemDialog
  // ============================================
  public NewItemDialog(String szTitle, 
    Frame parent)
  {
    super(parent, szTitle, true);
    setSize(400, 300);
    this.addWindowListener(this);
    
    tfName = new TextField(20);
    tfValue = new TextField(20);
    
    lbName = new Label("Name:");
    lbValue = new Label("Value:");
    
    bOK = new Button("OK");
    bCancel = new Button("Cancel");
    
    bOK.addActionListener(this);
    bCancel.addActionListener(this);

    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints c = 
      new GridBagConstraints();
    
    setLayout(gbl);
    
// -----------------------
// Name
// -----------------------

    c.anchor = GridBagConstraints.NORTHWEST; 
    c.fill   = GridBagConstraints.NONE;  
    c.gridheight = 1;
    c.gridwidth  = 1;
    c.gridx = GridBagConstraints.RELATIVE; 
    c.gridy = GridBagConstraints.RELATIVE; 
    c.insets = new Insets(10, 10, 10, 10);
    
    gbl.setConstraints(lbName, c);
    add(lbName);
    
    c.fill = GridBagConstraints.HORIZONTAL;  
    c.gridwidth  = GridBagConstraints.REMAINDER; 
    
    gbl.setConstraints(tfName, c);
    add(tfName);
    
// -----------------------
// Value
// -----------------------

    c.fill   = GridBagConstraints.NONE;  
    c.gridwidth  = 1;
    
    gbl.setConstraints(lbValue, c);
    add(lbValue);
    
    c.fill   = GridBagConstraints.HORIZONTAL;  
    c.gridwidth  = GridBagConstraints.REMAINDER; 
    c.weighty = 1.0;
    
    gbl.setConstraints(tfValue, c);
    add(tfValue);
    
// -----------------------
// OK
// -----------------------

    c.weighty = 0;
    c.fill   = GridBagConstraints.NONE;  
    c.gridwidth  = 1;
    c.ipadx = 32;
    
    gbl.setConstraints(bOK, c);
    add(bOK);
    
// -----------------------
// Cancel
// -----------------------

    c.gridwidth  = GridBagConstraints.REMAINDER; 
    c.ipadx = 10;
    c.weightx = 1.0;
    
    gbl.setConstraints(bCancel, c);
    add(bCancel);
  }  

  // ============================================
  // actionPerformed
  // ============================================
  public void actionPerformed(ActionEvent e)
  {
    if(e.getSource().equals(bOK))
    {
      String szName = tfName.getText();
      String szValue = tfValue.getText();
      
      if(!szName.equals("") 
	&& !szValue.equals(""))
        szNewItem = szName + "=" + szValue;
      else	
        szNewItem = null;
 
      setVisible(false);
    }
    
    else if(e.getSource().equals(bCancel))
    {
      szNewItem = null;
      setVisible(false);
    }
  }
  
  // ============================================
  // getItem
  // ============================================
  public String getItem()
  {
    return szNewItem;
  }
  
  // ============================================
  // windowClosing
  // ============================================
  public void windowClosing(WindowEvent e)
  {
    setVisible(false);
  }
  
  public void windowOpened(WindowEvent e) {}
  public void windowClosed(WindowEvent e) {}
  public void windowIconified(WindowEvent e) {}
  public void windowDeiconified(WindowEvent e) {}
  public void windowActivated(WindowEvent e) {}
  public void windowDeactivated(WindowEvent e) {}
}

[Назад]