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.MetaDataView;
016
017import org.slf4j.Logger;
018import org.slf4j.LoggerFactory;
019
020import org.eclipse.swt.widgets.Composite;
021
022import hdf.object.Dataset;
023import hdf.object.Datatype;
024import hdf.object.Group;
025import hdf.object.HObject;
026import hdf.object.h5.H5Link;
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 * MetaDataView, based on whether the data object is a Group, Dataset, Datatype
034 * or other form of object.
035 *
036 * @author jhenderson
037 * @version 1.0 4/18/2018
038 */
039public class DefaultMetaDataViewFactory extends MetaDataViewFactory {
040
041    private static final Logger log = LoggerFactory.getLogger(DefaultMetaDataViewFactory.class);
042
043    @Override
044    public MetaDataView getMetaDataView(Composite parentObj, DataViewManager viewer, HObject theObj) throws ClassNotFoundException {
045        String dataViewName = null;
046        Object[] initargs = { parentObj, viewer, theObj };
047        MetaDataView theView = null;
048
049        if (theObj instanceof Group)
050            dataViewName = ViewProperties.DEFAULT_GROUP_METADATAVIEW_NAME;
051        else if (theObj instanceof Dataset)
052            dataViewName = ViewProperties.DEFAULT_DATASET_METADATAVIEW_NAME;
053        else if (theObj instanceof Datatype)
054            dataViewName = ViewProperties.DEFAULT_DATATYPE_METADATAVIEW_NAME;
055        else if (theObj instanceof H5Link)
056            dataViewName = ViewProperties.DEFAULT_LINK_METADATAVIEW_NAME;
057        else
058            dataViewName = null;
059
060        Class<?> theClass = null;
061        try {
062            log.trace("getMetaDataView(): Class.forName({})", dataViewName);
063
064            /* Attempt to load the class by the given name */
065            theClass = Class.forName(dataViewName);
066        }
067        catch (Exception ex) {
068            log.debug("getMetaDataView(): unable to load default MetaDataView class by name({})", dataViewName);
069            theClass = null;
070        }
071
072        if (theClass == null) throw new ClassNotFoundException();
073
074        try {
075            theView = (MetaDataView) Tools.newInstance(theClass, initargs);
076
077            log.trace("getMetaDataView(): returning MetaDataView instance {}", theView);
078        }
079        catch (Exception ex) {
080            log.debug("getMetaDataView(): Error instantiating class:", ex);
081            theView = null;
082        }
083
084        return theView;
085    }
086
087}