Example usage for javax.swing.table AbstractTableModel AbstractTableModel

List of usage examples for javax.swing.table AbstractTableModel AbstractTableModel

Introduction

In this page you can find the example usage for javax.swing.table AbstractTableModel AbstractTableModel.

Prototype

AbstractTableModel

Source Link

Usage

From source file:org.apache.oodt.cas.workflow.gui.perspective.view.impl.DefaultPropView.java

private JTable createTable(final ViewState state) {
    JTable table;/* w  w  w . java 2 s. c  o m*/
    final ModelGraph selected = state.getSelected();
    if (selected != null) {
        final Vector<Vector<String>> rows = new Vector<Vector<String>>();
        ConcurrentHashMap<String, String> keyToGroupMap = new ConcurrentHashMap<String, String>();
        Metadata staticMet = selected.getModel().getStaticMetadata();
        Metadata inheritedMet = selected.getInheritedStaticMetadata(state);
        Metadata completeMet = new Metadata();
        if (staticMet != null) {
            completeMet.replaceMetadata(staticMet.getSubMetadata(state.getCurrentMetGroup()));
        }
        if (selected.getModel().getExtendsConfig() != null) {
            for (String configGroup : selected.getModel().getExtendsConfig()) {
                Metadata extendsMetadata = state.getGlobalConfigGroups().get(configGroup).getMetadata()
                        .getSubMetadata(state.getCurrentMetGroup());
                for (String key : extendsMetadata.getAllKeys()) {
                    if (!completeMet.containsKey(key)) {
                        keyToGroupMap.put(key, configGroup);
                        completeMet.replaceMetadata(key, extendsMetadata.getAllMetadata(key));
                    }
                }
            }
        }
        if (inheritedMet != null) {
            Metadata inheritedMetadata = inheritedMet.getSubMetadata(state.getCurrentMetGroup());
            for (String key : inheritedMetadata.getAllKeys()) {
                if (!completeMet.containsKey(key)) {
                    keyToGroupMap.put(key, "__inherited__");
                    completeMet.replaceMetadata(key, inheritedMetadata.getAllMetadata(key));
                }
            }
        }
        List<String> keys = completeMet.getAllKeys();
        Collections.sort(keys);
        for (String key : keys) {
            if (key.endsWith("/envReplace")) {
                continue;
            }
            String values = StringUtils.join(completeMet.getAllMetadata(key), ",");
            Vector<String> row = new Vector<String>();
            row.add(keyToGroupMap.get(key));
            row.add(key);
            row.add(values);
            row.add(Boolean.toString(Boolean.parseBoolean(completeMet.getMetadata(key + "/envReplace"))));
            rows.add(row);
        }
        table = new JTable();// rows, new Vector<String>(Arrays.asList(new
                             // String[] { "key", "values", "envReplace" })));
        table.setModel(new AbstractTableModel() {
            public String getColumnName(int col) {
                switch (col) {
                case 0:
                    return "group";
                case 1:
                    return "key";
                case 2:
                    return "values";
                case 3:
                    return "envReplace";
                default:
                    return null;
                }
            }

            public int getRowCount() {
                return rows.size() + 1;
            }

            public int getColumnCount() {
                return 4;
            }

            public Object getValueAt(int row, int col) {
                if (row >= rows.size()) {
                    return null;
                }
                String value = rows.get(row).get(col);
                if (value == null && col == 3) {
                    return "false";
                }
                if (value == null && col == 0) {
                    return "__local__";
                }
                return value;
            }

            public boolean isCellEditable(int row, int col) {
                if (row >= rows.size()) {
                    return selected.getModel().getStaticMetadata().containsGroup(state.getCurrentMetGroup());
                }
                if (col == 0) {
                    return false;
                }
                String key = rows.get(row).get(1);
                return key == null || (selected.getModel().getStaticMetadata() != null
                        && selected.getModel().getStaticMetadata().containsKey(getKey(key, state)));
            }

            public void setValueAt(Object value, int row, int col) {
                if (row >= rows.size()) {
                    Vector<String> newRow = new Vector<String>(
                            Arrays.asList(new String[] { null, null, null, null }));
                    newRow.add(col, (String) value);
                    rows.add(newRow);
                } else {
                    Vector<String> rowValues = rows.get(row);
                    rowValues.add(col, (String) value);
                    rowValues.remove(col + 1);
                }
                this.fireTableCellUpdated(row, col);
            }

        });
        MyTableListener tableListener = new MyTableListener(state);
        table.getModel().addTableModelListener(tableListener);
        table.getSelectionModel().addListSelectionListener(tableListener);
    } else {
        table = new JTable(new Vector<Vector<String>>(),
                new Vector<String>(Arrays.asList(new String[] { "key", "values", "envReplace" })));
    }

    // table.setFillsViewportHeight(true);
    table.setSelectionBackground(Color.cyan);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    TableCellRenderer cellRenderer = new TableCellRenderer() {

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                boolean hasFocus, int row, int column) {
            JLabel field = new JLabel((String) value);
            if (column == 0) {
                field.setForeground(Color.gray);
            } else {
                if (isSelected) {
                    field.setBorder(new EtchedBorder(1));
                }
                if (table.isCellEditable(row, 1)) {
                    field.setForeground(Color.black);
                } else {
                    field.setForeground(Color.gray);
                }
            }
            return field;
        }

    };
    TableColumn groupCol = table.getColumnModel().getColumn(0);
    groupCol.setPreferredWidth(75);
    groupCol.setCellRenderer(cellRenderer);
    TableColumn keyCol = table.getColumnModel().getColumn(1);
    keyCol.setPreferredWidth(200);
    keyCol.setCellRenderer(cellRenderer);
    TableColumn valuesCol = table.getColumnModel().getColumn(2);
    valuesCol.setPreferredWidth(300);
    valuesCol.setCellRenderer(cellRenderer);
    TableColumn envReplaceCol = table.getColumnModel().getColumn(3);
    envReplaceCol.setPreferredWidth(75);
    envReplaceCol.setCellRenderer(cellRenderer);

    table.addMouseListener(new MouseListener() {
        public void mouseClicked(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON3 && DefaultPropView.this.table.getSelectedRow() != -1) {
                int row = DefaultPropView.this.table.getSelectedRow();// rowAtPoint(DefaultPropView.this.table.getMousePosition());
                String key = getKey((String) DefaultPropView.this.table.getValueAt(row, 1), state);
                Metadata staticMet = state.getSelected().getModel().getStaticMetadata();
                override.setVisible(staticMet == null || !staticMet.containsKey(key));
                delete.setVisible(staticMet != null && staticMet.containsKey(key));
                tableMenu.show(DefaultPropView.this.table, e.getX(), e.getY());
            }
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }

        public void mousePressed(MouseEvent e) {
        }

        public void mouseReleased(MouseEvent e) {
        }
    });

    return table;
}

From source file:org.gtdfree.GTDFree.java

/**
 * This method initializes aboutDialog   
 *    //from w w  w.  j  a  v  a 2 s . c  o  m
 * @return javax.swing.JDialog
 */
private JDialog getAboutDialog() {
    if (aboutDialog == null) {
        aboutDialog = new JDialog(getJFrame(), true);
        aboutDialog.setTitle(Messages.getString("GTDFree.About.Free")); //$NON-NLS-1$
        Image i = ApplicationHelper.loadImage(ApplicationHelper.icon_name_large_logo); //$NON-NLS-1$
        ImageIcon ii = new ImageIcon(i);

        JTabbedPane jtp = new JTabbedPane();

        JPanel jp = new JPanel();
        jp.setLayout(new GridBagLayout());
        JLabel jl = new JLabel("GTD-Free", ii, SwingConstants.CENTER); //$NON-NLS-1$
        jl.setIconTextGap(22);
        jl.setFont(jl.getFont().deriveFont((float) 24));
        jp.add(jl, new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(11, 11, 11, 11), 0, 0));
        String s = "Version " + ApplicationHelper.getVersion(); //$NON-NLS-1$
        jp.add(new JLabel(s, SwingConstants.CENTER), new GridBagConstraints(0, 1, 1, 1, 1, 0,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(4, 11, 4, 11), 0, 0));
        s = Messages.getString("GTDFree.About.DBType") //$NON-NLS-1$
                + getEngine().getGTDModel().getDataRepository().getDatabaseType();
        jp.add(new JLabel(s, SwingConstants.CENTER), new GridBagConstraints(0, 2, 1, 1, 1, 0,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(11, 11, 2, 11), 0, 0));
        s = Messages.getString("GTDFree.About.DBloc") + getEngine().getDataFolder(); //$NON-NLS-1$
        jp.add(new JLabel(s, SwingConstants.CENTER), new GridBagConstraints(0, 3, 1, 1, 1, 0,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 11, 4, 11), 0, 0));
        jp.add(new JLabel("Copyright  2008,2009 ikesan@users.sourceforge.net", SwingConstants.CENTER), //$NON-NLS-1$
                new GridBagConstraints(0, 4, 1, 1, 1, 0, GridBagConstraints.CENTER,
                        GridBagConstraints.HORIZONTAL, new Insets(11, 11, 11, 11), 0, 0));
        jtp.addTab("About", jp); //$NON-NLS-1$

        jp = new JPanel();
        jp.setLayout(new GridBagLayout());
        TableModel tm = new AbstractTableModel() {
            private static final long serialVersionUID = -8449423008172417278L;
            private String[] props;

            private String[] getProperties() {
                if (props == null) {
                    props = System.getProperties().keySet().toArray(new String[System.getProperties().size()]);
                    Arrays.sort(props);
                }
                return props;
            }

            @Override
            public String getColumnName(int column) {
                switch (column) {
                case 0:
                    return Messages.getString("GTDFree.About.Prop"); //$NON-NLS-1$
                case 1:
                    return Messages.getString("GTDFree.About.Val"); //$NON-NLS-1$
                default:
                    return null;
                }
            }

            public int getColumnCount() {
                return 2;
            }

            public int getRowCount() {
                return getProperties().length;
            }

            public Object getValueAt(int rowIndex, int columnIndex) {
                switch (columnIndex) {
                case 0:
                    return getProperties()[rowIndex];
                case 1:
                    return System.getProperty(getProperties()[rowIndex]);
                default:
                    return null;
                }
            }
        };
        JTable jt = new JTable(tm);
        jp.add(new JScrollPane(jt), new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER,
                GridBagConstraints.BOTH, new Insets(11, 11, 11, 11), 0, 0));
        jtp.addTab(Messages.getString("GTDFree.About.SysP"), jp); //$NON-NLS-1$

        jp = new JPanel();
        jp.setLayout(new GridBagLayout());
        JTextArea ta = new JTextArea();
        ta.setEditable(false);
        ta.setText(ApplicationHelper.loadLicense());
        ta.setCaretPosition(0);
        jp.add(new JScrollPane(ta), new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER,
                GridBagConstraints.BOTH, new Insets(11, 11, 11, 11), 0, 0));
        jtp.addTab("License", jp); //$NON-NLS-1$

        aboutDialog.setContentPane(jtp);
        aboutDialog.setSize(550, 300);
        //aboutDialog.pack();
        aboutDialog.setLocationRelativeTo(getJFrame());
    }
    return aboutDialog;
}

From source file:pt.webdetails.cda.utils.kettle.RowMetaToTableModel.java

/**
 * //w w  w . j a v a  2s .c  o m
 * @return converter from a list of {@link RowMetaAndData} to a {@link TableModel}
 */
public static Function<List<RowMetaAndData>, TableModel> getConverter() {
    return new Function<List<RowMetaAndData>, TableModel>() {
        private TableModelHeader header;

        @Override
        public TableModel apply(final List<RowMetaAndData> rowMetaAndData) {
            if (logger.isTraceEnabled()) {
                logger.trace(" new list received, " + rowMetaAndData.size() + " items ");
            }
            if (rowMetaAndData.isEmpty()) {
                return new EmptyTableModel();
            }
            if (header == null) {
                header = new TableModelHeader(rowMetaAndData.get(0).getRowMeta());
            }
            return new AbstractTableModel() {
                private static final long serialVersionUID = 1L;

                @Override
                public int getRowCount() {
                    return rowMetaAndData.size();
                }

                @Override
                public int getColumnCount() {
                    return header.getColumnCount();
                }

                @Override
                public String getColumnName(int column) {
                    return header.getColumnName(column);
                }

                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    return header.getColumnClass(columnIndex);
                }

                @Override
                public Object getValueAt(int rowIndex, int columnIndex) {
                    RowMetaAndData rowMetaData = rowMetaAndData.get(rowIndex);
                    Object value = rowMetaData.getData()[columnIndex];

                    //getting dates as java.sql.Timestamp and not as java.util.Date makes simpler to deal with them
                    //since they get formated as a simpler format when "stringded"
                    if (value != null && value instanceof Date && rowMetaData.getValueMeta(columnIndex)
                            .getType() == ValueMetaInterface.TYPE_DATE) {

                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime((Date) value);
                        return new Timestamp(calendar.getTimeInMillis());
                    }

                    return value;
                }
            };
        }
    };
}

From source file:tauargus.gui.PanelTable.java

public void setRowColumnVariables(Variable rowVariable, Variable columnVariable) {
    int VarDepth = 1;
    int MaxLevelChoice;
    isAdjusting = true;/*from  w w  w. j a  va2s .  co  m*/
    rowExpVarIndex = indexOfVariable(rowVariable);
    columnExpVarIndex = indexOfVariable(columnVariable);
    createExpVarPermutation();
    // Automatically adjust maximum number of levels to choose from, depending on variable
    VarDepth = rowVariable.GetDepthOfHierarchicalBoom(rowVariable.recoded);
    MaxLevelChoice = comboBoxNrOfVertLevels.getItemCount() - 1;
    if (VarDepth - MaxLevelChoice < 0) {
        for (int j = VarDepth; j < MaxLevelChoice; j++) {
            comboBoxNrOfVertLevels.removeItemAt(VarDepth + 1);
        }
    } else {
        for (int j = MaxLevelChoice + 1; j <= VarDepth; j++) {
            //comboBoxNrOfVertLevels.addItem(Integer.toString(j+1));
            comboBoxNrOfVertLevels.addItem(Integer.toString(j));
        }
    }
    comboBoxNrOfVertLevels.setSelectedIndex(1);

    if (!isSingleColumn) {
        comboBoxNrOfHorLevels.setVisible(true);
        LabelNrOfHorLevels.setVisible(true);
        buttonSelectView.setEnabled(true);
        VarDepth = columnVariable.GetDepthOfHierarchicalBoom(columnVariable.recoded);
        MaxLevelChoice = comboBoxNrOfHorLevels.getItemCount() - 1;
        if (VarDepth - MaxLevelChoice < 0) {
            for (int j = VarDepth; j < MaxLevelChoice; j++) {
                comboBoxNrOfHorLevels.removeItemAt(VarDepth + 1);
            }
        } else {
            for (int j = MaxLevelChoice + 1; j <= VarDepth; j++) {
                //comboBoxNrOfHorLevels.addItem(Integer.toString(j+1));
                comboBoxNrOfHorLevels.addItem(Integer.toString(j));
            }
        }
        comboBoxNrOfHorLevels.setSelectedIndex(1);
    } else {
        comboBoxNrOfHorLevels.setVisible(false);
        LabelNrOfHorLevels.setVisible(false);
        buttonSelectView.setEnabled(false);
    }

    //      Implicitly called by setting above 2 combo boxes.        
    //        createRowIndices();
    //        createColumnIndices();

    String hs = tableSet.expVar.get(rowExpVarIndex).name;
    if (!isSingleColumn) {
        hs = tableSet.expVar.get(rowExpVarIndex).name + " x " + tableSet.expVar.get(columnExpVarIndex).name;
    }
    labelRowColVars.setText(hs);

    int n = tableSet.expVar.size();
    for (int i = 0; i < n - 2; i++) {
        labelSpan[i].setText(tableSet.expVar.get(expVarPermutation[i + 2]).name);
        int expvarIndex = expVarPermutation[i + 2];
        comboBoxSpan[i].removeAllItems();
        for (int j = 0; j < codeList[expvarIndex].length; j++) {
            comboBoxSpan[i].addItem(codeList[expvarIndex][j].label);
        }
        comboBoxSpan[i].setSelectedIndex(0);
    }

    table.setModel(new AbstractTableModel() {
        @Override
        public int getColumnCount() {
            if (isSingleColumn) {
                return 2;
            } else {
                return 1 + columnCodeIndex.length;
            }
        }

        @Override
        public int getRowCount() {
            return rowCodeIndex.length;
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            if (columnIndex == 0) {
                return Code.class;
            } else {
                return Cell.class;
            }
        }

        @Override
        public String getColumnName(int column) {
            if (column == 0) {
                // upper left corner
                return "";
            } else {
                // column header
                if (isSingleColumn) {
                    return "Total";
                }
                Code code = getColumnCode(column);
                String s = "";
                if (code.state == Code.EXPANDED) {
                    s = "- ";
                } else if (code.state == Code.COLLAPSED) {
                    s = "+ ";
                }
                s += code.label;
                return s;
            }
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            if (columnIndex == 0) {
                // row header
                return getRowCode(rowIndex);
            } else {
                return getCell(rowIndex, columnIndex);
            }
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }
    });

    isAdjusting = false;
    ((AbstractTableModel) table.getModel()).fireTableStructureChanged();
    ((AbstractTableModel) table.getModel()).fireTableDataChanged();
    adjustColumnWidths();
    table.getSelectionModel().setSelectionInterval(0, 0);
    table.getColumnModel().getSelectionModel().setSelectionInterval(1, 1);
}

From source file:ubic.basecode.bio.geneset.GeneAnnotations.java

/**
 * @return//w w  w.j av  a 2  s  .co m
 */
public TableModel toTableModel() {
    return new AbstractTableModel() {
        private static final long serialVersionUID = 1L;
        private String[] columnNames = { "Probe", "Gene", "Description" };

        public int getColumnCount() {
            return 3;
        }

        @Override
        public String getColumnName(int i) {
            return columnNames[i];
        }

        public int getRowCount() {
            return selectedProbes.size();
        }

        public Object getValueAt(int i, int j) {

            String probeid = selectedProbes.get(i);
            switch (j) {
            case 0:
                return probeid;
            case 1:
                return getProbeGeneName(probeid);
            case 2:
                return getProbeDescription(probeid);
            default:
                return null;
            }
        }

    };
}