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 hdf.object.Dataset;
018import hdf.object.Datatype;
019import hdf.object.Group;
020import hdf.object.HObject;
021import hdf.object.h5.H5Link;
022import hdf.view.DataView.DataViewManager;
023import hdf.view.Tools;
024import hdf.view.ViewProperties;
025
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029import org.eclipse.swt.widgets.Composite;
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)
045        throws ClassNotFoundException
046    {
047        String dataViewName  = null;
048        Object[] initargs    = {parentObj, viewer, theObj};
049        MetaDataView theView = null;
050
051        if (theObj instanceof Group)
052            dataViewName = ViewProperties.DEFAULT_GROUP_METADATAVIEW_NAME;
053        else if (theObj instanceof Dataset)
054            dataViewName = ViewProperties.DEFAULT_DATASET_METADATAVIEW_NAME;
055        else if (theObj instanceof Datatype)
056            dataViewName = ViewProperties.DEFAULT_DATATYPE_METADATAVIEW_NAME;
057        else if (theObj instanceof H5Link)
058            dataViewName = ViewProperties.DEFAULT_LINK_METADATAVIEW_NAME;
059        else
060            dataViewName = null;
061
062        Class<?> theClass = null;
063        try {
064            log.trace("getMetaDataView(): Class.forName({})", dataViewName);
065
066            /* Attempt to load the class by the given name */
067            theClass = Class.forName(dataViewName);
068        }
069        catch (Exception ex) {
070            log.debug("getMetaDataView(): unable to load default MetaDataView class by name({})",
071                      dataViewName);
072            theClass = null;
073        }
074
075        if (theClass == null)
076            throw new ClassNotFoundException();
077
078        try {
079            theView = (MetaDataView)Tools.newInstance(theClass, initargs);
080
081            log.trace("getMetaDataView(): returning MetaDataView instance {}", theView);
082        }
083        catch (Exception ex) {
084            log.debug("getMetaDataView(): Error instantiating class:", ex);
085            theView = null;
086        }
087
088        return theView;
089    }
090}