001    /*
002     * TablePrinter.java
003     *
004     * Created on October 22, 2004, 1:48 PM
005     *
006     * This file is part of the STAR Scheduler.
007     * Copyright (c) 2003-2006 STAR Collaboration - Brookhaven National Laboratory
008     *
009     * STAR Scheduler is free software; you can redistribute it and/or modify
010     * it under the terms of the GNU General Public License as published by
011     * the Free Software Foundation; either version 2 of the License, or
012     * (at your option) any later version.
013     *
014     * STAR Scheduler is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017     * GNU General Public License for more details.
018     *
019     * You should have received a copy of the GNU General Public License
020     * along with STAR Scheduler; if not, write to the Free Software
021     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
022     */
023    
024    //package scheduler.src.gov.bnl.star.offline.scheduler.util.persistent;
025    package gov.bnl.star.offline.scheduler.util.persistent;
026    import java.util.*;
027    
028    /**
029     * Print a spaced table out of a list.
030     * @author  Levente Hajdu
031     */
032    public class TablePrinter {
033        
034        private  List rowList = new ArrayList();
035        private  String nullChar = "-";
036        private  int spacing = 2;
037        
038        /** Creates a new instance of TablePrinter */
039        public TablePrinter() {
040           
041        }
042        
043        /**
044         * used for testing and debugging
045         */
046        public  void main() { 
047            
048            int a = 2;
049            addRow();
050            addToRow("ID");
051            addToRow("FC201DA20A878E6B1A8080FE11923C6A_0");
052            addToRow("FC201DA20A878E6B1A8080FE11923C6A_1");
053            addToRow("FC201DA20A878E6B1A8080FE11923C6A_3");
054            addToRow(null);
055            addToRow(a);
056            addToRow("FC201DA20A878E6B1A8080FE11923C6A_100");
057            addRow("Queue");
058            addToRow("star_cas_short");
059            addToRow("star_cas_short");
060            addToRow("star_cas_long");
061            addRow();
062            addToRow("TimeLimit");
063            addToRow("n/a");
064            addToRow("5MIN");
065            addToRow("5MIN");
066            addToRow(1.45);
067            addToRow(null);
068            addToRow("n/a");
069            addToRow(null);
070            System.out.println("\n\n" + getTable() + "\n");
071             
072        }
073        
074        public  void clearTable(){rowList.clear();}
075        
076        public  void setSpacing(int i){spacing = i;}
077        
078        public  List addRow(){
079            List row = new ArrayList();
080            rowList.add(row);
081            return row;
082        }
083        
084        public  List addRow(String Header){
085            List row = new ArrayList();
086            rowList.add(row);
087            row.add(Header);
088            return row;
089        }
090        
091        
092        public  void addToRow(int o){((List) rowList.get(rowList.size()-1)).add(String.valueOf(o)); }
093        public  void addToRow(double o){((List) rowList.get(rowList.size()-1)).add(String.valueOf(o)); }
094        public  void addToRow(float o){((List) rowList.get(rowList.size()-1)).add(String.valueOf(o)); }
095        public  void addToRow(long o){((List) rowList.get(rowList.size()-1)).add(String.valueOf(o)); }
096        public  void addToRow(Object o){
097           if(o == null) o = nullChar;
098           ((List) rowList.get(rowList.size()-1)).add(o); 
099        }
100        
101        public  String getTable(){
102            Normalize();
103            String TableText = "";
104            
105            for(int row = 0; row != getMaxRows(); row++){
106                for(int col = 0; col != rowList.size(); col ++){
107                    List colX = (List) rowList.get(col);
108                    
109                   // if(colX.size() <= row) TableText = TableText.concat(nullChar);
110                   // else 
111                        TableText = TableText.concat(colX.get(row).toString());
112                }
113                TableText = TableText.concat("\n");
114            }
115            return TableText;
116        }
117        
118        public  int getMaxRows(){
119            int maxSize = 0;
120            for(int i = 0; i != rowList.size(); i ++){
121                List colX = (List) rowList.get(i);
122                if(colX.size() > maxSize) maxSize = colX.size();
123            }
124            return maxSize; 
125        }
126        
127        
128        public  void Normalize(){
129            int maxRows = getMaxRows();
130            for(int i = 0; i != rowList.size(); i ++){
131                List colX = (List) rowList.get(i);
132                int colWide = colWide(colX);
133                
134                for(int j = 0; j != maxRows; j++){
135                    
136                    if((colX.size() == j) || (colX.size() < j))colX.add(nullChar);
137                    
138                    if(colX.get(j) == null){
139                        colX.remove(j);
140                        colX.add(j, nullChar);
141                    }
142                    String reformated = colX.get(j).toString().concat(makeStringOf(' ', colWide - colX.get(j).toString().length()));
143                    colX.remove(j);
144                    colX.add(j, reformated);
145                }
146                
147                if(i == 0){ //don't put a spacer in the last col by leaving it out in the 1st
148                    colX.add(makeStringOf('-', colWide - spacing));
149                    colX.add(0,makeStringOf('-', colWide - spacing));
150                    colX.add(2,makeStringOf('-', colWide - spacing));
151                }
152                else{
153                    colX.add(makeStringOf('-', colWide));
154                    colX.add(0,makeStringOf('-', colWide));
155                    colX.add(2,makeStringOf('-', colWide));
156                }
157            }
158        }
159        
160        public  int colWide(List col){
161           int maxWide = 0;
162           for(int i = 0; i != col.size(); i++)
163                if(maxWide < col.get(i).toString().length()) maxWide = col.get(i).toString().length();
164           //int x = maxWide + spacing;
165    
166           return maxWide + spacing; 
167        }
168        
169        
170        public  String makeStringOf(char a, int size){
171            String x = "";
172            while(x.length() != size)
173                x = x.concat(String.valueOf(a));
174            return x;
175        }
176        
177        
178    }