Coverage Report - us.paulevans.basicxslt.OutputFrame
 
Classes in this File Line Coverage Branch Coverage Complexity
OutputFrame
0%
0/82
0%
0/8
2.125
OutputFrame$1
0%
0/3
N/A
2.125
 
 1  
 /*
 2  
  Copyright 2006 Paul Evans 
 3  
 
 4  
  Licensed under the Apache License, Version 2.0 (the "License"); 
 5  
  you may not use this file except in compliance with the License. 
 6  
  You may obtain a copy of the License at 
 7  
 
 8  
  http://www.apache.org/licenses/LICENSE-2.0 
 9  
 
 10  
  Unless required by applicable law or agreed to in writing, software 
 11  
  distributed under the License is distributed on an "AS IS" BASIS, 
 12  
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 13  
  See the License for the specific language governing permissions and 
 14  
  limitations under the License.
 15  
  */
 16  
 package us.paulevans.basicxslt;
 17  
 
 18  
 import java.awt.BorderLayout;
 19  
 import java.awt.Cursor;
 20  
 import java.awt.FlowLayout;
 21  
 import java.awt.event.ActionEvent;
 22  
 import java.awt.event.ActionListener;
 23  
 import java.awt.event.WindowAdapter;
 24  
 import java.awt.event.WindowEvent;
 25  
 import java.io.File;
 26  
 
 27  
 import javax.swing.JButton;
 28  
 import javax.swing.JFileChooser;
 29  
 import javax.swing.JMenu;
 30  
 import javax.swing.JMenuBar;
 31  
 import javax.swing.JMenuItem;
 32  
 import javax.swing.JPanel;
 33  
 import javax.swing.JScrollPane;
 34  
 import javax.swing.JTextArea;
 35  
 import javax.swing.JTree;
 36  
 
 37  
 import net.blueslate.commons.gui.GUIUtils;
 38  
 import net.blueslate.commons.gui.domtree.DOMTree;
 39  
 import net.blueslate.commons.io.IOUtils;
 40  
 import net.blueslate.commons.xml.TransformOutputProperties;
 41  
 import net.blueslate.commons.xml.XMLUtils;
 42  
 
 43  
 import org.apache.commons.lang.exception.ExceptionUtils;
 44  
 import org.apache.log4j.Logger;
 45  
 import org.w3c.dom.Document;
 46  
 import org.w3c.dom.Node;
 47  
 
 48  
 /**
 49  
  * Defines the output/results frame.
 50  
  * 
 51  
  * @author pevans
 52  
  * 
 53  
  */
 54  0
 public class OutputFrame extends DisposableFrame implements ActionListener {
 55  
 
 56  
         // get the i18n factory singleton instance...
 57  0
         private static final LabelStringFactory stringFactory = LabelStringFactory
 58  
                         .getInstance();
 59  
 
 60  
         // default frame width and height - these values are used if
 61  
         // a height and width are not found in the user's preferences...
 62  
         private static final String DEFAULT_FRAME_WIDTH = "635";
 63  
         private static final String DEFAULT_FRAME_HEIGHT = "615";
 64  
         
 65  
     // user-prefs property name prefix...
 66  
         private static final String PROPERTY_NAME_PREFIX = "output_frame_";
 67  
         
 68  
     // logger object...
 69  0
     private static final Logger logger = Logger.getLogger(OutputFrame.class);
 70  
 
 71  
         // instance members...
 72  
         private JButton closeBtn, saveOutputBtn;
 73  
         private JTextArea textArea;
 74  
         private JMenuItem close, transformTimings;
 75  
         private Node node;
 76  
         private UserPreferences userPrefs;
 77  
         private XSLRow xslRows[];
 78  
         private TransformOutputProperties lastTransformOutputProps;
 79  
         private byte transformResult[];
 80  
 
 81  
         /**
 82  
          * Constructor
 83  
          * 
 84  
          * @param aParent
 85  
          * @param saTitle
 86  
          * @param aText
 87  
          * @param aXSLRows
 88  
          */
 89  
         public OutputFrame(BasicXSLTFrame aParent, String saTitle, byte aText[],
 90  
                         XSLRow aXSLRows[]) {
 91  0
                 this(aParent, saTitle, null, null, aText, aXSLRows, false);
 92  0
         }
 93  
 
 94  
         /**
 95  
          * Constructor
 96  
          * 
 97  
          * @param aParent
 98  
          * @param saTitle
 99  
          * @param aResultXml
 100  
          * @param aLastTransformOutputProps
 101  
          * @param aXSLRows
 102  
          * @param aIncludeSaveOutputBtn
 103  
          */
 104  
         public OutputFrame(BasicXSLTFrame aParent, String saTitle,
 105  
                         Document aResultXml,
 106  
                         TransformOutputProperties aLastTransformOutputProps,
 107  
                         XSLRow aXSLRows[], boolean aIncludeSaveOutputBtn) {
 108  0
                 this(aParent, saTitle, aResultXml, aLastTransformOutputProps, null,
 109  
                                 aXSLRows, aIncludeSaveOutputBtn);
 110  0
         }
 111  
 
 112  
         /**
 113  
          * Constructor
 114  
          * 
 115  
          * @param aParent
 116  
          * @param saTitle
 117  
          * @param aResultXml
 118  
          * @param aLastTransformOutputProps
 119  
          * @param aText
 120  
          * @param aXSLRows
 121  
          * @param aIncludeSaveOutputBtn
 122  
          */
 123  
         private OutputFrame(BasicXSLTFrame aParent, String saTitle,
 124  
                         Document aResultXml,
 125  
                         TransformOutputProperties aLastTransformOutputProps, byte aText[],
 126  0
                         XSLRow aXSLRows[], boolean aIncludeSaveOutputBtn) {
 127  
 
 128  
                 JScrollPane scrollPane;
 129  
                 JTree tree;
 130  
                 JPanel southPanel;
 131  
 
 132  0
                 xslRows = aXSLRows;
 133  0
                 transformResult = aText;
 134  0
                 node = aResultXml;
 135  0
                 lastTransformOutputProps = aLastTransformOutputProps;
 136  0
                 buildMenuBar();
 137  0
                 southPanel = new JPanel(new FlowLayout());
 138  0
                 southPanel.add(closeBtn = new JButton(stringFactory
 139  
                                 .getString(LabelStringFactory.CLOSE_BUTTON)));
 140  0
                 if (aIncludeSaveOutputBtn) {
 141  0
                         southPanel.add(saveOutputBtn = new JButton(stringFactory.getString(
 142  
                                         LabelStringFactory.OUTPUT_FRAME_SAVE_OUTPUT_BTN)));
 143  0
                         saveOutputBtn.addActionListener(this);
 144  
                 }
 145  0
                 if (aResultXml != null) {
 146  0
                         tree = new DOMTree(aResultXml);
 147  0
                         GUIUtils.expandAll(tree, true);
 148  0
                         scrollPane = new JScrollPane(tree,
 149  
                                         JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
 150  
                                         JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
 151  0
                 } else {
 152  0
                         textArea = new JTextArea(new String(aText));
 153  0
                         textArea.setEditable(false);
 154  0
                         textArea.setLineWrap(true);
 155  0
                         textArea.setWrapStyleWord(true);
 156  0
                         scrollPane = new JScrollPane(textArea,
 157  
                                         JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
 158  
                                         JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
 159  
                 }
 160  0
                 closeBtn.addActionListener(this);
 161  0
                 getContentPane().setLayout(new BorderLayout());
 162  0
                 getContentPane().add(scrollPane, BorderLayout.CENTER);
 163  0
                 getContentPane().add(southPanel, BorderLayout.SOUTH);
 164  0
                 setTitle(saTitle);
 165  0
                 setWindowCloseListener();
 166  0
                 setSize();
 167  0
                 GUIUtils.center(this, aParent);
 168  0
                 setVisible(true);
 169  0
         }
 170  
 
 171  
         /**
 172  
          * Create the window-close listener
 173  
          * 
 174  
          */
 175  
         private void setWindowCloseListener() {
 176  0
                 addWindowListener(new WindowAdapter() {
 177  0
                         public void windowClosing(WindowEvent evt) {
 178  0
                                 dispose(userPrefs, PROPERTY_NAME_PREFIX);
 179  0
                         }
 180  
                 });
 181  0
         }
 182  
 
 183  
         /**
 184  
          * Set the size of the window
 185  
          * 
 186  
          */
 187  
         private void setSize() {
 188  
 
 189  
                 int width, height;
 190  
 
 191  0
                 userPrefs = Utils.getUserPrefs();
 192  0
                 width = Integer.parseInt(userPrefs.getProperty(PROPERTY_NAME_PREFIX
 193  
                                 + AppConstants.FRAME_WIDTH_PROP, DEFAULT_FRAME_WIDTH));
 194  0
                 height = Integer.parseInt(userPrefs.getProperty(PROPERTY_NAME_PREFIX
 195  
                                 + AppConstants.FRAME_HEIGHT_PROP, DEFAULT_FRAME_HEIGHT));
 196  0
                 setSize(width, height);
 197  0
         }
 198  
 
 199  
         /**
 200  
          * Builds the GUI menu bar.
 201  
          */
 202  
         private void buildMenuBar() {
 203  
 
 204  
                 JMenu file, view;
 205  
                 JMenuBar menuBar;
 206  
 
 207  0
                 menuBar = new JMenuBar();
 208  0
                 file = new JMenu(stringFactory.getString(
 209  
                                 LabelStringFactory.OF_FILE_MENU));
 210  0
                 file.setMnemonic(stringFactory.getMnemonic(
 211  
                                 LabelStringFactory.OF_FILE_MENU));
 212  0
                 close = new JMenuItem(stringFactory.getString(
 213  
                                 LabelStringFactory.OF_FILE_CLOSE_MI));
 214  0
                 close.setMnemonic(stringFactory.getMnemonic(
 215  
                                 LabelStringFactory.OF_FILE_CLOSE_MI));
 216  0
                 close.addActionListener(this);
 217  0
                 file.add(close);
 218  0
                 menuBar.add(file);
 219  0
                 if (xslRows != null) {
 220  0
                         view = new JMenu(stringFactory.getString(
 221  
                                         LabelStringFactory.OF_VIEW_MENU));
 222  0
                         view.setMnemonic(stringFactory.getMnemonic(
 223  
                                         LabelStringFactory.OF_VIEW_MENU));
 224  0
                         view.add(transformTimings = new JMenuItem(stringFactory.getString(
 225  
                                         LabelStringFactory.OF_VIEW_TRANSFORM_TIMINGS_DETAIL_MI)));
 226  0
                         transformTimings.setMnemonic(stringFactory.getMnemonic(
 227  
                                         LabelStringFactory.OF_VIEW_TRANSFORM_TIMINGS_DETAIL_MI));
 228  0
                         transformTimings.addActionListener(this);
 229  0
                         menuBar.add(view);
 230  
                 }
 231  0
                 setJMenuBar(menuBar);
 232  0
         }
 233  
 
 234  
         /**
 235  
          * Event handler method.
 236  
          * 
 237  
          * @param aEvt
 238  
          */
 239  
         public void actionPerformed(ActionEvent aEvt) {
 240  
 
 241  
                 int returnVal;
 242  
                 File fileToSave;
 243  
 
 244  0
                 setCursor(new Cursor(Cursor.WAIT_CURSOR));
 245  0
                 if (aEvt.getSource() == closeBtn || aEvt.getSource() == close) {
 246  0
                         dispose(userPrefs, PROPERTY_NAME_PREFIX);
 247  0
                 } else if (aEvt.getSource() == transformTimings) {
 248  0
                         new TimingsFrame(this, xslRows);
 249  0
                 } else if (aEvt.getSource() == saveOutputBtn) {
 250  
                         try {
 251  0
                                 returnVal = Utils.getInstance().getFileChooser().showSaveDialog(
 252  
                                                 this);
 253  0
                                 if (returnVal == JFileChooser.APPROVE_OPTION) {
 254  0
                                         fileToSave = 
 255  
                                                 Utils.getInstance().getFileChooser().getSelectedFile();
 256  0
                                         BasicXSLTFrame.setLastFileChosen(
 257  
                                                         fileToSave.getAbsolutePath());
 258  0
                                         if (textArea != null) {
 259  0
                                                 IOUtils.writeFile(fileToSave, transformResult);
 260  0
                                         } else {
 261  0
                                                 IOUtils.writeFile(fileToSave, XMLUtils.serialize(node,
 262  
                                                                 lastTransformOutputProps));
 263  
                                         }
 264  
                                 }
 265  0
                         } catch (Throwable aAny) {
 266  0
                                 setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
 267  0
                                 logger.error(ExceptionUtils.getFullStackTrace(aAny));
 268  0
                                 Utils.showErrorDialog(this, aAny);
 269  0
                         }
 270  
                 }
 271  0
         }
 272  
 }