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