Coverage Report - org.jtheque.utils.ui.LinedButtonBarBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
LinedButtonBarBuilder
0%
0/18
0%
0/4
1.5
 
 1  
 package org.jtheque.utils.ui;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque 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 JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import javax.swing.Action;
 20  
 import javax.swing.Box;
 21  
 import javax.swing.JButton;
 22  
 import javax.swing.JPanel;
 23  
 import java.awt.Color;
 24  
 import java.awt.Component;
 25  
 import java.awt.GridBagConstraints;
 26  
 import java.awt.GridBagLayout;
 27  
 
 28  
 /**
 29  
  * A builder for a button bar.
 30  
  *
 31  
  * @author Baptiste Wicht
 32  
  */
 33  
 public final class LinedButtonBarBuilder {
 34  
     private final JPanel panel;
 35  
     private final GridBagUtils gbc;
 36  
 
 37  
     private final int[] column;
 38  
 
 39  
     /**
 40  
      * Construct a new ButtonBarBuilder.
 41  
      *
 42  
      * @param lines The number of lines.
 43  
      */
 44  
     public LinedButtonBarBuilder(int lines) {
 45  0
         super();
 46  
 
 47  0
         column = new int[lines];
 48  
 
 49  0
         panel = new JPanel(new GridBagLayout());
 50  0
         panel.setBackground(Color.white);
 51  
 
 52  0
         gbc = new GridBagUtils();
 53  0
         gbc.setDefaultInsets(2, 2, 2, 2);
 54  
 
 55  0
         for (int i = 0; i < lines; i++) {
 56  0
             panel.add(Box.createHorizontalGlue(), gbc.gbcSet(column[i], i, GridBagConstraints.HORIZONTAL, GridBagConstraints.LINE_START, 1.0, 1.0));
 57  0
             column[i]++;
 58  
         }
 59  0
     }
 60  
 
 61  
     /**
 62  
      * Add an action to the bar.
 63  
      *
 64  
      * @param action The action to add.
 65  
      * @param line   The line.
 66  
      */
 67  
     private void addAction(Action action, int line) {
 68  0
         int lineIndex = line - 1;
 69  
 
 70  0
         panel.add(new JButton(action), gbc.gbcSet(column[lineIndex], lineIndex, GridBagConstraints.HORIZONTAL, GridBagConstraints.LINE_START));
 71  0
         column[lineIndex]++;
 72  0
     }
 73  
 
 74  
     /**
 75  
      * Add actions to the bar.
 76  
      *
 77  
      * @param line    The line to add the actions in.
 78  
      * @param actions The actions to add.
 79  
      */
 80  
     public void addActions(int line, Action... actions) {
 81  0
         for (Action action : actions) {
 82  0
             addAction(action, line);
 83  
         }
 84  0
     }
 85  
 
 86  
     /**
 87  
      * Return the panel.
 88  
      *
 89  
      * @return The panel.
 90  
      */
 91  
     public Component getPanel() {
 92  0
         return panel;
 93  
     }
 94  
 }