home *** CD-ROM | disk | FTP | other *** search
Wrap
import java.applet.Applet; import java.applet.AppletContext; import java.awt.Button; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Event; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Image; import java.awt.MediaTracker; import java.awt.TextArea; import java.awt.TextComponent; import java.awt.TextField; import java.io.DataInput; import java.io.DataInputStream; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; public class CWebView extends Applet implements OutlineOwner, Runnable { private static final boolean DEMO_VERSION = true; private static final int DEMO_MAX_NODES = 10000; private static boolean IS_WINDOWS_SYSTEM = true; public static final int ICON_NUMBER = 19; public static final int ICON_HEIGHT = 13; public static final int ICON_WIDTH = 13; public static final int SCROLL_DELAY_TIME = 80; public static final Color warningBack; public static final Color warningFore; public static final String copyright = "CLEARweb Java Applet. Copyright Clear Software Inc 1996. www.clearsoft.com/clearweb "; private static final String[][] ParameterInfo; String sourceURL; boolean useIcons = false; String helpURL = "http://www.clearsoft.com/applet/help.html"; boolean gotoFrame; String frameName; boolean showToolbar = true; boolean showFrameSwitch = true; boolean showDesc = true; int descRows = 2; boolean showAddress = true; String sDnloadText = "D"; String sHelpText = "?"; String sFrameText = "[|]"; String sNoFrameText = "[]"; String sSearchText = "S"; String sGotoText = "G"; String textFont = "Courier"; int textFontSize = 12; Color backColor = new Color(-1); Color foreColor = new Color(-16777216); Color backSelColor = new Color(-16776961); Color foreSelColor = new Color(-1); Color foreVisColor = new Color(-65536); String loadMapText = "Loading CLEARweb Map"; String loadURLText1 = "Loading:"; String loadURLText2 = " Please wait..."; String loadURLError1 = "Error: this node has no address"; String loadURLError2 = "Error: this node has invalid address"; boolean useFrames = true; Outline olBookmarks; Button bSearchTitle; Button bHelp; Button bDnload; Button bFrameSwitch; Button bGoto; TextArea taDescription; TextField tfSearchTitle; TextField tfAddress; TextField tfBanner; Image[] iconArray = new Image[19]; Thread loader; boolean loading = true; public CWebView() { if (System.getProperty("os.name").indexOf("indows") != -1) { IS_WINDOWS_SYSTEM = true; } else { IS_WINDOWS_SYSTEM = false; } } public void stop() { this.stopScrollingBanner(); } public void init() { this.parseParameters(); this.setupInterface(); this.loader = new Thread(this); this.loader.start(); if (!IS_WINDOWS_SYSTEM) { this.loader = null; } } private void parseParameters() { this.sourceURL = this.getStringParameter("Source", ""); this.useIcons = this.getBooleanParameter("ShowIcons", this.useIcons); if (((Applet)this).getParameter("UseFrame") != null) { this.gotoFrame = true; this.frameName = new String(((Applet)this).getParameter("UseFrame")); } else { this.gotoFrame = false; } if (((Applet)this).getParameter("ShowToolbar") != null) { try { int toolbarFormat = Integer.parseInt(((Applet)this).getParameter("ShowToolbar")); if (toolbarFormat < 0) { toolbarFormat = 0; } if (toolbarFormat > 2) { toolbarFormat = 2; } if (toolbarFormat > 0) { this.showToolbar = true; } if (toolbarFormat > 1) { this.showFrameSwitch = true; } else { this.showFrameSwitch = false; } } catch (NumberFormatException var9) { } } try { if (((Applet)this).getParameter("ShowDescription") != null) { this.descRows = Integer.parseInt(((Applet)this).getParameter("ShowDescription")); } if (this.descRows <= 0) { this.showDesc = false; } else { this.showDesc = true; } } catch (NumberFormatException var8) { } this.sDnloadText = this.getStringParameter("DnloadButtonText", this.sDnloadText); this.sHelpText = this.getStringParameter("HelpButtonText", this.sHelpText); this.sFrameText = this.getStringParameter("FrameButtonTextSplit", this.sFrameText); this.sNoFrameText = this.getStringParameter("FrameButtonTextFull", this.sNoFrameText); this.sSearchText = this.getStringParameter("SearchButtonText", this.sSearchText); this.sGotoText = this.getStringParameter("GotoButtonText", this.sGotoText); this.textFont = this.getStringParameter("Font", this.textFont); try { if (((Applet)this).getParameter("FontSize") != null) { this.textFontSize = Integer.parseInt(((Applet)this).getParameter("FontSize")); } } catch (NumberFormatException var7) { } try { if (((Applet)this).getParameter("BackColor") != null) { this.backColor = new Color(Integer.parseInt(((Applet)this).getParameter("BackColor"), 16)); } } catch (NumberFormatException var6) { } try { if (((Applet)this).getParameter("ForeColor") != null) { this.foreColor = new Color(Integer.parseInt(((Applet)this).getParameter("ForeColor"), 16)); } } catch (NumberFormatException var5) { } try { if (((Applet)this).getParameter("SelectedBackColor") != null) { this.backSelColor = new Color(Integer.parseInt(((Applet)this).getParameter("SelectedBackColor"), 16)); } } catch (NumberFormatException var4) { } try { if (((Applet)this).getParameter("SelectedForeColor") != null) { this.foreSelColor = new Color(Integer.parseInt(((Applet)this).getParameter("SelectedForeColor"), 16)); } } catch (NumberFormatException var3) { } try { if (((Applet)this).getParameter("VisitedForeColor") != null) { this.foreVisColor = new Color(Integer.parseInt(((Applet)this).getParameter("VisistedForeColor"), 16)); } } catch (NumberFormatException var2) { } this.loadMapText = this.getStringParameter("LoadMapText", this.loadMapText); this.loadURLText1 = this.getStringParameter("LoadURLText1", this.loadURLText1); this.loadURLText2 = this.getStringParameter("LoadURLText2", this.loadURLText2); this.loadURLError1 = this.getStringParameter("LoadURLError1", this.loadURLError1); this.loadURLError2 = this.getStringParameter("LoadURLError2", this.loadURLError2); } private void setupInterface() { GridBagLayout gbl = new GridBagLayout(); ((Container)this).setLayout(gbl); int nextX = 0; int nextY = 0; this.tfBanner = new TextField(); this.tfBanner.setBackground(Color.red); this.tfBanner.setForeground(Color.white); this.addControl(this.tfBanner, gbl, 0, 0, nextX, 1, 100, 0, 2); this.tfBanner.setEditable(false); ++nextY; if (this.showToolbar) { this.bDnload = new Button(this.sDnloadText); this.addControl(this.bDnload, gbl, nextX, nextY, 1, 1, 0, 0, 0); ++nextX; this.bHelp = new Button(this.sHelpText); this.addControl(this.bHelp, gbl, nextX, nextY, 1, 1, 0, 0, 0); ++nextX; if (this.showFrameSwitch) { this.bFrameSwitch = new Button(this.sFrameText); this.addControl(this.bFrameSwitch, gbl, nextX, nextY, 1, 1, 0, 0, 0); ++nextX; } this.tfSearchTitle = new TextField(); this.addControl(this.tfSearchTitle, gbl, nextX, nextY, 1, 1, 100, 0, 2); ++nextX; this.bSearchTitle = new Button(this.sSearchText); this.addControl(this.bSearchTitle, gbl, nextX, nextY, 1, 1, 0, 0, 0); ++nextX; ++nextY; } this.olBookmarks = new Outline(this); this.olBookmarks.setColors(this.backColor, this.foreColor, this.backSelColor, this.foreSelColor, this.foreVisColor); this.olBookmarks.setFont(this.textFont, this.textFontSize); this.addControl(this.olBookmarks, gbl, 0, nextY, nextX, 1, 100, 100, 1); ++nextY; if (this.showAddress) { this.tfAddress = new TextField(); this.addControl(this.tfAddress, gbl, 0, nextY, nextX - 1, 1, 100, 0, 2); this.tfAddress.setEditable(false); this.bGoto = new Button(this.sGotoText); this.addControl(this.bGoto, gbl, nextX - 1, nextY, 1, 1, 0, 0, 0); ++nextY; } if (this.showDesc) { this.taDescription = new TextArea(this.descRows, 20); this.addControl(this.taDescription, gbl, 0, nextY, nextX, this.descRows, 100, 0, 2); this.taDescription.setEditable(false); ++nextY; } } public void run() { MediaTracker tracker = null; this.loading = true; if (this.useIcons) { tracker = new MediaTracker(this); for(int i = 0; i < 19; ++i) { this.iconArray[i] = ((Applet)this).getImage(((Applet)this).getCodeBase(), "icons/icon" + i + ".gif"); tracker.addImage(this.iconArray[i], 0); } tracker.statusAll(true); } else { for(int i = 0; i < 19; ++i) { this.iconArray[i] = null; } } this.olBookmarks.setUpdating(false); this.olBookmarks.disable(); try { URL url = new URL(((Applet)this).getDocumentBase(), this.sourceURL); this.createOutlineFromStream(new DataInputStream(url.openStream())); this.tfAddress.setText(this.loadMapText); if (this.useIcons) { try { tracker.waitForAll(); } catch (InterruptedException var5) { } } if (this.useIcons && (tracker.statusAll(true) & 8) == 0) { System.out.println("Error creating icons, quitting"); ((Applet)this).getAppletContext().showStatus("Error creating icons, quitting"); } else { this.olBookmarks.expandWholeOutline(); this.olBookmarks.setCurrentNode(0); this.olBookmarks.enable(); this.olBookmarks.setUpdating(true); this.loading = false; this.scrollLineOnTextField(this.tfBanner, "CLEARweb Java Applet. Copyright Clear Software Inc 1996. www.clearsoft.com/clearweb ", -1); } } catch (IOException e) { System.out.println("Error " + e); } } private boolean getBooleanParameter(String name, boolean def) { if (((Applet)this).getParameter(name) == null) { return def; } else { try { return Integer.parseInt(((Applet)this).getParameter(name)) != 0; } catch (NumberFormatException var3) { return def; } } } private String getStringParameter(String name, String def) { return ((Applet)this).getParameter(name) == null ? def : new String(((Applet)this).getParameter(name)); } private void scrollLineOnTextField(TextField field, String line, int times) { if (line != null) { if (field != null) { if (!IS_WINDOWS_SYSTEM) { ((TextComponent)field).setText(line); } else { String fillerLine = ""; String anim = fillerLine + line; while(times > 0 || times == -1) { ((TextComponent)field).setText(anim); anim = anim.substring(1); try { Thread.sleep(80L); } catch (InterruptedException var6) { break; } if (anim.length() == 0) { if (times > 0) { --times; } anim = fillerLine + line; } } ((TextComponent)field).setText(""); } } } } private void stopScrollingBanner() { if (!this.loading && this.loader != null) { this.loader.stop(); this.loader = null; this.tfBanner.setText("CLEARweb Java Applet. Copyright Clear Software Inc 1996. www.clearsoft.com/clearweb "); } } public void nodeSelected(Outline otl, int idx) { BookmarkNode nd = (BookmarkNode)otl.getNodeData(idx); if (this.showAddress) { this.tfAddress.setText(nd.NodeURL()); } if (this.showDesc) { this.taDescription.setText(nd.NodeDesc()); } this.stopScrollingBanner(); } public void nodeDoubleClicked(Outline otl, int idx, int where) { this.gotoItem(idx); } public void nodeExpanded(Outline otl, int idx) { } public void nodeCollapsed(Outline otl, int idx) { } public boolean handleEvent(Event evt) { if (evt.id == 402 && evt.target == this.tfSearchTitle && evt.key == 10) { this.findItem(this.tfSearchTitle.getText()); } return super.handleEvent(evt); } public boolean action(Event evt, Object arg) { this.stopScrollingBanner(); if (this.bFrameSwitch != null && arg.equals(this.bFrameSwitch.getLabel())) { this.useFrames = !this.useFrames; if (this.useFrames) { this.bFrameSwitch.setLabel(this.sFrameText); } else { this.bFrameSwitch.setLabel(this.sNoFrameText); } ((Container)this).layout(); } else if (this.bDnload != null && arg.equals(this.bDnload.getLabel())) { try { this.gotoPage((new URL(((Applet)this).getDocumentBase(), this.sourceURL)).toString()); } catch (MalformedURLException var3) { } } else if (this.bHelp != null && arg.equals(this.bHelp.getLabel())) { this.gotoPage(this.helpURL); } else if (this.bSearchTitle != null && arg.equals(this.bSearchTitle.getLabel())) { this.findItem(this.tfSearchTitle.getText()); } else { if (this.bGoto == null || !arg.equals(this.bGoto.getLabel())) { return false; } this.gotoItem(this.olBookmarks.getCurrentNode()); } return true; } private void findItem(String str) { int idx = this.olBookmarks.getCurrentNode(); for(int i = idx + 1; i < this.olBookmarks.getNodes(); ++i) { BookmarkNode bnd = (BookmarkNode)this.olBookmarks.getNodeData(i); if (bnd.NodeTitle().toUpperCase().indexOf(str.toUpperCase()) != -1) { this.olBookmarks.setCurrentNode(i); return; } } } private void gotoItem(int idx) { if (idx < this.olBookmarks.getNodes() && idx >= 0) { this.olBookmarks.setCurrentNode(idx); BookmarkNode nd = (BookmarkNode)this.olBookmarks.getNodeData(idx); this.gotoPage(nd.NodeURL()); nd.setSecondary(true); this.tfAddress.setText(nd.NodeURL()); } } private void gotoPage(String urlStr) { Color back = Color.red; Color fore = Color.red; if (urlStr.equals("")) { if (this.showAddress) { this.tfAddress.setText(this.loadURLError2); } } else { try { this.olBookmarks.disable(); if (urlStr.startsWith("www.")) { urlStr = "http://" + urlStr; } URL url = new URL(urlStr); AppletContext ac = ((Applet)this).getAppletContext(); if (!this.useFrames) { ac.showDocument(url, "_top"); } else if (this.gotoFrame) { ac.showDocument(url, this.frameName); } else { ac.showDocument(url, "_top"); } if (this.showAddress && IS_WINDOWS_SYSTEM) { fore = this.tfAddress.getForeground(); back = this.tfAddress.getBackground(); Font font = this.tfAddress.getFont(); this.tfAddress.setBackground(warningBack); this.tfAddress.setForeground(warningFore); this.tfAddress.setFont(new Font(font.getName(), 1, font.getSize())); String anim = " " + this.loadURLText1 + " " + urlStr + " " + this.loadURLText2; this.scrollLineOnTextField(this.tfAddress, anim, 1); this.tfAddress.setBackground(back); this.tfAddress.setForeground(fore); this.tfAddress.setFont(font); } } catch (MalformedURLException var12) { if (this.showAddress) { this.tfAddress.setText(this.loadURLError2); } } finally { this.olBookmarks.enable(); this.olBookmarks.requestFocus(); } } } private int getTitleURL(String str, StringBuffer title, StringBuffer url) { String testString = str.toUpperCase(); title.setLength(0); url.setLength(0); if (!testString.startsWith("<A")) { title.append(str); } else { int sp = str.indexOf(">"); int lp = str.indexOf(60, sp); title.append(str.substring(sp + 1, lp)); sp = testString.indexOf("HREF=\""); sp += 5; lp = str.indexOf(34, sp + 1); url.append(str.substring(sp + 1, lp)); } String ts = title.toString(); int sp = ts.lastIndexOf("{*S"); int lp = ts.lastIndexOf("}"); int id; try { id = Integer.parseInt(ts.substring(sp + 3, lp)); title.setLength(sp); } catch (NumberFormatException var9) { id = 1; } String var15 = null; return id; } private void addControl(Component cp, GridBagLayout gbl, int x, int y, int w, int h, int wx, int wy, int fill) { GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = x; gbc.gridy = y; gbc.gridwidth = w; gbc.gridheight = h; gbc.weightx = (double)wx; gbc.weighty = (double)wy; gbc.fill = fill; gbl.setConstraints(cp, gbc); ((Container)this).add(cp); } private void createOutlineFromStream(DataInput inp) { new String(""); new String(""); StringBuffer title = new StringBuffer(); StringBuffer url = new StringBuffer(); StringBuffer desc = new StringBuffer(); int currentNode = -1; this.olBookmarks.clearOutline(); int addMethod = 1; try { String currentLine = inp.readLine(); while(currentLine != null) { currentLine = currentLine.trim(); if (currentLine.indexOf("</DL>") != -1) { if (addMethod == 1) { currentNode = this.olBookmarks.getParent(currentNode); } else { currentNode = this.olBookmarks.getParent(this.olBookmarks.getParent(currentNode)); } addMethod = 1; } else if (currentLine.indexOf("<DL>") != -1) { addMethod = 1; } else if (currentLine.indexOf("<DT>") != -1) { int id = this.getTitleURL(currentLine.substring(currentLine.indexOf("<DT>") + 4), title, url); currentLine = inp.readLine(); desc.setLength(0); int sp = currentLine.indexOf("<DD>"); if (sp != -1) { desc.append(currentLine.substring(sp + 4)); for(currentLine = inp.readLine(); currentLine.indexOf(60) == -1; currentLine = inp.readLine()) { currentLine.trim(); desc.append(" " + currentLine.trim()); } } BookmarkNode bnd = new BookmarkNode(title.toString(), url.toString(), desc.toString()); if (id < 0) { id = 1; } if (id >= 19) { id = 1; } bnd.setIcon(this.iconArray[id]); this.olBookmarks.addNode(currentNode, addMethod, bnd); currentNode = this.olBookmarks.getNodes() - 1; addMethod = 3; if (currentNode == 9999) { return; } continue; } currentLine = inp.readLine(); } } catch (Exception e) { System.out.println("Error " + e); } } public String[][] getParameterInfo() { return ParameterInfo; } static { warningBack = Color.red; warningFore = Color.white; ParameterInfo = new String[][]{{"Source\t\t", "url\t\t", "URL of the source CWO relative to document"}, {"ShowIcons\t\t", "boolean(1/0)\t", "Use icons in the outline, default false(0)"}, {"HelpURL\t\t", "url\t\t", "The URL which should be shown when help button is pressed"}, {"UseFrame\t\t", "string\t\t", "The name of the content frame default \"\""}, {"ShowToolbar\t", "integer value(0/1/2)\t", "0-none, 1-without frame switch button, 2-full, default 2"}, {"ShowDescription\t", "integer value\t", "The number of rows in the description field(0 if none), default 2"}, {"DnloadButtonText\t", "string\t\t", "Text on the download button, default \"D\""}, {"HelpButtonText\t", "string\t\t", "Text on the help button, default \"?\""}, {"FrameButtonTextSplit\t", "string\t\t", "Text on the frame switch, when using other frame, default \"[|]\""}, {"FrameButtonTextFull\t", "string\t\t", "Text on the frame switch, when using same frame, default \"[]\""}, {"SearchButtonText\t", "string\t\t", "Text on the Search button, default \"S\""}, {"GotoButtonText\t", "string\t\t", "Text on the Goto button, default \"G\""}, {"Font\t\t", "string\t\t", "The name of the font to be used in the outline, default \"Courier\""}, {"FontSize\t\t", "integer value\t", "Size of the font to be used in the outline, default 12"}, {"BackColor\t\t", "hexadecimal value\t", "Color of the background of the outline, default FFFFFF"}, {"ForeColor\t\t", "hexadecimal value\t", "Color of the outline text, default 000000"}, {"SelectedBackColor\t", "hexadecimal value\t", "Color of the background of the selected node, default 0000FF"}, {"SelectedForeColor\t", "hexadecimal value\t", "Color of the foreground of the selected node, default FFFFFF"}, {"VisitedForeColor\t", "hexadecimal value\t", "Color of the foreground of the visited node, default FF0000"}, {"LoadMapText\t", "string\t\t", "The text which appears when loading data, default \"Loading CLEARweb Map\""}, {"LoadURLText1\t", "string\t\t", "The address line prefix for goto, default \"Loading\""}, {"LoadURLText2\t", "string\t\t", "The address line suffix for goto, default \" Please wait...\""}, {"LoadURLError1\t", "string\t\t", "There is no address error, default \"This item has no address\""}, {"LoadURLError2\t", "string\t\t", "Invalid address error, default \"Error: This item has invalid address\""}}; } }