NXvalidate  1
 All Classes Namespaces Files Functions Variables
NXvalidateGuiTree.java
Go to the documentation of this file.
1 /* NeXus - Neutron & X-ray Common Data Format
2  *
3  * NeXus file validation GUI tool.
4  *
5  * Copyright (C) 2010 Nexus Team
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * For further information, see <http://www.nexusformat.org/>
22  *
23  * NXvalidateGuiTree.java
24  *
25  */
26 package org.nexusformat.nxvalidate;
27 
28 import java.awt.Color;
29 import java.awt.Component;
30 
31 import javax.swing.Icon;
32 import javax.swing.ImageIcon;
33 import javax.swing.JLabel;
34 import javax.swing.JTree;
35 import javax.swing.tree.DefaultMutableTreeNode;
36 import javax.swing.tree.TreeCellRenderer;
37 
38 import org.xml.sax.Attributes;
39 
40 public class NXvalidateGuiTree {
41 
42  public static class ITag implements IconAndTipCarrier {
43 
44  private String name;
45  private String data;
46  private Attributes attr;
47  private Icon icon;
48  private String tipText;
49 
50  public ITag(String n, Attributes a) {
51  name = n;
52  attr = a;
53  for (int i = 0; i < attr.getLength(); i++) {
54  String aname = attr.getQName(i);
55  String value = attr.getValue(i);
56  if (aname.equals("icon")) {
57  tipText = value;
58  icon = new ImageIcon(value);
59  break;
60  }
61  }
62  }
63 
64  public String getName() {
65  return name;
66  }
67 
68  public Attributes getAttributes() {
69  return attr;
70  }
71 
72  public void setData(String d) {
73  data = d;
74  }
75 
76  public String getData() {
77  return data;
78  }
79 
80  public String getToolTipText() {
81  return tipText;
82  }
83 
84  public Icon getIcon() {
85  return icon;
86  }
87 
88  public void addData(String d) {
89  if (data == null) {
90  setData(d);
91  } else {
92  data += d;
93  }
94  }
95 
96  public String getAttributesAsString() {
97  StringBuffer buf = new StringBuffer(256);
98  for (int i = 0; i < attr.getLength(); i++) {
99  buf.append(attr.getQName(i));
100  buf.append("=\"");
101  buf.append(attr.getValue(i));
102  buf.append("\"");
103  }
104  return buf.toString();
105  }
106 
107  public String toString() {
108  String a = getAttributesAsString();
109  return name + ": " + a + (data == null ? "" : " (" + data + ")");
110  }
111  }
112 }
113 
114 interface IconAndTipCarrier {
115 
116  public Icon getIcon();
117 
118  public String getToolTipText();
119 }
120 
121 class IconAndTipRenderer extends JLabel implements TreeCellRenderer {
122 
126  private static final long serialVersionUID = -6749045036022861743L;
127  Color backColor = new Color(0xFF, 0xCC, 0xFF);
128  Icon openIcon, closedIcon, leafIcon;
129  String tipText = "";
130 
131  public IconAndTipRenderer(Icon open, Icon closed, Icon leaf) {
132  openIcon = open;
133  closedIcon = closed;
134  leafIcon = leaf;
135  setBackground(backColor);
136  setForeground(Color.black);
137  }
138 
139  public Component getTreeCellRendererComponent(JTree tree, Object value,
140  boolean selected, boolean expanded, boolean leaf, int row,
141  boolean hasFocus) {
142  setText(value.toString());
143  if (selected) {
144  setOpaque(true);
145  } else {
146  setOpaque(false);
147  }
148 
149  IconAndTipCarrier itc = null;
150  if (value instanceof DefaultMutableTreeNode) {
151  Object uo = ((DefaultMutableTreeNode) value).getUserObject();
152  if (uo instanceof IconAndTipCarrier) {
153  itc = (IconAndTipCarrier) uo;
154  }
155  } else if (value instanceof IconAndTipCarrier) {
156  itc = (IconAndTipCarrier) value;
157  }
158  if ((itc != null) && (itc.getIcon() != null)) {
159  setIcon(itc.getIcon());
160  tipText = itc.getToolTipText();
161  } else {
162  tipText = " ";
163  if (expanded) {
164  setIcon(openIcon);
165  } else if (leaf) {
166  setIcon(leafIcon);
167  } else {
168  setIcon(closedIcon);
169  }
170  }
171  return this;
172  }
173 
174  public String getToolTipText() {
175  return tipText;
176  }
177 }