View Javadoc

    1   // ========================================================================
    2   // $Id: FrameSet.java,v 1.4 2004/11/20 13:32:33 gregwilkins Exp $
    3   // Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
    4   // ------------------------------------------------------------------------
    5   // Licensed under the Apache License, Version 2.0 (the "License");
    6   // you may not use this file except in compliance with the License.
    7   // You may obtain a copy of the License at
    8   // http://www.apache.org/licenses/LICENSE-2.0
    9   // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  // ========================================================================
    15
    16  package org.mortbay.html;
    17  import java.io.IOException;
    18  import java.io.Writer;
    19  import java.util.Enumeration;
    20  import java.util.Hashtable;
    21  import java.util.Vector;
    22
    23  /* ---------------------------------------------------------------- */
    24  /** FrameSet.
    25   * <p>
    26   * Usage
    27   * <pre>
    28   *      FrameSet set = new FrameSet("FrameTest","*,*","*,*");
    29   *      set.frame(0,0).name("Frame1",req.getRequestPath()+"?Frame=1");
    30   *      set.frame(0,1).name("Frame2",req.getRequestPath()+"?Frame=2");
    31   *      set.frame(1,0).name("Frame3",req.getRequestPath()+"?Frame=3");
    32   *      set.frame(1,1).name("Frame4",req.getRequestPath()+"?Frame=4");
    33   *      set.write(new Writer(res.getOutputStream()));
    34   * </pre>
    35   * @version $Id: FrameSet.java,v 1.4 2004/11/20 13:32:33 gregwilkins Exp $
    36   * @author Greg Wilkins
    37  */
    38  public class FrameSet extends Page
    39  {
    40      Frame[][] frames=null;
    41      String colSpec=null;
    42      String rowSpec=null;
    43      int cols;
    44      int rows;
    45      String border="";
    46      Vector frameNames=null;
    47      Hashtable frameMap=null;
    48
    49      /* ------------------------------------------------------------ */
    50      /** FrameSet constructor.
    51       * @param colSpec Comma separated list of column widths specified
    52       *                as pixels, percentage or '*' for variable
    53       */
    54      public FrameSet(String title, String colSpec, String rowSpec)
    55      {
    56          super(title);
    57
    58          this.colSpec=colSpec;
    59          this.rowSpec=rowSpec;
    60
    61          cols=1;
    62          rows=1;
    63
    64          int i=-1;
    65          while(colSpec != null && (i=colSpec.indexOf(",",i+1))>=0)
    66              cols++;
    67
    68          i=-1;
    69          while(rowSpec != null && (i=rowSpec.indexOf(",",i+1))>=0)
    70              rows++;
    71
    72          frames=new Frame[cols][rows];
    73          for(int c=0;c<cols;c++)
    74              for(int r=0;r<rows;r++)
    75                  frames[c][r]=new Frame();
    76      }
    77
    78      /* ------------------------------------------------------------ */
    79      public Frame frame(int col, int row)
    80      {
    81          return frames[col][row];
    82      }
    83
    84      /* ------------------------------------------------------------ */
    85      public FrameSet border(boolean threeD, int width, String color)
    86      {
    87          border=" frameborder=\""+(threeD?"yes":"no")+"\"";
    88          if (width>=0)
    89              border+=" border=\""+width+"\"";
    90
    91          if (color!=null)
    92              border+=" bordercolor=\""+color+"\"";
    93          return this;
    94      }
    95
    96      /* ----------------------------------------------------------------- */
    97      public Enumeration namedFrames()
    98      {
    99          if (frameNames==null)
    100             return new Vector().elements();
    101         return frameNames.elements();
    102     }
    103
    104     /* ----------------------------------------------------------------- */
    105     public Frame frame(String name)
    106     {
    107         if (frameMap==null)
    108             return null;
    109         return (Frame) frameMap.get(name);
    110     }
    111
    112     /* ----------------------------------------------------------------- */
    113     /** Name a frame.
    114      * By convention, frame names match Page section names
    115      */
    116     public Frame nameFrame(String name,int col, int row)
    117     {
    118         if (frameMap==null)
    119         {
    120             frameMap=new Hashtable(10);
    121             frameNames=new Vector(10);
    122         }
    123
    124         Frame frame = frames[col][row];
    125         if (frame==null)
    126             frame = frames[col][row] = new Frame();
    127
    128         if (frameMap.get(name)==null)
    129             frameNames.addElement(name);
    130         frameMap.put(name,frame);
    131         frame.name(name);
    132
    133         return frame;
    134     }
    135
    136
    137     /* ----------------------------------------------------------------- */
    138     public void write(Writer out)
    139          throws IOException
    140     {
    141         writeHtmlHead(out);
    142
    143         out.write("<frameset "+border);
    144
    145         if(colSpec!=null)
    146             out.write(" cols=\""+colSpec+"\"");
    147         if(rowSpec!=null)
    148             out.write(" rows=\""+rowSpec+"\"");
    149         out.write(">");
    150
    151         for(int r=0;r<rows;r++)
    152             for(int c=0;c<cols;c++)
    153                 frames[c][r].write(out);
    154
    155         out.write("<noframes>");
    156         writeElements(out);
    157         out.write("</noframes>");
    158
    159         out.write("</frameset>");
    160         out.write("</html>");
    161     }
    162 };
    163
    164