1 /*
2  * @(#)FavoritesNode.java	1.5 06/10/30
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc.  All Rights Reserved.
5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6  *
7  * This code is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License version 2 only, as
9  * published by the Free Software Foundation.  Sun designates this
10  * particular file as subject to the "Classpath" exception as provided
11  * by Sun in the LICENSE file that accompanied this code.
12  *
13  * This code is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16  * version 2 for more details (a copy is included in the LICENSE file that
17  * accompanied this code).
18  *
19  * You should have received a copy of the GNU General Public License version
20  * 2 along with this work; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22  *
23  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
24  * CA 95054 USA or visit www.sun.com if you need additional information or
25  * have any questions.
26  */
27 
28 package javax.help;
29 
30 import javax.swing.tree.*;
31 import java.beans.*;
32 import java.io.OutputStream;
33 import java.io.OutputStreamWriter;
34 import java.io.IOException;
35 import java.util.Enumeration;
36 import java.io.ObjectOutputStream;
37 import java.io.ObjectInputStream;
38 import java.io.ByteArrayInputStream;
39 import java.io.ByteArrayOutputStream;
40 
41 /**
42  * A class for Favorites node. This class forces none-folders to have children.
43  *
44  * @author Richard Gregor
45  * @version   1.5     10/30/06
46  */
47 
48 public class FavoritesNode extends DefaultMutableTreeNode  {
49     /**
50      * Header part of xml file
51      */
52     public static final String HEADER="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
53     "<!DOCTYPE favorites\n PUBLIC \""+FavoritesView.publicIDString+
54     "\"\n        \"http://java.sun.com/products/javahelp/favorites_2_0.dtd\">\n"+
55     "\n<favorites version=\"2.0\">\n";
56 
57     /**
58      * XML element name
59      */
60     public static final String ELEMENT="favoriteitem";
61     /**
62      * Footer of xml document
63      */
64     public static final String FOOTER="</favorites>";
65     /**
66      * FavoritesItem userObject of this node
67      */
68     private FavoritesItem item;
69     /**
70      * Creates a FavoritesNode for FavoritesItem.
71      *
72      * @param item The FavoritesItem
73      */
FavoritesNode(FavoritesItem item)74     public FavoritesNode(FavoritesItem item) {
75         super(item);
76         this.item = item;
77     }
78 
79     /**
80      * Returns wheter node is allowed to have children or not.
81      */
getAllowsChildren()82     public boolean getAllowsChildren() {
83         return ((FavoritesItem) getUserObject()).isFolder();
84     }
85 
86     /**
87      * Adds the child node.
88      *
89      * @param child The DefaultMutableTreeNode with FavoritesItem as UserObject.
90      */
add(DefaultMutableTreeNode child)91     public void add(DefaultMutableTreeNode child) {
92         super.add(child);
93         FavoritesItem childItem = (FavoritesItem) child.getUserObject();
94         FavoritesItem oldParent = childItem.getParent();
95         FavoritesItem newParent = (FavoritesItem) getUserObject();
96         newParent.add(childItem);
97     }
98 
99     /**
100      * Removes the child node.
101      *
102      * @param child Node to remove.
103      */
remove(DefaultMutableTreeNode child)104     public void remove(DefaultMutableTreeNode child) {
105         super.remove(child);
106         FavoritesItem childItem = (FavoritesItem) ((FavoritesNode) child).getUserObject();
107         FavoritesItem ParentItem = (FavoritesItem) getUserObject();
108         if (parent != null)
109             ParentItem.remove(childItem);
110     }
111     /**
112      * Returns the number of visible children
113      *
114      */
getVisibleChildCount()115     public int getVisibleChildCount(){
116         int count = 0;
117         if( item == null)
118             return 0;
119 
120         for(Enumeration en = item.getChildren().elements(); en.hasMoreElements();){
121             FavoritesItem nItem =(FavoritesItem)en.nextElement();
122             if(nItem.isVisible())
123                 count++;
124         }
125         return count;
126     }
127     /**
128      * Returns the string representation of offset.
129      */
getOffset()130     public String getOffset(){
131         String parentOffset = null;
132         String offset = null;
133 
134         FavoritesNode parent = (FavoritesNode)getParent();
135         if(parent != null){
136             parentOffset = parent.getOffset();
137             offset = parentOffset + "  ";
138         }else
139             offset = "  ";
140 
141         return offset;
142     }
143     /**
144      * Exports nodes descendants to the OutputStream
145      *
146      * @param out The OutputStream
147      */
export(OutputStream out)148     public void export(OutputStream out) throws IOException{
149         OutputStreamWriter writer = new OutputStreamWriter(out);
150         writer = exportHeader(out);
151         //exportNode(writer);
152         Enumeration chldn = children();
153         if(!(chldn.equals(DefaultMutableTreeNode.EMPTY_ENUMERATION))){
154             while(chldn.hasMoreElements()){
155                 FavoritesNode node = (FavoritesNode)chldn.nextElement();
156                 node.exportNode(writer);
157             }
158         }
159         writer.write(FOOTER);
160         //out.close();
161         writer.close();
162     }
163 
164     /**
165      * Exports node and its descendants to the xml file according favorites.dtd.
166      *
167      * @param out The OutputStream
168      */
exportNode(OutputStreamWriter writer)169     public void exportNode(OutputStreamWriter writer) throws IOException{
170         TreeNode paren = getParent();
171         FavoritesItem item = (FavoritesItem)getUserObject();
172         writer.write(getOffset()+"<"+getXMLElement()+ " text=\""+item.getName()+"\" ");
173         String target = item.getTarget();
174         if(target != null)
175             writer.write("target=\""+target+"\" ");
176         String url = item.getURLSpec();
177         if(url != null)
178             writer.write("url=\""+url+"\"");
179         String hstitle = item.getHelpSetTitle();
180         if(hstitle != null)
181             writer.write(" hstitle=\""+hstitle+"\"");
182         Enumeration chldn = children();
183         if(chldn.equals(DefaultMutableTreeNode.EMPTY_ENUMERATION))
184             writer.write("/>\n");
185         else{
186             writer.write(">\n");
187             Enumeration offspring = children.elements();
188             while(offspring.hasMoreElements()){
189                 FavoritesNode off = (FavoritesNode)offspring.nextElement();
190                 debug("offspring: "+off);
191                 off.exportNode(writer);
192             }
193             writer.write(getOffset()+"</"+ELEMENT+">\n");
194         }
195 
196 
197     }
198 
199     /**
200      * Exports header defined for this type of node to the OutputStream.
201      *
202      * @param out The OutputStream.
203      */
exportHeader(OutputStream out)204     public OutputStreamWriter exportHeader(OutputStream out) throws IOException{
205         //OutputStreamWriter writer = new OutputStreamWriter(out,"UTF-8")
206 
207         OutputStreamWriter writer = new OutputStreamWriter(out,"UTF-8");
208         writer.write(HEADER);
209         return writer;
210     }
211 
212     /**
213      * Returns the XML header string
214      */
getXMLHeader()215     public String getXMLHeader(){
216         return HEADER;
217     }
218     /**
219      * Returns the XML element string
220      */
getXMLElement()221     public String getXMLElement(){
222         return ELEMENT;
223     }
224 
225     /**
226      * Returns  the deep copy of node
227      */
getDeepCopy()228     public FavoritesNode getDeepCopy(){
229 
230 	return new FavoritesNode((FavoritesItem)item.clone());
231 	/*
232         FavoritesNode copy = null;
233         ObjectOutputStream out = null;
234         ObjectInputStream in = null;
235 
236         try{
237             ByteArrayOutputStream bOut = new ByteArrayOutputStream();
238             out = new ObjectOutputStream(bOut);
239             out.writeObject(this);
240             out.flush();
241 
242             in = new ObjectInputStream(new ByteArrayInputStream(bOut.toByteArray()));
243             copy =(FavoritesNode) in.readObject();
244 
245             out.close();
246             in.close();
247         }catch(Exception e){
248             System.err.println(e);
249         }
250 
251         return copy;
252 	*/
253 
254     }
255 
256     /**
257      * Returns wheter node is visible or not
258      */
isVisible()259     public boolean isVisible(){
260         return item.isVisible();
261     }
262 
263     /**
264      * Sets visibility of node
265      */
setVisible(boolean vis)266     public void setVisible(boolean vis){
267         item.setVisible(vis);
268     }
269 
270     /**
271      * Debugging code
272      */
273     private static final boolean debug = false;
debug(String msg)274     private static void debug(String msg) {
275   	if (debug) {
276   	    System.err.println("FavoritesNode: "+msg);
277 	}
278     }
279 
280 }
281