Example usage for javax.swing JTable getPreferredScrollableViewportSize

List of usage examples for javax.swing JTable getPreferredScrollableViewportSize

Introduction

In this page you can find the example usage for javax.swing JTable getPreferredScrollableViewportSize.

Prototype

public Dimension getPreferredScrollableViewportSize() 

Source Link

Document

Returns the preferred size of the viewport for this table.

Usage

From source file:org.apache.taverna.activities.rest.ui.config.RESTActivityConfigurationPanel.java

public static void setVisibleRowCount(JTable table, int visibleRows) {
    int height = 0;
    for (int row = 0; row < visibleRows; row++)
        height += table.getRowHeight(row);

    table.setPreferredScrollableViewportSize(
            new Dimension(table.getPreferredScrollableViewportSize().width, height));
}

From source file:edu.ku.brc.stats.StatGroupTable.java

/**
 * Calculates and sets the each column to it preferred size
 * @param table the table to fix ups/*  w  w  w . j a  v  a2  s .c  o m*/
 */
public static void calcColumnWidths(JTable table) {
    JTableHeader header = table.getTableHeader();

    TableCellRenderer defaultHeaderRenderer = null;

    if (header != null) {
        defaultHeaderRenderer = header.getDefaultRenderer();
    }

    TableColumnModel columns = table.getColumnModel();
    TableModel data = table.getModel();

    int margin = columns.getColumnMargin(); // only JDK1.3

    int rowCount = data.getRowCount();

    int totalWidth = 0;

    for (int i = columns.getColumnCount() - 1; i >= 0; --i) {
        TableColumn column = columns.getColumn(i);

        int columnIndex = column.getModelIndex();

        int width = -1;

        TableCellRenderer h = column.getHeaderRenderer();

        if (h == null)
            h = defaultHeaderRenderer;

        if (h != null) // Not explicitly impossible
        {
            Component c = h.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, -1, i);

            width = c.getPreferredSize().width;
        }

        for (int row = rowCount - 1; row >= 0; --row) {
            TableCellRenderer r = table.getCellRenderer(row, i);

            Component c = r.getTableCellRendererComponent(table, data.getValueAt(row, columnIndex), false,
                    false, row, i);

            width = Math.max(width, c.getPreferredSize().width + 10); // adding an arbitray 10 pixels to make it look nicer
        }

        if (width >= 0) {
            column.setPreferredWidth(width + margin); // <1.3: without margin
        }

        totalWidth += column.getPreferredWidth();
    }

    // If you like; This does not make sense for two many columns!
    Dimension size = table.getPreferredScrollableViewportSize();
    //if (totalWidth > size.width)
    {
        size.height = Math.min(size.height, table.getRowHeight() * visibleRows);
        size.width = totalWidth;
        table.setPreferredScrollableViewportSize(size);
    }

}

From source file:edu.ku.brc.ui.UIHelper.java

/**
 * Sizes the table to number of rows using getRowHeight
 * @param table the table to be sized//from   w ww. j av  a  2  s.c  om
 * @param rows the number of rows
 */
public static void setVisibleRowCount(final JTable table, final int rows) {
    if (table != null) {
        table.setPreferredScrollableViewportSize(
                new Dimension(table.getPreferredScrollableViewportSize().width, rows * table.getRowHeight()));
    }
}

From source file:edu.ku.brc.ui.UIHelper.java

/**
 * Sizes the table to number of rows using the height of actual rows.
 * @param table the table to be sized/*from w ww . ja  v  a2  s  .c  o  m*/
 * @param rows the number of rows
 */
public static void setVisibleRowCountForHeight(final JTable table, final int rows) {
    if (table != null) {
        int height = 0;
        for (int row = 0; row < rows; row++)
            height += table.getRowHeight(row);

        table.setPreferredScrollableViewportSize(
                new Dimension(table.getPreferredScrollableViewportSize().width, height));
    }
}

From source file:edu.ku.brc.ui.UIHelper.java

/**
 * Calculates and sets the each column to it preferred size.  NOTE: This
 * method also sets the table height to 10 rows.
 * //from   ww  w  .  jav a 2 s . co m
 * @param table the table to fix up
 * @param numRowsHeight the number of rows to make the table height (or null not to set it)
 */
public static void calcColumnWidths(final JTable table, final Integer numRowsHeight, final Integer maxWidth) {
    if (table != null) {
        JTableHeader header = table.getTableHeader();

        TableCellRenderer defaultHeaderRenderer = null;

        if (header != null) {
            defaultHeaderRenderer = header.getDefaultRenderer();
        }

        TableColumnModel columns = table.getColumnModel();
        TableModel data = table.getModel();

        int margin = columns.getColumnMargin(); // only JDK1.3

        int rowCount = data.getRowCount();

        int totalWidth = 0;

        for (int i = columns.getColumnCount() - 1; i >= 0; --i) {
            TableColumn column = columns.getColumn(i);

            int columnIndex = column.getModelIndex();

            int width = -1;

            TableCellRenderer h = column.getHeaderRenderer();

            if (h == null)
                h = defaultHeaderRenderer;

            if (h != null) // Not explicitly impossible
            {
                Component c = h.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, -1,
                        i);

                width = c.getPreferredSize().width;
            }

            for (int row = rowCount - 1; row >= 0; --row) {
                TableCellRenderer r = table.getCellRenderer(row, i);

                Component c = r.getTableCellRendererComponent(table, data.getValueAt(row, columnIndex), false,
                        false, row, i);

                width = Math.max(width, c.getPreferredSize().width + 10); // adding an arbitray 10 pixels to make it look nicer

                if (maxWidth != null) {
                    width = Math.min(width, maxWidth);
                }
            }

            if (width >= 0) {
                column.setPreferredWidth(width + margin); // <1.3: without margin
            } else {
                // ???
            }

            totalWidth += column.getPreferredWidth();
        }

        // If you like; This does not make sense for two many columns!
        Dimension size = table.getPreferredScrollableViewportSize();
        //if (totalWidth > size.width)
        {
            if (numRowsHeight != null) {
                size.height = Math.min(size.height, table.getRowHeight() * numRowsHeight);
            }
            size.width = totalWidth;
            table.setPreferredScrollableViewportSize(size);
        }
    }
}

From source file:edu.ku.brc.specify.conversion.GenericDBConversion.java

/**
 * @return/*ww w  . j  a  va  2 s.  com*/
 */
public CollectionResultType initialize() {

    collectionInfoList = CollectionInfo.getCollectionInfoList(oldDBConn, false);
    //fixIdaho();
    if (collectionInfoList == null) {
        if (CollectionInfo.isAskForFix()) {
            if (ConvertTaxonHelper.fixTaxonomicUnitType(oldDBConn)) {
                collectionInfoList = CollectionInfo.getCollectionInfoList(oldDBConn, true);
            } else {
                try {
                    oldDBConn.close();
                } catch (SQLException e) {
                }
                System.exit(0);
            }
        } else {
            try {
                oldDBConn.close();
            } catch (SQLException e) {
            }
            System.exit(0);
        }
    }

    collectionInfoShortList = CollectionInfo.getFilteredCollectionInfoList();

    if (collectionInfoList != null && collectionInfoList.size() > 0) {
        int paleoCnt = 0;

        // This is a Hash of TaxonObjectType to see how many collections use the same TaxonObjectType
        HashMap<Integer, HashSet<CollectionInfo>> taxonomyTypeHash = new HashMap<Integer, HashSet<CollectionInfo>>();

        // Get a List for each type of Paleo Collection, hashed by the Root Id
        HashMap<Integer, Vector<CollectionInfo>> paleoColInfoHash = new HashMap<Integer, Vector<CollectionInfo>>();
        HashMap<Integer, HashSet<DisciplineType.STD_DISCIPLINES>> paleoDispTypeHash = new HashMap<Integer, HashSet<DisciplineType.STD_DISCIPLINES>>();

        for (CollectionInfo colInfo : collectionInfoShortList) {
            // Tracks a 'set' of CollectionInfo objects for each TaxonomyTypeId
            HashSet<CollectionInfo> taxonomyTypeSet = taxonomyTypeHash.get(colInfo.getTaxonomyTypeId());
            if (taxonomyTypeSet == null) {
                System.out.println("Creating TxTypeID: " + colInfo.getTaxonomyTypeId() + "  From "
                        + colInfo.getCatSeriesName());

                taxonomyTypeSet = new HashSet<CollectionInfo>();
                taxonomyTypeHash.put(colInfo.getTaxonomyTypeId(), taxonomyTypeSet);
            } else {
                System.out.println("Adding TxTypeID: " + colInfo.getTaxonomyTypeId() + "  From "
                        + colInfo.getCatSeriesName() + "  " + taxonomyTypeSet.size());
            }
            taxonomyTypeSet.add(colInfo);

            //---
            DisciplineType dType = getStandardDisciplineName(colInfo.getTaxonomyTypeName(),
                    colInfo.getColObjTypeName(), colInfo.getCatSeriesName());
            colInfo.setDisciplineTypeObj(dType);

            if (dType != null && dType.isPaleo()) {
                Vector<CollectionInfo> ciList = paleoColInfoHash.get(colInfo.getTaxonNameId());
                if (ciList == null) {
                    ciList = new Vector<CollectionInfo>();
                    paleoColInfoHash.put(colInfo.getTaxonNameId(), ciList);
                }
                ciList.add(colInfo);

                HashSet<DisciplineType.STD_DISCIPLINES> typeDispSet = paleoDispTypeHash
                        .get(colInfo.getTaxonNameId());
                if (typeDispSet == null) {
                    typeDispSet = new HashSet<DisciplineType.STD_DISCIPLINES>();
                    paleoDispTypeHash.put(colInfo.getTaxonNameId(), typeDispSet);
                }
                typeDispSet.add(colInfo.getDisciplineTypeObj().getDisciplineType());

                paleoCnt++;
            }
            System.out.println("--------------------------------------");
            //System.out.println(colInfo.toString()+"\n");
        } // for loop

        int cnt = 0;
        StringBuilder msg = new StringBuilder();
        for (Integer taxonomyTypId : taxonomyTypeHash.keySet()) {
            HashSet<CollectionInfo> taxonomyTypeSet = taxonomyTypeHash.get(taxonomyTypId);
            if (taxonomyTypeSet.size() > 1) {
                msg.append(
                        String.format("<html>TaxonomyTypeId %d has more than one Discpline/Collection:<br><OL>",
                                taxonomyTypId));
                for (CollectionInfo ci : taxonomyTypeSet) {
                    msg.append(String.format("<LI>%s - %s - %s</LI>", ci.getCatSeriesName(),
                            ci.getColObjTypeName(), ci.getTaxonomyTypeName()));
                }
                msg.append("</OL>");
                cnt++;
            }
        }

        if (cnt > 0) {
            JOptionPane.showConfirmDialog(null, msg.toString(), "Taxomony Type Issues",
                    JOptionPane.CLOSED_OPTION, JOptionPane.QUESTION_MESSAGE);
        }

        // Will be zero for no Paleo collections
        if (paleoCnt > 1) {
            // Check to see if they all use the same tree
            if (paleoColInfoHash.size() > 1) {
                msg.setLength(0);
                // We get here when there is more than one Taxon Tree for the Paleo Collections
                for (Integer treeId : paleoColInfoHash.keySet()) {
                    Vector<CollectionInfo> ciList = paleoColInfoHash.get(treeId);
                    CollectionInfo colInfo = ciList.get(0);
                    msg.append(String.format("The following collections use Taxon Tree '%s':\n",
                            colInfo.getTaxonomyTypeName()));
                    for (CollectionInfo ci : paleoColInfoHash.get(treeId)) {
                        DisciplineType dType = getStandardDisciplineName(ci.getTaxonomyTypeName(),
                                ci.getColObjTypeName(), ci.getCatSeriesName());

                        String name = String.format("%s / %s / %s / %s / %s", ci.getCatSeriesPrefix(),
                                ci.getCatSeriesName(), ci.getColObjTypeName(), ci.getTaxonomyTypeName(),
                                dType.toString());
                        msg.append(name);
                        msg.append("\n");
                    }
                    msg.append("\n");
                }

                JOptionPane.showConfirmDialog(null, msg.toString(), "Paleo Taxon Tree Issues",
                        JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE);

            } else {
                StringBuilder colNames = new StringBuilder();
                for (Integer treeId : paleoColInfoHash.keySet()) {
                    for (CollectionInfo ci : paleoColInfoHash.get(treeId)) {
                        colNames.append("<LI>");
                        colNames.append(ci.getCatSeriesName());
                        colNames.append("</LI>");
                    }
                }

                // You get here when all the Paleo Disciplines use the same tree
                String msgStr = "<html>All the Paleo Collections need to use the same Taxon Tree and<br>therefore needs to be in the same discipline:<br><ol>";
                JOptionPane.showConfirmDialog(null, msgStr + colNames.toString(), "Paleo Taxon Tree Issues",
                        JOptionPane.CLOSED_OPTION, JOptionPane.QUESTION_MESSAGE);

                for (Integer treeId : paleoColInfoHash.keySet()) {
                    Vector<CollectionInfo> ciList = paleoColInfoHash.get(treeId);
                    CollectionInfo colInfo = ciList.get(0);
                    for (CollectionInfo ci : paleoColInfoHash.get(treeId)) {
                        ci.setDisciplineTypeObj(colInfo.getDisciplineTypeObj());
                    }
                }
            }
            //
        }

        DefaultTableModel model = CollectionInfo.getCollectionInfoTableModel(false);
        if (model.getRowCount() > 1) {
            TableWriter colInfoTblWriter = convLogger.getWriter("colinfo.html", "Collection Info");

            colInfoTblWriter.startTable();
            colInfoTblWriter.logHdr(CollectionInfoModel.getHeaders());

            Object[] row = new Object[model.getColumnCount()];
            for (int r = 0; r < model.getRowCount(); r++) {
                for (int i = 0; i < model.getColumnCount(); i++) {
                    row[i] = model.getValueAt(r, i);
                }
                colInfoTblWriter.logObjRow(row);
            }
            colInfoTblWriter.endTable();
            colInfoTblWriter.println("<BR><h3>Collections to be Created.</h3>");
            colInfoTblWriter.startTable();
            colInfoTblWriter.logHdr(CollectionInfoModel.getHeaders());

            model = CollectionInfo.getCollectionInfoTableModel(true);
            row = new Object[model.getColumnCount()];
            for (int r = 0; r < model.getRowCount(); r++) {
                for (int i = 0; i < model.getColumnCount(); i++) {
                    row[i] = model.getValueAt(r, i);
                }
                colInfoTblWriter.logObjRow(row);
            }
            colInfoTblWriter.endTable();
            colInfoTblWriter.close();

            File file = new File(colInfoTblWriter.getFileName());
            if (file != null && file.exists()) {
                try {
                    AttachmentUtils.openURI(file.toURI());

                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }

        for (CollectionInfo ci : CollectionInfo.getFilteredCollectionInfoList()) {
            String sql = "select preparationmethod, ct.* from usyscollobjprepmeth pt inner join usysmetafieldsetsubtype st on st.fieldsetsubtypeid = pt.fieldsetsubtypeid "
                    + "inner join collectionobjecttype ct1 on ct1.collectionobjecttypeid = st.fieldvalue "
                    + "inner join collectionobjecttype ct on ct.collectionobjecttypename = replace(ct1.collectionobjecttypename, ' Preparation', '') "
                    + "inner join catalogseriesdefinition csd on csd.objecttypeid = ct.collectionobjecttypeid "
                    + "inner join catalogseries cs on cs.catalogseriesid = csd.catalogseriesid "
                    + "WHERE csd.catalogseriesid = " + ci.getCatSeriesId();

            System.out.println("\n------------------");
            System.out.println(ci.getCatSeriesName());
            System.out.println(sql);
            System.out.println("------------------");

            int i = 0;
            Vector<Object[]> list = BasicSQLUtils.query(oldDBConn, sql);
            if (list.size() > 0) {
                for (Object[] row : list) {
                    System.out.print(i + " - ");
                    for (Object col : row) {
                        System.out.print(col != null ? col.toString() : "null");
                        System.out.print(", ");
                    }
                    System.out.println();
                    i++;
                }
            } else {
                System.out.println("No Results");
            }

            sql = "select ct.*, (select relatedsubtypevalues from usysmetacontrol c "
                    + "left join usysmetafieldsetsubtype fst on fst.fieldsetsubtypeid = c.fieldsetsubtypeid "
                    + "where objectid = 10290 and ct.taxonomytypeid = c.relatedsubtypevalues) as DeterminationTaxonType "
                    + "from collectiontaxonomytypes ct where ct.biologicalobjecttypeid = "
                    + ci.getColObjTypeId();

            sql = String.format(
                    "SELECT CollectionTaxonomyTypesID, BiologicalObjectTypeID, CollectionObjectTypeName FROM (select ct.*, "
                            + "(SELECT distinct relatedsubtypevalues FROM usysmetacontrol c "
                            + "LEFT JOIN usysmetafieldsetsubtype fst ON fst.fieldsetsubtypeid = c.fieldsetsubtypeid "
                            + "WHERE objectid = 10290 AND ct.taxonomytypeid = c.relatedsubtypevalues) AS DeterminationTaxonType "
                            + "FROM collectiontaxonomytypes ct WHERE ct.biologicalobjecttypeid = %d) T1 "
                            + "INNER JOIN collectionobjecttype cot ON T1.biologicalobjecttypeid = cot.CollectionObjectTypeID",
                    ci.getColObjTypeId());

            System.out.println("\n------------------");
            System.out.println(ci.getColObjTypeName());
            System.out.println(sql);
            System.out.println("------------------");

            i = 0;
            list = BasicSQLUtils.query(oldDBConn, sql);
            if (list.size() > 0) {
                for (Object[] row : list) {
                    System.out.print(i + " - ");
                    for (Object col : row) {
                        System.out.print(col != null ? col.toString() : "null");
                        System.out.print(", ");
                    }
                    System.out.println();
                    i++;
                }
            } else {
                System.out.println("No Results");
            }
        }

        /*
                
        String sql = " select ct.*, (select relatedsubtypevalues from usysmetacontrol c " +
          "left join usysmetafieldsetsubtype fst on fst.fieldsetsubtypeid = c.fieldsetsubtypeid " +
          "where objectid = 10290 and ct.taxonomytypeid = c.relatedsubtypevalues) as DeterminationTaxonType " +
          "from collectiontaxonomytypes ct where ct.biologicalobjecttypeid = 13";
                
        System.out.println("\n------------------");
        System.out.println("List of the taxonomytypes associated with a CollectionObjectTypeID");
        System.out.println(sql);
        System.out.println("------------------");
                
        int i = 0;
        Vector<Object[]> list = BasicSQLUtils.query(oldDBConn, sql);
        if (list.size() > 0)
        {
        for (Object[] row : list)
        {
            System.out.print(i+" - ");
            for (Object col: row)
            {
                System.out.print(col != null ? col.toString() : "null");
                System.out.print(", ");
            }
            System.out.println();
        }
        } else
        {
        System.out.println("No Results");
        }*/

        CellConstraints cc = new CellConstraints();
        PanelBuilder pb = new PanelBuilder(new FormLayout("f:p:g", "p,2px,f:p:g,10px,p,2px,p:g,8px"));

        JTable tableTop = new JTable(CollectionInfo.getCollectionInfoTableModel(false));
        JTable tableBot = new JTable(
                CollectionInfo.getCollectionInfoTableModel(!CollectionInfo.DOING_ACCESSSION));

        int rows = 10;
        tableTop.setPreferredScrollableViewportSize(new Dimension(
                tableTop.getPreferredScrollableViewportSize().width, rows * tableTop.getRowHeight()));
        tableBot.setPreferredScrollableViewportSize(new Dimension(
                tableBot.getPreferredScrollableViewportSize().width, rows * tableBot.getRowHeight()));

        pb.add(UIHelper.createLabel("Available Specify 5 Taxononmic Types", SwingConstants.CENTER),
                cc.xy(1, 1));
        pb.add(UIHelper.createScrollPane(tableTop), cc.xy(1, 3));

        pb.add(UIHelper.createLabel("Specify 5 Collections to be Created", SwingConstants.CENTER), cc.xy(1, 5));
        pb.add(UIHelper.createScrollPane(tableBot), cc.xy(1, 7));

        pb.setDefaultDialogBorder();
        CustomDialog dlg = new CustomDialog(null, "Taxononic Types", true, pb.getPanel());
        dlg.createUI();

        dlg.setSize(1024, 500);

        UIHelper.centerWindow(dlg);
        dlg.setAlwaysOnTop(true);
        dlg.setVisible(true);

        if (dlg.isCancelled()) {
            return CollectionResultType.eCancel;
        }

        Pair<CollectionInfo, DisciplineType> pair = CollectionInfo.getDisciplineType(oldDBConn);
        if (pair == null || pair.second == null) {
            CollectionInfo colInfo = pair.first;
            disciplineType = getStandardDisciplineName(colInfo.getTaxonomyTypeName(),
                    colInfo.getColObjTypeName(), colInfo.getCatSeriesName());
        } else {
            disciplineType = pair.second;
        }

        return disciplineType != null ? CollectionResultType.eOK : CollectionResultType.eError;
    }
    return CollectionResultType.eError;
}