javax.help.FavoritesNode Maven / Gradle / Ivy
/**
* @(#) FavoritesNode.java 1.4 - last change made 10/12/03
*
* Copyright (c) 2001 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 javax.help;
import javax.swing.tree.*;
import java.beans.*;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
/**
* A class for Favorites node. This class forces none-folders to have children.
*
* @author Richard Gregor
* @version 1.4 10/12/03
*/
public class FavoritesNode extends DefaultMutableTreeNode {
/**
* Header part of xml file
*/
public static final String HEADER="\n"+
"\n"+
"\n\n";
/**
* XML element name
*/
public static final String ELEMENT="favoriteitem";
/**
* Footer of xml document
*/
public static final String FOOTER=" ";
/**
* FavoritesItem userObject of this node
*/
private FavoritesItem item;
/**
* Creates a FavoritesNode for FavoritesItem.
*
* @param item The FavoritesItem
*/
public FavoritesNode(FavoritesItem item) {
super(item);
this.item = item;
}
/**
* Returns wheter node is allowed to have children or not.
*/
public boolean getAllowsChildren() {
return ((FavoritesItem) getUserObject()).isFolder();
}
/**
* Adds the child node.
*
* @param child The DefaultMutableTreeNode with FavoritesItem as UserObject.
*/
public void add(DefaultMutableTreeNode child) {
super.add(child);
FavoritesItem childItem = (FavoritesItem) child.getUserObject();
FavoritesItem oldParent = childItem.getParent();
FavoritesItem newParent = (FavoritesItem) getUserObject();
newParent.add(childItem);
}
/**
* Removes the child node.
*
* @param child Node to remove.
*/
public void remove(DefaultMutableTreeNode child) {
super.remove(child);
FavoritesItem childItem = (FavoritesItem) ((FavoritesNode) child).getUserObject();
FavoritesItem ParentItem = (FavoritesItem) getUserObject();
if (parent != null)
ParentItem.remove(childItem);
}
/**
* Returns the number of visible children
*
*/
public int getVisibleChildCount(){
int count = 0;
if( item == null)
return 0;
for(Enumeration en = item.getChildren().elements(); en.hasMoreElements();){
FavoritesItem nItem =(FavoritesItem)en.nextElement();
if(nItem.isVisible())
count++;
}
return count;
}
/**
* Returns the string representation of offset.
*/
public String getOffset(){
String parentOffset = null;
String offset = null;
FavoritesNode parent = (FavoritesNode)getParent();
if(parent != null){
parentOffset = parent.getOffset();
offset = parentOffset + " ";
}else
offset = " ";
return offset;
}
/**
* Exports nodes descendants to the OutputStream
*
* @param out The OutputStream
*/
public void export(OutputStream out) throws IOException{
OutputStreamWriter writer = new OutputStreamWriter(out);
writer = exportHeader(out);
//exportNode(writer);
Enumeration chldn = children();
if(!(chldn.equals(DefaultMutableTreeNode.EMPTY_ENUMERATION))){
while(chldn.hasMoreElements()){
FavoritesNode node = (FavoritesNode)chldn.nextElement();
node.exportNode(writer);
}
}
writer.write(FOOTER);
//out.close();
writer.close();
}
/**
* Exports node and its descendants to the xml file according favorites.dtd.
*
* @param out The OutputStream
*/
public void exportNode(OutputStreamWriter writer) throws IOException{
TreeNode paren = getParent();
FavoritesItem item = (FavoritesItem)getUserObject();
writer.write(getOffset()+"<"+getXMLElement()+ " text=\""+item.getName()+"\" ");
String target = item.getTarget();
if(target != null)
writer.write("target=\""+target+"\" ");
String url = item.getURLSpec();
if(url != null)
writer.write("url=\""+url+"\"");
String hstitle = item.getHelpSetTitle();
if(hstitle != null)
writer.write(" hstitle=\""+hstitle+"\"");
Enumeration chldn = children();
if(chldn.equals(DefaultMutableTreeNode.EMPTY_ENUMERATION))
writer.write("/>\n");
else{
writer.write(">\n");
Enumeration offspring = children.elements();
while(offspring.hasMoreElements()){
FavoritesNode off = (FavoritesNode)offspring.nextElement();
debug("offspring: "+off);
off.exportNode(writer);
}
writer.write(getOffset()+"\n");
}
}
/**
* Exports header defined for this type of node to the OutputStream.
*
* @param out The OutputStream.
*/
public OutputStreamWriter exportHeader(OutputStream out) throws IOException{
//OutputStreamWriter writer = new OutputStreamWriter(out,"UTF-8")
OutputStreamWriter writer = new OutputStreamWriter(out,"UTF-8");
writer.write(HEADER);
return writer;
}
/**
* Returns the XML header string
*/
public String getXMLHeader(){
return HEADER;
}
/**
* Returns the XML element string
*/
public String getXMLElement(){
return ELEMENT;
}
/**
* Returns the deep copy of node
*/
public FavoritesNode getDeepCopy(){
return new FavoritesNode((FavoritesItem)item.clone());
/*
FavoritesNode copy = null;
ObjectOutputStream out = null;
ObjectInputStream in = null;
try{
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
out = new ObjectOutputStream(bOut);
out.writeObject(this);
out.flush();
in = new ObjectInputStream(new ByteArrayInputStream(bOut.toByteArray()));
copy =(FavoritesNode) in.readObject();
out.close();
in.close();
}catch(Exception e){
System.err.println(e);
}
return copy;
*/
}
/**
* Returns wheter node is visible or not
*/
public boolean isVisible(){
return item.isVisible();
}
/**
* Sets visibility of node
*/
public void setVisible(boolean vis){
item.setVisible(vis);
}
/**
* Debugging code
*/
private static final boolean debug = false;
private static void debug(String msg) {
if (debug) {
System.err.println("FavoritesNode: "+msg);
}
}
}