Coverage Report - us.paulevans.basicxslt.XSLRow
 
Classes in this File Line Coverage Branch Coverage Complexity
XSLRow
0%
0/162
0%
0/21
1.5
 
 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.Color;
 19  
 import java.awt.Font;
 20  
 import java.awt.event.ActionListener;
 21  
 import java.text.MessageFormat;
 22  
 import java.util.List;
 23  
 
 24  
 import javax.swing.JButton;
 25  
 import javax.swing.JCheckBox;
 26  
 import javax.swing.JComboBox;
 27  
 import javax.swing.JLabel;
 28  
 import javax.swing.JPanel;
 29  
 import javax.swing.JTextField;
 30  
 
 31  
 import net.blueslate.commons.xml.TransformOutputProperties;
 32  
 import net.blueslate.commons.xml.TransformParameters;
 33  
 
 34  
 import org.apache.commons.lang.StringUtils;
 35  
 
 36  
 /**
 37  
  * Models a row in the XSL-array.  This is a helper class that makes it easier
 38  
  * to manage the adding, removing and inserting of XSL stylesheet rows.
 39  
  * @author Paul Evans
 40  
  */
 41  
 public class XSLRow {
 42  
         
 43  
         // get the i18n factory singleton instance...
 44  0
         private static final LabelStringFactory stringFactory = 
 45  
                 LabelStringFactory.getInstance();
 46  
         
 47  
         // static constants...
 48  
         public static final int VALIDATE_INDEX = 1;
 49  
         public static final int ON_OFF_INDEX = 2;
 50  
         public static final int VIEW_EDIT_OUTPUT_PROPS_INDEX = 3;
 51  
         public static final int CLEAR_OUTPUT_PROPS_INDEX = 4;
 52  
         public static final int VIEW_EDIT_PARAMETERS_INDEX = 5;
 53  
         public static final int CLEAR_ALL_PARAMETERS_INDEX = 6;
 54  
         public static final int PERFORM_IDENTITY_TRANSFORM_INDEX = 7;
 55  0
         public static final String ON_OFF_ITEM_PREFIX = stringFactory.getString(
 56  
                         LabelStringFactory.XML_ACTION_TURNONOFF_PREFIX);
 57  
         
 58  
         // The set of actions that can be taken on this stylesheet...
 59  0
         public static final String ACTIONS[] = {
 60  
                 AppConstants.SEPARATOR,
 61  
                 stringFactory.getString(LabelStringFactory.XML_ACTION_VALIDATE),
 62  
                 ON_OFF_ITEM_PREFIX, 
 63  
                 stringFactory.getString(
 64  
                                 LabelStringFactory.XML_ACTION_OUTPUT_PROPERTIES),
 65  
                 stringFactory.getString(
 66  
                                 LabelStringFactory.XML_ACTION_CLEAR_OUTPUT_PROPERTIES),
 67  
                 stringFactory.getString(LabelStringFactory.XML_ACTION_PARAMETERS),
 68  
                 stringFactory.getString(LabelStringFactory.XML_ACTION_CLEAR_PARAMETERS),
 69  
                 stringFactory.getString(LabelStringFactory.XML_ACTION_PERFORM_IT)
 70  
         };
 71  
         
 72  
         // instance members...
 73  
         private JPanel parent;
 74  
         private JButton browseBtn, insertBtn;
 75  
         private JTextField textField;
 76  
         private JCheckBox removeCb;
 77  
         private JLabel label, indicatorLabel;
 78  
         private boolean onOffButtonValue;
 79  
         private long timeToTransform;
 80  
         private TransformOutputProperties transformOutputProperties;
 81  
         private TransformParameters transformParameters;
 82  
         private JComboBox action;
 83  
         private int index;
 84  
         private boolean areOutputPropertiesSet;
 85  
         
 86  
         /**
 87  
          * Constructor
 88  
          * @param aActionListener
 89  
          * @param aParent
 90  
          * @param aInsertBtn
 91  
          * @param aLabel
 92  
          * @param aTextField
 93  
          * @param aRemoveCb
 94  
          * @param aBrowse
 95  
          * @param aAction
 96  
          * @param aOutputPropertiesIndicator
 97  
          * @param aIndex
 98  
          */
 99  
         public XSLRow(ActionListener aActionListener, JPanel aParent, 
 100  
                         JButton aInsertBtn, JLabel aLabel, JTextField aTextField, 
 101  
                         JCheckBox aRemoveCb, JButton aBrowse, JComboBox aAction, 
 102  0
                         JLabel aOutputPropertiesIndicator, int aIndex) {
 103  0
                 parent = aParent;
 104  0
                 insertBtn = aInsertBtn;
 105  0
                 label = aLabel;
 106  0
                 browseBtn = aBrowse;
 107  0
                 textField = aTextField;
 108  0
                 removeCb = aRemoveCb;
 109  0
                 action = aAction;
 110  0
                 indicatorLabel = aOutputPropertiesIndicator;
 111  0
                 index = aIndex;
 112  0
                 onOffButtonValue = true; // init to true...
 113  0
                 refreshActionItems();
 114  0
                 indicatorLabel.setFont(new Font("arial", Font.PLAIN, 10));
 115  0
                 insertBtn.addActionListener(aActionListener);
 116  0
                 indicatorLabel.setToolTipText(stringFactory.getString(
 117  
                                 LabelStringFactory.XSLROW_OUTPUT_PROPERTIES_SPECIFIED));
 118  0
                 removeCb.addActionListener(aActionListener);
 119  0
                 browseBtn.addActionListener(aActionListener);
 120  0
                 action.addActionListener(aActionListener);
 121  0
                 action.setActionCommand(AppConstants.TAKE_ACTION);
 122  0
                 removeCb.setActionCommand(AppConstants.REMOVE_CB);
 123  0
                 textField.setBackground(Color.GREEN);
 124  0
                 transformOutputProperties = new TransformOutputProperties();
 125  0
                 transformParameters = new TransformParameters();
 126  0
                 setToolTips();
 127  0
         }
 128  
         
 129  
         /**
 130  
          * Sets the various tool tips on the GUI components found on this row.
 131  
          *
 132  
          */
 133  
         private void setToolTips() {
 134  0
                 insertBtn.setToolTipText(stringFactory.getString(
 135  
                                 LabelStringFactory.XSLROW_TOOL_TIP_INSERT_STYLESHEET));
 136  0
                 textField.setToolTipText(stringFactory.getString(
 137  
                                 LabelStringFactory.XSLROW_TOOL_TIP_PICK_STYLESHEET));
 138  0
                 removeCb.setToolTipText(stringFactory.getString(
 139  
                                 LabelStringFactory.XSLROW_TOOL_TIP_REMOVE_CHECKBOX));
 140  0
                 browseBtn.setToolTipText(stringFactory.getString(
 141  
                                 LabelStringFactory.XSLROW_TOOL_TIP_BROWSE_BTN));
 142  0
                 action.setToolTipText(stringFactory.getString(
 143  
                                 LabelStringFactory.XSLROW_TOOL_TIP_TAKE_ACTION));
 144  0
         }
 145  
         
 146  
         /**
 147  
          * Setter - sets the index of the XSL row
 148  
          * @param aIndex
 149  
          */
 150  
         public void setIndex(int aIndex) {
 151  0
                 index = aIndex;
 152  0
                 label.setText(MessageFormat.format(stringFactory.getString(
 153  
                                 LabelStringFactory.XSLROW_XSL_LABEL), index+1));
 154  0
         }
 155  
         
 156  
         /**
 157  
          * Getter
 158  
          * @return
 159  
          */
 160  
         public JButton getInsertBtn() {
 161  0
                 return insertBtn;
 162  
         }
 163  
         
 164  
         /**
 165  
          * Getter
 166  
          * @return
 167  
          */
 168  
         public String getDescription() {
 169  0
                 return StringUtils.replace(label.getText(), ":", "").trim();
 170  
         }
 171  
         
 172  
         /**
 173  
          * Clears the output properties set on this stylesheet
 174  
          *
 175  
          */
 176  
         public void clearOutputProperties() {
 177  0
                 transformOutputProperties.clear();
 178  0
         }
 179  
         
 180  
         /**
 181  
          * Clears the parameters set on this stylesheet
 182  
          *
 183  
          */
 184  
         public void clearAllParameters() {
 185  0
                 transformParameters.clear();
 186  0
                 refreshIndicatorLabel();
 187  0
         }
 188  
         
 189  
         /**
 190  
          * Getter
 191  
          * @return
 192  
          */
 193  
         public JLabel getIndicatorLabel() {
 194  0
                 return indicatorLabel;
 195  
         }
 196  
         
 197  
         /**
 198  
          * Sets if output properties are set or not on this stylesheet
 199  
          * @param aAreOutputPropertiesSet
 200  
          */
 201  
         public void setAreOutputPropertiesSet(boolean aAreOutputPropertiesSet) {
 202  0
                 areOutputPropertiesSet = aAreOutputPropertiesSet;
 203  0
                 refreshIndicatorLabel();
 204  0
         }
 205  
         
 206  
         /**
 207  
          * Refreshes the indicator label
 208  
          *
 209  
          */
 210  
         public void refreshIndicatorLabel() {
 211  
                 
 212  
                 StringBuffer labelText;
 213  
                 StringBuffer toolTip;
 214  
                 
 215  0
                 labelText = new StringBuffer("");
 216  0
                 toolTip = new StringBuffer();
 217  0
                 if (areOutputPropertiesSet) {
 218  0
                         labelText.append("OP");
 219  0
                         toolTip.append("Output properties");
 220  
                 }
 221  0
                 if (transformParameters.getSize() > 0) {
 222  0
                         if (areOutputPropertiesSet) {
 223  0
                                 labelText.append(",");
 224  0
                                 toolTip.append(" and ");
 225  
                         } 
 226  0
                         labelText.append("P");
 227  0
                         toolTip.append("Parameters");
 228  
                 }
 229  0
                 if (areOutputPropertiesSet || (transformParameters.getSize() > 0)) {
 230  0
                         toolTip.append(" have been specified for this Stylesheet");
 231  
                 }
 232  0
                 indicatorLabel.setText(labelText.toString());
 233  0
                 indicatorLabel.setToolTipText(toolTip.toString());
 234  0
         }
 235  
         
 236  
         /**
 237  
          * Getter
 238  
          * @return
 239  
          */
 240  
         public boolean areOutputPropertiesSet() {
 241  0
                 return areOutputPropertiesSet;
 242  
         }
 243  
         
 244  
         /**
 245  
          * Refreshes the action-items combo-box
 246  
          *
 247  
          */
 248  
         public void refreshActionItems() {
 249  
                 
 250  
                 int loop;
 251  
                 String item;
 252  
                 
 253  0
                 action.removeAllItems();
 254  0
                 action.addItem(stringFactory.getString(
 255  
                                 LabelStringFactory.XML_ACTION_TAKE_ACTION));
 256  0
                 for (loop = 0; loop < ACTIONS.length; loop++) {
 257  0
                         item = ACTIONS[loop];
 258  0
                         if (loop == ON_OFF_INDEX) {
 259  0
                                 item += onOffButtonValue ? stringFactory.getString(
 260  
                                                 LabelStringFactory.XML_ACTION_TURNONOFF_OFF) : 
 261  
                                                         stringFactory.getString(
 262  
                                                                         LabelStringFactory.XML_ACTION_TURNONOFF_ON);
 263  
                         }
 264  0
                         action.addItem(item);
 265  
                 }
 266  0
                 action.setSelectedIndex(0);
 267  0
         }
 268  
         
 269  
         /**
 270  
          * Setter
 271  
          * @param aIndex
 272  
          */
 273  
         public void setSelectedActionIndex(int aIndex) {
 274  0
                 action.setSelectedIndex(aIndex);
 275  0
         }
 276  
         
 277  
         /**
 278  
          * Getter
 279  
          * @return
 280  
          */
 281  
         public JComboBox getAction() {
 282  0
                 return action;
 283  
         }
 284  
         
 285  
         /**
 286  
          * Getter
 287  
          * @return
 288  
          */
 289  
         public int getIndex() {
 290  0
                 return index;
 291  
         }
 292  
         
 293  
         /**
 294  
          * Getter
 295  
          * @return
 296  
          */
 297  
         public TransformOutputProperties getTransformOutputProperties() {
 298  0
                 return transformOutputProperties;
 299  
         }
 300  
         
 301  
         /**
 302  
          * Getter
 303  
          * @return
 304  
          */
 305  
         public TransformParameters getTransformParameters() {
 306  0
                 return transformParameters;
 307  
         }
 308  
         
 309  
         /**
 310  
          * Setter
 311  
          * @param aTimeToTransform
 312  
          */
 313  
         public void setTimeToTransform(long aTimeToTransform) {
 314  0
                 timeToTransform = aTimeToTransform;
 315  0
         }
 316  
         
 317  
         /**
 318  
          * Getter
 319  
          * @return
 320  
          */
 321  
         public long getTimeToTransform() {
 322  0
                 return timeToTransform;
 323  
         }
 324  
         
 325  
         /**
 326  
          * Returns true if this xsl row is toggled-on and the contents of the
 327  
          * text-field is not blank
 328  
          * @return
 329  
          */
 330  
         public boolean isOnAndNotEmpty() {
 331  0
                 return onOffButtonValue && StringUtils.isNotBlank(textField.getText());
 332  
         }
 333  
         
 334  
         /**
 335  
          * Returns if this row is toggled-on or not.
 336  
          * @return
 337  
          */
 338  
         public boolean isOn() {
 339  0
                 return onOffButtonValue; 
 340  
         }
 341  
         
 342  
         /**
 343  
          * Overridden toStrings
 344  
          */
 345  
         public String toString() {
 346  
                 
 347  
                 StringBuffer str;
 348  
                 
 349  0
                 str = new StringBuffer();
 350  0
                 str.append("label text: [" + label.getText() + "]");
 351  0
                 return str.toString();
 352  
         }
 353  
         
 354  
         /**
 355  
          * Removes all of the rows
 356  
          * @param aRows
 357  
          */
 358  
         public static void removeAll(List aRows) {
 359  
                 
 360  
                 int loop, size;
 361  
                 XSLRow xslRow;
 362  
                 
 363  0
                 size = aRows.size();
 364  0
                 for (loop = size -1; loop >= 0; loop--) {
 365  0
                         xslRow = (XSLRow)aRows.get(loop);
 366  0
                         xslRow.parent.remove(xslRow.insertBtn);
 367  0
                         xslRow.parent.remove(xslRow.label);
 368  0
                         xslRow.parent.remove(xslRow.textField);
 369  0
                         xslRow.parent.remove(xslRow.browseBtn);
 370  0
                         xslRow.parent.remove(xslRow.removeCb);
 371  0
                         xslRow.parent.remove(xslRow.action);
 372  0
                         xslRow.parent.remove(xslRow.indicatorLabel);
 373  0
                         aRows.remove(loop);                                        
 374  
                 }
 375  0
         }
 376  
         
 377  
         /**
 378  
          * Removes all the checked rows from aRows
 379  
          * @param aRows
 380  
          * @return
 381  
          */
 382  
         public static int removeChecked(List aRows) {
 383  
                 
 384  
                 int loop, size, numRemoved;
 385  
                 XSLRow xslRow;
 386  
                 JPanel parent;
 387  
                 
 388  0
                 numRemoved = 0;
 389  0
                 size = aRows.size();
 390  0
                 parent = null;
 391  0
                 for (loop = size -1; loop >= 0; loop--) {
 392  0
                         xslRow = (XSLRow)aRows.get(loop);
 393  0
                         parent = xslRow.parent;
 394  0
                         if (xslRow.removeCb.isSelected()) {
 395  0
                                 xslRow.parent.remove(xslRow.insertBtn);
 396  0
                                 xslRow.parent.remove(xslRow.label);
 397  0
                                 xslRow.parent.remove(xslRow.textField);
 398  0
                                 xslRow.parent.remove(xslRow.browseBtn);
 399  0
                                 xslRow.parent.remove(xslRow.removeCb);
 400  0
                                 xslRow.parent.remove(xslRow.action);
 401  0
                                 xslRow.parent.remove(xslRow.indicatorLabel);
 402  0
                                 aRows.remove(loop);                                
 403  0
                                 numRemoved++;
 404  
                         }
 405  
                 }
 406  0
                 if (numRemoved > 0) {
 407  0
                         parent.repaint();
 408  0
                         parent.revalidate();
 409  
                 }
 410  0
                 size = aRows.size();
 411  0
                 for (loop = 0; loop < size; loop++) {
 412  0
                         ((XSLRow)aRows.get(loop)).setIndex(loop);
 413  
                 }
 414  0
                 return numRemoved;
 415  
         }
 416  
         
 417  
         /**
 418  
          * Getter
 419  
          * @return
 420  
          */
 421  
         public JTextField getTextField() {
 422  0
                 return textField;
 423  
         }
 424  
         
 425  
         /**
 426  
          * Toggles this row
 427  
          *
 428  
          */
 429  
         public void toggleOnOffBtn() {
 430  0
                 onOffButtonValue = !onOffButtonValue;
 431  0
                 setOn(onOffButtonValue);
 432  0
         }
 433  
         
 434  
         /**
 435  
          * Turns this row 'on'
 436  
          * @param aOn
 437  
          */
 438  
         public void setOn(boolean aOn) {
 439  0
                 onOffButtonValue = aOn;
 440  0
                 textField.setBackground(aOn ? Color.GREEN : Color.lightGray);
 441  0
                 insertBtn.setEnabled(aOn);
 442  0
                 label.setEnabled(aOn);
 443  0
                 browseBtn.setEnabled(aOn);
 444  0
                 removeCb.setEnabled(aOn);
 445  0
                 refreshActionItems();
 446  0
                 textField.setEnabled(aOn);
 447  0
         }
 448  
         
 449  
         /**
 450  
          * Returns the row from aRows that holds the reference to aAction
 451  
          * @param aRows
 452  
          * @param aAction
 453  
          * @return
 454  
          */
 455  
         public static XSLRow getRowByAction(List aRows, JComboBox aAction) {
 456  
                 
 457  
                 XSLRow row, tmpRow;
 458  
                 int loop, size;
 459  
                 
 460  0
                 row = null;
 461  0
                 size = aRows.size();
 462  0
                 for (loop = 0; loop < size; loop++) {
 463  0
                         if ((tmpRow = (XSLRow)aRows.get(loop)).action == aAction) {
 464  0
                                 row = tmpRow;
 465  0
                                 break;
 466  
                         }
 467  
                 }
 468  0
                 return row;
 469  
         }        
 470  
         
 471  
         /**
 472  
          * Returns the row from aRows that holds the reference to aInsertBtn
 473  
          * @param aRows
 474  
          * @param aInsertBtn
 475  
          * @return
 476  
          */
 477  
         public static XSLRow getRowByInsertBtn(List aRows, 
 478  
                         JButton aInsertBtn) {
 479  
                 
 480  
                 XSLRow row, tmpRow;
 481  
                 int loop, size;
 482  
                 
 483  0
                 row = null;
 484  0
                 size = aRows.size();
 485  0
                 for (loop = 0; loop < size; loop++) {
 486  0
                         if ((tmpRow = (XSLRow)aRows.get(loop)).insertBtn == aInsertBtn) {
 487  0
                                 row = tmpRow;
 488  0
                                 break;
 489  
                         }
 490  
                 }
 491  0
                 return row;
 492  
         }
 493  
         
 494  
         /**
 495  
          * Returns the row from aRows that holds the reference to aBrowseBtn
 496  
          * @param aRows
 497  
          * @param aBrowseBtn
 498  
          * @return
 499  
          */
 500  
         public static XSLRow getRowByBrowseBtn(List aRows, JButton aBrowseBtn) {
 501  
                 
 502  
                 XSLRow row, tmpRow;
 503  
                 int loop, size;
 504  
                 
 505  0
                 row = null;
 506  0
                 size = aRows.size();
 507  0
                 for (loop = 0; loop < size; loop++) {
 508  0
                         if ((tmpRow = (XSLRow)aRows.get(loop)).browseBtn == aBrowseBtn) {
 509  0
                                 row = tmpRow;
 510  0
                                 break;
 511  
                         }
 512  
                 }
 513  0
                 return row;
 514  
         }
 515  
         
 516  
         /**
 517  
          * Getter
 518  
          * @return
 519  
          */
 520  
         public JButton getBrowseBtn() {
 521  0
                 return browseBtn;
 522  
         }
 523  
 
 524  
         /**
 525  
          * Getter
 526  
          * @return
 527  
          */
 528  
         public JLabel getLabel() {
 529  0
                 return label;
 530  
         }
 531  
 
 532  
         /**
 533  
          * Getter
 534  
          * @return
 535  
          */
 536  
         public JCheckBox getRemoveCb() {
 537  0
                 return removeCb;
 538  
         }
 539  
 
 540  
         /**
 541  
          * Setter
 542  
          * @param label
 543  
          */
 544  
         public void setLabel(JLabel label) {
 545  0
                 this.label = label;
 546  0
         }
 547  
 }