Coverage Report - org.jtheque.utils.print.ArrayPrinter
 
Classes in this File Line Coverage Branch Coverage Complexity
ArrayPrinter
0%
0/17
0%
0/2
2
ArrayPrinter$1
N/A
N/A
2
ArrayPrinter$ArrayPrintable
0%
0/20
0%
0/6
2
 
 1  
 package org.jtheque.utils.print;
 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 java.awt.FontMetrics;
 20  
 import java.awt.Graphics;
 21  
 import java.awt.print.PageFormat;
 22  
 import java.awt.print.Pageable;
 23  
 import java.awt.print.Printable;
 24  
 import java.util.ArrayList;
 25  
 import java.util.Collection;
 26  
 import java.util.List;
 27  
 
 28  
 /**
 29  
  * This class permit to print an array of lines.
 30  
  *
 31  
  * @author Baptiste Wicht
 32  
  */
 33  0
 final class ArrayPrinter implements Pageable {
 34  
     private final List<String> list;
 35  
     private int nbreElementsByPage;
 36  
 
 37  
     private final int numberOfPages;
 38  
     private final PageFormat format;
 39  
 
 40  
     private static final int LINE_HEIGHT = 14;
 41  
     private static final int HEIGHT_MARGIN = 150;
 42  
 
 43  
     /**
 44  
      * Construct a new <code>ArrayPrinter</code> with a list of lines.
 45  
      *
 46  
      * @param list The list of the lines we want to print
 47  
      */
 48  
     ArrayPrinter(Collection<String> list) {
 49  0
         super();
 50  
 
 51  0
         this.list = new ArrayList<String>(list);
 52  
 
 53  0
         format = new PageFormat();
 54  0
         format.setOrientation(PageFormat.PORTRAIT);
 55  0
         format.setPaper(new A4());
 56  
 
 57  0
         nbreElementsByPage = (int) Math.floor((format.getImageableHeight() - HEIGHT_MARGIN) / LINE_HEIGHT);
 58  0
         numberOfPages = list.size() / nbreElementsByPage;
 59  0
     }
 60  
 
 61  
     @Override
 62  
     public int getNumberOfPages() {
 63  0
         return numberOfPages;
 64  
     }
 65  
 
 66  
     @Override
 67  
     public PageFormat getPageFormat(int pageIndex) {
 68  0
         return format;
 69  
     }
 70  
 
 71  
     @Override
 72  
     public Printable getPrintable(int pageIndex) {
 73  0
         if (pageIndex > numberOfPages) {
 74  0
             throw new IndexOutOfBoundsException();
 75  
         }
 76  
 
 77  0
         int start = nbreElementsByPage * pageIndex;
 78  0
         int stop = nbreElementsByPage * (pageIndex + 1) - 1;
 79  
 
 80  0
         stop = Math.max(stop, list.size());
 81  
 
 82  0
         return new ArrayPrintable(start, stop);
 83  
     }
 84  
 
 85  
     /**
 86  
      * A printable for an array of lines.
 87  
      *
 88  
      * @author Baptiste Wicht
 89  
      */
 90  0
     private final class ArrayPrintable implements Printable {
 91  
         private int start;
 92  
         private final int stop;
 93  
 
 94  
         private static final int MARGIN = 75;
 95  
         private static final int FONT_SIZE = 12;
 96  
 
 97  
         /**
 98  
          * Construct a new ArrayPrintable.
 99  
          *
 100  
          * @param start The start index.
 101  
          * @param stop  The end index.
 102  
          */
 103  0
         private ArrayPrintable(int start, int stop) {
 104  0
             super();
 105  
 
 106  0
             this.start = start;
 107  0
             this.stop = stop;
 108  0
         }
 109  
 
 110  
         @Override
 111  
         public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
 112  0
             FontMetrics metrics = graphics.getFontMetrics();
 113  
 
 114  0
             graphics.setFont(graphics.getFont().deriveFont(FONT_SIZE));
 115  
 
 116  0
             int increment = metrics.getHeight() + 2;
 117  
 
 118  0
             if (nbreElementsByPage == 0) {
 119  0
                 double heightPg = pageFormat.getImageableHeight();
 120  0
                 nbreElementsByPage = (int) (heightPg / increment);
 121  
             }
 122  
 
 123  0
             if (pageIndex >= numberOfPages) {
 124  0
                 return NO_SUCH_PAGE;
 125  
             } else {
 126  0
                 int vertical = MARGIN;
 127  
 
 128  0
                 while (start <= stop) {
 129  0
                     vertical += increment;
 130  0
                     graphics.drawString(list.get(start), MARGIN, vertical);
 131  0
                     start++;
 132  
                 }
 133  
 
 134  0
                 return PAGE_EXISTS;
 135  
             }
 136  
         }
 137  
     }
 138  
 }