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

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

Оглавление

Назад

// ==========================================
// DirOpen.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 DirOpen
{
  public static void main(String args[])
  {
    FrameWindow frame;
    frame =  new FrameWindow("View Directory");
    frame.setVisible(true);
  }
}

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

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

  // ============================================
  // actionPerformed
  // ============================================
  public void actionPerformed(ActionEvent e)
  {
    String szSelected = null;
    
    if(e.getSource().equals(miOpen))
    {
      DirDialog d = new DirDialog(
	"Dir: ", this);
      d.show();
      
      szSelected = d.getDirectory();
      if(szSelected != null)
      {
	ta.append(szSelected + "\n");
      }
    }
    
    else if(e.getSource().equals(miExit))
    {
      setVisible(false);
      System.exit(0);
    }
  }
  
  // ============================================
  // 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 DirDialog
// ============================================
class DirDialog extends Dialog
  implements ActionListener, WindowListener,
    ItemListener
{
  List l;
  Button bOK;
  Button bCancel;
  
  String szCurrentDir;
  String szUserHome;
  String szParent;
  
  String szSelected = null;
  String szBoxTitle;
  
  // ============================================
  // DirDialog
  // ============================================
  public DirDialog(String szTitle, Frame parent)
  {
    super(parent, szTitle, true);
    setSize(400, 300);
    szBoxTitle = szTitle;
    this.addWindowListener(this);
    
    l = new List(5, false);
    
    Properties p = System.getProperties();
    szUserHome = p.getProperty("user.home");
    szCurrentDir = szUserHome;
    szSelected = szCurrentDir;
    
    setTitle(szBoxTitle + szSelected);  
    
    updateList(null);
    
    bOK = new Button("OK");
    bCancel = new Button("Cancel");
    
    bOK.addActionListener(this);
    bCancel.addActionListener(this);
    l.addActionListener(this);
    l.addItemListener(this);

    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints c = 
      new GridBagConstraints();
    
    setLayout(gbl);
    
// -----------------------
// ListBox
// -----------------------
	        
    c.anchor = GridBagConstraints.NORTHWEST; 
    c.fill   = GridBagConstraints.BOTH;  
    c.gridheight = 1;
    c.gridwidth  = GridBagConstraints.REMAINDER; 
    c.gridx = GridBagConstraints.RELATIVE; 
    c.gridy = GridBagConstraints.RELATIVE; 
    c.insets = new Insets(10, 10, 10, 10);
    c.weightx = 0;
    c.weighty = 1.0;
    
    gbl.setConstraints(l, c);
    add(l);
    
// -----------------------
// OK
// -----------------------

    c.weightx = 0;
    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);
  }  

  // ============================================
  // getDirectory
  // ============================================
  public String getDirectory()
  {
    return szSelected;
  }

  // ============================================
  // itemStateChanged
  // ============================================
  public void itemStateChanged(ItemEvent e)
  {
    String s = l.getSelectedItem();
    
    if(s.equals(".."))
    {
      szSelected = null;
    }
    else
    {
      szSelected = szCurrentDir + s +
       File.separator;
      
      setTitle(szBoxTitle + szSelected);  
    }  
  }

  // ============================================
  // actionPerformed
  // ============================================
  public void actionPerformed(ActionEvent e)
  {
    if(e.getSource().equals(bOK))
    {
      String s = l.getSelectedItem();
      
      if(s != null)
      {
        if(!s.equals(".."))
        {
	  szSelected = szCurrentDir + s +
	    File.separator;
	}
	else
	  szSelected = null;
      }
      else
	szSelected = null;
      
      setVisible(false);
    }
    
    else if(e.getSource().equals(bCancel))
    {
      szSelected = null;
      setVisible(false);
    }
      
    else if(e.getSource().equals(l))
    {
      String s = l.getSelectedItem();
      updateList(s);
    }
  }
  
  // ============================================
  // updateList
  // ============================================
  void updateList(String szSelected)
  {
    String[] sDirList;
    File f = null;
    
    l.removeAll();
    
    if(szSelected == null)
    {
      f = new File(szCurrentDir);
    }  
    
    else if(szSelected.equals(".."))
    {
      if(szCurrentDir.endsWith("\\"))
      {
	szCurrentDir = 
	  szCurrentDir.substring(0,
	  szCurrentDir.length() - 1);
      }
      	
      f = new File(szCurrentDir);
      szParent = f.getParent();
      szCurrentDir = szParent;
      
      f = new File(szCurrentDir);
      szCurrentDir += File.separator;
    }
    
    else
    {
      szCurrentDir += (szSelected + 
        File.separator);
      f = new File(szCurrentDir);
    }
    
    szParent = f.getParent();
    if(szParent != null)
    {
      l.add("..");
    }
    
    sDirList = f.list(new MaskFilter());
    
    for(int i = 0; i < sDirList.length; i++)
    {
      l.add(sDirList[i]);
    }
  }
  
  // ============================================
  // 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) {}
}

// ============================================
// Class MaskFilter
// ============================================
class MaskFilter
  implements FilenameFilter
{
  // ============================================
  // accept
  // ============================================
  public boolean accept(File f, String name)
  {
      return(new File(f, name).isDirectory());
  }
}

[Назад]