Clover coverage report -
Coverage timestamp: Sun Apr 18 2004 21:32:30 EDT
file stats: LOC: 294   Methods: 9
NCLOC: 184   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
BorderPanel.java 36.4% 67.4% 88.9% 63.3%
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   
 import javax.swing.JPanel;
 20   
 import javax.swing.border.TitledBorder;
 21   
 import javax.swing.border.EtchedBorder;
 22   
 import java.util.Vector;
 23   
 import javax.swing.JLabel;
 24   
 import java.awt.GridBagLayout;
 25   
 import java.awt.GridBagConstraints;
 26   
 import java.awt.Color;
 27   
 import javax.swing.text.AttributeSet;
 28   
 import javax.swing.text.SimpleAttributeSet;
 29   
 import javax.swing.text.html.CSS;
 30   
 import java.util.Enumeration;
 31   
 import java.util.prefs.*;
 32   
 
 33   
 /**
 34   
  * Panel to show and manipulate border settings for a rectangular
 35   
  * object such as a table cell
 36   
  *
 37   
  * @author Ulrich Hilger
 38   
  * @author Light Development
 39   
  * @author <a href="http://www.lightdev.com">http://www.lightdev.com</a>
 40   
  * @author <a href="mailto:info@lightdev.com">info@lightdev.com</a>
 41   
  * @author published under the terms and conditions of the
 42   
  *      GNU General Public License,
 43   
  *      for details see file gpl.txt in the distribution
 44   
  *      package of this software
 45   
  *
 46   
  * @version stage 11, April 27, 2003
 47   
  */
 48   
 
 49   
 public class BorderPanel extends JPanel implements AttributeComponent {
 50   
 
 51   
     private Vector components = new Vector();
 52   
 
 53   
     /** the attributes for border width */
 54   
     private CombinedAttribute bWidth;
 55   
 
 56   
     /** the attributes for border color */
 57   
     private CombinedAttribute bColor;
 58   
 
 59   
     /** the color value to compare to for determining changes */
 60   
     private String oColor;
 61   
 
 62   
     /** the width value to compare to for determining changes */
 63   
     private String oWidth;
 64   
 
 65   
     /** indicates if a call to setValue is for initial setting or for changes */
 66   
     private int setValueCalls = 0;
 67   
 
 68  1
     public BorderPanel() {
 69  1
       super();
 70   
 
 71   
       // set layout
 72  1
       GridBagLayout g = new GridBagLayout();
 73  1
       setLayout(g);
 74   
 
 75   
       // constraints to use on our GridBagLayout
 76  1
       GridBagConstraints c = new GridBagConstraints();
 77   
 
 78  1
       addSettings(g,
 79   
                   c,
 80   
                   FrmMain.dynRes.getResourceString(FrmMain.resources, "topLabel"),
 81   
                   CombinedAttribute.ATTR_TOP,
 82   
                   0,
 83   
                   0);
 84  1
       addSettings(g,
 85   
                   c,
 86   
                   FrmMain.dynRes.getResourceString(FrmMain.resources, "rightLabel"),
 87   
                   CombinedAttribute.ATTR_RIGHT,
 88   
                   1,
 89   
                   1);
 90  1
       addSettings(g,
 91   
                   c,
 92   
                   FrmMain.dynRes.getResourceString(FrmMain.resources, "bottomLabel"),
 93   
                   CombinedAttribute.ATTR_BOTTOM,
 94   
                   1,
 95   
                   0);
 96  1
       addSettings(g,
 97   
                   c,
 98   
                   FrmMain.dynRes.getResourceString(FrmMain.resources, "leftLabel"),
 99   
                   CombinedAttribute.ATTR_LEFT,
 100   
                   0,
 101   
                   1);
 102   
     }
 103   
 
 104  4
     private void addSettings(GridBagLayout g, GridBagConstraints c,
 105   
                              String title, int side,
 106   
                              int x, int y)
 107   
     {
 108  4
       BorderSettings bs = new BorderSettings(title, side);
 109  4
       Util.addGridBagComponent(this, bs, g, c, x, y, GridBagConstraints.WEST);
 110  4
       components.addElement(bs);
 111   
     }
 112   
 
 113   
     /**
 114   
      * set the value of this <code>AttributeComponent</code>
 115   
      *
 116   
      * @param a  the set of attributes possibly having an
 117   
      *          attribute this component can display
 118   
      *
 119   
      * @return true, if the set of attributes had a matching attribute,
 120   
      *            false if not
 121   
      */
 122  1
     public boolean setValue(AttributeSet a) {
 123  1
       boolean success = true;
 124  1
       Enumeration e = components.elements();
 125  1
       bWidth = new CombinedAttribute(CSS.Attribute.BORDER_WIDTH, a, true);
 126  1
       bColor = new CombinedAttribute(CSS.Attribute.BORDER_COLOR, a, true);
 127  1
       if(++setValueCalls < 2) {
 128  1
         oColor = bColor.getAttribute();
 129  1
         oWidth = bWidth.getAttribute();
 130   
       }
 131  1
       while(e.hasMoreElements()) {
 132  4
         ((BorderSettings) e.nextElement()).setValue(bWidth, bColor);
 133   
       }
 134  1
       return success;
 135   
     }
 136   
 
 137   
     /**
 138   
      * get the value of this <code>AttributeComponent</code>
 139   
      *
 140   
      * @return the value selected from this component
 141   
      */
 142  1
     public AttributeSet getValue() {
 143  1
       SimpleAttributeSet set = new SimpleAttributeSet();
 144  1
       Enumeration e = components.elements();
 145  1
       BorderSettings bs;
 146  1
       for(int i = 0; i < components.size(); i++) {
 147  4
         bs = (BorderSettings) components.elementAt(i);
 148  4
         bColor.setAttribute(i, bs.getBorderColor());
 149  4
         bWidth.setAttribute(i, bs.getBorderWidth());
 150   
       }
 151  1
       String newValue = bColor.getAttribute();
 152  1
       String writeMode = PrefsDialog.PREFS_WRITE_MODE_HTML32;
 153  1
       try {
 154  1
         Preferences prefs = Preferences.userNodeForPackage(getClass().forName("PrefsDialog"));
 155  1
         writeMode = prefs.get(PrefsDialog.PREFSID_WRITE_MODE, writeMode);
 156   
       }
 157   
       catch(Exception ex) {}
 158  1
       if(writeMode.equalsIgnoreCase(PrefsDialog.PREFS_WRITE_MODE_HTML32)) {
 159  1
         newValue = bWidth.getAttribute(CombinedAttribute.ATTR_TOP);
 160  1
         Util.styleSheet().addCSSAttribute(set, CSS.Attribute.BORDER_TOP_WIDTH, newValue);
 161  1
         Util.styleSheet().addCSSAttribute(set, CSS.Attribute.BORDER_RIGHT_WIDTH, newValue);
 162  1
         Util.styleSheet().addCSSAttribute(set, CSS.Attribute.BORDER_BOTTOM_WIDTH, newValue);
 163  1
         Util.styleSheet().addCSSAttribute(set, CSS.Attribute.BORDER_LEFT_WIDTH, newValue);
 164   
       }
 165   
       else {
 166  0
         if((((oColor == null) && (newValue != null)) || (!oColor.equalsIgnoreCase(newValue)))) {
 167  0
           set.addAttribute(CSS.Attribute.BORDER_COLOR, newValue);
 168   
         }
 169  0
         newValue = bWidth.getAttribute();
 170  0
         if(!oWidth.equalsIgnoreCase(newValue)) {
 171  0
           set.addAttribute(CSS.Attribute.BORDER_WIDTH, newValue);
 172   
         }
 173   
       }
 174  1
       return set;
 175   
     }
 176   
 
 177  0
     public AttributeSet getValue(boolean includeUnchanged) {
 178  0
       if(includeUnchanged) {
 179  0
         SimpleAttributeSet set = new SimpleAttributeSet();
 180  0
         Enumeration e = components.elements();
 181  0
         BorderSettings bs;
 182  0
         for(int i = 0; i < components.size(); i++) {
 183  0
           bs = (BorderSettings) components.elementAt(i);
 184  0
           bColor.setAttribute(i, bs.getBorderColor());
 185  0
           bWidth.setAttribute(i, bs.getBorderWidth());
 186   
         }
 187  0
         String newValue = bColor.getAttribute();
 188  0
         String writeMode = PrefsDialog.PREFS_WRITE_MODE_HTML32;
 189  0
         try {
 190  0
           Preferences prefs = Preferences.userNodeForPackage(getClass().forName("PrefsDialog"));
 191  0
           writeMode = prefs.get(PrefsDialog.PREFSID_WRITE_MODE, writeMode);
 192   
         }
 193   
         catch(Exception ex) {}
 194  0
         if(writeMode.equalsIgnoreCase(PrefsDialog.PREFS_WRITE_MODE_HTML32)) {
 195  0
           newValue = bWidth.getAttribute(CombinedAttribute.ATTR_TOP);
 196  0
           Util.styleSheet().addCSSAttribute(set, CSS.Attribute.BORDER_TOP_WIDTH, newValue);
 197  0
           Util.styleSheet().addCSSAttribute(set, CSS.Attribute.BORDER_RIGHT_WIDTH, newValue);
 198  0
           Util.styleSheet().addCSSAttribute(set, CSS.Attribute.BORDER_BOTTOM_WIDTH, newValue);
 199  0
           Util.styleSheet().addCSSAttribute(set, CSS.Attribute.BORDER_LEFT_WIDTH, newValue);
 200   
         }
 201   
         else {
 202  0
           set.addAttribute(CSS.Attribute.BORDER_COLOR, newValue);
 203  0
           newValue = bWidth.getAttribute();
 204  0
           set.addAttribute(CSS.Attribute.BORDER_WIDTH, newValue);
 205   
         }
 206  0
         return set;
 207   
       }
 208   
       else {
 209  0
         return getValue();
 210   
       }
 211   
     }
 212   
 
 213   
     /**
 214   
      * Panel to show and manipulate border settings
 215   
      */
 216   
     private class BorderSettings extends JPanel {
 217   
 
 218   
       /** the border side */
 219   
       private int side;
 220   
 
 221   
       /** selector for border width */
 222   
       private SizeSelectorPanel bWidth;
 223   
 
 224   
       /** selector for border color */
 225   
       private ColorPanel bColor;
 226   
 
 227   
       /**
 228   
        * construct a <code>BorderSettings</code> panel
 229   
        *
 230   
        * @param title  the title of this object
 231   
        * @param borderKey  the attribute key for the border width this
 232   
        * object represents
 233   
        * @param colorKey  the attribute key for the border color this
 234   
        * object represents
 235   
        */
 236  4
       public BorderSettings(String title, int side) {
 237  4
         super();
 238   
 
 239  4
         this.side = side;
 240   
         // set layout
 241  4
         GridBagLayout g = new GridBagLayout();
 242  4
         setLayout(g);
 243   
 
 244   
         // constraints to use on our GridBagLayout
 245  4
         GridBagConstraints c = new GridBagConstraints();
 246   
 
 247   
         // set border and title
 248  4
         setBorder(new TitledBorder(new EtchedBorder(
 249   
                     EtchedBorder.LOWERED),
 250   
                     title));
 251   
         // add the width control and label
 252  4
         Util.addGridBagComponent(this,
 253   
                    new JLabel(FrmMain.dynRes.getResourceString(FrmMain.resources, "borderWidthLabel")),
 254   
                    g, c, 0, 0, GridBagConstraints.EAST);
 255  4
         bWidth = new SizeSelectorPanel(CSS.Attribute.BORDER_WIDTH, null, false, SizeSelectorPanel.TYPE_LABEL);
 256  4
         Util.addGridBagComponent(this,bWidth,
 257   
                                  g, c, 1, 0, GridBagConstraints.WEST);
 258   
 
 259   
         // add the color control and label
 260  4
         Util.addGridBagComponent(this,
 261   
                    new JLabel(FrmMain.dynRes.getResourceString(FrmMain.resources, "borderColorLabel")),
 262   
                    g, c, 0, 1, GridBagConstraints.EAST);
 263  4
         bColor = new ColorPanel(null, Color.black, CSS.Attribute.BORDER_COLOR);
 264  4
         Util.addGridBagComponent(this,bColor,
 265   
                                  g, c, 1, 1, GridBagConstraints.WEST);
 266   
       }
 267   
 
 268  4
       public String getBorderColor() {
 269  4
         return bColor.getAttr();
 270   
       }
 271   
 
 272  4
       public String getBorderWidth() {
 273  4
         return bWidth.getAttr();
 274   
       }
 275   
 
 276   
       /**
 277   
        * set the value of this <code>AttributeComponent</code>
 278   
        *
 279   
        * @param color  the <code>CombinedAttribute</code> to take the color from
 280   
        *
 281   
        */
 282  4
       public void setValue(CombinedAttribute borderWidths, CombinedAttribute borderColors) {
 283  4
         String attr = borderColors.getAttribute(side);
 284   
         //System.out.println("BorderSettings setValue attr='" + attr + "'");
 285  4
         if(attr != null) {
 286  4
           this.bColor.setValue(attr);
 287   
         }
 288  4
         attr = borderWidths.getAttribute(side);
 289  4
         if(attr != null) {
 290  4
           this.bWidth.setValue(attr);
 291   
         }
 292   
       }
 293   
     }
 294   
 }