| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| LabelFieldPanel |
|
| 2.5714285714285716;2.571 |
| 1 | /* | |
| 2 | ||
| 3 | dsh-layout Layout managers for lightweight components. | |
| 4 | Copyright (c) 2003-2013 held jointly by the individual authors. | |
| 5 | ||
| 6 | This library is free software; you can redistribute it and/or modify it | |
| 7 | under the terms of the GNU Lesser General Public License as published | |
| 8 | by the Free Software Foundation; either version 3 of the License, or (at | |
| 9 | your option) any later version. | |
| 10 | ||
| 11 | This library is distributed in the hope that it will be useful, but WITHOUT | |
| 12 | ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or | |
| 13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | |
| 14 | License for more details. | |
| 15 | ||
| 16 | You should have received a copy of the GNU Lesser General Public License | |
| 17 | along with this library; if not, write to the Free Software Foundation, | |
| 18 | Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
| 19 | ||
| 20 | > http://www.fsf.org/licensing/licenses/lgpl.html | |
| 21 | > http://www.opensource.org/licenses/lgpl-license.php | |
| 22 | ||
| 23 | */ | |
| 24 | package org.dishevelled.layout; | |
| 25 | ||
| 26 | import java.awt.Component; | |
| 27 | ||
| 28 | import javax.swing.Box; | |
| 29 | import javax.swing.JComponent; | |
| 30 | import javax.swing.JLabel; | |
| 31 | import javax.swing.JPanel; | |
| 32 | import javax.swing.LookAndFeel; | |
| 33 | import javax.swing.UIManager; | |
| 34 | ||
| 35 | /** | |
| 36 | * Label field panel. | |
| 37 | * | |
| 38 | * @author Michael Heuer | |
| 39 | * @version $Revision$ $Date$ | |
| 40 | */ | |
| 41 | public class LabelFieldPanel | |
| 42 | extends JPanel | |
| 43 | { | |
| 44 | /** True if a final component has been added to this label field panel. */ | |
| 45 | private boolean hasFinal; | |
| 46 | ||
| 47 | /** Label field layout for this label field panel. */ | |
| 48 | private final LabelFieldLayout layout; | |
| 49 | ||
| 50 | /** JDK <= 1.5 MacOSX look and feel class name. */ | |
| 51 | private static final String JDK15_MAC_OSX_LOOK_AND_FEEL_CLASS_NAME = "apple.laf.AquaLookAndFeel"; | |
| 52 | ||
| 53 | /** JDK >= 1.6 MacOSX look and feel class name. */ | |
| 54 | private static final String JDK16_MAC_OSX_LOOK_AND_FEEL_CLASS_NAME = "com.apple.laf.AquaLookAndFeel"; | |
| 55 | ||
| 56 | ||
| 57 | /** | |
| 58 | * Create a new label field panel. | |
| 59 | */ | |
| 60 | public LabelFieldPanel() | |
| 61 | { | |
| 62 | 5 | super(); |
| 63 | 5 | layout = new LabelFieldLayout(); |
| 64 | 5 | super.setLayout(layout); |
| 65 | 5 | } |
| 66 | ||
| 67 | ||
| 68 | /** | |
| 69 | * Return the percentage of the width of this label field panel to | |
| 70 | * use for labels. | |
| 71 | * | |
| 72 | * @return the percentage of the width of this label field panel | |
| 73 | * to use for labels | |
| 74 | */ | |
| 75 | public final float getLabelPercent() | |
| 76 | { | |
| 77 | 1 | return layout.getLabelPercent(); |
| 78 | } | |
| 79 | ||
| 80 | /** | |
| 81 | * Set the percentage of the width of this label field panel to | |
| 82 | * use for labels to <code>labelPercent</code>. The | |
| 83 | * sum of the label and field percentages must be less | |
| 84 | * than <code>1.0f</code>. | |
| 85 | * | |
| 86 | * @param labelPercent the percentage of the width of this label field panel | |
| 87 | * to use for labels, must be <code>>= 0.0f</code> and <code><= 1.0f</code> | |
| 88 | */ | |
| 89 | public final void setLabelPercent(final float labelPercent) | |
| 90 | { | |
| 91 | 2 | layout.setLabelPercent(labelPercent); |
| 92 | 0 | } |
| 93 | ||
| 94 | /** | |
| 95 | * Return the percentage of the width of this label field panel to | |
| 96 | * use for fields. | |
| 97 | * | |
| 98 | * @return the percentage of the width of this label field panel | |
| 99 | * to use for fields | |
| 100 | */ | |
| 101 | public final float getFieldPercent() | |
| 102 | { | |
| 103 | 1 | return layout.getFieldPercent(); |
| 104 | } | |
| 105 | ||
| 106 | /** | |
| 107 | * Set the percentage of the width of this label field panel to use | |
| 108 | * for fields to <code>fieldPercent</code>. The sum of | |
| 109 | * the label and field percentages must be less than or | |
| 110 | * equal to <code>1.0f</code>. | |
| 111 | * | |
| 112 | * @param fieldPercent the percentage of the width of this label field panel | |
| 113 | * to use for fields, must be <code>>= 0.0f</code> and <code><= 1.0f</code> | |
| 114 | */ | |
| 115 | public final void setFieldPercent(final float fieldPercent) | |
| 116 | { | |
| 117 | 2 | layout.setFieldPercent(fieldPercent); |
| 118 | 0 | } |
| 119 | ||
| 120 | /** | |
| 121 | * Add the specified label to this label field panel. | |
| 122 | * | |
| 123 | * @param text label text | |
| 124 | */ | |
| 125 | public final void addLabel(final String text) | |
| 126 | { | |
| 127 | 2 | JLabel label = new JLabel(text); |
| 128 | 2 | addLabel(label); |
| 129 | 2 | } |
| 130 | ||
| 131 | /** | |
| 132 | * Add the specified label to this label field panel. | |
| 133 | * | |
| 134 | * @param label label to add, must not be null | |
| 135 | */ | |
| 136 | public final void addLabel(final JLabel label) | |
| 137 | { | |
| 138 | 4 | if (label == null) |
| 139 | { | |
| 140 | 1 | throw new IllegalArgumentException("label must not be null"); |
| 141 | } | |
| 142 | 3 | super.add(label, layout.wideLabel()); |
| 143 | 3 | layout.nextLine(); |
| 144 | 3 | } |
| 145 | ||
| 146 | /** | |
| 147 | * Add the specified field to this label field panel. | |
| 148 | * | |
| 149 | * @param field field to add, must not be null | |
| 150 | */ | |
| 151 | public final void addField(final JComponent field) | |
| 152 | { | |
| 153 | 2 | if (field == null) |
| 154 | { | |
| 155 | 1 | throw new IllegalArgumentException("field must not be null"); |
| 156 | } | |
| 157 | 1 | super.add(field, layout.wideField()); |
| 158 | 1 | layout.nextLine(); |
| 159 | 1 | } |
| 160 | ||
| 161 | /** | |
| 162 | * Add the specified field to this label field panel. | |
| 163 | * | |
| 164 | * @param labelText label text | |
| 165 | * @param fieldText field text | |
| 166 | */ | |
| 167 | public final void addField(final String labelText, final String fieldText) | |
| 168 | { | |
| 169 | 2 | addField(labelText, new JLabel(fieldText)); |
| 170 | 2 | } |
| 171 | ||
| 172 | /** | |
| 173 | * Add the specified label and field to this label field panel. | |
| 174 | * | |
| 175 | * @param text label text | |
| 176 | * @param field field to add, must not be null | |
| 177 | */ | |
| 178 | public final void addField(final String text, final JComponent field) | |
| 179 | { | |
| 180 | 5 | if (field == null) |
| 181 | { | |
| 182 | 1 | throw new IllegalArgumentException("field must not be null"); |
| 183 | } | |
| 184 | 4 | JLabel label = new JLabel(text); |
| 185 | 4 | label.setLabelFor(field); |
| 186 | 4 | addField(label, field); |
| 187 | 4 | } |
| 188 | ||
| 189 | /** | |
| 190 | * Add the specified label and field to this label field panel. | |
| 191 | * | |
| 192 | * @param label label to add, must not be null | |
| 193 | * @param field field to add, must not be null | |
| 194 | */ | |
| 195 | public final void addField(final JLabel label, final JComponent field) | |
| 196 | { | |
| 197 | 8 | if (label == null) |
| 198 | { | |
| 199 | 1 | throw new IllegalArgumentException("label must not be null"); |
| 200 | } | |
| 201 | 7 | if (field == null) |
| 202 | { | |
| 203 | 1 | throw new IllegalArgumentException("field must not be null"); |
| 204 | } | |
| 205 | 6 | if (field instanceof JLabel) |
| 206 | { | |
| 207 | 3 | if (isMacOSXLookAndFeel()) |
| 208 | { | |
| 209 | 3 | label.setHorizontalAlignment(JLabel.RIGHT); |
| 210 | 3 | super.add(label, layout.macLabelLabel()); |
| 211 | } | |
| 212 | else | |
| 213 | { | |
| 214 | 0 | super.add(label, layout.labelLabel()); |
| 215 | } | |
| 216 | 3 | super.add(field, layout.labelField()); |
| 217 | } | |
| 218 | else | |
| 219 | { | |
| 220 | 3 | if (isMacOSXLookAndFeel()) |
| 221 | { | |
| 222 | 3 | label.setHorizontalAlignment(JLabel.RIGHT); |
| 223 | 3 | super.add(label, layout.macLabel()); |
| 224 | } | |
| 225 | else | |
| 226 | { | |
| 227 | 0 | super.add(label, layout.label()); |
| 228 | } | |
| 229 | 3 | super.add(field, layout.field()); |
| 230 | } | |
| 231 | 6 | layout.nextLine(); |
| 232 | 6 | } |
| 233 | ||
| 234 | /** | |
| 235 | * Add spacing to this label field panel of at least the specified | |
| 236 | * number of pixels. | |
| 237 | * | |
| 238 | * @param spacing number of pixels, must be <code>>= 0</code> | |
| 239 | */ | |
| 240 | public final void addSpacing(final int spacing) | |
| 241 | { | |
| 242 | 3 | if (spacing < 0) |
| 243 | { | |
| 244 | 1 | throw new IllegalArgumentException("spacing must be at least zero, was " + spacing); |
| 245 | } | |
| 246 | 2 | super.add(Box.createVerticalStrut(spacing), layout.spacing()); |
| 247 | 2 | layout.nextLine(); |
| 248 | 2 | } |
| 249 | ||
| 250 | /** | |
| 251 | * Add final spacing to this label field panel, that is spacing | |
| 252 | * that stretches vertically to cover space left at the bottom of | |
| 253 | * this container. Only one final component may be added to this | |
| 254 | * label field panel. | |
| 255 | * | |
| 256 | * @throws IllegalStateException if a final component has already been | |
| 257 | * added to this label field panel | |
| 258 | */ | |
| 259 | public final void addFinalSpacing() | |
| 260 | { | |
| 261 | 5 | if (hasFinal) |
| 262 | { | |
| 263 | 3 | throw new IllegalStateException("already added a final component to this label field panel"); |
| 264 | } | |
| 265 | 2 | super.add(Box.createGlue(), layout.finalSpacing()); |
| 266 | 2 | hasFinal = true; |
| 267 | 2 | } |
| 268 | ||
| 269 | /** | |
| 270 | * Add final spacing to this label field panel of at least the specified | |
| 271 | * number of pixels, that is spacing that stretches vertically to cover | |
| 272 | * space left at the bottom of this container. Only one final component | |
| 273 | * may be added to this label field panel. | |
| 274 | * | |
| 275 | * @param spacing minimum number of pixels, must be <code>>= 0</code> | |
| 276 | * @throws IllegalStateException if a final component has already been | |
| 277 | * added to this label field panel | |
| 278 | */ | |
| 279 | public final void addFinalSpacing(final int spacing) | |
| 280 | { | |
| 281 | 5 | if (hasFinal) |
| 282 | { | |
| 283 | 3 | throw new IllegalStateException("already added a final component to this label field panel"); |
| 284 | } | |
| 285 | 2 | if (spacing < 0) |
| 286 | { | |
| 287 | 1 | throw new IllegalArgumentException("spacing must be at least zero, was " + spacing); |
| 288 | } | |
| 289 | 1 | super.add(Box.createVerticalStrut(spacing), layout.finalSpacing()); |
| 290 | 1 | hasFinal = true; |
| 291 | 1 | } |
| 292 | ||
| 293 | /** | |
| 294 | * Add the specified final field to this label field panel, | |
| 295 | * that is a field that stretches horizontally across both the | |
| 296 | * label and field columns and stretches vertically to cover space | |
| 297 | * left at the bottom of this container. Only one final component | |
| 298 | * may be added to this label field panel. | |
| 299 | * | |
| 300 | * @param field field to add, must not be null | |
| 301 | * @throws IllegalStateException if a final component has already been | |
| 302 | * added to this label field panel | |
| 303 | */ | |
| 304 | public final void addFinalField(final JComponent field) | |
| 305 | { | |
| 306 | 5 | if (hasFinal) |
| 307 | { | |
| 308 | 3 | throw new IllegalStateException("already added a final component to this label field panel"); |
| 309 | } | |
| 310 | 2 | if (field == null) |
| 311 | { | |
| 312 | 1 | throw new IllegalArgumentException("field must not be null"); |
| 313 | } | |
| 314 | 1 | super.add(field, layout.finalWideField()); |
| 315 | 1 | hasFinal = true; |
| 316 | 1 | } |
| 317 | ||
| 318 | // override JPanel methods | |
| 319 | ||
| 320 | /** {@inheritDoc} */ | |
| 321 | public final Component add(final Component component) | |
| 322 | { | |
| 323 | 1 | throw new UnsupportedOperationException("add operation not supported by LabelFieldPanel"); |
| 324 | } | |
| 325 | ||
| 326 | /** {@inheritDoc} */ | |
| 327 | public final Component add(final String name, final Component component) | |
| 328 | { | |
| 329 | 1 | throw new UnsupportedOperationException("add operation not supported by LabelFieldPanel"); |
| 330 | } | |
| 331 | ||
| 332 | /** {@inheritDoc} */ | |
| 333 | public final Component add(final Component component, final int index) | |
| 334 | { | |
| 335 | 1 | throw new UnsupportedOperationException("add operation not supported by LabelFieldPanel"); |
| 336 | } | |
| 337 | ||
| 338 | /** {@inheritDoc} */ | |
| 339 | public final void add(final Component component, final Object constraints) | |
| 340 | { | |
| 341 | 1 | throw new UnsupportedOperationException("add operation not supported by LabelFieldPanel"); |
| 342 | } | |
| 343 | ||
| 344 | /** {@inheritDoc} */ | |
| 345 | public final void add(final Component component, final Object constraints, final int index) | |
| 346 | { | |
| 347 | 1 | throw new UnsupportedOperationException("add operation not supported by LabelFieldPanel"); |
| 348 | } | |
| 349 | ||
| 350 | ||
| 351 | /** | |
| 352 | * Return true if the current look and feel is the MacOSX | |
| 353 | * look and feel, specifically <code>apple.laf.AquaLookAndFeel</code>. | |
| 354 | * | |
| 355 | * @see #MAC_OSX_LOOK_AND_FEEL_CLASS_NAME | |
| 356 | * @return true if the current look and feel is the MacOSX look and feel | |
| 357 | */ | |
| 358 | private static boolean isMacOSXLookAndFeel() | |
| 359 | { | |
| 360 | 6 | LookAndFeel lookAndFeel = UIManager.getLookAndFeel(); |
| 361 | 6 | if (lookAndFeel == null) |
| 362 | { | |
| 363 | 0 | return false; |
| 364 | } | |
| 365 | 6 | return JDK15_MAC_OSX_LOOK_AND_FEEL_CLASS_NAME.equals(lookAndFeel.getClass().getName()) |
| 366 | || JDK16_MAC_OSX_LOOK_AND_FEEL_CLASS_NAME.equals(lookAndFeel.getClass().getName()); | |
| 367 | } | |
| 368 | } |