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 java.util.List;
018
019import org.slf4j.Logger;
020import org.slf4j.LoggerFactory;
021
022import org.eclipse.swt.SWT;
023import org.eclipse.swt.custom.ScrolledComposite;
024import org.eclipse.swt.layout.GridData;
025import org.eclipse.swt.layout.GridLayout;
026import org.eclipse.swt.widgets.Composite;
027import org.eclipse.swt.widgets.Label;
028import org.eclipse.swt.widgets.Table;
029import org.eclipse.swt.widgets.TableColumn;
030import org.eclipse.swt.widgets.TableItem;
031import org.eclipse.swt.widgets.Text;
032
033import hdf.hdf5lib.H5;
034import hdf.hdf5lib.HDF5Constants;
035import hdf.object.Dataset;
036import hdf.object.Datatype;
037import hdf.object.Group;
038import hdf.object.HObject;
039import hdf.object.h5.H5Link;
040import hdf.object.nc2.NC2Group;
041import hdf.view.ViewProperties;
042import hdf.view.DataView.DataViewManager;
043
044/**
045 *
046 * The metadata view interface for displaying group metadata information
047 */
048public class DefaultGroupMetaDataView extends DefaultLinkMetaDataView implements MetaDataView {
049
050    private static final Logger log = LoggerFactory.getLogger(DefaultGroupMetaDataView.class);
051
052    /**
053     *The metadata view interface for displaying metadata information
054     *
055     * @param parentComposite
056     *        the parent visual object
057     * @param viewer
058     *        the viewer to use
059     * @param theObj
060     *        the object to display the metadata info
061     */
062    public DefaultGroupMetaDataView(Composite parentComposite, DataViewManager viewer, HObject theObj) {
063        super(parentComposite, viewer, theObj);
064    }
065
066    @Override
067    protected void addObjectSpecificContent() {
068        super.addObjectSpecificContent();
069
070        Group g = (Group) dataObject;
071        List<?> mlist = g.getMemberList();
072        int n = mlist.size();
073
074        log.trace("addObjectSpecificContent(): group object extra info mlist size = {}", n);
075
076        Label label;
077
078        if (isH5) {
079            StringBuilder objCreationStr = new StringBuilder("Creation Order NOT Tracked");
080
081            long ocplID = -1;
082            try {
083                if (g.isRoot()) {
084                    ocplID = H5.H5Fget_create_plist(g.getFID());
085                }
086                else {
087                    long objid = -1;
088                    try {
089                        objid = g.open();
090                        if (objid >= 0) {
091                            ocplID = H5.H5Gget_create_plist(objid);
092                        }
093                    }
094                    finally {
095                        g.close(objid);
096                    }
097                }
098                if (ocplID >= 0) {
099                    int creationOrder = H5.H5Pget_link_creation_order(ocplID);
100                    log.trace("createGeneralObjectInfoPane(): creationOrder={}", creationOrder);
101                    if ((creationOrder & HDF5Constants.H5P_CRT_ORDER_TRACKED) > 0) {
102                        objCreationStr.setLength(0);
103                        objCreationStr.append("Creation Order Tracked");
104                        if ((creationOrder & HDF5Constants.H5P_CRT_ORDER_INDEXED) > 0)
105                            objCreationStr.append(" and Indexed");
106                    }
107                }
108            }
109            finally {
110                H5.H5Pclose(ocplID);
111            }
112
113            /* Creation order section */
114            label = new Label(generalObjectInfoPane, SWT.LEFT);
115            label.setFont(curFont);
116            label.setText("Link Creation Order: ");
117
118            Text text = new Text(generalObjectInfoPane, SWT.SINGLE | SWT.BORDER);
119            text.setEditable(false);
120            text.setFont(curFont);
121            text.setText(objCreationStr.toString());
122            text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
123        }
124        if (isN3) {
125            StringBuilder objDimensionStr = new StringBuilder("No Dimensions");
126            int[] listDimSelector = { 0, 0, 1};
127            try {
128                List ncDimensions = ((NC2Group)g).getMetadata(listDimSelector);
129                if (ncDimensions != null) {
130                    int listCnt = ncDimensions.size();
131                    log.trace("createGeneralObjectInfoPane(): ncDimensions={}", listCnt);
132                    if (listCnt > 0)
133                        objDimensionStr.setLength(0);
134                    for (int i = 0; i < listCnt; i++) {
135                        objDimensionStr.append(((NC2Group)g).netcdfDimensionString(i));
136                        if (i < listCnt - 1)
137                            objDimensionStr.append("\n");
138                    }
139                }
140            }
141            catch (Exception e) {
142                log.debug("Error retrieving dimensions of object '" + dataObject.getName() + "':", e);
143            }
144
145            /* Dimensions section */
146            label = new Label(generalObjectInfoPane, SWT.LEFT);
147            label.setFont(curFont);
148            label.setText("Dimensions: ");
149
150            ScrolledComposite dimensionScroller = new ScrolledComposite(generalObjectInfoPane, SWT.V_SCROLL | SWT.BORDER);
151            dimensionScroller.setExpandHorizontal(true);
152            dimensionScroller.setExpandVertical(true);
153            dimensionScroller.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 5, 1));
154
155            Text text = new Text(dimensionScroller, SWT.MULTI | SWT.BORDER);
156            text.setEditable(false);
157            text.setFont(curFont);
158            text.setText(objDimensionStr.toString());
159            text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
160            dimensionScroller.setContent(text);
161
162            StringBuilder objEnumTypedefStr = new StringBuilder("No Enums");
163            int[] listEnumSelector = { 0, 0, 0, 1};
164            try {
165                List ncEnums = ((NC2Group)g).getMetadata(listEnumSelector);
166                if (ncEnums != null) {
167                    int listCnt = ncEnums.size();
168                    log.trace("createGeneralObjectInfoPane(): ncEnums={}", listCnt);
169                    if (listCnt > 0)
170                        objEnumTypedefStr.setLength(0);
171                    for (int i = 0; i < listCnt; i++) {
172                        objEnumTypedefStr.append(((NC2Group)g).netcdfTypedefString(i));
173                        if (i < listCnt - 1)
174                            objEnumTypedefStr.append("\n");
175                    }
176                }
177            }
178            catch (Exception e) {
179                log.debug("Error retrieving enums of object '" + dataObject.getName() + "':", e);
180            }
181
182            /* Dimensions section */
183            label = new Label(generalObjectInfoPane, SWT.LEFT);
184            label.setFont(curFont);
185            label.setText("Enums: ");
186
187            ScrolledComposite enumScroller = new ScrolledComposite(generalObjectInfoPane, SWT.V_SCROLL | SWT.BORDER);
188            enumScroller.setExpandHorizontal(true);
189            enumScroller.setExpandVertical(true);
190            enumScroller.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 5, 1));
191
192            text = new Text(enumScroller, SWT.MULTI | SWT.BORDER);
193            text.setEditable(false);
194            text.setFont(curFont);
195            text.setText(objEnumTypedefStr.toString());
196            text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
197            enumScroller.setContent(text);
198        }
199        org.eclipse.swt.widgets.Group groupInfoGroup = new org.eclipse.swt.widgets.Group(generalObjectInfoPane, SWT.NONE);
200        groupInfoGroup.setFont(curFont);
201        groupInfoGroup.setText("Group Members");
202        groupInfoGroup.setLayout(new GridLayout(1, true));
203        groupInfoGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
204
205        if (g.getNumberOfMembersInFile() < ViewProperties.getMaxMembers()) {
206            label = new Label(groupInfoGroup, SWT.RIGHT);
207            label.setFont(curFont);
208            label.setText("Number of members: " + n);
209        }
210        else {
211            label = new Label(groupInfoGroup, SWT.RIGHT);
212            label.setFont(curFont);
213            label.setText("Number of members: " + n + " (in memory)," + "" + g.getNumberOfMembersInFile() + " (in file)");
214        }
215
216        String[] columnNames = { "Name", "Type" };
217
218        Table memberTable = new Table(groupInfoGroup, SWT.BORDER);
219        memberTable.setLinesVisible(true);
220        memberTable.setHeaderVisible(true);
221        memberTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
222        memberTable.setFont(curFont);
223
224        for (int i = 0; i < columnNames.length; i++) {
225            TableColumn column = new TableColumn(memberTable, SWT.NONE);
226            column.setText(columnNames[i]);
227            column.setMoveable(false);
228        }
229
230        if (mlist != null && n > 0) {
231            String rowData[][] = new String[n][2];
232            for (int i = 0; i < n; i++) {
233                HObject theObj = (HObject) mlist.get(i);
234                rowData[i][0] = theObj.getName();
235                if (theObj instanceof Group) {
236                    rowData[i][1] = "Group";
237                }
238                else if (theObj instanceof Dataset) {
239                    rowData[i][1] = "Dataset";
240                }
241                else if (theObj instanceof Datatype) {
242                    rowData[i][1] = "Datatype";
243                }
244                else if (theObj instanceof H5Link) {
245                    rowData[i][1] = "Link";
246                }
247                else
248                    rowData[i][1] = "Unknown";
249            }
250
251            for (int i = 0; i < rowData.length; i++) {
252                TableItem item = new TableItem(memberTable, SWT.NONE);
253                item.setFont(curFont);
254                item.setText(0, rowData[i][0]);
255                item.setText(1, rowData[i][1]);
256            }
257
258            // set cell height for large fonts
259            // int cellRowHeight = Math.max(16,
260            // table.getFontMetrics(table.getFont()).getHeight());
261            // table.setRowHeight(cellRowHeight);
262        }
263
264        for (int i = 0; i < columnNames.length; i++) {
265            memberTable.getColumn(i).pack();
266        }
267    }
268
269}