NXvalidate  1
 All Classes Namespaces Files Functions Variables
TextPaneStyle.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 Stephen Rankin
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  * TextPaneStyle.java
24  *
25  */
26 package org.nexusformat.nxvalidate;
27 
28 import java.awt.Color;
29 import java.util.logging.Level;
30 import javax.swing.JTextPane;
31 import javax.swing.text.BadLocationException;
32 import javax.swing.text.Style;
33 import javax.swing.text.StyleConstants;
34 import javax.swing.text.StyleContext;
35 import javax.swing.text.StyledDocument;
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
38 
43 public class TextPaneStyle {
44 
45  private JTextPane pane = null;
46 
47  public TextPaneStyle(JTextPane pane) {
48  this.pane = pane;
49  }
50 
51  private void printValidationErrors(NXNodeMapper node, StyledDocument doc,
52  int type) throws BadLocationException {
53 
54  String newline = "\n";
55 
56  doc.insertString(doc.getLength(), newline + newline,
57  doc.getStyle("heading"));
58 
59  if(type == 0){
60  doc.insertString(doc.getLength(), "Validation Tests:" +
61  newline + newline, doc.getStyle("heading"));
62  }
63  else{
64  doc.insertString(doc.getLength(), "Warning Tests:" +
65  newline + newline, doc.getStyle("heading"));
66  }
67 
68  if (node.getNodeTests() != null) {
69  for (int i = 0; i < node.getNodeTests().size(); ++i) {
70  doc.insertString(doc.getLength(),
71  node.getNodeTests().get(i), doc.getStyle("bold"));
72  doc.insertString(doc.getLength(),
73  newline + newline, doc.getStyle("heading"));
74  }
75  }
76 
77  if(type == 0){
78  doc.insertString(doc.getLength(), "Validation Errors:" +
79  newline + newline, doc.getStyle("errorheading"));
80  }
81  else{
82  doc.insertString(doc.getLength(), "Warning Errors:" +
83  newline + newline, doc.getStyle("warningheading"));
84  }
85 
86  if (node.getNodeTexts() != null) {
87  for (int i = 0; i < node.getNodeTexts().size(); ++i) {
88 
89  if(type == 0){
90  doc.insertString(doc.getLength(),
91  node.getNodeTexts().get(i), doc.getStyle("error"));
92  }
93  else{
94  doc.insertString(doc.getLength(),
95  node.getNodeTexts().get(i), doc.getStyle("warning"));
96  }
97 
98  doc.insertString(doc.getLength(),
99  newline + newline, doc.getStyle("heading"));
100  }
101  }
102 
103  if(type == 0){
104  doc.insertString(doc.getLength(), "Diagnostic Errors:" +
105  newline + newline, doc.getStyle("errorheading"));
106  }
107  else{
108  doc.insertString(doc.getLength(), "Diagnostic Errors:" +
109  newline + newline, doc.getStyle("warningheading"));
110  }
111 
112  if (node.getNodeDiags() != null) {
113  for (int i = 0; i < node.getNodeDiags().size(); ++i) {
114 
115  if(type == 0){
116  doc.insertString(doc.getLength(),
117  node.getNodeDiags().get(i), doc.getStyle("error"));
118  }
119  else{
120  doc.insertString(doc.getLength(),
121  node.getNodeDiags().get(i), doc.getStyle("warning"));
122  }
123 
124  doc.insertString(doc.getLength(),
125  newline + newline, doc.getStyle("heading"));
126  }
127  }
128 
129  }
130 
131  public void updateTextPane(NXNodeMapper node) {
132 
133  String newline = "\n";
134 
135  StyledDocument doc = pane.getStyledDocument();
136 
137  addStylesToDocument(doc);
138  String[] atts = node.getAttributeList();
139 
140  try {
141 
142  doc.remove(0, doc.getLength());
143 
144  doc.insertString(0, node.toString()
145  + newline + newline, doc.getStyle("title"));
146 
147  doc.insertString(doc.getLength(), "Attributes:"
148  + newline + newline, doc.getStyle("heading"));
149 
150  for (int i = 0; i
151  < atts.length; i++) {
152  doc.insertString(doc.getLength(),
153  "@ " + atts[i] + newline, doc.getStyle("bold"));
154  }
155 
156  doc.insertString(doc.getLength(), newline + newline,
157  doc.getStyle("heading"));
158 
159  doc.insertString(doc.getLength(), "Node Value:"
160  + newline + newline, doc.getStyle("heading"));
161 
162  doc.insertString(doc.getLength(),
163  node.getValue(), doc.getStyle("bold"));
164 
165  if(node.getBadNode()){
166  printValidationErrors(node, doc, 0);
167  }
168  else if(node.getWarnNode()){
169  printValidationErrors(node, doc, 1);
170  }
171 
172  } catch (BadLocationException ex) {
173  Logger.getLogger(TextPaneStyle.class.getName()).log(Level.SEVERE,
174  null, ex);
175  }
176 
177  }
178 
179  private void addStylesToDocument(StyledDocument doc) {
180 
181  //Initialize some styles.
182  Style def = StyleContext.getDefaultStyleContext().
183  getStyle(StyleContext.DEFAULT_STYLE);
184 
185  Style regular = doc.addStyle("regular", def);
186  StyleConstants.setFontFamily(def, "SansSerif");
187 
188  Style s = doc.addStyle("italic", regular);
189  StyleConstants.setItalic(s, true);
190 
191  s = doc.addStyle("bold", regular);
192  StyleConstants.setBold(s, true);
193 
194  s = doc.addStyle("small", regular);
195  StyleConstants.setFontSize(s, 10);
196 
197  s = doc.addStyle("large", regular);
198  StyleConstants.setFontSize(s, 16);
199 
200  s = doc.addStyle("heading", regular);
201  StyleConstants.setFontSize(s, 16);
202  StyleConstants.setBold(s, true);
203 
204  s = doc.addStyle("title", regular);
205  StyleConstants.setFontSize(s, 24);
206  StyleConstants.setBold(s, true);
207 
208  s = doc.addStyle("errorheading", regular);
209  StyleConstants.setFontSize(s, 16);
210  StyleConstants.setBold(s, true);
211  StyleConstants.setForeground(s, Color.red);
212 
213  s = doc.addStyle("error", regular);
214  StyleConstants.setBold(s, true);
215  StyleConstants.setForeground(s, Color.red);
216 
217  s = doc.addStyle("warningheading", regular);
218  StyleConstants.setFontSize(s, 16);
219  StyleConstants.setBold(s, true);
220  StyleConstants.setForeground(s, Color.BLUE);
221 
222  s = doc.addStyle("warning", regular);
223  StyleConstants.setBold(s, true);
224  StyleConstants.setForeground(s, Color.BLUE);
225 
226  }
227 }