/*
* $Header: /home/cvs/jakarta-slide/src/contrib/webdavgui/src/java/org/apache/webdav/ui/filechooser/plafui/SPMetalFileChooserUI.java,v 1.2 2001/07/24 02:57:33 msmith Exp $
* $Revision: 1.2 $
* $Date: 2001/07/24 02:57:33 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.webdav.ui.filechooser.plafui;
import java.io.File;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import javax.swing.filechooser.FileFilter;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.metal.MetalFileChooserUI;
import org.apache.webdav.ui.filechooser.dir.*;
import org.apache.webdav.ui.filechooser.*;
public class SPMetalFileChooserUI extends MetalFileChooserUI {
private Action approveAction = new ApproveAction();
private Action gotoHomeAction = new GotoHomeAction();
private Action gotoParentDirectoryAction = new GotoParentDirectoryAction();
public SPMetalFileChooserUI(JFileChooser filechooser) {
super(filechooser);
}
//
// ComponentUI Interface Implementation methods
//
public static ComponentUI createUI(JComponent c) {
return new SPMetalFileChooserUI((JFileChooser)c);
}
public Action getGoHomeAction() {
return gotoHomeAction;
}
public Action getApproveSelectionAction() {
return approveAction;
}
public Action getChangeToParentDirectoryAction() {
return gotoParentDirectoryAction;
}
protected MouseListener createDoubleClickListener(JFileChooser fc,
JList list) {
list.setCellRenderer(new MetalFileRenderer());
return new MetalDoubleClickListener(list);
}
protected class MetalDoubleClickListener extends MouseAdapter {
protected JList list;
public MetalDoubleClickListener(JList list) {
this.list = list;
}
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
int index = list.locationToIndex(e.getPoint());
if(index >= 0) {
getSPFileChooser().setCursor(
Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
SPFile f = (SPFile) list.getModel().getElementAt(index);
if (f.isTraversable()) {
list.clearSelection();
f.refreshContent();
getSPFileChooser().setCurrentDirectory(f);
} else {
getSPFileChooser().approveSelection();
}
getSPFileChooser().setCursor(Cursor.getDefaultCursor());
}
}
}//mouseClicked()
} // MetalDoubleClickListener inner class
/**
* Responds to an Open or Save request
*/
protected class ApproveAction extends AbstractAction {
public void actionPerformed(ActionEvent e) {
SPFileChooser chooser = getSPFileChooser();
String filename = getFileName();
FileSystemView fs = chooser.getFileSystemView();
SPFile dir = (SPFile) chooser.getCurrentDirectory();
if(filename == null || filename.equals("")) {
// no file selected, multiple selection off,
// therefore cancel the approve action
return;
}
if(filename != null) {
// Remove whitespace from beginning and end of filename
filename = filename.trim();
}
if(filename != null && !filename.equals("")) {
FileFilter fFilter = chooser.getFileFilter();
if ( (chooser.getDialogType() == JFileChooser.SAVE_DIALOG)
&& (chooser.isFilterNaming())
&& (fFilter instanceof SPFileFilter) ) {
SPFileFilter filter = (SPFileFilter) fFilter;
int dotIndex = filename.indexOf(".");
if (dotIndex < 0) {
filename = filename.concat(
(String) filter.getFileExtension(0));
}
}
// check for directory change action
SPFile selectedFile = (SPFile) fs.createFileObject(
chooser.getCurrentDirectory(), filename);
boolean isDir = selectedFile.isDirectory();
boolean isDirSelEnabled = chooser.isDirectorySelectionEnabled();
boolean isFileSelEnabled = chooser.isFileSelectionEnabled();
if(isDir && !isDirSelEnabled) {
chooser.setCurrentDirectory(selectedFile);
} else if ((!isDir && isFileSelEnabled)
|| (isDir && isDirSelEnabled)) {
chooser.setSelectedFile(selectedFile);
chooser.approveSelection();
}
return;
}
chooser.setSelectedFile(null);
chooser.setSelectedFiles(null);
chooser.cancelSelection();
}//actionPerformed();
}//ApproveAction inner class
/**
* Acts on the "home" key event or equivalent event.
*/
protected class GotoHomeAction extends AbstractAction {
protected GotoHomeAction() {
super("Go Home");
}
public void actionPerformed(ActionEvent e) {
getSPFileChooser().setCursor(
Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
SPFile homeDir = getSPFileChooser().getHomeDirectory();
homeDir.refreshContent();
getSPFileChooser().setCurrentDirectory(homeDir);
getSPFileChooser().setCursor(Cursor.getDefaultCursor());
}
}//GotoHomeAction inner class
protected class GotoParentDirectoryAction extends AbstractAction {
protected GotoParentDirectoryAction() {
super("Go Up");
}
public void actionPerformed(ActionEvent e) {
SPFileChooser chooser = getSPFileChooser();
chooser.setCursor(
Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
File currentDir = chooser.getCurrentDirectory();
SPFile parentDir = (SPFile) chooser.getFileSystemView().
getParentDirectory(currentDir);
parentDir.refreshContent();
chooser.setCurrentDirectory(parentDir);
chooser.setCursor(Cursor.getDefaultCursor());
}
}//GotoParentDirectoryAction inner class
protected class MetalFileRenderer extends MetalFileChooserUI.FileRenderer {
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected,
cellHasFocus);
File file = (File) value;
if (! isSelected) {
Color textColor = getSPFileChooser().getTextColor(file);
setForeground(textColor);
}
return this;
}
}//MetalFileRenderer inner class
private SPFileChooser getSPFileChooser() {
return (SPFileChooser) getFileChooser();
}
} // End of SPMotifFileChooserUI class