src.com.ibm.as400.util.html.FileTreeElement Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jt400-jdk8 Show documentation
Show all versions of jt400-jdk8 Show documentation
The Open Source version of the IBM Toolbox for Java
///////////////////////////////////////////////////////////////////////////////
//
// JTOpen (IBM Toolbox for Java - OSS version)
//
// Filename: FileTreeElement.java
//
// The source code contained herein is licensed under the IBM Public License
// Version 1.0, which has been approved by the Open Source Initiative.
// Copyright (C) 1997-2002 International Business Machines Corporation and
// others. All rights reserved.
//
///////////////////////////////////////////////////////////////////////////////
package com.ibm.as400.util.html;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import java.util.Properties;
import java.beans.PropertyVetoException;
import java.beans.PropertyChangeSupport;
import com.ibm.as400.access.Trace;
import com.ibm.as400.access.IFSJavaFile;
import com.ibm.as400.access.IFSFile;
import com.ibm.as400.access.ExtendedIllegalArgumentException;
import com.ibm.as400.util.servlet.ServletHyperlink;
/**
* The FileTreeElement class represents the Integrated File System within an HTMLTree view.
*
* This example creates an FileTreeElement object:
*
*
*
* // Create an HTMLTree object.
* HTMLTree tree = new HTMLTree(httpServletRequest);
*
* // Create a URLParser object.
* URLParser urlParser = new URLParser(httpServletRequest.getRequestURI());
*
* // Create a object to represent the connection to the system.
* AS400 system = new AS400(mySystem, myUserId, myPassword);
*
* // Create an IFS object.
* IFSJavaFile root = new IFSJavaFile(system, "/QIBM");
*
* // Create a DirFilter object and get the directories.
* DirFilter filter = new DirFilter();
* File[] dirList = root.listFiles(filter);
*
*
* for (int i=0; i < dirList.length; i++)
* {
* // Create a FileTreeElement.
* FileTreeElement node = new FileTreeElement(dirList[i]);
*
* // Set the Icon URL.
* ServletHyperlink sl = new ServletHyperlink(urlParser.getURI());
* sl.setHttpServletResponse(resp);
* node.setIconUrl(sl);
*
* // Add the FileTreeElement to the tree.
* tree.addElement(node);
* }
*
*
* Once the elements are added to an HTMLTree object, the FileTreeElements will look like this:
*
*
*
*
*
* +
*
*
* include
*
*
*
*
* +
*
*
* locales
*
*
*
*
* +
*
*
* ProdData
*
*
*
*
* +
*
*
* Test Folder
*
*
*
*
* +
*
*
* UserData
*
*
*
*
* +
*
*
* XML
*
*
*
*
* FileTreeElement objects generate the following events:
*
* - ElementEvent - The events fired are:
*
* - elementAdded
*
- elementRemoved
*
* - PropertyChangeEvent
*
*
* @see com.ibm.as400.util.html.DirFilter
**/
public class FileTreeElement extends HTMLTreeElement implements java.io.Serializable
{
private static final String copyright = "Copyright (C) 1997-2002 International Business Machines Corporation and others.";
static final long serialVersionUID = 210948696422108527L;
private File file_;
private boolean populated_ = false;
private String parameter_; //@CRS
private String shareName_; // @B1A @CRS
private String sharePath_; // @B1A @CRS
private int patternMatching_ = -1; // @KKB
/**
* Constructs a default FileTreeElement object.
**/
public FileTreeElement()
{
}
/**
* Constructs a FileTreeElement with the specified file.
*
* @param file The file.
**/
public FileTreeElement(File file)
{
setFile(file);
setText(file.getName());
}
//@CRS
/**
* Constructs a FileTreeElement with the specified file.
*
* @param file The file.
* @param parameter
**/
public FileTreeElement(File file, String parameter)
{
setFile(file);
setText(file.getName());
parameter_ = parameter;
}
/**
* Constructs a FileTreeElement with the specified file,
* NetServer shareName and sharePath.
*
* @param file The file.
* @param shareName The name of the NetServer share.
* @param sharePath The path of the NetServer share.
**/
public FileTreeElement(File file, String shareName, String sharePath) // @B1A
{ // @B1A
setFile(file); // @B1A
setShareName(shareName); // @B1A
setSharePath(sharePath); // @B1A
} // @B1A
//@CRS
/**
* Constructs a FileTreeElement with the specified file,
* NetServer shareName and sharePath.
*
* @param file The file.
* @param shareName The name of the NetServer share.
* @param sharePath The path of the NetServer share.
* @param parameter
**/
public FileTreeElement(File file, String shareName, String sharePath, String parameter) // @B1A
{ // @B1A
setFile(file); // @B1A
setShareName(shareName); // @B1A
setSharePath(sharePath); // @B1A
parameter_ = parameter;
} // @B1A
/**
* Added the necessary properties to the text url so that if they
* click on the texturl and are using the FileListElement class,
* they will properly see the directory listing. This will
* avoid externalizing the properties we are passing on the
* HttpServletRequest to show the FileListElements. This only
* applies to FileTreeElements.
**/
private void addProperties()
{
if (getTextUrl() != null)
{
ServletHyperlink sl = (ServletHyperlink)getTextUrl().clone();
// If a share name has been specified, then remove the actual path of the share from the // @B1A
// path info and replace it with just the share name and the directories after the share. // @B1A
if (shareName_ != null) // @B1A
{
// @B1A
String absPath = file_.getAbsolutePath().replace('\\','/'); // @B1A
if (sharePath_.charAt(0) != '/') // @B1A
absPath = absPath.substring(1); // @B1A
if (Trace.isTraceOn()) // @B1A
{
// @B1A
Trace.log(Trace.INFORMATION, "FileTree absolute path: " + absPath); // @B1A
Trace.log(Trace.INFORMATION, "FileTree share path: " + sharePath_); // @B1A
} // @B1A
StringBuffer pathInfo = new StringBuffer(shareName_); // @B1A @CRS
String remainingPath = absPath.substring(sharePath_.length()); //@CRS
if (remainingPath.length() > 0 && remainingPath.charAt(0) != '/') pathInfo.append('/'); //@CRS @KKC
pathInfo.append(remainingPath); // @B1A @CRS
if (parameter_ != null) //@CRS
{
Properties parms = sl.getProperties(); //@CRS
if (parms == null) parms = new Properties(); //@CRS
parms.put(parameter_, pathInfo.toString()); //@CRS
try { sl.setProperties(parms); } catch(PropertyVetoException pve) {} //@CRS
}
else //@CRS
{
sl.setPathInfo(pathInfo.toString()); // @B1A
}
} // @B1A
else
{
String pathInfo = file_.getAbsolutePath().replace('\\', '/'); //@CRS
if (parameter_ != null) //@CRS
{
Properties parms = sl.getProperties(); //@CRS
if (parms == null) parms = new Properties(); //@CRS
parms.put(parameter_, pathInfo); //@CRS
try { sl.setProperties(parms); } catch(PropertyVetoException pve) {} //@CRS
}
else //@CRS
{
sl.setPathInfo(pathInfo); // @A3C @CRS
}
}
if (Trace.isTraceOn()) // @B1A
{
Trace.log(Trace.INFORMATION, "FileTree path Info: " + sl.getPathInfo()); // @B1A
}
try
{
sl.setText(file_.getName());
}
catch (PropertyVetoException e)
{ /* Ignore */
}
setTextUrl(sl);
}
}
/**
* Returns the file represented by this FileTreeElement.
* @return File
**/
public File getFile()
{
return file_;
}
/**
* Returns the NetServer share name.
* @return share name
**/
public String getShareName() // @B1A
{ // @B1A
// Need to check for null
// before performing a toString().
if (shareName_ == null)
return null;
else
return shareName_; // @B1A @CRS
}
/**
* Returns the NetServer share path.
* @return share path
**/
public String getSharePath() // @B1A
{ // @B1A
// Need to check for null
// before performing a toString().
if (sharePath_ == null)
return null;
else
return sharePath_; // @B1A @CRS
}
/**
* Indicates if the FileTreeElement is a leaf.
*
* @return true if the element is a leaf, false otherwise.
**/
public boolean isLeaf()
{
// We don't want the user to have to add the path and list properties to
// the TextUrl for the parent elements in the tree.
if (getTextUrl() != null /*&& getTextUrl().getProperties() == null*/) // @B2C
addProperties();
if (!populated_)
return file_.isFile();
else
return super.isLeaf();
}
/**
* Deserializes and initializes transient data.
**/
private void readObject(java.io.ObjectInputStream in)
throws java.io.IOException, ClassNotFoundException
{
in.defaultReadObject();
// changes_ = new PropertyChangeSupport(this);
}
/**
* Indicates which FileTreeElement is selected. The hashcode is used
* to determine which element within the tree to expand or collapse.
*
* @param hashcode The hashcode.
**/
public void selected(int hashcode)
{
// @C1D
if (hashcode == this.hashCode())
{
if ((file_.isDirectory()) && (!populated_))
{
File[] files;
if (file_ instanceof IFSJavaFile) //$A1A
{
if(patternMatching_ != -1) //@KKB specify the pattern matching to be used
{
try{
((IFSJavaFile)file_).setPatternMatching(patternMatching_);
}
catch(IOException e)
{
// error occured setting pattern matching for the file, default value will be used
if(Trace.isTraceOn())
Trace.log(Trace.ERROR, e);
}
}
// @B6A
// When we are using IFSJavaFile objects, we can use
// the listFiles() method becuase it is not dependant on any
// JDK1.2 code. Using listFiles() will also cache information
// like if it is a directory, so we don't flow another call to the
// system to find that out. We can then build both the
// directory and file list at the same time.
File[] filesAndDirs = ((IFSJavaFile) file_).listFiles(); // @B6A
// The vector of directories.
Vector dv = new Vector(); // @B6A
for (int i=0; i IFSFile.PATTERN_OS2)
throw new ExtendedIllegalArgumentException("patternMatching",
ExtendedIllegalArgumentException.PARAMETER_VALUE_NOT_VALID);
patternMatching_ = patternMatching;
}
}