home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / jfc.bin / MotifDirectoryPaneUI.java < prev    next >
Text File  |  1998-02-26  |  11KB  |  357 lines

  1. /*
  2.  * @(#)MotifDirectoryPaneUI.java    1.5 98/02/04
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.motif;
  22.  
  23. import com.sun.java.swing.*;
  24. import com.sun.java.swing.preview.*;
  25. import com.sun.java.swing.preview.JDirectoryPane;
  26. import com.sun.java.swing.plaf.basic.BasicListCellRenderer;
  27. import com.sun.java.swing.event.*;
  28. import com.sun.java.swing.plaf.*;
  29. import java.awt.*;
  30. import java.awt.event.*;
  31. import java.beans.*;
  32. import java.io.File;
  33. import java.util.*;
  34.  
  35. /**
  36.  * Motif L&F implementation of DirectoryPaneUI.  
  37.  *
  38.  * @version 1.5 02/04/98
  39.  *
  40.  * @author Jeff Dinkins
  41.  */
  42. public class MotifDirectoryPaneUI extends DirectoryPaneUI implements ActionListener {
  43.  
  44.     protected JDirectoryPane directoryPane;
  45.  
  46.     protected JList fileList;
  47.     protected JList folderList;
  48.  
  49.     protected JTextField pathField;
  50.     protected JTextField filterField;
  51.     protected JTextField fileField;
  52.  
  53.     private static final Dimension hstrut10 = new Dimension(10, 1);
  54.     private static final Dimension vstrut10 = new Dimension(1, 10);
  55.  
  56.     private static final Insets insets = new Insets(10, 10, 10, 10);
  57.  
  58.     private static Dimension prefListSize = new Dimension(150, 75);
  59.  
  60.     //
  61.     // ComponentUI Interface Implementation methods
  62.     //
  63.     public static ComponentUI createUI(JComponent c) {
  64.         return new MotifDirectoryPaneUI((JDirectoryPane)c);
  65.     }
  66.  
  67.     public MotifDirectoryPaneUI(JDirectoryPane b) {
  68.     }
  69.  
  70.     /**
  71.      * Create this UI's lists.
  72.      */
  73.     public void installUI(JComponent c) {
  74.     directoryPane = (JDirectoryPane) c;
  75.     directoryPane.setLayout(new BorderLayout());
  76.  
  77.     JPanel interior = new JPanel() {
  78.         public Insets getInsets() {
  79.         return insets;
  80.         }
  81.     };
  82.     interior.setLayout(new BoxLayout(interior, BoxLayout.Y_AXIS));
  83.     directoryPane.add(interior, BorderLayout.CENTER);
  84.  
  85.     // *** Path
  86.     JLabel l = new JLabel("Enter path or folder name:");
  87.     interior.add(l);
  88.  
  89.     pathField = new JTextField("/home/bob");
  90.     File currentDirectory = directoryPane.getModel().getCurrentDirectory();
  91.     String curDirName = currentDirectory.getPath();
  92.     pathField.setText(curDirName);
  93.  
  94.     pathField.setAlignmentX(JComponent.LEFT_ALIGNMENT);
  95.     // Change to folder on return
  96.     pathField.addActionListener(this);
  97.     interior.add(pathField);
  98.  
  99.     interior.add(Box.createRigidArea(vstrut10));
  100.  
  101.     // *** Center Panel
  102.     JPanel centerPanel = new JPanel();
  103.     centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.X_AXIS));
  104.     centerPanel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
  105.     centerPanel.setAlignmentY(JComponent.TOP_ALIGNMENT);
  106.     interior.add(centerPanel);
  107.     interior.add(Box.createRigidArea(vstrut10));
  108.  
  109.     // *** Left Panel - Filter & folderList
  110.     JPanel leftPanel = new JPanel();
  111.     leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
  112.     leftPanel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
  113.     leftPanel.setAlignmentY(JComponent.TOP_ALIGNMENT);
  114.     centerPanel.add(leftPanel);
  115.     centerPanel.add(Box.createRigidArea(hstrut10));
  116.  
  117.     l = new JLabel("Filter");
  118.     leftPanel.add(l);
  119.  
  120.     filterField = new JTextField("*");
  121.     filterField.setEnabled(false);
  122.     filterField.setAlignmentX(JComponent.LEFT_ALIGNMENT);
  123.     leftPanel.add(filterField);
  124.     leftPanel.add(Box.createRigidArea(vstrut10));
  125.  
  126.     l = new JLabel("Folders");
  127.     leftPanel.add(l);
  128.     
  129.     folderList = new JList();
  130.     folderList.setCellRenderer(new LabelCellRenderer(true));
  131.     folderList.setModel(new FilteredDirectoryModel(directoryPane.getModel(), true));
  132.     folderList.setVisibleRowCount(10);
  133.  
  134.     // Open folder on double click
  135.     folderList.addMouseListener(new MouseAdapter() {
  136.         public void mouseClicked(MouseEvent e) {
  137.         int index = folderList.getSelectedIndex();
  138.         File file = null;
  139.         if(index == 0) {
  140.             // PENDING(jeff): get the up level directory name
  141.             pathField.setText("up one");
  142.             File curDir = directoryPane.getModel().getCurrentDirectory();
  143.             pathField.setText(curDir.getParent());
  144.             if (e.getClickCount() == 2) {
  145.             directoryPane.goUp();
  146.             // clear the list selections
  147.             folderList.clearSelection();
  148.             fileList.clearSelection();
  149.             }
  150.         } else {
  151.             file = (File) folderList.getSelectedValue();
  152.             if(file != null) {
  153.             pathField.setText(file.getPath());
  154.             if (e.getClickCount() == 2) {
  155.                 Vector allfiles = directoryPane.getModel().getTypedFiles();
  156.                 index = allfiles.indexOf(file);
  157.                 ListSelectionModel selectionModel = directoryPane.getListSelectionModel();
  158.                 selectionModel.setSelectionInterval(index, index);
  159.                 directoryPane.performDoubleClick();
  160.                 
  161.                 // clear the list selections
  162.                 folderList.clearSelection();
  163.                 fileList.clearSelection();
  164.             }
  165.             }
  166.         }
  167.         fileField.setText("");
  168.         }
  169.     });
  170.  
  171.     JScrollPane folderScroller = new JScrollPane(folderList);
  172.     folderScroller.setAlignmentX(JComponent.LEFT_ALIGNMENT);
  173.     folderScroller.setPreferredSize(prefListSize);
  174.     leftPanel.add(folderScroller);
  175.  
  176.     // *** Right Panel - filesList
  177.     JPanel rightPanel = new JPanel() {
  178.         public Dimension getMaximumSize() {
  179.         return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);
  180.         }
  181.     };
  182.     rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
  183.     rightPanel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
  184.     rightPanel.setAlignmentY(JComponent.TOP_ALIGNMENT);
  185.     centerPanel.add(rightPanel);
  186.  
  187.  
  188.     l = new JLabel("Files");
  189.     rightPanel.add(l);
  190.  
  191.     fileList = new JList();
  192.     fileList.setCellRenderer(new LabelCellRenderer(false));
  193.     fileList.setModel(new FilteredDirectoryModel(directoryPane.getModel(), false));
  194.     fileList.setVisibleRowCount(10);
  195.  
  196.     // Open file on double click
  197.     fileList.addMouseListener(new MouseAdapter() {
  198.         public void mouseClicked(MouseEvent e) {
  199.         File file = (File) fileList.getSelectedValue();
  200.         if(file != null) {
  201.             fileField.setText(file.getPath());
  202.             Vector allfiles = directoryPane.getModel().getTypedFiles();
  203.             int index = allfiles.indexOf(file);
  204.             ListSelectionModel selectionModel = directoryPane.getListSelectionModel();
  205.             selectionModel.setSelectionInterval(index, index);
  206.         }
  207.  
  208.         if (e.getClickCount() == 2) {
  209.             // PENDING(jeff) - open file
  210.             /*
  211.             Vector allfiles = directoryPane.getModel().getTypedFiles();
  212.             index = allfiles.indexOf(file);
  213.             ListSelectionModel selectionModel = directoryPane.getListSelectionModel();
  214.             selectionModel.setSelectionInterval(index, index);
  215.             */
  216.         } 
  217.         }
  218.     });
  219.  
  220.     JScrollPane fileScroller = new JScrollPane(fileList);
  221.     fileScroller.setPreferredSize(prefListSize);
  222.      fileScroller.setAlignmentX(JComponent.LEFT_ALIGNMENT);
  223.     rightPanel.add(fileScroller);
  224.  
  225.     // filename Field
  226.     l = new JLabel("Enter file name:");
  227.     interior.add(l);
  228.  
  229.     fileField = new JTextField();
  230.     fileField.setAlignmentX(JComponent.LEFT_ALIGNMENT);
  231.     fileField.setEnabled(false);
  232.     interior.add(fileField);
  233.  
  234.     }   
  235.  
  236.     public void actionPerformed(ActionEvent e) {
  237.     JComponent source = (JComponent) e.getSource();
  238.     if(source == pathField) {
  239.         directoryPane.setCurrentDirectory(new File(pathField.getText()));
  240.     } else if (source == fileField) {
  241.         // PENDING(jeff) - set the current file to the
  242.         // file specified by the text field
  243.     }
  244.     }
  245.  
  246.     static class LabelCellRenderer extends BasicListCellRenderer  {
  247.  
  248.     boolean folderView;
  249.     
  250.     LabelCellRenderer(boolean folderView) {
  251.         this.folderView = folderView;
  252.     }
  253.  
  254.     public Component getListCellRendererComponent(JList list, Object value, int index,
  255.                               boolean isSelected, boolean cellHasFocus) {
  256.  
  257.         super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
  258.         if(folderView && index == 0) {
  259.         setName((String) value);
  260.         } else {
  261.         TypedFile file = (TypedFile)value; 
  262.         String name = file.getName();
  263.         if (name.equals("")) name = file.getPath();
  264.         setText(name);
  265.         }
  266.         return this;
  267.     }
  268.     }
  269.  
  270.     class FilteredDirectoryModel implements ListModel, ListDataListener {
  271.  
  272.     private EventListenerList listenerList = new EventListenerList();
  273.     private DirectoryModel directoryModel;
  274.  
  275.     private Vector files;
  276.     private boolean filterForFolders;
  277.  
  278.     FilteredDirectoryModel(DirectoryModel directoryModel, boolean filterForFolders) {
  279.         this.directoryModel = directoryModel;
  280.         this.filterForFolders = filterForFolders;
  281.         directoryModel.addListDataListener(this);
  282.         updateFileList();
  283.     }
  284.  
  285.     public void updateFileList() {
  286.         Vector allfiles = directoryModel.getTypedFiles();
  287.         if(allfiles != null) {
  288.         files = new Vector();
  289.         if(filterForFolders) {
  290.             files.addElement("..");
  291.         }
  292.         for (int i = 0; i < allfiles.size(); i++) {
  293.             File f = (File) allfiles.elementAt(i);
  294.             if(filterForFolders && f.isDirectory()) {
  295.             files.addElement(f);
  296.             } else if(!filterForFolders && !f.isDirectory()) {
  297.             files.addElement(f);
  298.             }
  299.         }
  300.         }
  301.     }
  302.  
  303.     public void contentsChanged(ListDataEvent e) {
  304.         updateFileList();
  305.         fireContentsChanged();
  306.     }
  307.  
  308.     public void intervalAdded(ListDataEvent e) {
  309.     }
  310.  
  311.     public void intervalRemoved(ListDataEvent e) {
  312.     }
  313.  
  314.     public int getSize() {
  315.         return files.size();
  316.     }
  317.  
  318.     public Object getElementAt(int index) {
  319.         return files.elementAt(index);
  320.     }
  321.     
  322.     public void addListDataListener(ListDataListener l) {
  323.         listenerList.add(ListDataListener.class, l);
  324.     }
  325.     
  326.     public void removeListDataListener(ListDataListener l) {
  327.         listenerList.remove(ListDataListener.class, l);
  328.     }
  329.     
  330.     /*
  331.      * Notify all listeners that have registered interest for
  332.      * notification on this event type.  The event instance 
  333.      * is lazily created using the parameters passed into 
  334.      * the fire method.
  335.      * @see EventListenerList
  336.      */
  337.     protected void fireContentsChanged() {
  338.         // Guaranteed to return a non-null array
  339.         Object[] listeners = listenerList.getListenerList();
  340.         ListDataEvent e = null;
  341.         // Process the listeners last to first, notifying
  342.         // those that are interested in this event
  343.         for (int i = listeners.length-2; i>=0; i-=2) {
  344.         if (listeners[i]==ListDataListener.class) {
  345.             // Lazily create the event:
  346.             if (e == null) {
  347.             e = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED,
  348.                           0, getSize()-1);
  349.             }
  350.             ((ListDataListener)listeners[i+1]).contentsChanged(e);
  351.         }           
  352.         }
  353.     }
  354.     
  355.     }
  356. }
  357.