Coverage Report - org.jtheque.core.utils.ui.PanelBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
PanelBuilder
0 %
0/86
0 %
0/10
1.147
 
 1  
 package org.jtheque.core.utils.ui;
 2  
 
 3  
 import org.jdesktop.swingx.JXTree;
 4  
 import org.jtheque.core.managers.Managers;
 5  
 import org.jtheque.core.managers.view.able.IViewManager;
 6  
 import org.jtheque.core.managers.view.impl.components.JThequeCheckBox;
 7  
 import org.jtheque.core.managers.view.impl.components.JThequeI18nLabel;
 8  
 import org.jtheque.utils.ui.ButtonBarBuilder;
 9  
 import org.jtheque.utils.ui.GridBagUtils;
 10  
 
 11  
 import javax.swing.Action;
 12  
 import javax.swing.DefaultComboBoxModel;
 13  
 import javax.swing.JButton;
 14  
 import javax.swing.JCheckBox;
 15  
 import javax.swing.JComboBox;
 16  
 import javax.swing.JComponent;
 17  
 import javax.swing.JLabel;
 18  
 import javax.swing.JList;
 19  
 import javax.swing.JPanel;
 20  
 import javax.swing.JScrollPane;
 21  
 import javax.swing.JSeparator;
 22  
 import javax.swing.JTextArea;
 23  
 import javax.swing.ListCellRenderer;
 24  
 import javax.swing.ListModel;
 25  
 import javax.swing.ListSelectionModel;
 26  
 import javax.swing.border.Border;
 27  
 import javax.swing.tree.TreeCellRenderer;
 28  
 import javax.swing.tree.TreeModel;
 29  
 import java.awt.Color;
 30  
 import java.awt.Component;
 31  
 import java.awt.Font;
 32  
 import java.awt.GridBagLayout;
 33  
 import java.awt.Insets;
 34  
 import java.awt.LayoutManager;
 35  
 
 36  
 /*
 37  
  * This file is part of JTheque.
 38  
  *
 39  
  * JTheque is free software: you can redistribute it and/or modify
 40  
  * it under the terms of the GNU General Public License as published by
 41  
  * the Free Software Foundation, either version 3 of the License.
 42  
  *
 43  
  * JTheque is distributed in the hope that it will be useful,
 44  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 45  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 46  
  * GNU General Public License for more details.
 47  
  *
 48  
  * You should have received a copy of the GNU General Public License
 49  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 50  
  */
 51  
 
 52  
 /**
 53  
  * A panel builder.
 54  
  *
 55  
  * @author Baptiste Wicht
 56  
  */
 57  
 public class PanelBuilder {
 58  
     private final JPanel panel;
 59  
 
 60  
     private final GridBagUtils gbc;
 61  
 
 62  
     public static final int BOLD = 1;
 63  
     public static final int ITALIC = 2;
 64  
 
 65  
     /**
 66  
      * Construct a new PanelBuilder.
 67  
      */
 68  
     public PanelBuilder() {
 69  0
         this(new JPanel(), true);
 70  0
     }
 71  
 
 72  
     /**
 73  
      * Construct a new panel builder.
 74  
      *
 75  
      * @param panel The panel to build.
 76  
      */
 77  
     public PanelBuilder(JPanel panel) {
 78  0
         this(panel, true);
 79  0
     }
 80  
 
 81  
     /**
 82  
      * Construct a new PanelBuilder with a specified layout.
 83  
      *
 84  
      * @param layout The layout to set to the builded panel.
 85  
      */
 86  
     public PanelBuilder(LayoutManager layout){
 87  0
         this(new JPanel(layout), false);
 88  0
     }
 89  
 
 90  
     /**
 91  
      * Construct a new PanelBuilder.
 92  
      *
 93  
      * @param panel The panel to build.
 94  
      * @param layout If true set a default layout (GridBagLayout) to the builded panel.
 95  
      */
 96  
     public PanelBuilder(JPanel panel, boolean layout){
 97  0
         super();
 98  
 
 99  0
         if(layout){
 100  0
             panel.setLayout(new GridBagLayout());
 101  
         }
 102  
 
 103  0
         this.panel = panel;
 104  0
         gbc = new GridBagUtils();
 105  
 
 106  0
         initJThequeDefaults();
 107  0
     }
 108  
 
 109  
     /**
 110  
      * Init the panel with the JTheque defaults.
 111  
      */
 112  
     void initJThequeDefaults() {
 113  0
         panel.setBackground(Managers.getManager(IViewManager.class).getViewDefaults().getBackgroundColor());
 114  0
         panel.setBorder(Borders.DIALOG_BORDER);
 115  0
     }
 116  
 
 117  
     /**
 118  
      * Add a component to the panel with the specified constraints.
 119  
      *
 120  
      * @param component   The component to add.
 121  
      * @param constraints The constraints to use to add to the panel.
 122  
      * @param <T>         The type of component.
 123  
      * @return The added component.
 124  
      */
 125  
     public <T extends Component> T add(T component, Object constraints) {
 126  0
         panel.add(component, constraints);
 127  
 
 128  0
         return component;
 129  
     }
 130  
 
 131  
     /**
 132  
      * Add a combo box.
 133  
      *
 134  
      * @param model       The combo box model.
 135  
      * @param constraints The constraints to use to add to the panel.
 136  
      * @return The added JComboBox.
 137  
      */
 138  
     public JComboBox addComboBox(DefaultComboBoxModel model, Object constraints) {
 139  0
         JComboBox combo = new JComboBox(model);
 140  
 
 141  0
         combo.setBackground(Managers.getManager(IViewManager.class).getViewDefaults().getBackgroundColor());
 142  
 
 143  0
         return add(combo, constraints);
 144  
     }
 145  
 
 146  
     /**
 147  
      * Add a combo box with a specific renderer.
 148  
      *
 149  
      * @param model       The combo box model.
 150  
      * @param renderer    The renderer to use.
 151  
      * @param constraints The constraints to use to add to the panel.
 152  
      * @return The added JComboBox.
 153  
      */
 154  
     public JComboBox addComboBox(DefaultComboBoxModel model, ListCellRenderer renderer, Object constraints) {
 155  0
         JComboBox combo = addComboBox(model, constraints);
 156  
 
 157  0
         combo.setRenderer(renderer);
 158  
 
 159  0
         return combo;
 160  
     }
 161  
 
 162  
     /**
 163  
      * Add an internationalized check box.
 164  
      *
 165  
      * @param key         The internationalization key.
 166  
      * @param constraints The constraints to use to add to the panel.
 167  
      * @return The added JCheckBox.
 168  
      */
 169  
     public JCheckBox addI18nCheckBox(String key, Object constraints) {
 170  0
         return add(new JThequeCheckBox(key), constraints);
 171  
     }
 172  
 
 173  
     /**
 174  
      * Add a <code>Jlist</code> with the specified model.
 175  
      *
 176  
      * @param model       The model to use.
 177  
      * @param renderer    The list renderer.
 178  
      * @param constraints The constraints to use to add to the panel.
 179  
      * @return The added JList.
 180  
      */
 181  
     public JList addList(ListModel model, ListCellRenderer renderer, Object constraints) {
 182  0
         JList list = new JList(model);
 183  0
         list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 184  0
         list.setVisibleRowCount(12);
 185  0
         list.setValueIsAdjusting(true);
 186  
 
 187  0
         if (renderer != null) {
 188  0
             list.setCellRenderer(renderer);
 189  
         }
 190  
 
 191  0
         addScrolled(list, constraints);
 192  
 
 193  0
         return list;
 194  
     }
 195  
 
 196  
     /**
 197  
      * Add a button to the panel.
 198  
      *
 199  
      * @param action      The action to use with the button.
 200  
      * @param constraints The constraints to use to add to the panel.
 201  
      * @return The added button.
 202  
      */
 203  
     public JButton addButton(Action action, Object constraints) {
 204  0
         return add(new JButton(action), constraints);
 205  
     }
 206  
 
 207  
     /**
 208  
      * Add a label to the panel.
 209  
      *
 210  
      * @param constraints The constraints to use to add to the panel.
 211  
      * 
 212  
      * @return The added label.
 213  
      */
 214  
     public JLabel addLabel(Object constraints) {
 215  0
         return add(new JLabel(), constraints);
 216  
     }
 217  
 
 218  
     /**
 219  
      * Add a label to the panel.
 220  
      *
 221  
      * @param text        The text of the label.
 222  
      * @param constraints The constraints to use to add to the panel.
 223  
      * @return The added label.
 224  
      */
 225  
     public JLabel addLabel(String text, Object constraints) {
 226  0
         return add(new JLabel(text), constraints);
 227  
     }
 228  
 
 229  
     /**
 230  
      * Add a label with a specific foreground to the panel.
 231  
      *
 232  
      * @param text        The text of the label.
 233  
      * @param foreground  The foreground color.
 234  
      * @param constraints The constraints to use to add to the panel.
 235  
      * @return The added label.
 236  
      */
 237  
     public JLabel addLabel(String text, Color foreground, Object constraints) {
 238  0
         JLabel label = addLabel(text, constraints);
 239  
 
 240  0
         label.setForeground(foreground);
 241  
 
 242  0
         return label;
 243  
     }
 244  
 
 245  
     /**
 246  
      * Add an internationalized label to the panel.
 247  
      *
 248  
      * @param key         The i18n key.
 249  
      * @param constraints The constraints to use to add to the panel.
 250  
      * @return The added label.
 251  
      */
 252  
     public JThequeI18nLabel addI18nLabel(String key, Object constraints) {
 253  0
         return addI18nLabel(key, 0, constraints);
 254  
     }
 255  
 
 256  
     /**
 257  
      * Add an internationalized label with a specified style to the panel.
 258  
      *
 259  
      * @param key         The i18n key.
 260  
      * @param constraints The constraints to use to add to the panel.
 261  
      * @param style       The font style.
 262  
      * @return The added label.
 263  
      */
 264  
     public JThequeI18nLabel addI18nLabel(String key, int style, Object constraints) {
 265  0
         JThequeI18nLabel label = new JThequeI18nLabel(key);
 266  
 
 267  0
         if ((style & BOLD) == BOLD) {
 268  0
             label.setFont(label.getFont().deriveFont(Font.BOLD));
 269  
         }
 270  
 
 271  0
         if ((style & ITALIC) == ITALIC) {
 272  0
             label.setFont(label.getFont().deriveFont(Font.ITALIC));
 273  
         }
 274  
 
 275  0
         return add(label, constraints);
 276  
     }
 277  
 
 278  
     /**
 279  
      * Add a scrolled text area.
 280  
      *
 281  
      * @param text        The text of the text area.
 282  
      * @param constraints The constraints to use to add to the panel.
 283  
      */
 284  
     public void addScrolledTextArea(String text, Object constraints) {
 285  0
         addScrolled(new JTextArea(text), constraints);
 286  0
     }
 287  
 
 288  
     /**
 289  
      * Add a JTree in a JScrollpane
 290  
      *
 291  
      * @param model The tree model.
 292  
      * @param renderer The tree renderer.
 293  
      * @param constraints The constraints to add the tree with.
 294  
      *
 295  
      * @return The added tree.
 296  
      */
 297  
     public JXTree addScrolledTree(TreeModel model, TreeCellRenderer renderer, Object constraints){
 298  0
         JXTree tree = new JXTree(model);
 299  
 
 300  0
         if(renderer != null){
 301  0
             tree.setCellRenderer(renderer);
 302  
         }
 303  
 
 304  0
         addScrolled(tree, constraints);
 305  
 
 306  0
         return tree;
 307  
     }
 308  
 
 309  
     /**
 310  
      * Add a scrolled component.
 311  
      *
 312  
      * @param view        The view.
 313  
      * @param constraints The constraints to use to add to the panel.
 314  
      */
 315  
     public void addScrolled(JComponent view, Object constraints) {
 316  0
         JScrollPane scrollPane = new JScrollPane(view);
 317  
 
 318  0
         scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
 319  0
         scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
 320  
 
 321  0
         add(scrollPane, constraints);
 322  0
     }
 323  
 
 324  
     /**
 325  
      * Add an internationalized separator.
 326  
      *
 327  
      * @param key         The title key.
 328  
      * @param constraints The constraints to use to add to the panel.
 329  
      */
 330  
     public void addI18nSeparator(String key, Object constraints) {
 331  0
         PanelBuilder separatorBuilder = addPanel(constraints);
 332  
 
 333  0
         separatorBuilder.addI18nLabel(key, gbcSet(0, 0));
 334  0
         separatorBuilder.add(new JSeparator(), gbcSet(1, 0, GridBagUtils.HORIZONTAL));
 335  0
     }
 336  
 
 337  
     /**
 338  
      * Add a button bar.
 339  
      *
 340  
      * @param constraints The constraints to use to add to the panel.
 341  
      * @param actions     The actions to add to the button bar.
 342  
      */
 343  
     public void addButtonBar(Object constraints, Action... actions) {
 344  0
         ButtonBarBuilder builder = new ButtonBarBuilder();
 345  
 
 346  0
         builder.addGlue();
 347  
 
 348  0
         builder.addActions(actions);
 349  
 
 350  0
         add(builder.getPanel(), constraints);
 351  0
     }
 352  
 
 353  
     /**
 354  
      * Add a panel to the panel.
 355  
      *
 356  
      * @param constraints The constraints to use to add to the panel.
 357  
      * @return The builder of the intern panel builder.
 358  
      */
 359  
     public PanelBuilder addPanel(Object constraints) {
 360  0
         PanelBuilder builder = new PanelBuilder();
 361  
 
 362  0
         add(builder.getPanel(), constraints);
 363  
 
 364  0
         return builder;
 365  
     }
 366  
 
 367  
     /**
 368  
      * Add a panel to the panel.
 369  
      *
 370  
      * @param layout The layout to use.
 371  
      * @param constraints The constraints to use to add to the panel.
 372  
      *
 373  
      * @return The builder of the intern panel builder.
 374  
      */
 375  
     public PanelBuilder addPanel(LayoutManager layout, Object constraints) {
 376  0
         PanelBuilder builder = new PanelBuilder(layout);
 377  
 
 378  0
         add(builder.getPanel(), constraints);
 379  
 
 380  0
         return builder;
 381  
     }
 382  
 
 383  
     /**
 384  
      * Return the builded panel.
 385  
      *
 386  
      * @return The builded panel.
 387  
      */
 388  
     public JComponent getPanel() {
 389  0
         return panel;
 390  
     }
 391  
 
 392  
     /**
 393  
      * Set the border of the panel.
 394  
      *
 395  
      * @param border The border to set to the panel. 
 396  
      */
 397  
     public void setBorder(Border border){
 398  0
         panel.setBorder(border);
 399  0
     }
 400  
 
 401  
     /* GridBagUtils delegate methods */
 402  
 
 403  
     /**
 404  
      * Set the default insets.
 405  
      *
 406  
      * @param defaultInsets The default insets.
 407  
      */
 408  
     public void setDefaultInsets(Insets defaultInsets) {
 409  0
         gbc.setDefaultInsets(defaultInsets);
 410  0
     }
 411  
 
 412  
     /**
 413  
      * Create a new layout constraints.
 414  
      *
 415  
      * @param x       The x position on the grid.
 416  
      * @param y       The y position on the grid.
 417  
      * @param fill    The fill constraint.
 418  
      * @param anchor  The anchor constraint.
 419  
      * @param weightx The weight in x axis.
 420  
      * @param weighty The weight in y axis.
 421  
      * @return The layout constraints.
 422  
      */
 423  
     public Object gbcSet(int x, int y, int fill, int anchor, double weightx, double weighty) {
 424  0
         return gbc.gbcSet(x, y, fill, anchor, weightx, weighty);
 425  
     }
 426  
 
 427  
     /**
 428  
      * Create a new layout constraints.
 429  
      *
 430  
      * @param x       The x position on the grid.
 431  
      * @param y       The y position on the grid.
 432  
      * @param fill    The fill constraint.
 433  
      * @param anchor  The anchor constraint.
 434  
      * @param width   The col span.
 435  
      * @param height  The row span.
 436  
      * @param weightx The weight in x axis.
 437  
      * @param weighty The weight in y axis.
 438  
      * @return The layout constraints.
 439  
      */
 440  
     public Object gbcSet(int x, int y, int fill, int anchor, int width, int height, double weightx, double weighty) {
 441  0
         return gbc.gbcSet(x, y, fill, anchor, width, height, weightx, weighty);
 442  
     }
 443  
 
 444  
     /**
 445  
      * Configure and return the GridBagConstraints object.
 446  
      *
 447  
      * @param x       The x position of the component.
 448  
      * @param y       The y position of the component.
 449  
      * @param fill    The fill constraints.
 450  
      * @param anchor  The anchor of the component.
 451  
      * @param width   The col span.
 452  
      * @param height  The row span.
 453  
      * @param weightx The col fill weight.
 454  
      * @param weighty The row fill weight.
 455  
      * @param ipadx   The x internal padding width.
 456  
      * @param ipady   The y internal padding height.
 457  
      * @return The <code>GridBagConstraints</code> object.
 458  
      */
 459  
     public Object gbcSet(int x, int y, int fill, int anchor, int width, int height, double weightx, double weighty, int ipadx, int ipady) {
 460  0
         return gbc.gbcSet(x, y, fill, anchor, width, height, weightx, weighty, ipadx, ipady);
 461  
     }
 462  
 
 463  
     /**
 464  
      * Create a new layout constraints.
 465  
      *
 466  
      * @param x      The x position on the grid.
 467  
      * @param y      The y position on the grid.
 468  
      * @param fill   The fill constraint.
 469  
      * @param anchor The anchor constraint.
 470  
      * @param width  The col span.
 471  
      * @param height The row span.
 472  
      * @return The layout constraints.
 473  
      */
 474  
     public Object gbcSet(int x, int y, int fill, int anchor, int width, int height) {
 475  0
         return gbc.gbcSet(x, y, fill, anchor, width, height);
 476  
     }
 477  
 
 478  
     /**
 479  
      * Create a new layout constraints.
 480  
      *
 481  
      * @param x      The x position on the grid.
 482  
      * @param y      The y position on the grid.
 483  
      * @param fill   The fill constraint.
 484  
      * @param width  The col span.
 485  
      * @param height The row span.
 486  
      * @return The layout constraints.
 487  
      */
 488  
     public Object gbcSet(int x, int y, int fill, int width, int height) {
 489  0
         return gbc.gbcSet(x, y, fill, GridBagUtils.BASELINE_LEADING, width, height);
 490  
     }
 491  
 
 492  
     /**
 493  
      * Create a new layout constraints.
 494  
      *
 495  
      * @param x      The x position on the grid.
 496  
      * @param y      The y position on the grid.
 497  
      * @param fill   The fill constraint.
 498  
      * @param anchor The anchor constraint.
 499  
      * @return The layout constraints.
 500  
      */
 501  
     public Object gbcSet(int x, int y, int fill, int anchor) {
 502  0
         return gbc.gbcSet(x, y, fill, anchor);
 503  
     }
 504  
 
 505  
     /**
 506  
      * Create a new layout constraints.
 507  
      *
 508  
      * @param x    The x position on the grid.
 509  
      * @param y    The y position on the grid.
 510  
      * @param fill The fill constraint.
 511  
      * @return The layout constraints.
 512  
      */
 513  
     public Object gbcSet(int x, int y, int fill) {
 514  0
         return gbc.gbcSet(x, y, fill);
 515  
     }
 516  
 
 517  
     /**
 518  
      * Create a new layout constraints.
 519  
      *
 520  
      * @param x The x position on the grid.
 521  
      * @param y The y position on the grid.
 522  
      * @return The layout constraints.
 523  
      */
 524  
     public Object gbcSet(int x, int y) {
 525  0
         return gbc.gbcSet(x, y);
 526  
     }
 527  
 }