Coverage Report - us.paulevans.basicxslt.TransformParametersFrame
 
Classes in this File Line Coverage Branch Coverage Complexity
TransformParametersFrame
0%
0/135
0%
0/18
0
TransformParametersFrame$1
0%
0/3
N/A
0
 
 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.FlowLayout;
 20  
 import java.awt.Font;
 21  
 import java.awt.Frame;
 22  
 import java.awt.GridBagConstraints;
 23  
 import java.awt.GridBagLayout;
 24  
 import java.awt.event.ActionEvent;
 25  
 import java.awt.event.ActionListener;
 26  
 import java.awt.event.WindowAdapter;
 27  
 import java.awt.event.WindowEvent;
 28  
 import java.io.IOException;
 29  
 import java.util.ArrayList;
 30  
 import java.util.List;
 31  
 
 32  
 import javax.swing.JButton;
 33  
 import javax.swing.JCheckBox;
 34  
 import javax.swing.JFrame;
 35  
 import javax.swing.JLabel;
 36  
 import javax.swing.JOptionPane;
 37  
 import javax.swing.JPanel;
 38  
 import javax.swing.JScrollPane;
 39  
 import javax.swing.JSeparator;
 40  
 import javax.swing.JTextField;
 41  
 
 42  
 import net.blueslate.commons.gui.GUIUtils;
 43  
 import net.blueslate.commons.xml.TransformParameters;
 44  
 
 45  
 import org.apache.commons.lang.StringUtils;
 46  
 
 47  
 /**
 48  
  * Defines the parameters frame
 49  
  * @author pevans
 50  
  *
 51  
  */
 52  
 public class TransformParametersFrame extends JFrame 
 53  
         implements ActionListener {                
 54  
         
 55  
         // static constants...
 56  
         private static final int PARAM_NAME_TF_SIZE = 15;
 57  
         private static final int PARAM_VALUE_TF_SIZE = 15;
 58  
         private static final int PARAM_NS_TF_SIZE = 20;
 59  
         
 60  
         // default number of parameter-rows to display...
 61  
         private static final int INITIAL_PARAMETERS = 3;
 62  
         
 63  
         // frame width and height values...
 64  
         private static final int FRAME_WIDTH = 650;
 65  
         private static final int FRAME_HEIGHT = 330;
 66  
         
 67  
         // get the i18n factory singleton instance...
 68  0
         private static final LabelStringFactory stringFactory = 
 69  
                 LabelStringFactory.getInstance();
 70  
 
 71  
         // instance members...
 72  
         private JButton cancelBtn, okayBtn, addParamBtn, removeCheckedBtn;
 73  
         private XSLRow xslRow;
 74  
         private JPanel parametersPanel;
 75  
         private GridBagLayout parametersPanelLayout;
 76  
         private GridBagConstraints parametersPanelConstraints;
 77  
         private List<ParameterTextFieldGroup> parameterTextFieldGroups;
 78  
         private TransformParameters parameters;
 79  
 
 80  
         /**
 81  
          * Constructor
 82  
          * @param aParent
 83  
          * @param aXSLRow
 84  
          * @throws IOException
 85  
          */
 86  0
         public TransformParametersFrame(Frame aParent, XSLRow aXSLRow) {
 87  
                 
 88  
                 JPanel southPanel;
 89  
                 
 90  0
                 xslRow = aXSLRow;
 91  0
                 addWindowListener(new WindowAdapter() {
 92  0
                         public void windowClosing(WindowEvent evt) {
 93  0
                                 dispose();
 94  0
                         }
 95  
                 });        
 96  0
                 parameterTextFieldGroups = new ArrayList<ParameterTextFieldGroup>();
 97  0
                 parameters = xslRow.getTransformParameters();
 98  0
                 southPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
 99  0
                 southPanel.add(okayBtn = new JButton(stringFactory.getString(
 100  
                                 LabelStringFactory.OK_BUTTON)));
 101  0
                 southPanel.add(cancelBtn = new JButton(stringFactory.getString(
 102  
                                 LabelStringFactory.CANCEL_BUTTON)));
 103  0
                 okayBtn.addActionListener(this);
 104  0
                 cancelBtn.addActionListener(this);                
 105  0
                 getContentPane().setLayout(new BorderLayout());
 106  0
                 getContentPane().add(buildNorthPanel(), BorderLayout.NORTH);
 107  0
                 getContentPane().add(southPanel, BorderLayout.SOUTH);
 108  0
                 getContentPane().add(buildMainPanel(), BorderLayout.CENTER);                
 109  0
                 setTitle(stringFactory.getString(
 110  
                                 LabelStringFactory.PARAMS_FRAME_TRANSFORM_PARAMETERS));                
 111  0
                 setSize(FRAME_WIDTH, FRAME_HEIGHT);
 112  0
                 GUIUtils.center(this, aParent);
 113  0
                 initializeGUI();
 114  0
                 setVisible(true);
 115  0
         }
 116  
         
 117  
         /**
 118  
          * Builds the north panel
 119  
          * @return
 120  
          */
 121  
         private JPanel buildNorthPanel() {
 122  
                 
 123  
                 GridBagLayout layout;
 124  
                 GridBagConstraints constraints;
 125  
                 JPanel panel;
 126  
                 int row, col;
 127  
                 JLabel headerLabel;
 128  
                 
 129  0
                 layout = new GridBagLayout();
 130  0
                 constraints = new GridBagConstraints();
 131  0
                 panel = new JPanel(layout);
 132  0
                 row = 0;
 133  0
                 col = 0;
 134  0
                 headerLabel = new JLabel(stringFactory.getString(
 135  
                                 LabelStringFactory.PARAMS_FRAME_TRANSFORM_PARAMETERS) + " (" + 
 136  
                                 xslRow.getDescription() + ")");
 137  0
                 headerLabel.setFont(new Font("arial", Font.PLAIN, 18));
 138  0
                 JLabel fileLabel = new JLabel(stringFactory.getString(
 139  
                                 LabelStringFactory.PARAMS_FRAME_FILE_LBL_WITH_COLON) + 
 140  
                                 xslRow.getTextField().getText());
 141  0
                 fileLabel.setFont(new Font("arial", Font.PLAIN, 12));                        
 142  0
                 GUIUtils.add(panel, headerLabel, layout, constraints, row++, col, 
 143  
                         1, 1, 1, 1, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH,
 144  
                         GUIUtils.MED_LARGE_INSETS);
 145  0
                 GUIUtils.add(panel, new JSeparator(), layout, constraints, row++, col, 
 146  
                         1, 1, 1, 1, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH, 
 147  
                         GUIUtils.MED_LARGE_INSETS);
 148  0
                 GUIUtils.add(panel, fileLabel, layout, constraints, row++, col, 
 149  
                         1, 1, 1, 1, GridBagConstraints.NORTHEAST, 
 150  
                         GridBagConstraints.BOTH, GUIUtils.MED_LARGE_INSETS);                                
 151  0
                 return panel;
 152  
         }    
 153  
         
 154  
         /**
 155  
          * Initializes the GUI
 156  
          *
 157  
          */
 158  
         private void initializeGUI() {
 159  0
                 removeCheckedBtn.setEnabled(false);
 160  0
         }
 161  
         
 162  
         /**
 163  
          * Builds the main panel
 164  
          * @return
 165  
          */
 166  
         private JPanel buildMainPanel() {
 167  
                 
 168  
                 int loop, size;
 169  
                 JPanel main;
 170  
                 
 171  0
                 main = new JPanel(new BorderLayout());
 172  0
                 parametersPanelLayout = new GridBagLayout();
 173  0
                 parametersPanelConstraints = new GridBagConstraints();
 174  0
                 parametersPanel = new JPanel(parametersPanelLayout);
 175  0
                 size = parameters.getSize();
 176  0
                 if (size == 0) {
 177  0
                         size = INITIAL_PARAMETERS;
 178  
                 }
 179  0
                 for (loop = 0; loop < size; loop++) {
 180  0
                         parameterTextFieldGroups.add(new ParameterTextFieldGroup(
 181  
                                         parametersPanel, 
 182  
                                         new JTextField(PARAM_NAME_TF_SIZE),
 183  
                                         new JTextField(PARAM_VALUE_TF_SIZE), 
 184  
                                         new JTextField(PARAM_NS_TF_SIZE),
 185  
                                         new JCheckBox()));
 186  
                 }
 187  0
                 rebuildParametersPanel();
 188  0
                 main.add(new JScrollPane(parametersPanel,
 189  
                         JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
 190  
                         JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.CENTER);
 191  0
                 main.add(buildAddRemovePanel(), BorderLayout.SOUTH);
 192  0
                 return main;
 193  
         }
 194  
         
 195  
         /**
 196  
          * Builds the add/remove panel
 197  
          * @return
 198  
          */
 199  
         private JPanel buildAddRemovePanel() {
 200  
                 
 201  
                 int row;
 202  
                 GridBagLayout layout;
 203  
                 GridBagConstraints constraints;
 204  
                 JPanel buttons;
 205  
                 
 206  0
                 row = 0;
 207  0
                 layout = new GridBagLayout();
 208  0
                 constraints = new GridBagConstraints();
 209  0
                 buttons = new JPanel(new FlowLayout(FlowLayout.LEFT));
 210  0
                 buttons.add(addParamBtn = new JButton(stringFactory.getString(
 211  
                                 LabelStringFactory.PARAMS_FRAME_ADD_PARAMETER)));
 212  0
                 buttons.add(removeCheckedBtn = new JButton(stringFactory.getString(
 213  
                                 LabelStringFactory.PARAMS_FRAME_REMOVE_CHECKED)));
 214  0
                 addParamBtn.addActionListener(this);
 215  0
                 removeCheckedBtn.addActionListener(this);
 216  0
                 JPanel addRemovePanel = new JPanel(layout);
 217  0
                 GUIUtils.add(addRemovePanel, buttons, layout, 
 218  
                         constraints, row++, 0, 1, 1, GridBagConstraints.WEST, 
 219  
                         GridBagConstraints.NONE, GUIUtils.SMALL_INSETS);
 220  0
                 GUIUtils.add(addRemovePanel, new JSeparator(), layout, 
 221  
                         constraints, row++, 0, 1, 1, 1, 1, GridBagConstraints.WEST, 
 222  
                         GridBagConstraints.HORIZONTAL, GUIUtils.SMALL_INSETS);
 223  0
                 return addRemovePanel;
 224  
         }
 225  
         
 226  
         /**
 227  
          * Rebuilds the parameters panel
 228  
          *
 229  
          */
 230  
         private void rebuildParametersPanel() {
 231  
                 
 232  
                 int loop, row, size;
 233  
                 JTextField paramNameTf, paramValueTf, paramNamespaceURITf;
 234  
                 JCheckBox removeCb;
 235  
         String paramNames[];
 236  
         ParameterTextFieldGroup paramTfGroup;
 237  
         
 238  0
         row = 0;
 239  0
                 parametersPanel.removeAll();                
 240  0
                 GUIUtils.add(parametersPanel, new JLabel(stringFactory.getString(
 241  
                                 LabelStringFactory.PARAMS_FRAME_NAME_LBL)), 
 242  
                                 parametersPanelLayout, parametersPanelConstraints, row, 0, 1, 1, 
 243  
                                 GridBagConstraints.WEST, GridBagConstraints.BOTH, 
 244  
                                 GUIUtils.SMALL_INSETS);
 245  0
                 GUIUtils.add(parametersPanel, new JLabel(stringFactory.getString(
 246  
                                 LabelStringFactory.PARAMS_FRAME_VALUE_LBL)), 
 247  
                                 parametersPanelLayout, parametersPanelConstraints, row, 1, 1, 1, 
 248  
                                 GridBagConstraints.WEST, GridBagConstraints.BOTH, 
 249  
                                 GUIUtils.SMALL_INSETS);
 250  0
                 GUIUtils.add(parametersPanel, new JLabel(stringFactory.getString(
 251  
                                 LabelStringFactory.PARAMS_FRAME_NAMESPACE_URI_LBL)), 
 252  
                                 parametersPanelLayout, parametersPanelConstraints, row, 2, 1, 1, 
 253  
                                 GridBagConstraints.WEST, GridBagConstraints.BOTH, 
 254  
                                 GUIUtils.SMALL_INSETS);
 255  0
                 GUIUtils.add(parametersPanel, new JLabel(stringFactory.getString(
 256  
                                 LabelStringFactory.PARAMS_FRAME_REMOVE_LBL)), 
 257  
                                 parametersPanelLayout, parametersPanelConstraints, row++, 3, 1, 
 258  
                                 1, GridBagConstraints.WEST, GridBagConstraints.BOTH, 
 259  
                                 GUIUtils.SMALL_INSETS);
 260  0
                 paramNames = parameters.getParameterNames();
 261  0
                 size = parameterTextFieldGroups.size();
 262  0
                 for (loop = 0; loop < size; loop++) {
 263  0
                         paramTfGroup = (ParameterTextFieldGroup)
 264  
                         parameterTextFieldGroups.get(loop);
 265  0
                         GUIUtils.add(parametersPanel, paramNameTf = 
 266  
                                 paramTfGroup.getParamNameTf(), parametersPanelLayout, 
 267  
                                 parametersPanelConstraints, row, 0, 1, 1, 
 268  
                                 GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, 
 269  
                                 GUIUtils.SMALL_INSETS);
 270  0
                         GUIUtils.add(parametersPanel, paramValueTf = 
 271  
                                 paramTfGroup.getParamValueTf(), parametersPanelLayout, 
 272  
                                 parametersPanelConstraints, row, 1, 1, 1, 
 273  
                                 GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, 
 274  
                                 GUIUtils.SMALL_INSETS);
 275  0
                         GUIUtils.add(parametersPanel, paramNamespaceURITf = 
 276  
                                 paramTfGroup.getParamNamespaceURITf(), parametersPanelLayout, 
 277  
                                 parametersPanelConstraints, row, 2, 1, 1, 
 278  
                                 GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, 
 279  
                                 GUIUtils.SMALL_INSETS);
 280  0
                         GUIUtils.add(parametersPanel, removeCb = paramTfGroup.getRemoveCb(), 
 281  
                                         parametersPanelLayout, parametersPanelConstraints, row++, 3, 
 282  
                                         1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE, 
 283  
                                         GUIUtils.SMALL_INSETS);
 284  0
                         removeCb.addActionListener(this);
 285  0
                         if (loop < paramNames.length) {
 286  0
                                 paramNameTf.setText(TransformParameters.getParameterName(
 287  
                                                 paramNames[loop]));
 288  0
                                 paramValueTf.setText(parameters.getParameter(
 289  
                                                 paramNames[loop]).toString());
 290  0
                                 paramNamespaceURITf.setText(TransformParameters.getNamespaceURI(
 291  
                                                 paramNames[loop]));
 292  
                         }                
 293  
                 }
 294  0
         }
 295  
         
 296  
         /**
 297  
          * Writes the parameter values to the "parameters" object.
 298  
          *
 299  
          */
 300  
         private void saveParameters() {
 301  
                 
 302  
                 int loop, size;
 303  
                 ParameterTextFieldGroup paramTextFieldGroup;
 304  
                 
 305  0
                 parameters.clear();
 306  0
                 size = parameterTextFieldGroups.size();
 307  0
                 for (loop = 0; loop < size; loop++) {
 308  0
                         paramTextFieldGroup = (ParameterTextFieldGroup)
 309  
                         parameterTextFieldGroups.get(loop);
 310  
                         
 311  
                         // ignore row if all 3 textfields are blank...
 312  0
                         if (!(StringUtils.isBlank(
 313  
                                         paramTextFieldGroup.getParamNamespaceURITf().getText()) &&
 314  
                                 StringUtils.isBlank(
 315  
                                                 paramTextFieldGroup.getParamNameTf().getText()) &&
 316  
                                 StringUtils.isBlank(
 317  
                                                 paramTextFieldGroup.getParamValueTf().getText()))) {
 318  0
                                 parameters.setParameter(
 319  
                                         paramTextFieldGroup.getParamNamespaceURITf().getText(),
 320  
                                         paramTextFieldGroup.getParamNameTf().getText(),
 321  
                                         paramTextFieldGroup.getParamValueTf().getText());
 322  
                         }
 323  
                 }
 324  0
         }
 325  
         
 326  
         /**
 327  
          * Refreshes the parameters panel
 328  
          *
 329  
          */
 330  
         private void refreshParametersPanel() {
 331  0
                 rebuildParametersPanel();
 332  0
                 parametersPanel.repaint();
 333  0
                 parametersPanel.revalidate();
 334  0
         }
 335  
         
 336  
         /**
 337  
          * Enables or disables the remove-checked button
 338  
          *
 339  
          */
 340  
         private void setEnabledRemoveCheckedBtn() {
 341  
                 
 342  
                 int loop, size;
 343  
                 ParameterTextFieldGroup paramTextFieldGroup;
 344  
                 boolean enable;
 345  
                 
 346  0
                 size = parameterTextFieldGroups.size();
 347  0
                 enable = false;
 348  0
                 for (loop = 0; loop < size; loop++) {
 349  0
                         paramTextFieldGroup = (ParameterTextFieldGroup)
 350  
                         parameterTextFieldGroups.get(loop);
 351  0
                         if (paramTextFieldGroup.getRemoveCb().isSelected()) {
 352  0
                                 enable = true;
 353  0
                                 break;
 354  
                         }
 355  
                 }
 356  0
                 removeCheckedBtn.setEnabled(enable);
 357  0
         }
 358  
         
 359  
         /**
 360  
          * Validates the entered-parameter values
 361  
          * @return
 362  
          */
 363  
         private boolean validateParams() {
 364  
                 
 365  
                 boolean areAllValid;
 366  
                 int loop, size;
 367  
                 ParameterTextFieldGroup paramTextFieldGroup;
 368  
                 
 369  0
                 areAllValid = true;
 370  0
                 size = parameterTextFieldGroups.size();
 371  0
                 for (loop = 0; loop < size; loop++) {
 372  0
                         paramTextFieldGroup = (ParameterTextFieldGroup)
 373  
                         parameterTextFieldGroups.get(loop);
 374  0
                         if (StringUtils.isNotBlank(
 375  
                                         paramTextFieldGroup.getParamValueTf().getText()) &&
 376  
                                 StringUtils.isBlank(
 377  
                                                 paramTextFieldGroup.getParamNameTf().getText())) {
 378  0
                                 areAllValid = false;
 379  0
                                 Utils.showDialog(this, 
 380  
                                                 stringFactory.getString(LabelStringFactory.
 381  
                                                                 PARAMS_FRAME_CANNOT_HAVE_EMPTY_PARAM_VALUE),
 382  
                                                 stringFactory.getString(LabelStringFactory.
 383  
                                                                 PARAMS_FRAME_INVALID_PARAMETER), 
 384  
                                                 JOptionPane.ERROR_MESSAGE);
 385  0
                                 break;
 386  0
                         } else if (StringUtils.isNotBlank(
 387  
                                         paramTextFieldGroup.getParamNamespaceURITf().getText()) &&
 388  
                                         StringUtils.isBlank(
 389  
                                                         paramTextFieldGroup.getParamNameTf().getText())) {
 390  0
                                 areAllValid = false;
 391  0
                                 Utils.showDialog(this, 
 392  
                                                 stringFactory.getString(LabelStringFactory.
 393  
                                                                 PARAMS_FRAME_CANNOT_HAVE_EMPTY_PARAM_VALUE),
 394  
                                                 stringFactory.getString(LabelStringFactory.
 395  
                                                                 PARAMS_FRAME_INVALID_PARAMETER), 
 396  
                                                 JOptionPane.ERROR_MESSAGE);
 397  0
                                 break;
 398  
                         }
 399  
                 }
 400  0
                 return areAllValid;
 401  
         }
 402  
         
 403  
         /**
 404  
          * Event handler
 405  
          */
 406  
         public void actionPerformed(ActionEvent aEvent) {
 407  
                 
 408  
                 Object eventSource;
 409  
                 
 410  0
                 eventSource = aEvent.getSource();
 411  0
                 if (eventSource == cancelBtn) {
 412  0
                         dispose();
 413  0
                 } else if (eventSource == okayBtn) {
 414  0
                         if (validateParams()) {
 415  0
                                 saveParameters();
 416  0
                                 xslRow.refreshIndicatorLabel();
 417  0
                                 dispose();
 418  0
                         }
 419  0
                 } else if (eventSource == addParamBtn) {
 420  0
                         parameterTextFieldGroups.add(new ParameterTextFieldGroup(
 421  
                                         parametersPanel, new JTextField(PARAM_NAME_TF_SIZE),
 422  
                                         new JTextField(PARAM_VALUE_TF_SIZE), 
 423  
                                         new JTextField(PARAM_NS_TF_SIZE), new JCheckBox()));
 424  0
                         refreshParametersPanel();
 425  0
                 } else if (eventSource instanceof JCheckBox) {
 426  0
                         setEnabledRemoveCheckedBtn();
 427  0
                 } else if (eventSource == removeCheckedBtn) {
 428  0
                         ParameterTextFieldGroup.removeChecked(parameterTextFieldGroups);
 429  
                         // always make sure there's at least one parameter-entry row...
 430  0
                         if (parameterTextFieldGroups.size() == 0) {
 431  0
                                 parameterTextFieldGroups.add(new ParameterTextFieldGroup(
 432  
                                                 parametersPanel, new JTextField(PARAM_NAME_TF_SIZE),
 433  
                                                 new JTextField(PARAM_VALUE_TF_SIZE), 
 434  
                                                 new JTextField(PARAM_NS_TF_SIZE), new JCheckBox()));
 435  0
                                 refreshParametersPanel();
 436  
                         }
 437  0
                         removeCheckedBtn.setEnabled(false);
 438  
                 }
 439  0
         }
 440  
 }
 441