|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * ImageJ software for multidimensional image processing and analysis. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2017 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck |
| 7 | + * Institute of Molecular Cell Biology and Genetics. |
| 8 | + * %% |
| 9 | + * Redistribution and use in source and binary forms, with or without |
| 10 | + * modification, are permitted provided that the following conditions are met: |
| 11 | + * |
| 12 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer. |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | + * this list of conditions and the following disclaimer in the documentation |
| 16 | + * and/or other materials provided with the distribution. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 22 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | + * POSSIBILITY OF SUCH DAMAGE. |
| 29 | + * #L% |
| 30 | + */ |
| 31 | + |
| 32 | +package org.scijava.table; |
| 33 | + |
| 34 | +import org.scijava.display.AbstractDisplay; |
| 35 | +import org.scijava.display.Display; |
| 36 | +import org.scijava.plugin.Plugin; |
| 37 | + |
| 38 | +/** |
| 39 | + * Default display for {@link Table}s, including {@link DoubleTable}s. |
| 40 | + * |
| 41 | + * @author Curtis Rueden |
| 42 | + */ |
| 43 | +@Plugin(type = Display.class) |
| 44 | +public class DefaultTableDisplay extends AbstractDisplay<Table<?, ?>> implements |
| 45 | + TableDisplay |
| 46 | +{ |
| 47 | + |
| 48 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 49 | + public DefaultTableDisplay() { |
| 50 | + super((Class) Table.class); |
| 51 | + } |
| 52 | + |
| 53 | + // -- Display methods -- |
| 54 | + |
| 55 | + @Override |
| 56 | + public boolean canDisplay(final Class<?> c) { |
| 57 | + if (c == double[].class || c == double[][].class) return true; |
| 58 | + return super.canDisplay(c); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void display(final Object o) { |
| 63 | + // wrap 1D array as results table |
| 64 | + if (o instanceof double[]) { |
| 65 | + display(wrapArrayAsTable(new double[][] { (double[]) o })); |
| 66 | + return; |
| 67 | + } |
| 68 | + // wrap 2D array as results table |
| 69 | + if (o instanceof double[][]) { |
| 70 | + display(wrapArrayAsTable((double[][]) o)); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + super.display(o); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public boolean isDisplaying(final Object o) { |
| 79 | + if (super.isDisplaying(o)) return true; |
| 80 | + |
| 81 | + // check for wrapped arrays |
| 82 | + if (o instanceof double[]) { |
| 83 | + arrayEqualsTable(new double[][] {(double[]) o}); |
| 84 | + } |
| 85 | + if (o instanceof double[][]) { |
| 86 | + arrayEqualsTable((double[][]) o); |
| 87 | + } |
| 88 | + |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + // -- Helper methods -- |
| 93 | + |
| 94 | + private GenericTable wrapArrayAsTable(final double[][] array) { |
| 95 | + final GenericTable table = new DefaultGenericTable(); |
| 96 | + int rowCount = 0; |
| 97 | + for (int d = 0; d < array.length; d++) { |
| 98 | + final DoubleColumn column = new DoubleColumn(); |
| 99 | + column.setArray(array[d]); |
| 100 | + table.add(column); |
| 101 | + if (rowCount < array[d].length) rowCount = array[d].length; |
| 102 | + } |
| 103 | + table.setRowCount(rowCount); |
| 104 | + return table; |
| 105 | + } |
| 106 | + |
| 107 | + private boolean arrayEqualsTable(final double[][] array) { |
| 108 | + for (final Table<?, ?> table : this) { |
| 109 | + if (!(table instanceof DoubleTable)) continue; |
| 110 | + final DoubleTable resultsTable = (DoubleTable) table; |
| 111 | + if (array.length != resultsTable.getColumnCount()) continue; |
| 112 | + boolean equal = true; |
| 113 | + for (int c = 0; c < array.length; c++) { |
| 114 | + if (array[c] != resultsTable.get(c).getArray()) { |
| 115 | + equal = false; |
| 116 | + break; |
| 117 | + } |
| 118 | + } |
| 119 | + return equal; |
| 120 | + } |
| 121 | + return false; |
| 122 | + } |
| 123 | + |
| 124 | +} |
0 commit comments