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 org.eclipse.swt.SWT;
018import org.eclipse.swt.events.TraverseEvent;
019import org.eclipse.swt.events.TraverseListener;
020import org.eclipse.swt.layout.GridData;
021import org.eclipse.swt.layout.GridLayout;
022import org.eclipse.swt.widgets.Composite;
023import org.eclipse.swt.widgets.Event;
024import org.eclipse.swt.widgets.Label;
025import org.eclipse.swt.widgets.Listener;
026import org.eclipse.swt.widgets.Text;
027
028import hdf.object.FileFormat;
029import hdf.object.Group;
030import hdf.object.HObject;
031import hdf.object.h5.H5Link;
032import hdf.view.Tools;
033import hdf.view.DataView.DataViewManager;
034
035public class DefaultLinkMetaDataView extends DefaultBaseMetaDataView implements MetaDataView {
036
037    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DefaultLinkMetaDataView.class);
038
039    public DefaultLinkMetaDataView(Composite parentComposite, DataViewManager viewer, HObject theObj) {
040        super(parentComposite, viewer, theObj);
041    }
042
043    @Override
044    protected void addObjectSpecificContent() {
045        /* For HDF5 links, add a box to allow changing of the link target */
046        if (dataObject.getLinkTargetObjName() != null) {
047            org.eclipse.swt.widgets.Group linkTargetGroup = new org.eclipse.swt.widgets.Group(generalObjectInfoPane, SWT.NONE);
048            linkTargetGroup.setFont(curFont);
049            linkTargetGroup.setText("Link Target Info");
050            linkTargetGroup.setLayout(new GridLayout(2, false));
051
052            /*
053             * If this object is purely a link, meaning a soft/external/other link that does
054             * not point to an existing object, allow the Link Target Info Group to take up
055             * all the available vertical space in the MetaDataView.
056             *
057             * Otherwise, if this link points to an existing object, it will not be an
058             * instanceof H5Link. Rather, it will be an instanceof Dataset, instanceof
059             * Group, etc. and will contain the information necessary to determine that the
060             * object is really a link.
061             *
062             * In this case, we don't allow the Link Target Info Group to take up all the
063             * available vertical space, in order to allow other object Metadata information
064             * to be comfortable displayed in the MetaDataView.
065             */
066            if (dataObject instanceof H5Link)
067                linkTargetGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
068            else
069                linkTargetGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
070
071            Label label = new Label(linkTargetGroup, SWT.LEFT);
072            label.setFont(curFont);
073            label.setText("Link To Target: ");
074
075            final Text linkTarget = new Text(linkTargetGroup, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL);
076            linkTarget.setFont(curFont);
077            linkTarget.setText(dataObject.getLinkTargetObjName());
078            linkTarget.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
079            linkTarget.addTraverseListener(new TraverseListener() {
080                @Override
081                public void keyTraversed(TraverseEvent e) {
082                    if (e.detail == SWT.TRAVERSE_RETURN) {
083                        changeLinkTarget(linkTarget.getText());
084                    }
085                }
086            });
087            linkTarget.addListener(SWT.FocusOut, new Listener() {
088                @Override
089                public void handleEvent(Event arg0) {
090                    changeLinkTarget(linkTarget.getText());
091                }
092            });
093        }
094    }
095
096    private void changeLinkTarget(String linkTargetName) {
097        Group pgroup = null;
098        try {
099            pgroup = (Group) dataObject.getFileFormat().get(dataObject.getPath());
100        }
101        catch (Exception ex) {
102            log.debug("addObjectSpecificContent(): parent group get failure:", ex);
103        }
104
105        if (pgroup == null) {
106            log.debug("addObjectSpecificContent(): parent group is null");
107            display.beep();
108            Tools.showError(display.getShells()[0], "Select", "Parent group is null.\nLink target change failed.");
109            return;
110        }
111
112        if (linkTargetName != null)
113            linkTargetName = linkTargetName.trim();
114
115        int linkType = Group.LINK_TYPE_SOFT;
116        if (dataObject.getLinkTargetObjName().contains(FileFormat.FILE_OBJ_SEP))
117            linkType = Group.LINK_TYPE_EXTERNAL;
118        else if ((linkTargetName != null) && (linkTargetName.equals("/"))) { // do not allow to link to the root
119            display.beep();
120            Tools.showError(display.getShells()[0], "Select", "Link to root not allowed.\nLink target change failed.");
121            return;
122        }
123
124        // no change
125        if ((linkTargetName != null) && (linkTargetName.equals(dataObject.getLinkTargetObjName())))
126            return;
127
128        // invalid name
129        if (linkTargetName == null || linkTargetName.length() < 1) return;
130
131        try {
132            dataObject.getFileFormat().createLink(pgroup, dataObject.getName(), linkTargetName, linkType);
133            dataObject.setLinkTargetObjName(linkTargetName);
134        }
135        catch (Exception ex) {
136            log.debug("addObjectSpecificContent(): createLink() failure:", ex);
137            display.beep();
138            Tools.showError(display.getShells()[0], "Select", "Link target change failed." + ex.getMessage());
139            return;
140        }
141
142        Tools.showInformation(display.getShells()[0], "Link target changed.", "Reload file to display changes.");
143    }
144
145}