001: // ========================================================================
002: // $Id: FrameSet.java,v 1.4 2004/11/20 13:32:33 gregwilkins Exp $
003: // Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
004: // ------------------------------------------------------------------------
005: // Licensed under the Apache License, Version 2.0 (the "License");
006: // you may not use this file except in compliance with the License.
007: // You may obtain a copy of the License at
008: // http://www.apache.org/licenses/LICENSE-2.0
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014: // ========================================================================
015:
016: package org.mortbay.html;
017:
018: import java.io.IOException;
019: import java.io.Writer;
020: import java.util.Enumeration;
021: import java.util.Hashtable;
022: import java.util.Vector;
023:
024: /* ---------------------------------------------------------------- */
025: /** FrameSet.
026: * <p>
027: * Usage
028: * <pre>
029: * FrameSet set = new FrameSet("FrameTest","*,*","*,*");
030: * set.frame(0,0).name("Frame1",req.getRequestPath()+"?Frame=1");
031: * set.frame(0,1).name("Frame2",req.getRequestPath()+"?Frame=2");
032: * set.frame(1,0).name("Frame3",req.getRequestPath()+"?Frame=3");
033: * set.frame(1,1).name("Frame4",req.getRequestPath()+"?Frame=4");
034: * set.write(new Writer(res.getOutputStream()));
035: * </pre>
036: * @version $Id: FrameSet.java,v 1.4 2004/11/20 13:32:33 gregwilkins Exp $
037: * @author Greg Wilkins
038: */
039: public class FrameSet extends Page {
040: Frame[][] frames = null;
041: String colSpec = null;
042: String rowSpec = null;
043: int cols;
044: int rows;
045: String border = "";
046: Vector frameNames = null;
047: Hashtable frameMap = null;
048:
049: /* ------------------------------------------------------------ */
050: /** FrameSet constructor.
051: * @param colSpec Comma separated list of column widths specified
052: * as pixels, percentage or '*' for variable
053: */
054: public FrameSet(String title, String colSpec, String rowSpec) {
055: super (title);
056:
057: this .colSpec = colSpec;
058: this .rowSpec = rowSpec;
059:
060: cols = 1;
061: rows = 1;
062:
063: int i = -1;
064: while (colSpec != null
065: && (i = colSpec.indexOf(",", i + 1)) >= 0)
066: cols++;
067:
068: i = -1;
069: while (rowSpec != null
070: && (i = rowSpec.indexOf(",", i + 1)) >= 0)
071: rows++;
072:
073: frames = new Frame[cols][rows];
074: for (int c = 0; c < cols; c++)
075: for (int r = 0; r < rows; r++)
076: frames[c][r] = new Frame();
077: }
078:
079: /* ------------------------------------------------------------ */
080: public Frame frame(int col, int row) {
081: return frames[col][row];
082: }
083:
084: /* ------------------------------------------------------------ */
085: public FrameSet border(boolean threeD, int width, String color) {
086: border = " frameborder=\"" + (threeD ? "yes" : "no") + "\"";
087: if (width >= 0)
088: border += " border=\"" + width + "\"";
089:
090: if (color != null)
091: border += " bordercolor=\"" + color + "\"";
092: return this ;
093: }
094:
095: /* ----------------------------------------------------------------- */
096: public Enumeration namedFrames() {
097: if (frameNames == null)
098: return new Vector().elements();
099: return frameNames.elements();
100: }
101:
102: /* ----------------------------------------------------------------- */
103: public Frame frame(String name) {
104: if (frameMap == null)
105: return null;
106: return (Frame) frameMap.get(name);
107: }
108:
109: /* ----------------------------------------------------------------- */
110: /** Name a frame.
111: * By convention, frame names match Page section names
112: */
113: public Frame nameFrame(String name, int col, int row) {
114: if (frameMap == null) {
115: frameMap = new Hashtable(10);
116: frameNames = new Vector(10);
117: }
118:
119: Frame frame = frames[col][row];
120: if (frame == null)
121: frame = frames[col][row] = new Frame();
122:
123: if (frameMap.get(name) == null)
124: frameNames.addElement(name);
125: frameMap.put(name, frame);
126: frame.name(name);
127:
128: return frame;
129: }
130:
131: /* ----------------------------------------------------------------- */
132: public void write(Writer out) throws IOException {
133: writeHtmlHead(out);
134:
135: out.write("<frameset " + border);
136:
137: if (colSpec != null)
138: out.write(" cols=\"" + colSpec + "\"");
139: if (rowSpec != null)
140: out.write(" rows=\"" + rowSpec + "\"");
141: out.write(">");
142:
143: for (int r = 0; r < rows; r++)
144: for (int c = 0; c < cols; c++)
145: frames[c][r].write(out);
146:
147: out.write("<noframes>");
148: writeElements(out);
149: out.write("</noframes>");
150:
151: out.write("</frameset>");
152: out.write("</html>");
153: }
154: };
|