Coverage Report - org.jtheque.primary.view.impl.frames.ChoiceView
 
Classes in this File Line Coverage Branch Coverage Complexity
ChoiceView
0 %
0/24
0 %
0/2
1.143
 
 1  
 package org.jtheque.primary.view.impl.frames;
 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 org.jtheque.core.managers.error.JThequeError;
 20  
 import org.jtheque.core.managers.persistence.able.DataContainerProvider;
 21  
 import org.jtheque.core.managers.view.impl.frame.abstraction.SwingDialogView;
 22  
 import org.jtheque.core.utils.ui.PanelBuilder;
 23  
 import org.jtheque.core.utils.ui.ValidationUtils;
 24  
 import org.jtheque.primary.od.able.Data;
 25  
 import org.jtheque.primary.utils.DataTypeManager;
 26  
 import org.jtheque.primary.view.able.IChoiceView;
 27  
 import org.jtheque.primary.view.impl.actions.choice.AcValidateChoiceView;
 28  
 import org.jtheque.primary.view.impl.models.DataContainerCachedComboBoxModel;
 29  
 import org.jtheque.utils.ui.SwingUtils;
 30  
 
 31  
 import javax.swing.Action;
 32  
 import javax.swing.JComponent;
 33  
 import java.awt.Container;
 34  
 import java.util.Collection;
 35  
 
 36  
 /**
 37  
  * A view to choose an object from a list.
 38  
  *
 39  
  * @author Baptiste Wicht
 40  
  */
 41  
 public final class ChoiceView extends SwingDialogView implements IChoiceView {
 42  
         private DataContainerCachedComboBoxModel<?> model;
 43  
 
 44  
         private String content;
 45  
 
 46  
         /**
 47  
          * Construct a new ChoiceView. 
 48  
          */
 49  
         public ChoiceView(){
 50  0
                 super();
 51  
 
 52  0
                 setLocationRelativeTo(getParent());
 53  0
         }
 54  
 
 55  
         /**
 56  
          * Reload the content of the view.
 57  
          *
 58  
          * @param content The content.
 59  
          */
 60  
         private void reload(String content){
 61  0
                 this.content = content;
 62  
 
 63  0
                 setTitle(DataTypeManager.getTextForDataType(content));
 64  0
                 setContentPane(buildContentPane());
 65  0
                 pack();
 66  0
         }
 67  
 
 68  
         /**
 69  
          * Build and return the content pane.
 70  
          *
 71  
          * @return Le content pane
 72  
          */
 73  
         private Container buildContentPane(){
 74  0
                 PanelBuilder builder = new PanelBuilder();
 75  
 
 76  0
                 model = new DataContainerCachedComboBoxModel(DataContainerProvider.getInstance().getContainerForDataType(content));
 77  
 
 78  0
                 Action validateAction = new AcValidateChoiceView();
 79  
 
 80  0
                 JComponent comboElements = builder.addComboBox(model, builder.gbcSet(0, 0));
 81  0
                 SwingUtils.addFieldValidateAction(comboElements, validateAction);
 82  
 
 83  0
                 builder.addButtonBar(builder.gbcSet(1, 0), validateAction, getCloseAction("choice.actions.cancel"));
 84  
 
 85  0
                 return builder.getPanel();
 86  
         }
 87  
 
 88  
         @Override
 89  
         public Data getSelectedItem(){
 90  0
                 return model.getSelectedData();
 91  
         }
 92  
 
 93  
         @Override
 94  
         public void display(String content){
 95  0
                 reload(content);
 96  
 
 97  0
                 display();
 98  0
         }
 99  
 
 100  
         @Override
 101  
         public void refreshText(){
 102  0
                 if (content != null){
 103  0
                         setTitle(DataTypeManager.getTextForDataType(content));
 104  
                 }
 105  0
         }
 106  
 
 107  
         @Override
 108  
         protected void validate(Collection<JThequeError> errors){
 109  0
                 ValidationUtils.rejectIfNothingSelected(model, "choice.view.title", errors);
 110  0
         }
 111  
 }