Coverage Report - org.argouml.cognitive.critics.ui.TableModelCritics
 
Classes in this File Line Coverage Branch Coverage Complexity
TableModelCritics
36%
25/69
35%
19/54
4.571
TableModelCritics$1
100%
2/2
N/A
4.571
TableModelCritics$2
0%
0/3
N/A
4.571
 
 1  
 /* $Id: TableModelCritics.java 17817 2010-01-12 18:34:59Z linus $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009 Contributors - see below
 4  
  * All rights reserved. This program and the accompanying materials
 5  
  * are made available under the terms of the Eclipse Public License v1.0
 6  
  * which accompanies this distribution, and is available at
 7  
  * http://www.eclipse.org/legal/epl-v10.html
 8  
  *
 9  
  * Contributors:
 10  
  *    mvw
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 2007 The Regents of the University of California. All
 17  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 18  
 // software and its documentation without fee, and without a written
 19  
 // agreement is hereby granted, provided that the above copyright notice
 20  
 // and this paragraph appear in all copies. This software program and
 21  
 // documentation are copyrighted by The Regents of the University of
 22  
 // California. The software program and documentation are supplied "AS
 23  
 // IS", without any accompanying services from The Regents. The Regents
 24  
 // does not warrant that the operation of the program will be
 25  
 // uninterrupted or error-free. The end-user understands that the program
 26  
 // was developed for research purposes and is advised not to rely
 27  
 // exclusively on the program for any reason. IN NO EVENT SHALL THE
 28  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 29  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 30  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 31  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 32  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 33  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 35  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 36  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 37  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 38  
 
 39  
 package org.argouml.cognitive.critics.ui;
 40  
 
 41  
 import java.beans.PropertyChangeEvent;
 42  
 import java.beans.VetoableChangeListener;
 43  
 import java.util.ArrayList;
 44  
 import java.util.Collections;
 45  
 import java.util.Comparator;
 46  
 import java.util.Iterator;
 47  
 import java.util.List;
 48  
 
 49  
 import javax.swing.SwingUtilities;
 50  
 import javax.swing.table.AbstractTableModel;
 51  
 
 52  
 import org.apache.log4j.Logger;
 53  
 import org.argouml.cognitive.Agency;
 54  
 import org.argouml.cognitive.Critic;
 55  
 import org.argouml.cognitive.Translator;
 56  
 
 57  
 /**
 58  
  * The Table Model from the Critics Browser dialog. <p>
 59  
  * 
 60  
  * This class used to be part of the CriticBrowserDialog.java file. <p>
 61  
  * 
 62  
  * In advanced mode, this model also handles 
 63  
  * priority, supportedDecisions, knowledgeTypes.
 64  
  */
 65  
 class TableModelCritics extends AbstractTableModel
 66  
     implements VetoableChangeListener {
 67  
 
 68  20
     private static final Logger LOG =
 69  
         Logger.getLogger(TableModelCritics.class);
 70  
 
 71  
     private List<Critic> critics;
 72  
     private boolean advanced;
 73  
 
 74  
     /**
 75  
      * Constructor.
 76  
      *
 77  
      * @param advancedMode true if we show advanced columns
 78  
      */
 79  20
     public TableModelCritics(boolean advancedMode) { 
 80  20
         critics = new ArrayList<Critic>(Agency.getCriticList());
 81  
         // Set initial sorting on Critic Headline
 82  10459
         Collections.sort(critics, new Comparator<Critic>() {
 83  
             public int compare(Critic o1, Critic o2) {
 84  10439
                 return o1.getHeadline().compareTo(o2.getHeadline());
 85  
             }
 86  
         });
 87  20
         advanced = advancedMode;
 88  20
     }
 89  
 
 90  
     /**
 91  
      * @param row the selected row
 92  
      * @return the Critic shown on that row
 93  
      */
 94  
     public Critic getCriticAtRow(int row) {
 95  0
         return critics.get(row);
 96  
     }
 97  
 
 98  
     //  TableModel implemetation
 99  
     /*
 100  
      * @see javax.swing.table.TableModel#getColumnCount()
 101  
      */
 102  
     public int getColumnCount() { 
 103  80
         return advanced ? 6 : 3; 
 104  
     }
 105  
 
 106  
     /*
 107  
      * @see javax.swing.table.TableModel#getColumnName(int)
 108  
      */
 109  
     public String getColumnName(int c) {
 110  60
         if (c == 0)
 111  20
             return Translator.localize("dialog.browse.column-name.active");
 112  40
         if (c == 1)
 113  20
             return Translator.localize("dialog.browse.column-name.headline");
 114  20
         if (c == 2)
 115  20
             return Translator.localize("dialog.browse.column-name.snoozed");
 116  0
         if (c == 3)
 117  0
             return Translator.localize("dialog.browse.column-name.priority");
 118  0
         if (c == 4)
 119  0
             return Translator.localize(
 120  
                     "dialog.browse.column-name.supported-decision");
 121  0
         if (c == 5)
 122  0
             return Translator.localize(
 123  
                     "dialog.browse.column-name.knowledge-type");
 124  0
         throw new IllegalArgumentException();
 125  
     }
 126  
 
 127  
     /*
 128  
      * @see javax.swing.table.TableModel#getColumnClass(int)
 129  
      */
 130  
     public Class< ? > getColumnClass(int c) {
 131  1071
         if (c == 0) {
 132  357
             return Boolean.class;
 133  
         }
 134  714
         if (c == 1) {
 135  357
             return String.class;
 136  
         }
 137  357
         if (c == 2) {
 138  357
             return String.class;
 139  
         }
 140  0
         if (c == 3) {
 141  0
             return Integer.class;
 142  
         }
 143  0
         if (c == 4) {
 144  0
             return String.class;
 145  
         }
 146  0
         if (c == 5) {
 147  0
             return String.class;
 148  
         }
 149  0
         throw new IllegalArgumentException();
 150  
     }
 151  
 
 152  
     /*
 153  
      * @see javax.swing.table.TableModel#isCellEditable(int, int)
 154  
      */
 155  
     public boolean isCellEditable(int row, int col) {
 156  0
         return col == 0;
 157  
     }
 158  
 
 159  
     /*
 160  
      * @see javax.swing.table.TableModel#getRowCount()
 161  
      */
 162  
     public int getRowCount() {
 163  762
         if (critics == null) return 0;
 164  762
         return critics.size();
 165  
     }
 166  
 
 167  
     /*
 168  
      * @see javax.swing.table.TableModel#getValueAt(int, int)
 169  
      */
 170  
     public Object getValueAt(int row, int col) {
 171  1071
         Critic cr = critics.get(row);
 172  1071
         if (col == 0) return cr.isEnabled() ? Boolean.TRUE : Boolean.FALSE;
 173  714
         if (col == 1) return cr.getHeadline();
 174  357
         if (col == 2) return cr.isActive() ? "no" : "yes";
 175  0
         if (col == 3) return cr.getPriority();
 176  0
         if (col == 4) return listToString(cr.getSupportedDecisions());
 177  0
         if (col == 5) return listToString(cr.getKnowledgeTypes());
 178  0
         throw new IllegalArgumentException();
 179  
     }
 180  
     
 181  
     private String listToString(List l) {
 182  0
         StringBuffer buf = new StringBuffer();
 183  0
         Iterator i = l.iterator();
 184  0
         boolean hasNext = i.hasNext();
 185  0
         while (hasNext) {
 186  0
             Object o = i.next();
 187  0
             buf.append(String.valueOf(o));
 188  0
             hasNext = i.hasNext();
 189  0
             if (hasNext)
 190  0
                 buf.append(", ");
 191  0
         }
 192  0
         return buf.toString();
 193  
     }
 194  
 
 195  
     /*
 196  
      * @see javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int)
 197  
      */
 198  
     public void setValueAt(Object aValue, int rowIndex, int columnIndex)  {
 199  0
         LOG.debug("setting table value " + rowIndex + ", " + columnIndex);
 200  0
         if (columnIndex != 0) return;
 201  0
         if (!(aValue instanceof Boolean)) return;
 202  0
         Boolean enable = (Boolean) aValue;
 203  0
         Critic cr = critics.get(rowIndex);
 204  0
         cr.setEnabled(enable.booleanValue());
 205  0
         fireTableRowsUpdated(rowIndex, rowIndex); //TODO:
 206  0
     }
 207  
 
 208  
     /*
 209  
      * TODO: Why is this here? Who is calling this?
 210  
      * 
 211  
      * @see java.beans.VetoableChangeListener#vetoableChange(java.beans.PropertyChangeEvent)
 212  
      */
 213  
     public void vetoableChange(PropertyChangeEvent pce) {
 214  0
         SwingUtilities.invokeLater(new Runnable() {
 215  
             public void run() {
 216  0
                 fireTableStructureChanged();
 217  0
             }
 218  
         });
 219  0
     }
 220  
 
 221  
     /**
 222  
      * @param advancedMode true causes advanced mode
 223  
      */
 224  
     void setAdvanced(boolean advancedMode) {
 225  0
         advanced = advancedMode;
 226  0
         fireTableStructureChanged();
 227  0
     }
 228  
 }