DBVIEW
src/org/dbview/utils/dot/label/AbstractGrid.java
00001 /*
00002         DbView - Graph Visualization
00003     Copyright (C) 2012  Denis BEURIVE
00004 
00005     This program is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 */
00018 
00019 package org.dbview.utils.dot.label;
00020 
00021 import java.util.ArrayList;
00022 
00023 /**
00024  * This class is the base class for all classes that generates grids for node's labels.
00025  * @author Denis BEURIVE
00026  */
00027 public abstract class AbstractGrid
00028 {
00029     /**
00030      * This attribute represents the global orientation of the DOT's graph.
00031      * @see http://www.graphviz.org/doc/info/shapes.html
00032      */
00033     protected String _rankdir_ = "TB";
00034 
00035     /**
00036      * Set the initial orientation of a record node.
00037      * @param in_direction This string defines the direction. Values can be:
00038      *        <ul>
00039      *             <li>TB (Top to bottom)</li>
00040      *             <li>LR (Left to right)</li>
00041      *             <li>RL 'Right to left)</li>
00042      *        </ul>
00043      */
00044     public void setRankdir(String in_direction) { this._rankdir_ = in_direction; }
00045 
00046     /**
00047      * Create a grid by processing rows in a vertical layout.
00048      * @param in_data Rows.
00049      * @return The method returns the representation of the grid.
00050      */
00051     protected String _vRows_(ArrayList<ArrayList<String>> in_data)
00052     {
00053         if (0 == in_data.size()) { return ""; }
00054 
00055         // We assume that lines have the same sizes.
00056         int column_number = in_data.get(0).size();
00057         ArrayList<ArrayList<String>> columns = new ArrayList<ArrayList<String>>();
00058 
00059         for (int i=0; i<column_number; i++)
00060         {
00061             ArrayList<String> column = new ArrayList<String>();
00062             for (ArrayList<String> line: in_data) { column.add(line.get(i)); }
00063             columns.add(column);
00064         }
00065 
00066         ArrayList<String> tubes = new ArrayList<String>();
00067         for (ArrayList<String> column: columns) { tubes.add("{" + org.dbview.utils.Strings.join(column, "|") + "}"); }
00068         return org.dbview.utils.Strings.join(tubes, " | ");
00069     }
00070 
00071     /**
00072      * Create a grid by processing rows in a horizontal layout.
00073      * @param in_data Rows.
00074      * @return The method returns the representation of the grid.
00075      */
00076     protected String _hRows_(ArrayList<ArrayList<String>> in_data)
00077     {
00078         return "{" + this._vRows_(in_data) + "}";
00079     }
00080 
00081     /**
00082      * This method returns a string that represents the grid.
00083      * @return The method returns a string that represents the grid.
00084      */
00085     public abstract String toString();
00086 }