Coverage Report - wjhk.jupload2.gui.filepanel.FilePanelJFlatTable
 
Classes in this File Line Coverage Branch Coverage Complexity
FilePanelJFlatTable
46 %
25/54
30 %
6/20
2
 
 1  
 //
 2  
 // $Id: FilePanelJTable.java 95 2007-05-02 03:27:05 +0000 (mer., 02 mai 2007)
 3  
 // /C=DE/ST=Baden-Wuerttemberg/O=ISDN4Linux/OU=Fritz
 4  
 // Elfert/CN=svn-felfert@isdn4linux.de/emailAddress=fritz@fritz-elfert.de $
 5  
 //
 6  
 // jupload - A file upload applet.
 7  
 // Copyright 2007 The JUpload Team
 8  
 //
 9  
 // Created: ?
 10  
 // Creator: William JinHua Kwong
 11  
 // Last modified: $Date: 2015-03-10 20:59:42 +0100 (mar., 10 mars 2015) $
 12  
 //
 13  
 // This program is free software; you can redistribute it and/or modify it under
 14  
 // the terms of the GNU General Public License as published by the Free Software
 15  
 // Foundation; either version 2 of the License, or (at your option) any later
 16  
 // version. This program is distributed in the hope that it will be useful, but
 17  
 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 18  
 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 19  
 // details. You should have received a copy of the GNU General Public License
 20  
 // along with this program; if not, write to the Free Software Foundation, Inc.,
 21  
 // 675 Mass Ave, Cambridge, MA 02139, USA.
 22  
 
 23  
 package wjhk.jupload2.gui.filepanel;
 24  
 
 25  
 import java.awt.Cursor;
 26  
 import java.awt.event.MouseEvent;
 27  
 import java.awt.event.MouseListener;
 28  
 import java.util.Date;
 29  
 
 30  
 import javax.swing.JTable;
 31  
 import javax.swing.ListSelectionModel;
 32  
 import javax.swing.event.ListSelectionEvent;
 33  
 import javax.swing.table.JTableHeader;
 34  
 import javax.swing.table.TableColumnModel;
 35  
 
 36  
 import wjhk.jupload2.gui.JUploadPanel;
 37  
 import wjhk.jupload2.policies.UploadPolicy;
 38  
 
 39  
 /**
 40  
  * This class is the JTable that display file information to the users. Data is
 41  
  * handled by the wjhk.jupload2.gui.FilePanelDataModel2 class.
 42  
  */
 43  
 public class FilePanelJFlatTable extends JTable implements MouseListener {
 44  
 
 45  
     /** A generated serialVersionUID, to avoid warning during compilation */
 46  
     private static final long serialVersionUID = 5422667664740339798L;
 47  
 
 48  40
     protected int sortedColumnIndex = -1;
 49  
 
 50  40
     protected boolean sortedColumnAscending = true;
 51  
 
 52  
     // The current UploadPolicy
 53  
     UploadPolicy uploadPolicy;
 54  
 
 55  
     // The current DataModel
 56  
     FilePanelFlatDataModel2 filePanelDataModel;
 57  
 
 58  
     /**
 59  
      * Creates a new instance.
 60  
      * 
 61  
      * @param jup The parent upload panel.
 62  
      * @param uploadPolicy The policy for retrieval of various settings.
 63  
      */
 64  40
     public FilePanelJFlatTable(JUploadPanel jup, UploadPolicy uploadPolicy) {
 65  40
         this.uploadPolicy = uploadPolicy;
 66  
 
 67  40
         setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
 68  40
         setDefaultRenderer(Long.class, new SizeRenderer(uploadPolicy));
 69  40
         setDefaultRenderer(Date.class, new DateRenderer(uploadPolicy));
 70  40
         setDefaultRenderer(String.class, new NameRenderer());
 71  
 
 72  
         // setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 73  
 
 74  40
         JTableHeader header = getTableHeader();
 75  40
         header.setDefaultRenderer(new SortHeaderRenderer());
 76  
         // We add the mouse listener on the header (to manage column sorts) and
 77  
         // on the main part (to manage the contextual popup menu)
 78  40
         header.addMouseListener(this);
 79  40
         addMouseListener(jup.getMouseListener());
 80  40
     }
 81  
 
 82  
     /**
 83  
      * Set the model. Forces the model to be a FilePanelDataModel2. This method
 84  
      * calls the {@link JTable#setModel(javax.swing.table.TableModel)} method.
 85  
      * 
 86  
      * @param filePanelDataModel
 87  
      */
 88  
     public void setModel(FilePanelFlatDataModel2 filePanelDataModel) {
 89  40
         super.setModel(filePanelDataModel);
 90  40
         this.filePanelDataModel = filePanelDataModel;
 91  40
     }
 92  
 
 93  
     /**
 94  
      * Retrieve the currently sorted column.
 95  
      * 
 96  
      * @return the index of the currently sorted column.
 97  
      */
 98  
     public int getSortedColumnIndex() {
 99  0
         return this.sortedColumnIndex;
 100  
     }
 101  
 
 102  
     /**
 103  
      * Retrieve the current sort order.
 104  
      * 
 105  
      * @return true, if the current sort order is ascending, false otherwise.
 106  
      */
 107  
     public boolean isSortedColumnAscending() {
 108  0
         return this.sortedColumnAscending;
 109  
     }
 110  
 
 111  
     /**
 112  
      * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
 113  
      */
 114  
     public void mouseReleased(MouseEvent event) {
 115  
         // Displays the contextual menu ?
 116  0
         this.uploadPolicy.getContext().getUploadPanel().maybeOpenPopupMenu(
 117  
                 event);
 118  0
     }
 119  
 
 120  
     /**
 121  
      * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
 122  
      */
 123  
     public void mousePressed(MouseEvent event) {
 124  
         // Displays the contextual menu ?
 125  0
         this.uploadPolicy.getContext().getUploadPanel().maybeOpenPopupMenu(
 126  
                 event);
 127  0
     }
 128  
 
 129  
     /**
 130  
      * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
 131  
      */
 132  
     public void mouseClicked(MouseEvent event) {
 133  
         // Is this a double-click ?
 134  0
         if (event.getClickCount() == 2) {
 135  
             // Let's open the 'big' preview, if we're in picture mode.
 136  
             // We should have one selected row. Let's check it, you never knows
 137  
             // ! ;-)
 138  0
             int selectedRow = getSelectedRow();
 139  0
             if (selectedRow >= 0) {
 140  0
                 this.uploadPolicy.onFileSelected(this.filePanelDataModel
 141  0
                         .getFileDataAt(selectedRow));
 142  
             }
 143  0
         } else if (!this.uploadPolicy.getContext().getUploadPanel()
 144  0
                 .maybeOpenPopupMenu(event)) {
 145  
             // We did not open the displays the contextual menu. So we do what
 146  
             // we have to do: sort the clicked column
 147  0
             TableColumnModel colModel = getColumnModel();
 148  0
             int index = colModel.getColumnIndexAtX(event.getX());
 149  0
             int modelIndex = colModel.getColumn(index).getModelIndex();
 150  
 
 151  0
             FilePanelFlatDataModel2 model = (FilePanelFlatDataModel2) getModel();
 152  0
             if (model.isSortable(modelIndex)) {
 153  0
                 if (this.sortedColumnIndex == index) {
 154  0
                     this.sortedColumnAscending = !this.sortedColumnAscending;
 155  
                 }
 156  0
                 this.sortedColumnIndex = index;
 157  
 
 158  0
                 model.sortColumn(modelIndex, this.sortedColumnAscending);
 159  
             }
 160  
         }
 161  0
     }
 162  
 
 163  
     /**
 164  
      * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
 165  
      */
 166  
     public void mouseEntered(MouseEvent event) {
 167  
         // Nothing to do.
 168  0
     }
 169  
 
 170  
     /**
 171  
      * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
 172  
      */
 173  
     public void mouseExited(MouseEvent event) {
 174  
         // Nothing to do.
 175  0
     }
 176  
 
 177  
     /**
 178  
      * @see javax.swing.JTable#valueChanged(javax.swing.event.ListSelectionEvent)
 179  
      */
 180  
     @Override
 181  
     public void valueChanged(ListSelectionEvent e) {
 182  12
         super.valueChanged(e);
 183  
         // Ignore extra messages, and no action before initialization.
 184  12
         if (e.getValueIsAdjusting() || this.uploadPolicy == null)
 185  6
             return;
 186  
 
 187  
         //
 188  6
         ListSelectionModel lsm = (ListSelectionModel) e.getSource();
 189  6
         if (lsm.isSelectionEmpty()) {
 190  3
             this.uploadPolicy.onFileSelected(null);
 191  
         } else {
 192  3
             int selectedRow = lsm.getMinSelectionIndex();
 193  
             // if one file is selected, we let the current upload policy reacts.
 194  
             // Otherwise, we don't do anything.
 195  3
             if (selectedRow == lsm.getMaxSelectionIndex()) {
 196  0
                 Cursor previousCursor = this.uploadPolicy.setWaitCursor();
 197  0
                 this.uploadPolicy.onFileSelected(this.filePanelDataModel
 198  0
                         .getFileDataAt(selectedRow));
 199  0
                 this.uploadPolicy.setCursor(previousCursor);
 200  
             }
 201  
         }
 202  6
     }
 203  
 }