at.spardat.xma.mdl.util.DNodeInspector Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
// @(#) $Id: DNodeInspector.java 2089 2007-11-28 13:56:13Z s3460 $
package at.spardat.xma.mdl.util;
import java.util.Iterator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.widgets.Widget;
/**
* Opens a SWT window to show a hierarchy of DNodes.
*
* @author YSD, 03.05.2003 14:18:25
*/
public class DNodeInspector {
/**
* Constructor. Does not show. Use show to show.
*
* @param rootNode
*/
public DNodeInspector (DNode rootNode) {
root_ = rootNode;
}
/**
* Opens a non modal shell to show the hierachy.
*/
public void show () {
boolean myDisplay = false;
Display display = Display.getCurrent();
if (display == null) {
display = new Display();
myDisplay = true;
}
Shell shell = new Shell (display, SWT.MODELESS | SWT.SHELL_TRIM);
FormLayout layout= new FormLayout ();
layout.marginHeight = 5;
layout.marginWidth = 5;
shell.setLayout (layout);
Tree t = new Tree(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
FormData formData = new FormData(800, 400);
formData.top = new FormAttachment (0, 0);
formData.left = new FormAttachment (0, 0);
formData.right = new FormAttachment (100, 0);
formData.bottom = new FormAttachment (100, 0);
t.setLayoutData(formData);
recursivelyAddNodes (t, root_);
shell.addShellListener(new ShellAdapter() {
public void shellClosed(ShellEvent e) {
if (e.widget instanceof Shell) {
Shell s = (Shell)e.widget;
if (!s.isDisposed()) s.dispose();
}
}
});
// show
shell.pack();
shell.open();
if (myDisplay) {
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
}
// builds the tree
private void recursivelyAddNodes (Widget parent, DNode dNode) {
TreeItem ti;
if (parent instanceof Tree) {
ti = new TreeItem ((Tree)parent, SWT.NULL);
} else {
ti = new TreeItem ((TreeItem)parent, SWT.NULL);
}
ti.setText(dNode.getText());
// add sons of this
Iterator iter = dNode.getSons();
while (iter != null && iter.hasNext()) {
DNode son = (DNode) iter.next();
recursivelyAddNodes (ti, son);
}
ti.setExpanded(true);
}
// the root node
private DNode root_;
}