Clover coverage report - clover
Coverage timestamp: Sat Oct 8 2005 22:54:17 EDT
file stats: LOC: 209   Methods: 13
NCLOC: 164   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CallEditor.java 84.6% 89.7% 84.6% 88.1%
coverage coverage
 1   
 package abbot.editor.editors;
 2   
 
 3   
 import java.awt.event.*;
 4   
 import java.lang.reflect.*;
 5   
 import java.util.*;
 6   
 
 7   
 import javax.swing.*;
 8   
 
 9   
 import abbot.Log;
 10   
 import abbot.i18n.Strings;
 11   
 import abbot.script.*;
 12   
 import abbot.editor.widgets.*;
 13   
 
 14   
 /**
 15   
    Provide an editor for call steps.
 16   
    @author Blake Christensen <bchristen@users.sourceforge.net>
 17   
    @author twall@users.sourceforge.net
 18   
  */
 19   
 // TODO: add a help button for the selected method
 20   
 public class CallEditor extends StepEditor {
 21   
 
 22   
     private Call call;
 23   
     protected JTextField target;
 24   
     protected JComboBox method;
 25   
     protected ArrayEditor arguments;
 26   
     private String[] names = new String[0];
 27   
     private boolean fieldChanging;
 28   
 
 29  19
     public CallEditor(Call call) {
 30  19
         super(call);
 31  19
         this.call = call;
 32   
 
 33  19
         target = addTextField(Strings.get("TargetClass"),
 34   
                               call.getTargetClassName());
 35  19
         target.setName(TAG_CLASS);
 36   
 
 37  19
         method = addComboBox(Strings.get("Method"),
 38   
                              call.getMethodName(),
 39   
                              getMethodNames());
 40  19
         method.setName(TAG_METHOD);
 41   
 
 42  19
         arguments = addArrayEditor(Strings.get("Arguments"),
 43   
                                    call.getArguments());
 44  19
         arguments.setName(TAG_ARGS);
 45   
     }
 46   
 
 47  32
     protected Call getCall() { return call; }
 48   
 
 49   
     /** Provide a list of method names corresponding to the current target
 50   
         class.  Be careful overloading this method, since it is called from
 51   
         the Constructor.
 52   
     */
 53  54
     protected String[] getMethodNames() {
 54  54
         try {
 55  54
             Class cls = call.getTargetClass();
 56  25
             String[] names = getMethodNames(getMethods(cls, Modifier.PUBLIC));
 57  25
             Arrays.sort(names);
 58  25
             return names;
 59   
         }
 60   
         catch(ClassNotFoundException e) {
 61  29
             return new String[0];
 62   
         }
 63   
     }
 64   
     
 65  0
     protected Class getTargetClass() throws ClassNotFoundException {
 66  0
         try {
 67  0
             return call.getTargetClass();
 68   
         }
 69   
         catch(NoClassDefFoundError e) {
 70  0
             throw new ClassNotFoundException(e.getMessage());
 71   
         }
 72   
     }
 73   
 
 74   
     /** Return all public member and static methods. */
 75  32
     protected Map getMethods(Class cls, int mask) {
 76  32
         HashMap processed = new HashMap();
 77  32
         while (cls != null) {
 78  114
             Method[] methods = cls.getDeclaredMethods();
 79  114
             for (int i=0;i < methods.length;i++) {
 80  12491
                 if ((methods[i].getModifiers() & mask) == mask) {
 81  8602
                     processed.put(methods[i].getName(), methods[i]);
 82   
                 }
 83   
             }
 84  114
             cls = cls.getSuperclass();
 85   
         }
 86  32
         return processed;
 87   
     }
 88   
 
 89   
     /** Convert the given array of methods into an array of strings.
 90   
         Subclasses can do additional filtering here.
 91   
     */
 92  25
     protected String[] getMethodNames(Map methods) {
 93  25
         return (String[])methods.keySet().toArray(new String[methods.size()]);
 94   
     }
 95   
 
 96  77
     protected void validateTargetClass() {
 97  77
         try {
 98  77
             call.getTargetClass();
 99  9
             target.setForeground(DEFAULT_FOREGROUND);
 100   
         }
 101   
         catch(ClassNotFoundException e) {
 102  68
             target.setForeground(ERROR_FOREGROUND);
 103   
         }
 104   
         catch(NoClassDefFoundError e) {
 105  0
             target.setForeground(ERROR_FOREGROUND);
 106   
         }
 107   
     }
 108   
 
 109  96
     protected void validateMethod() {
 110  96
         try {
 111  96
             call.getMethod();
 112  17
             method.setForeground(DEFAULT_FOREGROUND);
 113   
         }
 114   
         catch(IllegalArgumentException e) {
 115  0
             method.setForeground(ERROR_FOREGROUND);
 116   
         }
 117   
         catch(NoSuchMethodException e) {
 118  11
             method.setForeground(ERROR_FOREGROUND);
 119   
         }
 120   
         catch(ClassNotFoundException e) {
 121  68
             method.setForeground(ERROR_FOREGROUND);
 122   
         }
 123   
         catch(NoClassDefFoundError e) {
 124  0
             target.setForeground(ERROR_FOREGROUND);
 125   
         }
 126   
     }
 127   
 
 128   
     /** Sychronize the UI with the Call data. */
 129  35
     private void availableMethodsChanged() {
 130  35
         fieldChanging = true;
 131  35
         String[] newNames = getMethodNames();
 132  35
         boolean changed = newNames.length != names.length;
 133  35
         for (int i=0;i < newNames.length && !changed;i++) {
 134  202
             changed = !newNames[i].equals(names[i]);
 135   
         }
 136  35
         if (changed) {
 137  5
             method.setModel(new DefaultComboBoxModel(newNames));
 138  5
             String name = call.getMethodName();
 139  5
             if (!name.equals(method.getSelectedItem()))
 140  5
                 method.setSelectedItem(name);
 141  5
             names = newNames;
 142   
         }
 143  35
         fieldChanging = false;
 144   
     }
 145   
 
 146   
     /** Sychronize the UI with the Call data. */
 147  6
     protected void targetClassChanged() {
 148  6
         fieldChanging = true;
 149  6
         target.setText(call.getTargetClassName());
 150  6
         fieldChanging = false;
 151  6
         availableMethodsChanged();
 152  6
         validateTargetClass();
 153  6
         validateMethod();
 154   
     }
 155   
 
 156   
     /** Sychronize the UI with the Call data. */
 157  0
     protected void methodChanged() {
 158  0
         if (!call.getMethodName().equals(method.getSelectedItem()))
 159  0
             method.setSelectedItem(call.getMethodName());
 160  0
         validateMethod();
 161   
     }
 162   
 
 163   
     /** Sychronize the UI with the Call data. */
 164  6
     protected void argumentsChanged() {
 165  6
         arguments.setValues(call.getArguments());
 166  6
         validateMethod();
 167   
     }
 168   
 
 169  98
     public void actionPerformed(ActionEvent ev) {
 170  98
         if (fieldChanging)
 171  19
             return;
 172   
 
 173  79
         Object src = ev.getSource();
 174  79
         if (src == target) {
 175  29
             String cname = target.getText().trim();
 176  29
             if (!cname.equals(call.getTargetClassName())) {
 177  29
                 call.setTargetClassName(cname);
 178  29
                 availableMethodsChanged();
 179  29
                 validateTargetClass();
 180  29
                 validateMethod();
 181  29
                 fireStepChanged();
 182   
             }
 183   
         }
 184  50
         else if (src == method) {
 185  12
             String name = (String)method.getSelectedItem();
 186  12
             if (!name.equals(call.getMethodName())) {
 187  8
                 call.setMethodName(name);
 188  8
                 validateMethod();
 189  8
                 fireStepChanged();
 190   
             }
 191   
         }
 192  38
         else if (src == arguments) {
 193   
             // FIXME check method signature and do component field if the
 194   
             // first arg is a component, do popup from available refs
 195   
             // FIXME check arguments against method signature
 196  5
             Object[] values = arguments.getValues();
 197  5
             String[] svalues = new String[values.length];
 198  5
             System.arraycopy(values, 0, svalues, 0, values.length);
 199  5
             call.setArguments(svalues);
 200  5
             validateMethod();
 201  5
             fireStepChanged();
 202   
         }
 203   
         else {
 204  33
             super.actionPerformed(ev);
 205   
         }
 206   
     }
 207   
 }
 208   
 
 209