Coverage Report - org.jtheque.primary.view.impl.components.panels.PrincipalDataPanel
 
Classes in this File Line Coverage Branch Coverage Complexity
PrincipalDataPanel
0 %
0/44
0 %
0/10
1.318
 
 1  
 package org.jtheque.primary.view.impl.components.panels;
 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.jdesktop.swingx.JXTree;
 20  
 import org.jtheque.core.managers.Managers;
 21  
 import org.jtheque.core.managers.error.JThequeError;
 22  
 import org.jtheque.core.managers.language.ILanguageManager;
 23  
 import org.jtheque.core.managers.view.able.IViewManager;
 24  
 import org.jtheque.core.managers.view.able.components.IModel;
 25  
 import org.jtheque.primary.od.able.Data;
 26  
 import org.jtheque.primary.view.able.PrincipalDataView;
 27  
 import org.jtheque.primary.view.able.ToolbarView;
 28  
 import org.jtheque.primary.view.impl.listeners.DisplayListListener;
 29  
 import org.jtheque.primary.view.impl.models.tree.JThequeTreeModel;
 30  
 import org.jtheque.primary.view.impl.sort.SortManager;
 31  
 
 32  
 import javax.swing.JPanel;
 33  
 import javax.swing.JTabbedPane;
 34  
 import java.util.ArrayList;
 35  
 import java.util.Collection;
 36  
 
 37  
 /**
 38  
  * A principal data panel.
 39  
  *
 40  
  * @author Baptiste Wicht
 41  
  */
 42  0
 public abstract class PrincipalDataPanel<M extends IModel> extends JPanel implements PrincipalDataView, DisplayListListener {
 43  
         private JThequeTreeModel treeModel;
 44  0
         private String sortMode = "None";
 45  
 
 46  
         private M model;
 47  
 
 48  
         /* Instances */
 49  0
         private static final SortManager SORTER = new SortManager();
 50  
 
 51  
         /**
 52  
          * Return the data tree.
 53  
          *
 54  
          * @return The data tree.
 55  
          */
 56  
         protected abstract JXTree getTree();
 57  
 
 58  
         /**
 59  
          * Return the data type of the panel.
 60  
          *
 61  
          * @return the data type of the panel.
 62  
          */
 63  
         protected abstract String getDataType();
 64  
 
 65  
         /**
 66  
          * Return the tree model of the panel.
 67  
          *
 68  
          * @return The <code>JThequeTreeModel</code> associated to the tree.
 69  
          */
 70  
         public final JThequeTreeModel getTreeModel(){
 71  0
                 return treeModel;
 72  
         }
 73  
 
 74  
         /**
 75  
          * Set the tree model of the panel.
 76  
          *
 77  
          * @param model the new tree model of the panel.
 78  
          */
 79  
         public final void setTreeModel(JThequeTreeModel model){
 80  0
                 treeModel = model;
 81  0
         }
 82  
 
 83  
         @Override
 84  
         public final void displayListChanged(){
 85  0
                 SORTER.sort(treeModel, getDataType(), sortMode);
 86  0
         }
 87  
 
 88  
         @Override
 89  
         public final M getModel(){
 90  0
                 return model;
 91  
         }
 92  
 
 93  
         /**
 94  
          * Set the model of the panel.
 95  
          *
 96  
          * @param model The model.
 97  
          */
 98  
         public final void setModel(M model){
 99  0
                 this.model = model;
 100  0
         }
 101  
 
 102  
         @Override
 103  
         public final void resort(){
 104  0
                 sort(sortMode);
 105  0
         }
 106  
 
 107  
         @Override
 108  
         public final void select(Data data){
 109  0
                 int index = 1;
 110  
 
 111  0
                 Object root = treeModel.getRoot();
 112  
 
 113  0
                 for (int i = 0; i < treeModel.getChildCount(root); i++){
 114  0
                         for (int j = 0; j < treeModel.getChildCount(treeModel.getChild(root, i)); j++){
 115  0
                                 Object element = treeModel.getChild(treeModel.getChild(root, i), j);
 116  
 
 117  0
                                 if (data.equals(element)){
 118  0
                                         getTree().setSelectionRow(index + j);
 119  0
                                         return;
 120  
                                 }
 121  
                         }
 122  
 
 123  0
                         index += treeModel.getChildCount(treeModel.getChild(root, i));
 124  
                 }
 125  0
         }
 126  
 
 127  
         @Override
 128  
         public void selectFirst(){
 129  0
                 if (sortMode == null || "None".equalsIgnoreCase(sortMode)){
 130  0
                         getTree().setSelectionRow(1);
 131  
                 } else {
 132  0
                         getTree().setSelectionRow(2);
 133  
                 }
 134  0
         }
 135  
 
 136  
         @Override
 137  
         public final void sort(String sort){
 138  0
                 sortMode = sort;
 139  0
                 SORTER.sort(treeModel, getDataType(), sort);
 140  0
         }
 141  
 
 142  
         @Override
 143  
         public final void display(){
 144  0
                 ((JTabbedPane) Managers.getManager(IViewManager.class).getMainComponent()).setSelectedComponent(this);
 145  0
         }
 146  
 
 147  
         @Override
 148  
         public final void refresh(){
 149  0
                 Managers.getManager(IViewManager.class).refresh(this);
 150  0
         }
 151  
 
 152  
         @Override
 153  
         public final void toFirstPlan(){
 154  
                 //Nothing to be done
 155  0
         }
 156  
 
 157  
         @Override
 158  
         public final void closeDown(){
 159  
                 //Nothing to be done
 160  0
         }
 161  
 
 162  
         @Override
 163  
         public final void sendMessage(String message, Object value){
 164  0
                 throw new UnsupportedOperationException();
 165  
         }
 166  
 
 167  
         /**
 168  
          * Return the message for the internationalization key.
 169  
          *
 170  
          * @param key The internationalization key.
 171  
          *
 172  
          * @return The message.
 173  
          */
 174  
         protected static String getMessage(String key){
 175  0
                 return Managers.getManager(ILanguageManager.class).getMessage(key);
 176  
         }
 177  
 
 178  
         @Override
 179  
         public final boolean validateContent(){
 180  0
                 Collection<JThequeError> errors = new ArrayList<JThequeError>(6);
 181  
 
 182  0
                 validate(errors);
 183  
 
 184  0
                 return errors.isEmpty();
 185  
         }
 186  
 
 187  
         @Override
 188  
         public Object getImpl(){
 189  0
                 return this;
 190  
         }
 191  
 
 192  
         @Override
 193  
         public ToolbarView getToolbarView(){
 194  0
                 return null;  //Default implementation
 195  
         }
 196  
 
 197  
         @Override
 198  
         public void clear(){
 199  
                 //Nothing by default
 200  0
         }
 201  
 
 202  
         /**
 203  
          * Validate the view and save all the validation's errors in the list.
 204  
          *
 205  
          * @param errors The error's list.
 206  
          */
 207  
         protected abstract void validate(Collection<JThequeError> errors);
 208  
 }