View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  
16  package org.fourthline.cling.support.shared;
17  
18  import org.fourthline.cling.model.ModelUtil;
19  import org.seamless.swing.Application;
20  import org.seamless.xml.DOM;
21  import org.seamless.xml.DOMParser;
22  import org.w3c.dom.Document;
23  
24  import javax.swing.JDialog;
25  import javax.swing.JScrollPane;
26  import javax.swing.JTextArea;
27  import java.awt.Dimension;
28  import java.awt.Frame;
29  import java.util.logging.Logger;
30  
31  /**
32   * @author Christian Bauer
33   */
34  public class TextExpandDialog extends JDialog {
35  
36      // TODO: Make this a plugin SPI and let the plugin impl decide how text should be detected and rendered
37  
38      private static Logger log = Logger.getLogger(TextExpandDialog.class.getName());
39  
40      public TextExpandDialog(Frame frame, String text) {
41          super(frame);
42          setResizable(true);
43  
44          JTextArea textArea = new JTextArea();
45          JScrollPane textPane = new JScrollPane(textArea);
46          textPane.setPreferredSize(new Dimension(500, 400));
47          add(textPane);
48  
49          String pretty;
50          if (text.startsWith("<") && text.endsWith(">")) {
51              try {
52                  pretty = new DOMParser() {
53                      @Override
54                      protected DOM createDOM(Document document) {
55                          return null;
56                      }
57                  }.print(text, 2, false);
58              } catch (Exception ex) {
59                  log.severe("Error pretty printing XML: " + ex.toString());
60                  pretty = text;
61              }
62          } else if (text.startsWith("http-get")) {
63              pretty = ModelUtil.commaToNewline(text);
64          } else {
65              pretty = text;
66          }
67  
68          textArea.setEditable(false);
69          textArea.setText(pretty);
70  
71          pack();
72          Application.center(this, getOwner());
73          setVisible(true);
74      }
75  }