Coverage Report - com.buckosoft.fibs.BuckoFIBS.gui.playerList.group.GroupSelectPane
 
Classes in this File Line Coverage Branch Coverage Complexity
GroupSelectPane
0%
0/121
0%
0/34
1.762
GroupSelectPane$1
0%
0/3
N/A
1.762
GroupSelectPane$2
0%
0/3
N/A
1.762
GroupSelectPane$3
0%
0/3
N/A
1.762
GroupSelectPane$CheckWithName
0%
0/1
N/A
1.762
 
 1  
 /******************************************************************************
 2  
  * GroupSelectPane.java - Display the pane the allows the user to select active groups
 3  
  * $Id$
 4  
  * 
 5  
  * BuckoFIBS - Backgammon by BuckoSoft
 6  
  * Copyright© 2010 - Dick Balaska - BuckoSoft, Corp.
 7  
  * 
 8  
  * $Log$
 9  
  * Revision 1.4  2011/01/04 17:42:39  dick
 10  
  * Don't disable isIgnore() groups.
 11  
  *
 12  
  * Revision 1.3  2011/01/01 20:41:33  dick
 13  
  * We have to use buttons instead of labels in order to figure out what we clicked on.
 14  
  * I don't like the way they look, but they work.
 15  
  *
 16  
  * Revision 1.2  2011/01/01 06:23:51  dick
 17  
  * Enable/disable groups on selecting All.
 18  
  *
 19  
  * Revision 1.1  2010/12/31 05:34:51  dick
 20  
  * Display the pane the allows the user to select active groups.
 21  
  *
 22  
  */
 23  
 
 24  
 /* 
 25  
  * This program is free software: you can redistribute it and/or modify
 26  
  * it under the terms of the GNU General Public License as published by
 27  
  * the Free Software Foundation, either version 3 of the License, or
 28  
  * (at your option) any later version.
 29  
  *
 30  
  * This program is distributed in the hope that it will be useful,
 31  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 32  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 33  
  * GNU General Public License for more details.
 34  
  *
 35  
  * You should have received a copy of the GNU General Public License
 36  
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 37  
  *
 38  
  * The Original Code is BuckoFIBS, <http://www.buckosoft.com/BuckoFIBS/>.
 39  
  * The Initial Developer of the Original Code is Dick Balaska and BuckoSoft, Corp.
 40  
  * 
 41  
  */
 42  
 package com.buckosoft.fibs.BuckoFIBS.gui.playerList.group;
 43  
 
 44  
 import java.awt.BorderLayout;
 45  
 import java.awt.GridBagConstraints;
 46  
 import java.awt.GridBagLayout;
 47  
 import java.awt.Insets;
 48  
 import java.awt.event.ItemEvent;
 49  
 import java.util.LinkedList;
 50  
 
 51  
 import javax.swing.JButton;
 52  
 import javax.swing.JCheckBox;
 53  
 import javax.swing.JLabel;
 54  
 import javax.swing.JPanel;
 55  
 import javax.swing.JScrollPane;
 56  
 
 57  
 import com.buckosoft.fibs.BuckoFIBS.GroupManager;
 58  
 import com.buckosoft.fibs.BuckoFIBS.gui.MainDialog;
 59  
 import com.buckosoft.fibs.domain.GroupOfPlayers;
 60  
 
 61  
 /** Display the pane the allows the user to select active groups.
 62  
  * @author Dick Balaska
 63  
  * @since 2010/12/30
 64  
  * @version $Revision$ <br> $Date$
 65  
  * @see <a href="http://cvs.buckosoft.com/Projects/BuckoFIBS/BuckoFIBS/src/main/java/com/buckosoft/fibs/BuckoFIBS/gui/playerList/PlayerListPane.java">cvs PlayerListPane.java</a>
 66  
  */
 67  0
 public class GroupSelectPane extends JPanel {
 68  
         private static final boolean DEBUG = false;
 69  
         private static final long serialVersionUID = 1L;
 70  0
         private JPanel jPanelMainPane = null;
 71  0
         private JCheckBox jCheckBoxAll = null;
 72  
 //        private JLabel jLabelAll = null;
 73  
 //        private JLabel jLabelAddNewGroup = null;
 74  0
         private JScrollPane scrollPane = null;  //  @jve:decl-index=0:visual-constraint="17,265"
 75  0
         private        GroupManager        groupManager = null;
 76  0
         private        MainDialog                mainDialog = null;
 77  
         
 78  0
         private LinkedList<CheckWithName> checkWithNameList = new LinkedList<CheckWithName>();
 79  
         
 80  0
         class CheckWithName {
 81  
                 int                                id;
 82  
                 JCheckBox                checkBox;
 83  
                 JButton                        label;
 84  
                 String                        name;
 85  
                 GroupOfPlayers        group;
 86  
         }
 87  
 
 88  
         /**
 89  
          * This is the default constructor
 90  
          */
 91  
         public GroupSelectPane() {
 92  0
                 super();
 93  0
                 initialize();
 94  0
         }
 95  
 
 96  
 /*        public GroupSelectPane(JComponent parent) {
 97  
                 super(parent);
 98  
                 initialize();
 99  
         }
 100  
 */
 101  
         /**
 102  
          * This method initializes this
 103  
          * 
 104  
          * @return void
 105  
          */
 106  
         private void initialize() {
 107  0
                 System.out.println("GroupSelectPane:<init>");
 108  0
                 this.setLayout(new BorderLayout());
 109  0
                 this.setSize(300, 200);
 110  0
                 this.add(getScrollPane());
 111  0
         }
 112  
 
 113  
         private JScrollPane getScrollPane() {
 114  0
                 if (scrollPane == null) {
 115  0
                         scrollPane = new JScrollPane();
 116  0
                         scrollPane.setViewportView(getJPanelMainPane());
 117  
                 }
 118  0
                 return(scrollPane);
 119  
         }
 120  
 
 121  
         /**
 122  
          * This method initializes jPanelMainPane        
 123  
          *         
 124  
          * @return javax.swing.JPanel        
 125  
          */
 126  
         private JPanel getJPanelMainPane() {
 127  0
                 if (jPanelMainPane == null) {
 128  0
                         jPanelMainPane = new JPanel();
 129  0
                         jPanelMainPane.setLayout(new GridBagLayout());
 130  
                 }
 131  0
                 return jPanelMainPane;
 132  
         }
 133  
 
 134  
         /** Set our reference to the GroupManager.
 135  
          * @param groupManager The one and only GroupManager
 136  
          */
 137  
         public void setGroupManager(GroupManager groupManager) {
 138  0
                 this.groupManager = groupManager;
 139  0
                 this.groupManager.setGroupSelectPane(this);
 140  0
         }
 141  
 
 142  
         public void setMainDialog(MainDialog mainDialog) {
 143  0
                 this.mainDialog = mainDialog;
 144  0
         }
 145  
         /**
 146  
          * This method initializes jCheckBoxAll        
 147  
          *         
 148  
          * @return javax.swing.JCheckBox        
 149  
          */
 150  
         private JCheckBox getJCheckBoxAll() {
 151  0
                 if (jCheckBoxAll == null) {
 152  0
                         jCheckBoxAll = new JCheckBox();
 153  
                 }
 154  0
                 return jCheckBoxAll;
 155  
         }
 156  
 
 157  
         public void repopulate() {
 158  0
                 jPanelMainPane = null;
 159  0
                 scrollPane.setViewportView(getJPanelMainPane());
 160  0
         }
 161  
 
 162  
         /** Entry point when the pane is shown and we need to populate the display with groups.
 163  
          */
 164  
         public void populatePane() {
 165  0
                 this.groupManager.populatePane();
 166  0
         }
 167  
 
 168  
         public void addGroup(GroupOfPlayers groupOfPlayers) {
 169  0
                 GridBagConstraints gridBagConstraintsR = new GridBagConstraints();
 170  0
                 gridBagConstraintsR.insets = new Insets(0, 0, 0, 1);
 171  0
                 gridBagConstraintsR.gridx = 1;
 172  
                 //gridBagConstraintsR.anchor = GridBagConstraints.WEST;
 173  0
                 gridBagConstraintsR.gridy = this.checkWithNameList.size();
 174  0
                 GridBagConstraints gridBagConstraintsL = new GridBagConstraints();
 175  0
                 gridBagConstraintsL.gridx = 0;
 176  0
                 gridBagConstraintsL.gridy = this.checkWithNameList.size();
 177  0
                 gridBagConstraintsL.insets = new Insets(0, 1, 0, 0);
 178  
 
 179  0
                 String s = groupOfPlayers.getGroupName();
 180  
                 JCheckBox        cb;
 181  0
                 if (s.equals("All"))
 182  0
                         cb = getJCheckBoxAll();
 183  
                 else {
 184  0
                         cb = new JCheckBox();
 185  0
                         cb.setName(s);
 186  
                 }
 187  0
                 cb.addItemListener(new java.awt.event.ItemListener() {
 188  
                         public void itemStateChanged(java.awt.event.ItemEvent e) {
 189  
                                 if (DEBUG)
 190  
                                         System.out.println("Group toggled: ");
 191  0
                                 groupChecked(e);
 192  0
                         }
 193  
                 });
 194  0
                 cb.getModel().setSelected(groupOfPlayers.isActive());
 195  0
                 jPanelMainPane.add(cb, gridBagConstraintsL);
 196  0
                 JButton l = null;
 197  0
                 if (s.equals("All")) {
 198  0
                         JLabel jl = new JLabel();
 199  0
                         jl.setText(s);
 200  0
                         jPanelMainPane.add(jl, gridBagConstraintsR);
 201  0
                 } else {
 202  0
                         l = new JButton();
 203  0
                         l.setBorder(null);
 204  0
                         l.addActionListener(new java.awt.event.ActionListener() {
 205  
                                 public void actionPerformed(java.awt.event.ActionEvent e) {
 206  0
                                         onEditGroup((JButton)e.getSource());
 207  0
                                 }
 208  
                         });
 209  0
                         gridBagConstraintsR.fill = GridBagConstraints.HORIZONTAL;
 210  0
                         if (groupOfPlayers.isIgnore())
 211  0
                                 l.setText("!" + s);
 212  
                         else
 213  0
                                 l.setText(s);
 214  0
                         jPanelMainPane.add(l, gridBagConstraintsR);
 215  
                 }
 216  0
                 CheckWithName cwn = new CheckWithName();
 217  0
                 cwn.id = groupOfPlayers.getId();
 218  0
                 cwn.checkBox = cb;
 219  0
                 cwn.name = s;
 220  0
                 cwn.label = l;
 221  0
                 cwn.group = groupOfPlayers;
 222  0
                 checkWithNameList.add(cwn);
 223  
                 
 224  0
         }
 225  
         
 226  
         /** After the GroupManager finishes populating the pane, Finish with the "Add new Group" button.
 227  
          */
 228  
         public void populateFinish() {
 229  0
                 GridBagConstraints gridBagConstraints = new GridBagConstraints();
 230  0
                 gridBagConstraints.gridwidth = 2;
 231  0
                 gridBagConstraints.gridx = 0;
 232  
                 //gridBagConstraints.gridy = 1;
 233  0
                 gridBagConstraints.insets = new Insets(10, 0, 0, 0);
 234  0
                 gridBagConstraints.anchor = GridBagConstraints.SOUTH;
 235  0
                 JLabel l = new JLabel();
 236  0
                 l.setText("Add New Group...");
 237  0
                 l.addMouseListener(new java.awt.event.MouseAdapter() {
 238  
                         public void mouseClicked(java.awt.event.MouseEvent e) {
 239  0
                                 handleAddNewGroupButton();
 240  0
                         }
 241  
                 });
 242  0
                 jPanelMainPane.add(l, gridBagConstraints);
 243  0
                 updateDisabledItems();
 244  0
         }
 245  
         public void savePaneToList() {
 246  0
                 for (CheckWithName cwn : checkWithNameList) {
 247  0
                         this.groupManager.updateGroupOfPlayers(cwn.id, cwn.name, cwn.checkBox.getModel().isSelected());
 248  0
                 }
 249  0
         }
 250  
         
 251  
         private void handleAddNewGroupButton() {
 252  0
                 EditGroupDialog egd = new EditGroupDialog(mainDialog, this);
 253  0
                 egd.setVisible(true);
 254  0
         }
 255  
         
 256  
         public void addNewGroupFromUser(GroupOfPlayers groupOfPlayers) {
 257  0
                 this.groupManager.addNewGroupFromUser(groupOfPlayers);
 258  0
         }
 259  
 
 260  
         public void editGroupFromUser(GroupOfPlayers groupOfPlayers) {
 261  0
                 this.groupManager.editGroupFromUser(groupOfPlayers);
 262  0
         }
 263  
 
 264  
         /** Here when the "All" checkbox is toggled
 265  
          * @param checked true = checked
 266  
          */
 267  
         private void groupChecked(ItemEvent e) {
 268  0
                 updateDisabledItems();
 269  0
                 for (CheckWithName cwn : checkWithNameList) {
 270  0
                         if (cwn.checkBox == e.getItem()) {
 271  0
                                 this.groupManager.updateGroupOfPlayers(cwn.id, cwn.name, cwn.checkBox.getModel().isSelected());
 272  
                         }
 273  0
                 }
 274  0
         }
 275  
 
 276  
         /** Depending on certain events, items get enabled and disabled.  Process them here.
 277  
          */
 278  
         private void updateDisabledItems() {
 279  0
                 boolean allMode = this.getJCheckBoxAll().getModel().isSelected();
 280  0
                 for (CheckWithName cwn : checkWithNameList) {
 281  0
                         if (cwn.name.equals("All"))
 282  0
                                 continue;
 283  0
                         if (!cwn.group.isIgnore()) {                // Always enable the ignores
 284  0
                                 cwn.checkBox.setEnabled(!allMode);
 285  0
                                 cwn.label.setEnabled(!allMode);
 286  
                         }
 287  0
                 }
 288  0
         }
 289  
         
 290  
         private void onEditGroup(JButton e) {
 291  0
                 for (CheckWithName cwn : checkWithNameList) {
 292  0
                         if (cwn.name.equals("All"))
 293  0
                                 continue;
 294  0
                         if (cwn.label == e) {
 295  0
                                 EditGroupDialog egd = new EditGroupDialog(mainDialog, this);
 296  0
                                 egd.setGroupOfPlayers(this.groupManager.getGroupOfPlayers(cwn.id));
 297  0
                                 egd.setVisible(true);
 298  0
                                 return;
 299  
                         }
 300  0
                 }
 301  0
         }
 302  
 }