📄 item.java
字号:
//Title: Curriculum Development Tool
//Copyright: Copyright (c) 2001
//Author: David Bradford - dbrad@medstat.med.utah.edu
//Company: Knowledge Weavers - http://medstat.med.utah.edu/kw/
//File: projects/tree/Item.java
//Description: A node in a project tree that contains a web page.
package cdt.projects.tree.nodes;
import cdt.projects.Project;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.zip.*;
import java.io.*;
/**
* This class is for a node in the tree that will be a page in the tree. So it will store the
* text that is edited by the user.
*
* @version 1.1
* @author David Bradford<BR>
* Brad Schaefer (<A HREF="mailto:schaefer@medstat.med.utah.edu">schaefer@medstat.med.utah.edu</A>)
*/
public class Item extends Node {
/** html contained in this node. */
private String text;
/** whether this page is visible or not. */
private boolean visible;
/** the <code>LinkedList</code> of files to append to this one */
private LinkedList appends;
/** Whether or not this node has been loaded yet */
private boolean isLoaded;
/**
* Creates an item.
*
* @param name name of the node.
*/
public Item(String name) {
this(name, new String("<html>\n<body>\n</body>\n</html>"), null, true);
}
/**
* Creates an item.
*
* @param name name of the node.
* @param text html contained in the node.
* @param parent parent of the node.
* @param visible whether the node is visible or not.
*/
public Item(String name, String text, Node parent, boolean visible) {
setName(name);
setParent(parent);
this.text = text;
this.visible = visible;
this.isLoaded = false;
this.appends = new LinkedList();
}
/**
* Gets the <code>LinkedList</code> of files to append to this node.
*
* @return Append nodes in this node.
*/
public LinkedList getAppends() {
return appends;
}
/**
* gets the html contained in this node plus whatever data in the
* {@link cdt.projects.tree.nodes.Append Append} Nodes.
*
* @return html in this node.
*/
public String getData() {
if(!isLoaded) {
StringBuffer ret = new StringBuffer();
String filepath = this.getFilePath();
File datFile = cdt.projects.Project.currentProject.getDataFile();
if(null != datFile) {
ZipFile zip;
try {
zip = new ZipFile(datFile, ZipFile.OPEN_READ);
ZipEntry entry = zip.getEntry(filepath);
InputStream in;
if(null != entry) {
in = zip.getInputStream(entry);
byte data[] = new byte[1000];
int numBytes;
while( (numBytes = in.read(data)) != -1) {
ret.append(new String(data, 0, numBytes));
}
in.close();
}
zip.close();
} catch(Exception e) {
e.printStackTrace();
// cdt.ErrorD.ErrorDlg.ErrorMsg("Unable to read from '" +filename+ "'");
}
} else {
// cdt.ErrorD.ErrorDlg.ErrorMsg("Unable to find a data file for the current project.");
}
LinkedList filteredList = new LinkedList();
Iterator iter = appends.iterator();
while(iter.hasNext()) {
Node n = (Node)iter.next();
if(n instanceof Append) {
ret.append(n.getData());
} else {
filteredList.add(n);
}
}
// We keep any nodes that weren't Append nodes here just in case they are
// important to the project somehow -B
appends = filteredList;
text = ret.toString().trim();
isLoaded = true;
}
return text;
}
/**
* Sets whether or not this file is considered to be 'loaded' or not.
* Be careful with this one, as being 'loaded' is something that is only
* used internally. This likely gets called in save routines and in
* the {@link cdt.projects.Project#createNew createNew} method.
*
* @param loaded If the Item is loaded or not.
*/
public void setIsLoaded(boolean loaded) {
isLoaded = loaded;
}
/**
* changes the html contained in this node.
*
* @param s new html to have this item contain.
*/
public void setData(String s) {
text = s;
isLoaded = true;
}
/**
* Inserts an {@link cdt.projects.tree.nodes.Append Append} node
* into this Item -- if it is not an Append node, we do not add the
* {@link cdt.projects.tree.nodes.Node Node}.
*
* @param into {@link cdt.projects.tree.nodes.Node Node} to insert into this Item.
*/
public void addNodeInto(Node into) {
if(into instanceof Append) {
appends.add(into);
into.setParent(this);
}
}
/**
* Makes a copy of this node.
*
* @return copy of this node.
*/
public Node copy() {
Node n = new Item(this.getName());
n.setFile(this.getFile());
n.setData(this.getData());
return n;
}
/**
* Is this node visible or not??
*
* @return whether this node is visible.
*/
public boolean isVisible() {
return visible;
}
/**
* Writes the project data to an OutputStream.
*
* @param out OutputStream to send project data to.
* @param dir Indicates the directory of the file this {@link cdt.projects.tree.nodes.Node Node} represents.
*/
public void writeProject(OutputStream out, String dir) {
try {
if(visible) {
out.write((new String("<item name=\"" +getName()+ "\" file=\"" +getFile()+ "\">\n")).getBytes());
} else {
out.write((new String("<item name=\"" +getName()+ "\" file=\"" +getFile()+
"\" hidden=\"true\">\n")).getBytes());
}
if(getFile().indexOf("file=") != -1) {
System.out.println(getName() + " | " +getFile());
}
Iterator iter = appends.iterator();
while(iter.hasNext()) {
Node ap = (Node)iter.next();
if(ap instanceof Append) {
out.write((new String("<append file=\"" +ap.getFile()+"\"/>\n")).getBytes());
} else {
// Handle nodes that aren't Appends? Probably not necessary
}
}
out.write((new String("</item>\n")).getBytes());
} catch(Exception e) {
e.printStackTrace();
cdt.ErrorD.ErrorDlg.ErrorMsg("Error writing project -> " +dir+getFile());
}
// The following is in order to not have to store the entire project in
// memory all the time. Hopefully this won't get us into trouble -B
if(Project.currentProject.getOpenNode() != this && isLoaded == true) {
this.setData(new String());
this.setIsLoaded(false);
}
}
/**
* Items cannot have children, as that makes them appear as folders
* defaultly in the tree.
*
* @return false.
*/
public boolean getAllowsChildren() {
return false;
}
/**
* a node has been dropped into this one so add it above this one.
*
* @param n node dropped.
*/
public void dropInto(Node n) {
addNodeAbove(n);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -