home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1998 November
/
Chip_1998-11_cd.bin
/
tema
/
Cafe
/
jfc.bin
/
MotifDirectoryPaneUI.java
< prev
next >
Wrap
Text File
|
1998-02-26
|
11KB
|
357 lines
/*
* @(#)MotifDirectoryPaneUI.java 1.5 98/02/04
*
* Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Sun.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
* SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
* SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
* THIS SOFTWARE OR ITS DERIVATIVES.
*
*/
package com.sun.java.swing.plaf.motif;
import com.sun.java.swing.*;
import com.sun.java.swing.preview.*;
import com.sun.java.swing.preview.JDirectoryPane;
import com.sun.java.swing.plaf.basic.BasicListCellRenderer;
import com.sun.java.swing.event.*;
import com.sun.java.swing.plaf.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.File;
import java.util.*;
/**
* Motif L&F implementation of DirectoryPaneUI.
*
* @version 1.5 02/04/98
*
* @author Jeff Dinkins
*/
public class MotifDirectoryPaneUI extends DirectoryPaneUI implements ActionListener {
protected JDirectoryPane directoryPane;
protected JList fileList;
protected JList folderList;
protected JTextField pathField;
protected JTextField filterField;
protected JTextField fileField;
private static final Dimension hstrut10 = new Dimension(10, 1);
private static final Dimension vstrut10 = new Dimension(1, 10);
private static final Insets insets = new Insets(10, 10, 10, 10);
private static Dimension prefListSize = new Dimension(150, 75);
//
// ComponentUI Interface Implementation methods
//
public static ComponentUI createUI(JComponent c) {
return new MotifDirectoryPaneUI((JDirectoryPane)c);
}
public MotifDirectoryPaneUI(JDirectoryPane b) {
}
/**
* Create this UI's lists.
*/
public void installUI(JComponent c) {
directoryPane = (JDirectoryPane) c;
directoryPane.setLayout(new BorderLayout());
JPanel interior = new JPanel() {
public Insets getInsets() {
return insets;
}
};
interior.setLayout(new BoxLayout(interior, BoxLayout.Y_AXIS));
directoryPane.add(interior, BorderLayout.CENTER);
// *** Path
JLabel l = new JLabel("Enter path or folder name:");
interior.add(l);
pathField = new JTextField("/home/bob");
File currentDirectory = directoryPane.getModel().getCurrentDirectory();
String curDirName = currentDirectory.getPath();
pathField.setText(curDirName);
pathField.setAlignmentX(JComponent.LEFT_ALIGNMENT);
// Change to folder on return
pathField.addActionListener(this);
interior.add(pathField);
interior.add(Box.createRigidArea(vstrut10));
// *** Center Panel
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.X_AXIS));
centerPanel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
centerPanel.setAlignmentY(JComponent.TOP_ALIGNMENT);
interior.add(centerPanel);
interior.add(Box.createRigidArea(vstrut10));
// *** Left Panel - Filter & folderList
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
leftPanel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
leftPanel.setAlignmentY(JComponent.TOP_ALIGNMENT);
centerPanel.add(leftPanel);
centerPanel.add(Box.createRigidArea(hstrut10));
l = new JLabel("Filter");
leftPanel.add(l);
filterField = new JTextField("*");
filterField.setEnabled(false);
filterField.setAlignmentX(JComponent.LEFT_ALIGNMENT);
leftPanel.add(filterField);
leftPanel.add(Box.createRigidArea(vstrut10));
l = new JLabel("Folders");
leftPanel.add(l);
folderList = new JList();
folderList.setCellRenderer(new LabelCellRenderer(true));
folderList.setModel(new FilteredDirectoryModel(directoryPane.getModel(), true));
folderList.setVisibleRowCount(10);
// Open folder on double click
folderList.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int index = folderList.getSelectedIndex();
File file = null;
if(index == 0) {
// PENDING(jeff): get the up level directory name
pathField.setText("up one");
File curDir = directoryPane.getModel().getCurrentDirectory();
pathField.setText(curDir.getParent());
if (e.getClickCount() == 2) {
directoryPane.goUp();
// clear the list selections
folderList.clearSelection();
fileList.clearSelection();
}
} else {
file = (File) folderList.getSelectedValue();
if(file != null) {
pathField.setText(file.getPath());
if (e.getClickCount() == 2) {
Vector allfiles = directoryPane.getModel().getTypedFiles();
index = allfiles.indexOf(file);
ListSelectionModel selectionModel = directoryPane.getListSelectionModel();
selectionModel.setSelectionInterval(index, index);
directoryPane.performDoubleClick();
// clear the list selections
folderList.clearSelection();
fileList.clearSelection();
}
}
}
fileField.setText("");
}
});
JScrollPane folderScroller = new JScrollPane(folderList);
folderScroller.setAlignmentX(JComponent.LEFT_ALIGNMENT);
folderScroller.setPreferredSize(prefListSize);
leftPanel.add(folderScroller);
// *** Right Panel - filesList
JPanel rightPanel = new JPanel() {
public Dimension getMaximumSize() {
return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);
}
};
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
rightPanel.setAlignmentX(JComponent.LEFT_ALIGNMENT);
rightPanel.setAlignmentY(JComponent.TOP_ALIGNMENT);
centerPanel.add(rightPanel);
l = new JLabel("Files");
rightPanel.add(l);
fileList = new JList();
fileList.setCellRenderer(new LabelCellRenderer(false));
fileList.setModel(new FilteredDirectoryModel(directoryPane.getModel(), false));
fileList.setVisibleRowCount(10);
// Open file on double click
fileList.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
File file = (File) fileList.getSelectedValue();
if(file != null) {
fileField.setText(file.getPath());
Vector allfiles = directoryPane.getModel().getTypedFiles();
int index = allfiles.indexOf(file);
ListSelectionModel selectionModel = directoryPane.getListSelectionModel();
selectionModel.setSelectionInterval(index, index);
}
if (e.getClickCount() == 2) {
// PENDING(jeff) - open file
/*
Vector allfiles = directoryPane.getModel().getTypedFiles();
index = allfiles.indexOf(file);
ListSelectionModel selectionModel = directoryPane.getListSelectionModel();
selectionModel.setSelectionInterval(index, index);
*/
}
}
});
JScrollPane fileScroller = new JScrollPane(fileList);
fileScroller.setPreferredSize(prefListSize);
fileScroller.setAlignmentX(JComponent.LEFT_ALIGNMENT);
rightPanel.add(fileScroller);
// filename Field
l = new JLabel("Enter file name:");
interior.add(l);
fileField = new JTextField();
fileField.setAlignmentX(JComponent.LEFT_ALIGNMENT);
fileField.setEnabled(false);
interior.add(fileField);
}
public void actionPerformed(ActionEvent e) {
JComponent source = (JComponent) e.getSource();
if(source == pathField) {
directoryPane.setCurrentDirectory(new File(pathField.getText()));
} else if (source == fileField) {
// PENDING(jeff) - set the current file to the
// file specified by the text field
}
}
static class LabelCellRenderer extends BasicListCellRenderer {
boolean folderView;
LabelCellRenderer(boolean folderView) {
this.folderView = folderView;
}
public Component getListCellRendererComponent(JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if(folderView && index == 0) {
setName((String) value);
} else {
TypedFile file = (TypedFile)value;
String name = file.getName();
if (name.equals("")) name = file.getPath();
setText(name);
}
return this;
}
}
class FilteredDirectoryModel implements ListModel, ListDataListener {
private EventListenerList listenerList = new EventListenerList();
private DirectoryModel directoryModel;
private Vector files;
private boolean filterForFolders;
FilteredDirectoryModel(DirectoryModel directoryModel, boolean filterForFolders) {
this.directoryModel = directoryModel;
this.filterForFolders = filterForFolders;
directoryModel.addListDataListener(this);
updateFileList();
}
public void updateFileList() {
Vector allfiles = directoryModel.getTypedFiles();
if(allfiles != null) {
files = new Vector();
if(filterForFolders) {
files.addElement("..");
}
for (int i = 0; i < allfiles.size(); i++) {
File f = (File) allfiles.elementAt(i);
if(filterForFolders && f.isDirectory()) {
files.addElement(f);
} else if(!filterForFolders && !f.isDirectory()) {
files.addElement(f);
}
}
}
}
public void contentsChanged(ListDataEvent e) {
updateFileList();
fireContentsChanged();
}
public void intervalAdded(ListDataEvent e) {
}
public void intervalRemoved(ListDataEvent e) {
}
public int getSize() {
return files.size();
}
public Object getElementAt(int index) {
return files.elementAt(index);
}
public void addListDataListener(ListDataListener l) {
listenerList.add(ListDataListener.class, l);
}
public void removeListDataListener(ListDataListener l) {
listenerList.remove(ListDataListener.class, l);
}
/*
* Notify all listeners that have registered interest for
* notification on this event type. The event instance
* is lazily created using the parameters passed into
* the fire method.
* @see EventListenerList
*/
protected void fireContentsChanged() {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
ListDataEvent e = null;
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==ListDataListener.class) {
// Lazily create the event:
if (e == null) {
e = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED,
0, getSize()-1);
}
((ListDataListener)listeners[i+1]).contentsChanged(e);
}
}
}
}
}