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 COPYING file, which can be found  *
009 * at the root of the source code distribution tree,                         *
010 * or in https://www.hdfgroup.org/licenses.                                  *
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.DataView.DataViewManager;
027import hdf.view.Tools;
028import hdf.view.ViewProperties;
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    private static final org.slf4j.Logger log =
040        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)
057        throws ClassNotFoundException
058    {
059        String dataViewName = null;
060        Object[] initargs   = {viewer, dataPropertiesMap};
061        TableView theView   = null;
062        HObject dataObject  = null;
063
064        /* Retrieve the data object to be displayed */
065        if (dataPropertiesMap != null)
066            dataObject = (HObject)dataPropertiesMap.get(ViewProperties.DATA_VIEW_KEY.OBJECT);
067
068        if (dataObject == null)
069            dataObject = viewer.getTreeView().getCurrentObject();
070
071        if (dataObject == null) {
072            log.debug("getTableView(): data object is null");
073            return null;
074        }
075
076        /*
077         * If the name of a specific TableView class to use has been passed in via the
078         * data options map, retrieve its name now, otherwise use the default TableView
079         * class.
080         */
081        dataViewName = (String)dataPropertiesMap.get(ViewProperties.DATA_VIEW_KEY.VIEW_NAME);
082        if (dataViewName == null || dataViewName.equals(ViewProperties.DEFAULT_MODULE_TEXT)) {
083            if (dataObject instanceof ScalarDS)
084                dataViewName = ViewProperties.DEFAULT_SCALAR_DATASET_TABLEVIEW_NAME;
085            else if (dataObject instanceof CompoundDS)
086                dataViewName = ViewProperties.DEFAULT_COMPOUND_DATASET_TABLEVIEW_NAME;
087            else
088                dataViewName = null;
089        }
090
091        Class<?> theClass = null;
092        try {
093            log.trace("getTableView(): Class.forName({})", dataViewName);
094
095            /* Attempt to load the class by the given name */
096            theClass = Class.forName(dataViewName);
097        }
098        catch (Exception ex) {
099            log.debug("getTableView(): unable to load default TableView class by name({})", dataViewName);
100            theClass = null;
101        }
102
103        if (theClass == null)
104            throw new ClassNotFoundException();
105
106        /* Check to see if there is a bitmask to be applied to the data */
107        BitSet bitmask = (BitSet)dataPropertiesMap.get(ViewProperties.DATA_VIEW_KEY.BITMASK);
108        if (bitmask != null) {
109            /*
110             * Create a copy of the data object in order to apply the bitmask
111             * non-destructively
112             */
113            HObject dCopy                              = null;
114            Constructor<? extends HObject> constructor = null;
115            Object[] paramObj                          = null;
116
117            try {
118                Class<?>[] paramClass = {FileFormat.class, String.class, String.class, long[].class};
119                constructor           = dataObject.getClass().getConstructor(paramClass);
120
121                paramObj = new Object[] {dataObject.getFileFormat(), dataObject.getName(),
122                                         dataObject.getPath(), dataObject.getOID()};
123            }
124            catch (Exception ex) {
125                constructor = null;
126            }
127
128            if (constructor != null) {
129                try {
130                    dCopy = constructor.newInstance(paramObj);
131                }
132                catch (Exception ex) {
133                    dCopy = null;
134                }
135            }
136
137            if (dCopy != null) {
138                try {
139                    ((DataFormat)dCopy).init();
140
141                    int space_type = ((DataFormat)dataObject).getSpaceType();
142                    int rank       = ((DataFormat)dataObject).getRank();
143                    System.arraycopy(((DataFormat)dataObject).getDims(), 0, ((DataFormat)dCopy).getDims(), 0,
144                                     rank);
145                    System.arraycopy(((DataFormat)dataObject).getStartDims(), 0,
146                                     ((DataFormat)dCopy).getStartDims(), 0, rank);
147                    System.arraycopy(((DataFormat)dataObject).getSelectedDims(), 0,
148                                     ((DataFormat)dCopy).getSelectedDims(), 0, rank);
149                    System.arraycopy(((DataFormat)dataObject).getStride(), 0, ((DataFormat)dCopy).getStride(),
150                                     0, rank);
151                    System.arraycopy(((DataFormat)dataObject).getSelectedIndex(), 0,
152                                     ((DataFormat)dCopy).getSelectedIndex(), 0, 3);
153                }
154                catch (Exception ex) {
155                    ex.printStackTrace();
156                }
157
158                dataPropertiesMap.put(ViewProperties.DATA_VIEW_KEY.OBJECT, dCopy);
159            }
160        }
161
162        try {
163            theView = (TableView)Tools.newInstance(theClass, initargs);
164
165            log.trace("getTableView(): returning TableView instance {}", theView);
166        }
167        catch (Exception ex) {
168            log.debug("getTableView(): Error instantiating class:", ex);
169            theView = null;
170        }
171
172        return theView;
173    }
174}