Clover coverage report -
Coverage timestamp: Sun Apr 18 2004 21:32:30 EDT
file stats: LOC: 298   Methods: 10
NCLOC: 161   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
SplitPanel.java 55% 36.9% 50% 40.6%
coverage coverage
 1   
 /*
 2   
  * SimplyHTML, a word processor based on Java, HTML and CSS
 3   
  * Copyright (C) 2002 Ulrich Hilger
 4   
  *
 5   
  * This program is free software; you can redistribute it and/or
 6   
  * modify it under the terms of the GNU General Public License
 7   
  * as published by the Free Software Foundation; either version 2
 8   
  * of the License, or (at your option) any later version.
 9   
  *
 10   
  * This program is distributed in the hope that it will be useful,
 11   
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12   
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13   
  * GNU General Public License for more details.
 14   
  *
 15   
  * You should have received a copy of the GNU General Public License
 16   
  * along with this program; if not, write to the Free Software
 17   
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 18   
  */
 19   
 
 20   
 import java.awt.*;
 21   
 import javax.swing.border.*;
 22   
 import javax.swing.*;
 23   
 import java.util.prefs.*;
 24   
 
 25   
 /**
 26   
  * A panel to manage a pluggable panel layout, i.e. around
 27   
  * a center panel, other panels can be placed very similar
 28   
  * to BorderLayout. The difference of this class to BorderLayout is
 29   
  * that it creates JSplitPanes for each panel.
 30   
  *
 31   
  * <p>This uses JTabbedPanes for each of the panels surrounding
 32   
  * the center panel.</p>
 33   
  *
 34   
  * @author Ulrich Hilger
 35   
  * @author Light Development
 36   
  * @author <a href="http://www.lightdev.com">http://www.lightdev.com</a>
 37   
  * @author <a href="mailto:info@lightdev.com">info@lightdev.com</a>
 38   
  * @author published under the terms and conditions of the
 39   
  *      GNU General Public License,
 40   
  *      for details see file gpl.txt in the distribution
 41   
  *      package of this software
 42   
  *
 43   
  * @version stage 11, April 27, 2003
 44   
  */
 45   
 
 46   
 public class SplitPanel extends JPanel {
 47   
 
 48   
   /**
 49   
    * Constructor
 50   
    */
 51  75
   public SplitPanel() {
 52  75
     super();
 53  75
     setLayout(new BorderLayout());
 54  75
     for(int i = 0; i < 4; i++) {
 55  300
       panels[i] = new JSplitPane();
 56  300
       panels[i].setBorder(new EmptyBorder(0, 0, 0, 0));
 57  300
       panels[i].setContinuousLayout(true);
 58   
     }
 59  75
     buildLayout();
 60  75
     restorePrefs();
 61   
   }
 62   
 
 63   
   /**
 64   
    * set up the panels
 65   
    */
 66  75
   private void buildLayout() {
 67  75
     this.removeAll();
 68  75
     panels[NORTH].setOrientation(JSplitPane.VERTICAL_SPLIT);
 69  75
     panels[SOUTH].setOrientation(JSplitPane.VERTICAL_SPLIT);
 70  75
     panels[WEST].setOrientation(JSplitPane.HORIZONTAL_SPLIT);
 71  75
     panels[EAST].setOrientation(JSplitPane.HORIZONTAL_SPLIT);
 72  75
     if(majorAxis == this.MAJOR_AXIS_VERTICAL) {
 73   
       //System.out.println("SplitPanel.buildLayout majorAxis = vertical");
 74  75
       panels[SOUTH].setTopComponent(panels[NORTH]);
 75  75
       panels[WEST].setRightComponent(panels[SOUTH]);
 76  75
       panels[EAST].setLeftComponent(panels[WEST]);
 77  75
       this.add(panels[EAST], BorderLayout.CENTER);
 78   
     }
 79   
     else {
 80   
       //System.out.println("SplitPanel.buildLayout majorAxis = horizontal");
 81  0
       panels[SOUTH].setTopComponent(panels[NORTH]);
 82  0
       panels[WEST].setRightComponent(panels[SOUTH]);
 83  0
       panels[EAST].setLeftComponent(panels[WEST]);
 84  0
       this.add(panels[SOUTH], BorderLayout.CENTER);
 85   
     }
 86   
   }
 87   
 
 88   
   /**
 89   
    * remove panels surrounding the center panel
 90   
    */
 91  0
   public void removeAllOuterPanels() {
 92  0
     JTabbedPane p;
 93  0
     if(majorAxis == MAJOR_AXIS_VERTICAL) {
 94  0
       p = (JTabbedPane) panels[NORTH].getTopComponent();
 95  0
       p.removeAll();
 96  0
       p.setVisible(false);
 97  0
       p = (JTabbedPane) panels[WEST].getLeftComponent();
 98  0
       p.removeAll();
 99  0
       p.setVisible(false);
 100  0
       p = (JTabbedPane) panels[SOUTH].getBottomComponent();
 101  0
       p.removeAll();
 102  0
       p.setVisible(false);
 103  0
       p = (JTabbedPane) panels[EAST].getRightComponent();
 104  0
       p.removeAll();
 105  0
       p.setVisible(false);
 106   
     }
 107   
     else {
 108   
       // pending...
 109   
     }
 110   
   }
 111   
 
 112   
   /**
 113   
    * set the location of a given divider
 114   
    *
 115   
    * @param panel  the panel to set the location for
 116   
    * @param loc  the relative location of the divider (0, 0.1, ..., 0.9, 1)
 117   
    */
 118  0
   public void setDivLoc(int panel, double loc) {
 119   
     //System.out.println("SplitPanel.setDivLoc panel=" + panel + ", loc=" + loc);
 120  0
     panels[panel].setDividerLocation(loc);
 121   
   }
 122   
 
 123   
   /**
 124   
    * get the divider location for a given panel
 125   
    *
 126   
    * @param panel the panel to get the divider location for
 127   
    *
 128   
    * @return the divider location
 129   
    */
 130  0
   public int getDivLoc(int panel) {
 131   
     //System.out.println("SplitPanel.getDivLoc panel=" + panel + ", loc=" + panels[panel].getDividerLocation());
 132  0
     return panels[panel].getDividerLocation();
 133   
   }
 134   
 
 135   
   /**
 136   
    * save divider locations to preferences
 137   
    */
 138  1
   public void savePrefs() {
 139  1
     Preferences prefs = Preferences.userNodeForPackage( getClass() );
 140  1
     for(int i = 0; i < 4; i++) {
 141  4
       prefs.putInt("divLoc" + i, panels[i].getDividerLocation());
 142   
       //System.out.println("SplitPanel.savePrefs divLoc" + i + "=" + panels[i].getDividerLocation());
 143   
     }
 144   
   }
 145   
 
 146   
   /**
 147   
    * restore divider locations from preferences
 148   
    */
 149  75
   public void restorePrefs() {
 150  75
     Preferences prefs = Preferences.userNodeForPackage( getClass() );
 151  75
     for(int i = 0; i < 4; i++) {
 152  300
       panels[i].setDividerLocation(prefs.getInt("divLoc" + i, 300));
 153   
       //System.out.println("SplitPanel.restorePrefs divLoc" + i + "=" + prefs.getInt("divLoc" + i, 300));
 154   
     }
 155   
   }
 156   
 
 157   
   /**
 158   
    * get the plug-in container for a given panel
 159   
    *
 160   
    * @param location  the location of the desired container (SplitPanel.NORTH, SOUTH, etc.)
 161   
    *
 162   
    * @return the plug-in container
 163   
    */
 164  0
   public JTabbedPane getPanel(int location) {
 165  0
     JTabbedPane c = null;
 166  0
     switch(location) {
 167  0
       case NORTH:
 168  0
         c = (JTabbedPane) panels[NORTH].getTopComponent();
 169  0
         break;
 170  0
       case EAST:
 171  0
         c = (JTabbedPane) panels[EAST].getRightComponent();
 172  0
         break;
 173  0
       case SOUTH:
 174  0
         c = (JTabbedPane) panels[SOUTH].getBottomComponent();
 175  0
         break;
 176  0
       case WEST:
 177  0
         c = (JTabbedPane) panels[WEST].getLeftComponent();
 178  0
         break;
 179   
     }
 180  0
     return c;
 181   
   }
 182   
 
 183   
   /**
 184   
    * show/hide dividers according to visibility of
 185   
    * their associated plug-in containers
 186   
    */
 187  75
   public void adjustDividerSizes() {
 188  75
     JTabbedPane tp;
 189  75
     JSplitPane sp;
 190   
 
 191  75
     sp = panels[NORTH];
 192  75
     if(sp.getTopComponent().isVisible()) {
 193   
       //System.out.println("north panel top is visible, showing divider");
 194  75
       sp.setDividerSize(2);
 195   
     }
 196   
     else {
 197  0
       sp.setDividerSize(0);
 198   
     }
 199   
 
 200  75
     sp = panels[WEST];
 201  75
     if(sp.getLeftComponent().isVisible()) {
 202   
       //System.out.println("west panel left is visible, showing divider");
 203  75
       sp.setDividerSize(2);
 204   
     }
 205   
     else {
 206  0
       sp.setDividerSize(0);
 207   
     }
 208   
 
 209  75
     sp = panels[SOUTH];
 210  75
     if(sp.getBottomComponent().isVisible()) {
 211   
       //System.out.println("south panel bottom is visible, showing divider");
 212  75
       sp.setDividerSize(2);
 213   
     }
 214   
     else {
 215  0
       sp.setDividerSize(0);
 216   
     }
 217   
 
 218  75
     sp = panels[EAST];
 219  75
     if(sp.getRightComponent().isVisible()) {
 220   
       //System.out.println("south panel right is visible, showing divider");
 221  75
       sp.setDividerSize(2);
 222   
     }
 223   
     else {
 224  0
       sp.setDividerSize(0);
 225   
     }
 226   
 
 227   
   }
 228   
 
 229   
   /**
 230   
    * add a plug-in container to this SplitPanel ata given location
 231   
    *
 232   
    * @param c  the plug-in container to add
 233   
    * @param location  the location to add to (SplitPanel.NORTH, SOUTH, etc.)
 234   
    */
 235  0
   public void addComponent(JTabbedPane c, int location) {
 236  0
     if(majorAxis == this.MAJOR_AXIS_VERTICAL) {
 237   
       //System.out.println("SplitPanel.addComponent majorAxis = vertical");
 238  0
       switch(location) {
 239  0
         case CENTER:
 240  0
           Debug.print("SplitPanel.addComponent CENTER");
 241  0
           panels[NORTH].remove(panels[NORTH].getBottomComponent());
 242  0
           panels[NORTH].setBottomComponent(c);
 243  0
           break;
 244  0
         case NORTH:
 245  0
           Debug.print("SplitPanel.addComponent NORTH");
 246  0
           panels[NORTH].remove(panels[NORTH].getTopComponent());
 247  0
           panels[NORTH].setTopComponent(c);
 248  0
           break;
 249  0
         case WEST:
 250  0
           Debug.print("SplitPanel.addComponent WEST");
 251  0
           panels[WEST].remove(panels[WEST].getLeftComponent());
 252  0
           panels[WEST].setLeftComponent(c);
 253  0
           break;
 254  0
         case SOUTH:
 255  0
           Debug.print("SplitPanel.addComponent SOUTH");
 256  0
           panels[SOUTH].remove(panels[SOUTH].getBottomComponent());
 257  0
           panels[SOUTH].setBottomComponent(c);
 258  0
           break;
 259  0
         case EAST:
 260   
           //System.out.println("SplitPanel.addComponent EAST");
 261  0
           panels[EAST].remove(panels[EAST].getRightComponent());
 262  0
           panels[EAST].setRightComponent(c);
 263  0
           break;
 264   
       }
 265   
     }
 266   
   }
 267   
 
 268   
   /* --------------- class fields start --------------- */
 269   
 
 270   
   /** constant for the major axis being the horizontal one */
 271   
   public static final int MAJOR_AXIS_HORIZONTAL = 1;
 272   
 
 273   
   /** constant for the major axis being the vertical one */
 274   
   public static final int MAJOR_AXIS_VERTICAL = 2;
 275   
 
 276   
   /** constant for the north plug-in container of this SplitPanel */
 277   
   public static final int NORTH = 0;
 278   
 
 279   
   /** constant for the east plug-in container of this SplitPanel */
 280   
   public static final int EAST = 1;
 281   
 
 282   
   /** constant for the south plug-in container of this SplitPanel */
 283   
   public static final int SOUTH = 2;
 284   
 
 285   
   /** constant for the west plug-in container of this SplitPanel */
 286   
   public static final int WEST = 3;
 287   
 
 288   
   /** constant for the center panel of this SplitPanel */
 289   
   public static final int CENTER = 4;
 290   
 
 291   
   /** the outer panels of this SplitPanel */
 292   
   private JSplitPane[] panels = new JSplitPane[4];
 293   
 
 294   
   /** current setting for major axis of this SplitPanel */
 295   
   private int majorAxis = this.MAJOR_AXIS_VERTICAL;
 296   
 
 297   
   /* ------ class fields end ------------------ */
 298   
 }