001 package nl.cwi.sen1.visplugin.table.model; 002 003 import java.io.FileOutputStream; 004 import java.io.IOException; 005 006 import nl.cwi.sen1.relationstores.Factory; 007 import nl.cwi.sen1.relationstores.types.IdCon; 008 import nl.cwi.sen1.relationstores.types.RElem; 009 import nl.cwi.sen1.relationstores.types.RElemElements; 010 import nl.cwi.sen1.relationstores.types.RStore; 011 import nl.cwi.sen1.relationstores.types.RTuple; 012 import nl.cwi.sen1.relationstores.types.RTupleRtuples; 013 import nl.cwi.sen1.relationstores.types.RType; 014 import nl.cwi.sen1.visplugin.VisualizationFactorySingleton; 015 016 /** 017 * Provides exporting functions for data in the SortableTableModel. 018 * 019 * @author Anton Gerdessen 020 * @author Arend van Beelen 021 * @date 13-03-2007 022 */ 023 public class SortableTableModelExporter { 024 025 Factory m_factory; 026 SortableTableModel m_model; 027 028 /** 029 * Default constructor. 030 * 031 * @param model The model on which the exporter will operate. 032 * 033 * @author Anton Gerdessen 034 * @author Arend van Beelen 035 * @date 13-03-2007 036 */ 037 public SortableTableModelExporter(SortableTableModel model) { 038 m_factory = VisualizationFactorySingleton.getFactoryInstance(); 039 m_model = model; 040 } 041 042 /** 043 * Exports a selection from a table model to an RStore file. 044 * 045 * @param filename File to save the generated RStore to. 046 * @param selectedRows An array containing the indexes of all the table 047 * rows that will be exported. If the array is empty, 048 * the entire table will be exported. 049 * 050 * @author Anton Gerdessen 051 * @author Arend van Beelen 052 * @date 13-03-2007 053 */ 054 public void exportToRStore(String filename, int[] selectedRows) { 055 056 // select all rows if an empty list of rows is given 057 if(selectedRows.length == 0) { 058 selectedRows = new int[m_model.getRowCount()]; 059 for(int rowIndex = 0; rowIndex < m_model.getRowCount(); rowIndex++) { 060 selectedRows[rowIndex] = rowIndex; 061 } 062 } 063 064 RElemElements elements = m_factory.makeRElemElements(); 065 for(int rowIndex : selectedRows) { 066 RElem element = m_model.getRElemForRow(rowIndex); 067 elements = elements.append(element); 068 } 069 IdCon variable = m_model.getRTupleVariable(); 070 RType rType = m_model.getRTupleRType(); 071 RElem value = m_factory.makeRElem_Set(elements); 072 RTuple rTuple = m_factory.makeRTuple_Rtuple(variable, rType, value); 073 RTupleRtuples rTupleRTuples = m_factory.makeRTupleRtuples(rTuple); 074 RStore rStore = m_factory.makeRStore_Rstore(rTupleRTuples); 075 try { 076 rStore.toTerm().writeToTextFile(new FileOutputStream(filename)); 077 } 078 catch(IOException exception) { 079 System.err.println("Could not write RStore to file " + filename + "."); 080 } 081 } 082 }