Example usage for javax.swing JTable tableChanged

List of usage examples for javax.swing JTable tableChanged

Introduction

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

Prototype

public void tableChanged(TableModelEvent e) 

Source Link

Document

Invoked when this table's TableModel generates a TableModelEvent.

Usage

From source file:org.simmi.GeneSetHead.java

License:asdf

public TransferHandler dragRows(final JTable table, final List<String> specs) {
    TransferHandler th = null;/*  ww w.j a  v  a2 s  .  c  om*/
    try {
        final DataFlavor ndf = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType);
        final DataFlavor df = DataFlavor.getTextPlainUnicodeFlavor();
        final String charset = df.getParameter("charset");
        final Transferable transferable = new Transferable() {
            @Override
            public Object getTransferData(DataFlavor arg0) throws UnsupportedFlavorException, IOException {
                if (arg0.equals(ndf)) {
                    int[] rr = currentRowSelection; //table.getSelectedRows();
                    List<String> selseq = new ArrayList<String>(rr.length);
                    for (int r : rr) {
                        int i = table.convertRowIndexToModel(r);
                        selseq.add(specs.get(i));
                    }
                    return selseq;
                } else {
                    String ret = "";//makeCopyString();
                    for (int r = 0; r < table.getRowCount(); r++) {
                        Object o = table.getValueAt(r, 0);
                        if (o != null) {
                            ret += o.toString();
                        } else {
                            ret += "";
                        }
                        for (int c = 1; c < table.getColumnCount(); c++) {
                            o = table.getValueAt(r, c);
                            if (o != null) {
                                ret += "\t" + o.toString();
                            } else {
                                ret += "\t";
                            }
                        }
                        ret += "\n";
                    }
                    //return arg0.getReaderForText( this );
                    return new ByteArrayInputStream(ret.getBytes(charset));
                }
                //return ret;
            }

            @Override
            public DataFlavor[] getTransferDataFlavors() {
                return new DataFlavor[] { df, ndf };
            }

            @Override
            public boolean isDataFlavorSupported(DataFlavor arg0) {
                if (arg0.equals(df) || arg0.equals(ndf)) {
                    return true;
                }
                return false;
            }
        };

        th = new TransferHandler() {
            private static final long serialVersionUID = 1L;

            public int getSourceActions(JComponent c) {
                return TransferHandler.COPY_OR_MOVE;
            }

            public boolean canImport(TransferHandler.TransferSupport support) {
                return true;
            }

            protected Transferable createTransferable(JComponent c) {
                currentRowSelection = table.getSelectedRows();

                return transferable;
            }

            public boolean importData(TransferHandler.TransferSupport support) {
                try {
                    System.err.println(table.getSelectedRows().length);

                    DataFlavor[] dfs = support.getDataFlavors();
                    if (support.isDataFlavorSupported(ndf)) {
                        Object obj = support.getTransferable().getTransferData(ndf);
                        ArrayList<String> seqs = (ArrayList<String>) obj;

                        /*ArrayList<String> newlist = new ArrayList<String>( serifier.lgse.size() );
                        for( int r = 0; r < table.getRowCount(); r++ ) {
                           int i = table.convertRowIndexToModel(r);
                           newlist.add( specs.get(i) );
                        }
                        serifier.lgseq.clear();
                        serifier.lgseq = newlist;*/

                        Point p = support.getDropLocation().getDropPoint();
                        int k = table.rowAtPoint(p);

                        specs.removeAll(seqs);
                        for (String s : seqs) {
                            specs.add(k++, s);
                        }

                        TableRowSorter<TableModel> trs = (TableRowSorter<TableModel>) table.getRowSorter();
                        trs.setSortKeys(null);

                        table.tableChanged(new TableModelEvent(table.getModel()));

                        return true;
                    } /* else if( support.isDataFlavorSupported( df ) ) {                     
                        Object obj = support.getTransferable().getTransferData( df );
                        InputStream is = (InputStream)obj;
                                
                        System.err.println( charset );
                        importReader( new BufferedReader(new InputStreamReader(is, charset)) );
                                
                        updateView();
                                
                        return true;
                      }  else if( support.isDataFlavorSupported( DataFlavor.stringFlavor ) ) {                     
                        Object obj = support.getTransferable().getTransferData( DataFlavor.stringFlavor );
                        String str = (String)obj;
                        importReader( new BufferedReader( new StringReader(str) ) );
                                
                        updateView();
                                
                        return true;
                      }*/
                } catch (UnsupportedFlavorException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return false;
            }
        };
    } catch (Exception e) {
        e.printStackTrace();
    }
    return th;
}

From source file:org.simmi.GeneSetHead.java

License:asdf

public List getSelspecContigs(List<JComponent> complist, final Map<String, List<Sequence>> speccontigMap,
        final String... selspec) {
    List<Sequence> contigs = null;
    final List<String> specs = new ArrayList<String>(speccontigMap.keySet());
    final JTable stable = new JTable();
    stable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    final TableModel stablemodel = new TableModel() {
        @Override//from   www .j a v  a  2  s.  co  m
        public int getRowCount() {
            return specs.size();
        }

        @Override
        public int getColumnCount() {
            return 1;
        }

        @Override
        public String getColumnName(int columnIndex) {
            return "Species";
        }

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

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

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            if (rowIndex >= 0 && rowIndex < specs.size())
                return specs.get(rowIndex);
            return null;
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        }

        @Override
        public void addTableModelListener(TableModelListener l) {
        }

        @Override
        public void removeTableModelListener(TableModelListener l) {
        }
    };
    stable.setModel(stablemodel);

    final JTable ctable = new JTable();
    ctable.setAutoCreateRowSorter(true);
    final TableModel ctablemodel = new TableModel() {
        @Override
        public int getRowCount() {
            int r = stable.getSelectedRow();
            String spec = selspec.length > 0 ? selspec[0] : (String) stable.getValueAt(r, 0);
            if (spec != null) {
                List<Sequence> contigs = geneset.speccontigMap.get(spec);
                return contigs.size();
            }
            return 0;
        }

        @Override
        public int getColumnCount() {
            return 1;
        }

        @Override
        public String getColumnName(int columnIndex) {
            return "Contigs";
        }

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

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

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            int r = stable.getSelectedRow();
            String spec = selspec.length > 0 ? selspec[0] : (String) stable.getValueAt(r, 0);
            List<Sequence> contigs = geneset.speccontigMap.get(spec);
            return contigs.get(rowIndex);
        }

        @Override
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        }

        @Override
        public void addTableModelListener(TableModelListener l) {
        }

        @Override
        public void removeTableModelListener(TableModelListener l) {
        }
    };
    ctable.setModel(ctablemodel);

    JScrollPane sscrollpane = new JScrollPane(stable);
    JScrollPane cscrollpane = new JScrollPane(ctable);

    FlowLayout flowlayout = new FlowLayout();
    JComponent c = new JComponent() {
    };
    c.setLayout(flowlayout);
    if (selspec.length == 0)
        c.add(sscrollpane);
    c.add(cscrollpane);

    if (complist != null) {
        for (JComponent comp : complist) {
            c.add(comp);
        }
    }

    stable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            ctable.tableChanged(new TableModelEvent(ctablemodel));
        }
    });
    JOptionPane.showMessageDialog(this, c);

    int[] rr = stable.getSelectedRows();
    if (rr.length > 1) {
        List slist = new ArrayList();
        for (int r : rr) {
            int i = stable.convertRowIndexToModel(r);
            slist.add(specs.get(i));
        }

        return slist;
    } else {
        int sr = stable.getSelectedRow();
        String spec = selspec.length > 0 ? selspec[0] : (String) stable.getValueAt(sr, 0);
        if (spec != null) {
            List<Sequence> ctgs = geneset.speccontigMap.get(spec);
            rr = ctable.getSelectedRows();
            contigs = new ArrayList<Sequence>();
            for (int r : rr) {
                int i = ctable.convertRowIndexToModel(r);
                contigs.add(ctgs.get(i));
            }
        }

        return contigs;
    }
}