001    package nl.cwi.sen1.visplugin.table;
002    
003    import java.awt.BorderLayout;
004    import java.awt.event.ActionEvent;
005    
006    import javax.swing.AbstractAction;
007    import javax.swing.JMenu;
008    import javax.swing.JMenuItem;
009    import javax.swing.JPanel;
010    import javax.swing.JScrollPane;
011    import javax.swing.JTable;
012    import javax.swing.JTextArea;
013    import javax.swing.table.JTableHeader;
014    import javax.swing.table.TableColumn;
015    import javax.swing.table.TableColumnModel;
016    
017    import nl.cwi.sen1.relationstores.types.Location;
018    import nl.cwi.sen1.relationstores.types.RTuple;
019    import nl.cwi.sen1.visplugin.VisualizationPluginController;
020    import nl.cwi.sen1.visplugin.VisualizationPluginWindow;
021    import nl.cwi.sen1.visplugin.table.model.HeaderListener;
022    import nl.cwi.sen1.visplugin.table.model.LocationButtonRenderer;
023    import nl.cwi.sen1.visplugin.table.model.SortButtonRenderer;
024    import nl.cwi.sen1.visplugin.table.model.SortableTableModel;
025    import nl.cwi.sen1.visplugin.table.model.SortableTableModelExporter;
026    import nl.cwi.sen1.visplugin.table.model.TableLocationButtonListener;
027    import aterm.ATerm;
028    
029    /**
030     * The TableVisualizationWindows class. It Extends VisualizationPluginWindow.
031     *
032     * @author Srinivasan Tharmarajah
033     * @author Wasim Alsaqaf
034     * @date 08-03-2007
035     */
036    public class TableVisualizationWindow extends VisualizationPluginWindow {
037    
038        private SortableTableModel m_model = new SortableTableModel();
039    
040        private JTable m_table;
041    
042        private static final long serialVersionUID = 1;
043    
044        /**
045         * Returns JPanel including JTable with Data.
046         *
047         * @author Srinivasan Tharmarajah
048         * @author Wasim Alsaqaf
049         * @author Anton Gerdessen
050         * @author Anton Lycklama a Nijeholt
051         * @author Bas Basten
052         * @author Arend van Beelen (refactoring)
053         * @date 13-3-2007
054         */
055        public JPanel render(RTuple fact) {
056            JPanel controlArea = new JPanel(new BorderLayout());
057    
058            try {
059                m_model.setRTupleData(fact);
060                
061                if (m_model.getRowCount() == 0) {
062                    controlArea.add(new JTextArea("This fact is empty."));
063                    return controlArea;
064                }
065    
066                m_table = new JTable(m_model);
067    
068                // create the buttons to enable header sorting
069                SortButtonRenderer sortRenderer = new SortButtonRenderer();
070                TableColumnModel columnModel = m_table.getColumnModel();
071                for (int columnNum = 0; columnNum < m_model.getColumnCount(); columnNum++) {
072                    columnModel.getColumn(columnNum).setHeaderRenderer(sortRenderer);
073                }
074    
075                // add the listener to the header to catch the mouse clicks
076                JTableHeader header = m_table.getTableHeader();
077                header.addMouseListener(new HeaderListener(header, sortRenderer));
078    
079                // create the buttons for location columns
080                for (int columnNum = 0; columnNum < m_model.getColumnCount(); columnNum++) {
081                    TableColumn modelColumn = columnModel.getColumn(columnNum);
082                    modelColumn.setIdentifier(new Integer(columnNum));
083    
084                    if (m_model.getValueAt(0, columnNum) instanceof Location) {
085                        LocationButtonRenderer locRenderer = new LocationButtonRenderer();
086                        TableColumn tableColumn = m_table.getColumn(new Integer(columnNum));
087                        tableColumn.setCellRenderer(locRenderer);
088                    }
089                }
090    
091                // ugly code but because there is no time we have to send the window
092                // to the listener so that the openLocationEditor function can be called
093                TableLocationButtonListener listener = new TableLocationButtonListener(this, m_table);
094                m_table.addMouseListener(listener);
095                m_table.addMouseMotionListener(listener);
096    
097                // finally add the table to a pane and add it to the JPanel
098                JScrollPane pane = new JScrollPane(m_table);
099                controlArea.add(pane, BorderLayout.CENTER);
100    
101            } catch (Exception ex) {
102                System.err.println(ex);
103                ex.printStackTrace();
104            }
105    
106            return controlArea;
107        }
108    
109        /**
110         * Create a menu
111         *
112         * @author Arjen van Schie & Michel Rijnders
113         * @date 20-2-2007
114         * @return The JMenu
115         */
116        protected JMenu createExtensionMenu() {
117            // create the menu item
118            JMenu extensionMenu = super.createExtensionMenu();
119    
120            // create the sub-item Export
121            JMenuItem export = new JMenuItem("Export to RStore");
122            export.addActionListener(new AbstractAction() {
123                public void actionPerformed(ActionEvent e) {
124                    getController().exportToClicked(
125                            VisualizationPluginController.requestType.rStore,
126                            getWindowId());
127                }
128    
129                private final static long serialVersionUID = 1;
130            });
131            extensionMenu.add(export);
132    
133            return extensionMenu;
134        }
135    
136        /**
137         * Exports the selected rows from the table to an RStore file.
138         *
139         * @param filename Filename of the file to export the RStore to.
140         *
141         * @author Arend van Beelen
142         * @date 13-3-2007
143         */
144        public void exportToRStore(String filename) {
145            SortableTableModelExporter exporter = new SortableTableModelExporter(m_model);
146    
147            exporter.exportToRStore(filename, m_table.getSelectedRows());
148    
149            if( getStudio() != null ) {
150                ATerm term = getStudio().getATermFactory().make("vp-rstore-exported(<str>)", filename);
151                getController().getBridge().sendEvent(term);
152            }
153        }
154    }