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.Attribute;
022import hdf.object.CompoundDS;
023import hdf.object.DataFormat;
024import hdf.object.FileFormat;
025import hdf.object.HObject;
026import hdf.object.ScalarDS;
027import hdf.view.Tools;
028import hdf.view.ViewProperties;
029import hdf.view.DataView.DataViewManager;
030
031/**
032 * A simple Factory class which returns concrete instances of the default
033 * TableView, based on whether the data object to be viewed is a scalar or
034 * compound dataset or is an attribute.
035 *
036 * @author jhenderson
037 * @version 1.0 4/18/2018
038 */
039public class DefaultTableViewFactory extends TableViewFactory {
040
041    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DefaultTableViewFactory.class);
042
043    @SuppressWarnings({ "rawtypes", "unchecked" })
044    @Override
045    public TableView getTableView(DataViewManager viewer, HashMap dataPropertiesMap) throws ClassNotFoundException {
046        String dataViewName = null;
047        Object[] initargs = { viewer, dataPropertiesMap };
048        TableView theView = null;
049        HObject dataObject = null;
050
051        /* Retrieve the data object to be displayed */
052        if (dataPropertiesMap != null)
053            dataObject = (HObject) dataPropertiesMap.get(ViewProperties.DATA_VIEW_KEY.OBJECT);
054
055        if (dataObject == null)
056            dataObject = viewer.getTreeView().getCurrentObject();
057
058        if (dataObject == null) {
059            log.debug("getTableView(): data object is null");
060            return null;
061        }
062
063        /*
064         * If the name of a specific TableView class to use has been passed in via the
065         * data options map, retrieve its name now, otherwise use the default TableView
066         * class.
067         */
068        dataViewName = (String) dataPropertiesMap.get(ViewProperties.DATA_VIEW_KEY.VIEW_NAME);
069        if (dataViewName == null || dataViewName.equals(ViewProperties.DEFAULT_MODULE_TEXT)) {
070            if (dataObject instanceof ScalarDS)
071                dataViewName = ViewProperties.DEFAULT_SCALAR_DATASET_TABLEVIEW_NAME;
072            else if (dataObject instanceof CompoundDS)
073                dataViewName = ViewProperties.DEFAULT_COMPOUND_DATASET_TABLEVIEW_NAME;
074            else if (dataObject instanceof Attribute) {
075                if (((Attribute) dataObject).getDatatype().isCompound())
076                    dataViewName = ViewProperties.DEFAULT_COMPOUND_DATASET_TABLEVIEW_NAME;
077                else
078                    dataViewName = ViewProperties.DEFAULT_SCALAR_ATTRIBUTE_TABLEVIEW_NAME;
079            }
080            else
081                dataViewName = null;
082        }
083
084        Class<?> theClass = null;
085        try {
086            log.trace("getTableView(): Class.forName({})", dataViewName);
087
088            /* Attempt to load the class by the given name */
089            theClass = Class.forName(dataViewName);
090        }
091        catch (Exception ex) {
092            log.debug("getTableView(): unable to load default TableView class by name({})", dataViewName);
093            theClass = null;
094        }
095
096        if (theClass == null) throw new ClassNotFoundException();
097
098        /* Check to see if there is a bitmask to be applied to the data */
099        BitSet bitmask = (BitSet) dataPropertiesMap.get(ViewProperties.DATA_VIEW_KEY.BITMASK);
100        if (bitmask != null) {
101            /*
102             * Create a copy of the data object in order to apply the bitmask
103             * non-destructively
104             */
105            HObject dCopy = null;
106            Constructor<? extends HObject> constructor = null;
107            Object[] paramObj = null;
108
109            try {
110                Class<?>[] paramClass = { FileFormat.class, String.class, String.class, long[].class };
111                constructor = dataObject.getClass().getConstructor(paramClass);
112
113                paramObj = new Object[] { dataObject.getFileFormat(), dataObject.getName(), dataObject.getPath(),
114                        dataObject.getOID() };
115            }
116            catch (Exception ex) {
117                constructor = null;
118            }
119
120            if (constructor != null) {
121                try {
122                    dCopy = constructor.newInstance(paramObj);
123                }
124                catch (Exception ex) {
125                    dCopy = null;
126                }
127            }
128
129            if (dCopy != null) {
130                try {
131                    ((DataFormat) dCopy).init();
132
133                    int rank = ((DataFormat) dataObject).getRank();
134                    System.arraycopy(((DataFormat) dataObject).getDims(), 0, ((DataFormat) dCopy).getDims(), 0, rank);
135                    System.arraycopy(((DataFormat) dataObject).getStartDims(), 0, ((DataFormat) dCopy).getStartDims(),0, rank);
136                    System.arraycopy(((DataFormat) dataObject).getSelectedDims(), 0, ((DataFormat) dCopy).getSelectedDims(), 0, rank);
137                    System.arraycopy(((DataFormat) dataObject).getStride(), 0, ((DataFormat) dCopy).getStride(), 0, rank);
138                    System.arraycopy(((DataFormat) dataObject).getSelectedIndex(), 0, ((DataFormat) dCopy).getSelectedIndex(), 0, 3);
139                }
140                catch (Exception ex) {
141                    ex.printStackTrace();
142                }
143
144                dataPropertiesMap.put(ViewProperties.DATA_VIEW_KEY.OBJECT, dCopy);
145            }
146        }
147
148        try {
149            theView = (TableView) Tools.newInstance(theClass, initargs);
150
151            log.trace("getTableView(): returning TableView instance {}", theView);
152        }
153        catch (Exception ex) {
154            log.debug("getTableView(): Error instantiating class:", ex);
155            theView = null;
156        }
157
158        return theView;
159    }
160
161}