001/* 002 * Copyright (c) 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved. 003 * 004 * Redistribution and use in source and binary forms, with or without 005 * modification, are permitted provided that the following conditions are met: 006 * 007 * o Redistributions of source code must retain the above copyright notice, 008 * this list of conditions and the following disclaimer. 009 * 010 * o Redistributions in binary form must reproduce the above copyright notice, 011 * this list of conditions and the following disclaimer in the documentation 012 * and/or other materials provided with the distribution. 013 * 014 * o Neither the name of JGoodies Karsten Lentzsch nor the names of 015 * its contributors may be used to endorse or promote products derived 016 * from this software without specific prior written permission. 017 * 018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 020 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 021 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 022 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 023 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 024 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 025 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 026 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 027 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 028 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030 031package com.jgoodies.forms.util; 032 033import java.util.logging.Logger; 034 035import com.jgoodies.forms.layout.ConstantSize; 036import com.jgoodies.forms.layout.Size; 037 038/** 039 * An abstract class that describes a layout and design style guide. 040 * It provides constants used to lay out panels consistently.<p> 041 * 042 * This class is work in progress and the API may change without notice. 043 * Therefore it is recommended to not write custom subclasses 044 * for production code. 045 * A future version of this class will likely collaborate with a class 046 * <code>LogicalSize</code> or <code>StyledSize</code>. 047 * 048 * @author Karsten Lentzsch 049 * @version $Revision$ 050 * 051 * @see com.jgoodies.forms.util.MacLayoutStyle 052 * @see com.jgoodies.forms.util.WindowsLayoutStyle 053 * @see com.jgoodies.forms.factories.FormFactory 054 * @see com.jgoodies.forms.factories.Borders 055 */ 056 057public abstract class LayoutStyle { 058 059 /** 060 * Holds the current layout style. 061 */ 062 private static LayoutStyle current = initialLayoutStyle(); 063 064 // Computing the initial layout style ************************************* 065 066 /** 067 * Computes and returns the initial <code>LayoutStyle</code>. 068 * Checks the OS name and returns <code>MacLayoutStyle</code> 069 * on Mac OS X and <code>WindowLayoutStyle</code> on all other platforms. 070 * 071 * @return MacLayoutStyle on Mac, WindowsLayoutStyle on all other platforms 072 */ 073 private static LayoutStyle initialLayoutStyle() { 074 if (isOSMac()) { 075 return MacLayoutStyle.INSTANCE; 076 } 077 return WindowsLayoutStyle.INSTANCE; 078 } 079 080 /** 081 * Checks and answers whether Java runs on a Mac by requesting 082 * the system property <em>os.name</em>. 083 * 084 * @return true on Mac, false on all other Platforms 085 */ 086 private static boolean isOSMac() { 087 return getSystemProperty("os.name").startsWith("Mac"); 088 } 089 090 /** 091 * Tries to look up the System property for the given key. 092 * In untrusted environments this may throw a SecurityException. 093 * In this case we catch the exception and answer <code>null</code>. 094 * 095 * @param key the name of the system property 096 * @return the system property's String value, or a blank string 097 * if there's no such value, or a SecurityException has been caught. 098 */ 099 private static String getSystemProperty(String key) { 100 try { 101 return System.getProperty(key); 102 } catch (SecurityException e) { 103 Logger.getLogger(LayoutStyle.class.getName()) 104 .warning("Can't read the System property " + key + "."); 105 return ""; 106 } 107 } 108 109 // Accessing the current style ****************************************** 110 111 /** 112 * Returns the current <code>LayoutStyle</code>. 113 * 114 * @return the current <code>LayoutStyle</code> 115 */ 116 public static LayoutStyle getCurrent() { 117 return current; 118 } 119 120 /** 121 * Set a new <code>LayoutStyle</code>. 122 * 123 * @param newLayoutStyle the style to be set 124 */ 125 public static void setCurrent(LayoutStyle newLayoutStyle) { 126 current = newLayoutStyle; 127 } 128 129 // Layout Sizes ********************************************************* 130 131 /** 132 * Returns this style's default button width. 133 * 134 * @return the default button width 135 * 136 * @see #getDefaultButtonHeight() 137 */ 138 public abstract Size getDefaultButtonWidth(); 139 140 /** 141 * Returns this style's default button height. 142 * 143 * @return the default button height 144 * 145 * @see #getDefaultButtonWidth() 146 */ 147 public abstract Size getDefaultButtonHeight(); 148 149 /** 150 * Returns this style's horizontal margin for general dialogs. 151 * 152 * @return the horizontal margin for general dialogs 153 * 154 * @see #getDialogMarginY() 155 * @see #getTabbedDialogMarginX() 156 */ 157 public abstract ConstantSize getDialogMarginX(); 158 159 /** 160 * Returns this style's vertical margin for general dialogs. 161 * 162 * @return the vertical margin for general dialogs 163 * 164 * @see #getDialogMarginX() 165 * @see #getTabbedDialogMarginY() 166 */ 167 public abstract ConstantSize getDialogMarginY(); 168 169 /** 170 * Returns this style's horizontal margin for dialogs that consist of 171 * a tabbed pane. 172 * 173 * @return the horizontal margin for dialogs that consist of a tabbed pane 174 * @since 1.0.3 175 * 176 * @see #getTabbedDialogMarginY() 177 * @see #getDialogMarginX() 178 */ 179 public abstract ConstantSize getTabbedDialogMarginX(); 180 181 /** 182 * Returns this style's vertical margin for dialogs that consist of 183 * a tabbed pane. 184 * 185 * @return the vertical margin for dialogs that consist of a tabbed pane 186 * @since 1.0.3 187 * 188 * @see #getTabbedDialogMarginX() 189 * @see #getDialogMarginY() 190 */ 191 public abstract ConstantSize getTabbedDialogMarginY(); 192 193 /** 194 * Returns a gap used to separate a label and associated control. 195 * 196 * @return a gap between label and associated control 197 * 198 * @see #getRelatedComponentsPadX() 199 * @see #getUnrelatedComponentsPadX() 200 */ 201 public abstract ConstantSize getLabelComponentPadX(); 202 203 /** 204 * Returns a horizontal gap used to separate related controls. 205 * 206 * @return a horizontal gap between related controls 207 * 208 * @see #getLabelComponentPadX() 209 * @see #getRelatedComponentsPadY() 210 * @see #getUnrelatedComponentsPadX() 211 */ 212 public abstract ConstantSize getRelatedComponentsPadX(); 213 214 /** 215 * Returns a vertical gap used to separate related controls. 216 * 217 * @return a vertical gap between related controls 218 * 219 * @see #getRelatedComponentsPadX() 220 * @see #getUnrelatedComponentsPadY() 221 */ 222 public abstract ConstantSize getRelatedComponentsPadY(); 223 224 /** 225 * Returns a horizontal gap used to separate unrelated controls. 226 * 227 * @return a horizontal gap between unrelated controls 228 * 229 * @see #getLabelComponentPadX() 230 * @see #getUnrelatedComponentsPadY() 231 * @see #getRelatedComponentsPadX() 232 */ 233 public abstract ConstantSize getUnrelatedComponentsPadX(); 234 235 /** 236 * Returns a vertical gap used to separate unrelated controls. 237 * 238 * @return a vertical gap between unrelated controls 239 * 240 * @see #getUnrelatedComponentsPadX() 241 * @see #getRelatedComponentsPadY() 242 */ 243 public abstract ConstantSize getUnrelatedComponentsPadY(); 244 245 /** 246 * Returns a narrow vertical pad used to separate lines. 247 * 248 * @return a narrow vertical pad used to separate lines 249 * 250 * @see #getLinePad() 251 * @see #getParagraphPad() 252 */ 253 public abstract ConstantSize getNarrowLinePad(); 254 255 /** 256 * Returns a narrow vertical pad used to separate lines. 257 * 258 * @return a vertical pad used to separate lines 259 * 260 * @see #getNarrowLinePad() 261 * @see #getParagraphPad() 262 */ 263 public abstract ConstantSize getLinePad(); 264 265 /** 266 * Returns a pad used to separate paragraphs. 267 * 268 * @return a vertical pad used to separate paragraphs 269 * 270 * @see #getNarrowLinePad() 271 * @see #getLinePad() 272 */ 273 public abstract ConstantSize getParagraphPad(); 274 275 /** 276 * Returns a pad used to separate a button bar from a component. 277 * 278 * @return a vertical pad used to separate paragraphs 279 * @since 1.0.3 280 * 281 * @see #getRelatedComponentsPadY() 282 * @see #getUnrelatedComponentsPadY() 283 */ 284 public abstract ConstantSize getButtonBarPad(); 285 286 /** 287 * Checks and answers whether buttons are typically ordered from 288 * left to right or from right to left. Useful for building button bars 289 * that shall comply with the platform's layout style guide.<p> 290 * 291 * For example the Windows style guide recommends to layout out 292 * <em>OK, Cancel, Apply</em> from left to right, where the 293 * Mac Aqua style guide recommends to layout out these buttons 294 * from right to left.<p> 295 * 296 * Although most button sequences shall honor this order 297 * some buttons require a left to right order. For example 298 * <em>Back, Next</em> or <em>Move Left, Move Right</em>.<p> 299 * 300 * @return true if buttons are typically ordered from left to right 301 * @since 1.0.3 302 * 303 * @see com.jgoodies.forms.builder.ButtonBarBuilder 304 * @see com.jgoodies.forms.factories.ButtonBarFactory 305 */ 306 public abstract boolean isLeftToRightButtonOrder(); 307 308}