    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.ImageView;
    016
    017import java.util.BitSet;
    018import java.util.HashMap;
    019
    020import hdf.view.Tools;
    021import hdf.view.ViewProperties;
    022import hdf.view.DataView.DataViewManager;
    023
    024/**
    025 * A simple Factory class which returns concrete instances of the default
    026 * ImageView.
    027 *
    028 * @author jhenderson
    029 * @version 1.0 4/18/2018
    030 */
    031public class DefaultImageViewFactory extends ImageViewFactory {
    032
    033    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DefaultImageViewFactory.class);
    034
    035    @SuppressWarnings({ "rawtypes", "unchecked" })
    036    @Override
    037    public ImageView getImageView(DataViewManager viewer, HashMap dataPropertiesMap) throws ClassNotFoundException {
    038        String dataViewName = null;
    039        Object[] initargs = { viewer, dataPropertiesMap };
    040        ImageView theView = null;
    041
    042        /*
    043         * If the name of a specific ImageView class to use has been passed in via the
    044         * data options map, retrieve its name now, otherwise use the default ImageView
    045         * class.
    046         */
    047        dataViewName = (String) dataPropertiesMap.get(ViewProperties.DATA_VIEW_KEY.VIEW_NAME);
    048        if (dataViewName == null || dataViewName.equals(ViewProperties.DEFAULT_MODULE_TEXT)) {
    049            dataViewName = ViewProperties.DEFAULT_IMAGEVIEW_NAME;
    050        }
    051
    052        Class<?> theClass = null;
    053        try {
    054            log.trace("getImageView(): Class.forName({})", dataViewName);
    055
    056            /* Attempt to load the class by the given name */
    057            theClass = Class.forName(dataViewName);
    058        }
    059        catch (Exception ex) {
    060            log.debug("getImageView(): unable to load default ImageView class by name({})", dataViewName);
    061            theClass = null;
    062        }
    063
    064        if (theClass == null) throw new ClassNotFoundException();
    065
    066        /* Add some data display properties if using the default ImageView */
    067        if (dataViewName.startsWith(ViewProperties.DEFAULT_IMAGEVIEW_NAME)) {
    068            BitSet bitmask = (BitSet) dataPropertiesMap.get(ViewProperties.DATA_VIEW_KEY.BITMASK);
    069            dataPropertiesMap.put(ViewProperties.DATA_VIEW_KEY.CONVERTBYTE, Boolean.valueOf((bitmask != null)));
    070        }
    071
    072        try {
    073            theView = (ImageView) Tools.newInstance(theClass, initargs);
    074
    075            log.trace("getImageView(): returning ImageView instance {}", theView);
    076        }
    077        catch (Exception ex) {
    078            log.debug("getImageView(): Error instantiating class:", ex);
    079            theView = null;
    080        }
    081
    082        return theView;
    083    }
    084
    085}