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