Example #1
0
 /**
  * Updates the field containing the table column count.
  *
  * @param tcModel model
  */
 private void updateColsField(TopcatModel tcModel) {
   final String text;
   if (tcModel == null) {
     text = null;
   } else {
     int nvis = tcModel.getColumnModel().getColumnCount();
     int ntot = tcModel.getDataModel().getColumnCount();
     text =
         nvis == ntot
             ? Integer.toString(nvis)
             : Integer.toString(nvis) + " / " + Integer.toString(ntot);
   }
   colsField_.setText(text);
 }
Example #2
0
 /**
  * Updates the field containing the table row count.
  *
  * @param tcModel model
  */
 private void updateRowsField(TopcatModel tcModel) {
   final String text;
   if (tcModel == null) {
     text = null;
   } else {
     long nvis = tcModel.getViewModel().getRowCount();
     long ntot = tcModel.getDataModel().getRowCount();
     text =
         nvis == ntot
             ? TopcatUtils.formatLong(nvis)
             : TopcatUtils.formatLong(nvis) + " / " + TopcatUtils.formatLong(ntot);
   }
   rowsField_.setText(text);
 }
Example #3
0
 /**
  * Updates the field containing the current row subset.
  *
  * @param tcModel model
  */
 private void updateSubsetField(TopcatModel tcModel) {
   final String text;
   if (tcModel == null) {
     text = null;
   } else {
     RowSubset subset = tcModel.getSelectedSubset();
     text = RowSubset.ALL.equals(subset) ? null : subset.toString();
   }
   subsetField_.setText(text);
 }
Example #4
0
 /**
  * Sets the table which is displayed in this panel.
  *
  * @param tcModel table to display
  */
 private void setDisplayedTable(TopcatModel tcModel) {
   if (tcModel_ != null) {
     tcModel_.removeTopcatListener(tcListener_);
     tcModel_.getColumnModel().removeColumnModelListener(colListener_);
   }
   tcModel_ = tcModel;
   if (saveChooser_ != null) {
     saveChooser_.setEnabled(tcModel != null);
   }
   updateNameField(tcModel);
   updateSubsetField(tcModel);
   updateOrderField(tcModel);
   updateColsField(tcModel);
   updateRowsField(tcModel);
   if (tcModel != null) {
     tcModel.addTopcatListener(tcListener_);
     tcModel.getColumnModel().addColumnModelListener(colListener_);
   }
 }
Example #5
0
  /**
   * Turns a TopcatModel into a StarTable, ready for serialization.
   *
   * @param tcModel model
   * @return table
   */
  public StarTable encode(TopcatModel tcModel) {

    /* Prepare table data and metadata for use and adjustment. */
    final StarTable dataModel = tcModel.getDataModel();
    List<DescribedValue> paramList = new ArrayList<DescribedValue>();
    long nrow = dataModel.getRowCount();
    ColumnStarTable extraTable = ColumnStarTable.makeTableWithRows(nrow);

    /* Mark as serialized TopcatModel. */
    paramList.add(new DescribedValue(IS_TCMODEL_INFO, Boolean.TRUE));
    paramList.add(new DescribedValue(VERSION_INFO, TopcatUtils.getVersion()));

    /* Record label. */
    paramList.add(new DescribedValue(LABEL_INFO, tcModel.getLabel()));

    /* Record column sequences. */
    ColumnList colList = tcModel.getColumnList();
    int nCol = colList.size();
    int[] icols = new int[nCol];
    boolean[] activs = new boolean[nCol];
    for (int jc = 0; jc < nCol; jc++) {
      icols[jc] = colList.getColumn(jc).getModelIndex();
      activs[jc] = colList.isActive(jc);
    }
    paramList.add(new DescribedValue(COLS_INDEX_INFO, icols));
    paramList.add(new DescribedValue(COLS_VISIBLE_INFO, activs));

    /* Record whether to broadcast rows. */
    paramList.add(new DescribedValue(SEND_ROWS_INFO, tcModel.getRowSendModel().isSelected()));

    /* Record sort order. */
    SortOrder sortOrder = tcModel.getSelectedSort();
    TableColumn sortCol = sortOrder == null ? null : sortOrder.getColumn();
    if (sortCol != null) {
      int icolSort = tcModel.getColumnList().indexOf(sortCol);
      if (icolSort >= 0) {
        boolean sense = tcModel.getSortSenseModel().isSelected();
        paramList.add(new DescribedValue(SORT_COLUMN_INFO, new Integer(icolSort)));
        paramList.add(new DescribedValue(SORT_SENSE_INFO, Boolean.valueOf(sense)));
      }
    }

    /* Store row subset flags in a new column. */
    List<RowSubset> subsetList = new ArrayList<RowSubset>(tcModel.getSubsets());
    boolean hadAll = subsetList.remove(RowSubset.ALL);
    assert hadAll;
    RowSubset[] subsets = subsetList.toArray(new RowSubset[0]);
    if (subsets.length > 0) {
      String[] subsetNames = new String[subsets.length];
      for (int is = 0; is < subsets.length; is++) {
        subsetNames[is] = subsets[is].getName();
      }
      paramList.add(new DescribedValue(SUBSET_NAMES_INFO, subsetNames));
      Object flagsArray = createFlagsArray(dataModel, subsets);
      if (flagsArray != null) {
        ColumnData flagsCol = ArrayColumn.makeColumn(SUBSET_FLAGS_INFO.getName(), flagsArray);
        ColumnInfo info = new ColumnInfo(SUBSET_FLAGS_INFO);
        info.setContentClass(flagsCol.getColumnInfo().getContentClass());
        flagsCol.setColumnInfo(info);
        extraTable.addColumn(flagsCol);
      }
    }

    /* Record current subset. */
    int iset = subsetList.indexOf(tcModel.getSelectedSubset());
    if (iset >= 0) {
      paramList.add(new DescribedValue(CURRENT_SUBSET_INFO, new Integer(iset)));
    }

    /* Copy parameters from the input table.
     * Be paranoid about possible name clashes. */
    for (Iterator it = dataModel.getParameters().iterator(); it.hasNext(); ) {
      Object item = it.next();
      if (item instanceof DescribedValue) {
        DescribedValue dval = (DescribedValue) item;
        String name = dval.getInfo().getName();
        String utype = dval.getInfo().getUtype();
        if (!isCodecUtype(utype)) {
          paramList.add(dval);
        }
      }
    }

    /* Prepare the output table object. */
    List<StarTable> joinList = new ArrayList<StarTable>();
    joinList.add(dataModel);
    if (extraTable.getColumnCount() > 0) {
      joinList.add(extraTable);
    }
    StarTable[] joins = joinList.toArray(new StarTable[0]);
    StarTable outTable =
        new MetaCopyStarTable(new JoinStarTable(joinList.toArray(new StarTable[0])));
    outTable.setName(dataModel.getName());

    /* Set the parameters. */
    outTable.getParameters().clear();
    outTable.getParameters().addAll(paramList);

    /* Return the result. */
    return outTable;
  }
Example #6
0
  /**
   * Does the work for the decoding. May throw an unchecked exception, for instance a
   * ClassCastException if certain metadata items are present but have the wrong type (not likely
   * excepting deliberate sabotage, but conceivable).
   *
   * @param table encoded table
   * @param location table location string
   * @param controlWindow control window
   * @return topcat model, or null
   */
  private TopcatModel doDecode(StarTable inTable, String location, ControlWindow controlWindow) {
    CodecTable codec = new CodecTable(inTable);

    /* Determine if this is a TopcatModel encoded by this class. */
    if (!Boolean.TRUE.equals(codec.getCodecValue(IS_TCMODEL_INFO))) {
      return null;
    }
    TopcatModel tcModel =
        TopcatModel.createRawTopcatModel(codec.getDataTable(), location, controlWindow);

    /* Get label. */
    tcModel.setLabel((String) codec.getCodecValue(LABEL_INFO));

    /* Get columns.  This is a bit involved, since a TopcatModel has
     * both a TableColumnModel and a ColumnList which must be updated
     * in a consistent way, to reflect the column order and which
     * columns are currently visible. */
    /* First get a record of the order of columns and their visibility. */
    int[] icols = (int[]) codec.getCodecValue(COLS_INDEX_INFO);
    boolean[] activs = (boolean[]) codec.getCodecValue(COLS_VISIBLE_INFO);
    TableColumnModel colModel = tcModel.getColumnModel();
    ColumnList colList = tcModel.getColumnList();
    int ncol = colModel.getColumnCount();
    assert ncol == colList.size();
    TableColumn[] tcols = new TableColumn[ncol];
    for (int ic = 0; ic < ncol; ic++) {
      tcols[ic] = colList.getColumn(icols[ic]);
    }

    /* Reorder the columns in the TableColumnModel to match the saved
     * order.  This has the effect of updating the ColumnList as well,
     * since it is a listener. */
    for (int ic = 0; ic < ncol; ic++) {
      TableColumn tcol = tcols[ic];
      if (colModel.getColumn(ic) != tcol) {
        int kc = -1;
        for (int jc = ic; jc < ncol && kc < 0; jc++) {
          if (colModel.getColumn(jc) == tcol) {
            kc = jc;
          }
        }
        assert kc >= 0;
        colModel.moveColumn(kc, ic);
      }
    }
    for (int ic = 0; ic < ncol; ic++) {
      assert colModel.getColumn(ic) == tcols[ic];
      assert colList.getColumn(ic) == tcols[ic];
    }

    /* Finally flag each column as visible or not, according to the saved
     * state. */
    for (int ic = 0; ic < ncol; ic++) {
      colList.setActive(ic, activs[ic]);
    }

    /* Get whether to broadcast rows. */
    tcModel.getRowSendModel().setSelected(Boolean.TRUE.equals(codec.getCodecValue(SEND_ROWS_INFO)));

    /* Get current subset index. */
    Integer indexCurrentSubset = (Integer) codec.getCodecValue(CURRENT_SUBSET_INFO);
    int iCurrentSubset = indexCurrentSubset != null ? indexCurrentSubset.intValue() : -1;

    /* Get subsets. */
    String[] subsetNames = (String[]) codec.getCodecValue(SUBSET_NAMES_INFO);
    if (subsetNames != null && subsetNames.length > 0) {
      RowSubset currentSubset = null;
      int nset = subsetNames.length;
      int icolSubsets = codec.getCodecColumnIndex(SUBSET_FLAGS_INFO);
      List<RowSubset> setList = new ArrayList<RowSubset>();
      for (int is = 0; is < nset; is++) {
        RowSubset rset = createRowSubset(subsetNames[is], inTable, icolSubsets, is);
        if (rset != null) {
          setList.add(rset);
        }
        if (is == iCurrentSubset) {
          currentSubset = rset;
        }
      }
      for (RowSubset rset : setList) {
        tcModel.addSubset(rset);
      }
      if (currentSubset != null) {
        tcModel.applySubset(currentSubset);
      }
    }

    /* Get sort order. */
    Integer icolSort = (Integer) codec.getCodecValue(SORT_COLUMN_INFO);
    if (icolSort != null) {
      int icSort = icolSort.intValue();
      boolean sortSense = Boolean.TRUE.equals(codec.getCodecValue(SORT_SENSE_INFO));
      TableColumn tcolSort = colList.getColumn(icSort);
      tcModel.getSortSenseModel().setSelected(sortSense);
      tcModel.sortBy(new SortOrder(tcolSort), sortSense);
    }

    /* Return result. */
    return tcModel;
  }
Example #7
0
 /**
  * Updates the field containing the current sort order.
  *
  * @param tcModel model
  */
 private void updateOrderField(TopcatModel tcModel) {
   orderField_.setText(tcModel == null ? null : tcModel.getSelectedSort().toString());
 }
Example #8
0
 /**
  * Updates the field containing the table name.
  *
  * @param tcModel model
  */
 private void updateNameField(TopcatModel tcModel) {
   nameField_.setText(tcModel == null ? null : tcModel.toString());
 }
Example #9
0
 public StarTable[] getTables() {
   return new StarTable[] {tcModel_.getApparentStarTable()};
 }