001/*****************************************************************************
002 * Copyright by The HDF Group.                                               *
003 * Copyright by the Board of Trustees of the University of Illinois.         *
004 * All rights reserved.                                                      *
005 *                                                                           *
006 * This file is part of the HDF Java Products distribution.                  *
007 * The full copyright notice, including terms governing use, modification,   *
008 * and redistribution, is contained in the files COPYING and Copyright.html. *
009 * COPYING can be found at the root of the source code distribution tree.    *
010 * Or, see https://support.hdfgroup.org/products/licenses.html               *
011 * If you do not have access to either file, you may request a copy from     *
012 * help@hdfgroup.org.                                                        *
013 ****************************************************************************/
014
015package hdf.view.TableView;
016
017import java.lang.reflect.Constructor;
018import java.util.BitSet;
019import java.util.HashMap;
020
021import hdf.object.CompoundDS;
022import hdf.object.DataFormat;
023import hdf.object.FileFormat;
024import hdf.object.HObject;
025import hdf.object.ScalarDS;
026import hdf.view.Tools;
027import hdf.view.ViewProperties;
028import hdf.view.DataView.DataViewManager;
029
030/**
031 * A simple Factory class which returns concrete instances of the default
032 * TableView, based on whether the data object to be viewed is a scalar or
033 * compound dataset or is an attribute.
034 *
035 * @author jhenderson
036 * @version 1.0 4/18/2018
037 */
038public class DefaultTableViewFactory extends TableViewFactory
039{
040    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DefaultTableViewFactory.class);
041
042    /**
043     * Get the TableView for the data object identified by the data properties mapping
044     *
045     * @param viewer
046     *        the data view manager
047     * @param dataPropertiesMap
048     *        the data properties map
049     *
050     * @return the TableView instance
051     *
052     * @throws ClassNotFoundException if a failure occurred
053     */
054    @SuppressWarnings({ "rawtypes", "unchecked" })
055    @Override
056    public TableView getTableView(DataViewManager viewer, HashMap dataPropertiesMap) throws ClassNotFoundException {
057        String dataViewName = null;
058        Object[] initargs = { viewer, dataPropertiesMap };
059        TableView theView = null;
060        HObject dataObject = null;
061
062        /* Retrieve the data object to be displayed */
063        if (dataPropertiesMap != null)
064            dataObject = (HObject) dataPropertiesMap.get(ViewProperties.DATA_VIEW_KEY.OBJECT);
065
066        if (dataObject == null)
067            dataObject = viewer.getTreeView().getCurrentObject();
068
069        if (dataObject == null) {
070            log.debug("getTableView(): data object is null");
071            return null;
072        }
073
074        /*
075         * If the name of a specific TableView class to use has been passed in via the
076         * data options map, retrieve its name now, otherwise use the default TableView
077         * class.
078         */
079        dataViewName = (String) dataPropertiesMap.get(ViewProperties.DATA_VIEW_KEY.VIEW_NAME);
080        if (dataViewName == null || dataViewName.equals(ViewProperties.DEFAULT_MODULE_TEXT)) {
081            if (dataObject instanceof ScalarDS)
082                dataViewName = ViewProperties.DEFAULT_SCALAR_DATASET_TABLEVIEW_NAME;
083            else if (dataObject instanceof CompoundDS)
084                dataViewName = ViewProperties.DEFAULT_COMPOUND_DATASET_TABLEVIEW_NAME;
085            else
086                dataViewName = null;
087        }
088
089        Class<?> theClass = null;
090        try {
091            log.trace("getTableView(): Class.forName({})", dataViewName);
092
093            /* Attempt to load the class by the given name */
094            theClass = Class.forName(dataViewName);
095        }
096        catch (Exception ex) {
097            log.debug("getTableView(): unable to load default TableView class by name({})", dataViewName);
098            theClass = null;
099        }
100
101        if (theClass == null) throw new ClassNotFoundException();
102
103        /* Check to see if there is a bitmask to be applied to the data */
104        BitSet bitmask = (BitSet) dataPropertiesMap.get(ViewProperties.DATA_VIEW_KEY.BITMASK);
105        if (bitmask != null) {
106            /*
107             * Create a copy of the data object in order to apply the bitmask
108             * non-destructively
109             */
110            HObject dCopy = null;
111            Constructor<? extends HObject> constructor = null;
112            Object[] paramObj = null;
113
114            try {
115                Class<?>[] paramClass = { FileFormat.class, String.class, String.class, long[].class };
116                constructor = dataObject.getClass().getConstructor(paramClass);
117
118                paramObj = new Object[] { dataObject.getFileFormat(), dataObject.getName(), dataObject.getPath(),
119                        dataObject.getOID() };
120            }
121            catch (Exception ex) {
122                constructor = null;
123            }
124
125            if (constructor != null) {
126                try {
127                    dCopy = constructor.newInstance(paramObj);
128                }
129                catch (Exception ex) {
130                    dCopy = null;
131                }
132            }
133
134            if (dCopy != null) {
135                try {
136                    ((DataFormat) dCopy).init();
137
138                    int space_type = ((DataFormat) dataObject).getSpaceType();
139                    int rank = ((DataFormat) dataObject).getRank();
140                    System.arraycopy(((DataFormat) dataObject).getDims(), 0, ((DataFormat) dCopy).getDims(), 0, rank);
141                    System.arraycopy(((DataFormat) dataObject).getStartDims(), 0, ((DataFormat) dCopy).getStartDims(),0, rank);
142                    System.arraycopy(((DataFormat) dataObject).getSelectedDims(), 0, ((DataFormat) dCopy).getSelectedDims(), 0, rank);
143                    System.arraycopy(((DataFormat) dataObject).getStride(), 0, ((DataFormat) dCopy).getStride(), 0, rank);
144                    System.arraycopy(((DataFormat) dataObject).getSelectedIndex(), 0, ((DataFormat) dCopy).getSelectedIndex(), 0, 3);
145                }
146                catch (Exception ex) {
147                    ex.printStackTrace();
148                }
149
150                dataPropertiesMap.put(ViewProperties.DATA_VIEW_KEY.OBJECT, dCopy);
151            }
152        }
153
154        try {
155            theView = (TableView) Tools.newInstance(theClass, initargs);
156
157            log.trace("getTableView(): returning TableView instance {}", theView);
158        }
159        catch (Exception ex) {
160            log.debug("getTableView(): Error instantiating class:", ex);
161            theView = null;
162        }
163
164        return theView;
165    }
166
167}