Clover coverage report - clover
Coverage timestamp: Sat Oct 8 2005 22:54:17 EDT
file stats: LOC: 446   Methods: 40
NCLOC: 389   Classes: 7
 
 Source file Conditionals Statements Methods TOTAL
MyCode.java 0% 0% 0% 0%
coverage
 1   
 package example;
 2   
 
 3   
 import java.awt.*;
 4   
 import java.awt.datatransfer.*;
 5   
 import java.awt.dnd.*;
 6   
 import java.awt.event.*;
 7   
 
 8   
 import javax.swing.*;
 9   
 import javax.swing.border.*;
 10   
 import javax.swing.event.*;
 11   
 import javax.swing.tree.*;
 12   
 
 13   
 // TODO: Organize the layout with labels/tooltips/descriptions of what
 14   
 // each one represents.  Illustrate all aspects of recording a Swing UI.
 15   
 //
 16   
 // Component types and actions
 17   
 // Focus accelerators
 18   
 // Menu items
 19   
 // Static/dynamic popup menus
 20   
 // Tooltips
 21   
 // Menu/button accelerators
 22   
 // Mnemonics
 23   
 // Tab-based keyboard traversal
 24   
 // Drag/drop
 25   
 
 26   
 public class MyCode {
 27   
 
 28   
     private static class PopupAdapter extends MouseAdapter {
 29   
         private boolean dynamic;
 30   
         private JPopupMenu cachedMenu = null;
 31   
         private int invokes = 0;
 32  0
         public PopupAdapter(boolean dynamic) {
 33  0
             this.dynamic = dynamic;
 34   
         }
 35   
         /** Some platforms popup here... */
 36  0
         public void mousePressed(MouseEvent ev) {
 37  0
             maybePopup(ev);
 38   
         }
 39   
         /** And some platforms popup here... */
 40  0
         public void mouseReleased(MouseEvent ev) {
 41  0
             maybePopup(ev);
 42   
         }
 43   
         /** And just in case... */
 44  0
         public void mouseClicked(MouseEvent ev) {
 45  0
             maybePopup(ev);
 46   
         }
 47  0
         private void maybePopup(MouseEvent ev) {
 48  0
             if (ev.isPopupTrigger()) {
 49  0
                 JPopupMenu menu = getPopupMenu();
 50  0
                 menu.pack();
 51  0
                 menu.show((Component)ev.getSource(), ev.getX(), ev.getY());
 52   
             }
 53   
         }
 54  0
         private JPopupMenu getPopupMenu() {
 55  0
             JPopupMenu menu = cachedMenu;
 56  0
             if (menu == null) {
 57  0
                 menu = new JPopupMenu();
 58  0
                 if (dynamic) {
 59  0
                     menu.add(new JMenuItem("Invoked " + ++invokes + " times"));
 60  0
                     menu.add(new JSeparator());
 61   
                 }
 62  0
                 menu.add(new JMenuItem("Black"));
 63  0
                 menu.add(new JMenuItem("Blue"));
 64  0
                 menu.add(new JMenuItem("Orange"));
 65  0
                 JMenu submenu = new JMenu("Other");
 66  0
                 submenu.add(new JMenuItem("White"));
 67  0
                 submenu.add(new JMenuItem("Green"));
 68  0
                 menu.add(submenu);
 69  0
                 if (!dynamic)
 70  0
                     cachedMenu = menu;
 71   
             }
 72  0
             return menu;
 73   
         }
 74   
     }
 75   
 
 76  0
     public static void setSystemProperty(String key, String value) {
 77  0
         System.setProperty(key, value);
 78   
     }
 79   
 
 80  0
     public static void main(String[] args) {
 81   
 
 82  0
         final JFrame frame = new JFrame("My Code");
 83  0
         JPanel pane = new JPanel();
 84  0
         pane.setName("My Pane");
 85  0
         JLabel label = new JLabel("Static");
 86  0
         label.addMouseListener(new PopupAdapter(false));
 87  0
         pane.add(label);
 88  0
         label = new JLabel("Dynamic");
 89  0
         label.addMouseListener(new PopupAdapter(true));
 90  0
         pane.add(label);
 91  0
         JButton button = new JButton("Button");
 92  0
         button.addActionListener(new ActionListener() {
 93  0
             public void actionPerformed(ActionEvent ae) {
 94  0
                 JOptionPane.showMessageDialog(frame, "My Dialog Message");
 95   
             }
 96   
         });
 97  0
         pane.add(button);
 98  0
         JTextField tf = new CustomTextField("Text field");
 99  0
         tf.setFocusAccelerator('a');
 100  0
         tf.setName("My Text Field");
 101  0
         pane.add(tf);
 102  0
         JComboBox cb = new JComboBox();
 103  0
         for (int i=0;i < 20;i++)
 104  0
             cb.addItem("Combo " + i);
 105  0
         pane.add(cb);
 106   
         //pane.add(new JSpinner());
 107   
 
 108  0
         frame.addWindowListener(new WindowAdapter() {
 109  0
             public void windowClosing(WindowEvent e) {
 110  0
                 System.exit(0);
 111   
             }
 112   
         });
 113   
 
 114  0
         ActionListener al = new ActionListener() {
 115  0
             public void actionPerformed(ActionEvent ev) {
 116   
                 //System.out.println("Action: " + ev.getActionCommand());
 117   
             }
 118   
         };
 119   
 
 120  0
         String[] myListData = { "zero", "one", "two", "three",
 121   
                                 "four", "five",
 122   
                                 "six", "seven", "eight"};
 123  0
         JList myList = new JList(myListData);
 124  0
         myList.setToolTipText("This is a list");
 125  0
         myList.setName("My List");
 126  0
         myList.setVisibleRowCount(4);
 127  0
         JScrollPane myScrollPane = new JScrollPane(myList);
 128  0
         myScrollPane.setName("My ScrollPane");
 129  0
         pane.add(myScrollPane);
 130   
 
 131  0
         DragLabel dl = new DragLabel("Drag me");
 132  0
         dl.setToolTipText("You can drag this label onto the tree to the right");
 133  0
         JPanel labeled = new JPanel(new BorderLayout());
 134  0
         labeled.add(dl, BorderLayout.WEST);
 135  0
         JTree myTree = new DropTree();
 136  0
         myTree.addMouseListener(new PopupAdapter(true));
 137  0
         myTree.setEditable(true);
 138  0
         myTree.setVisibleRowCount(4);
 139  0
         JScrollPane sp = new JScrollPane(myTree);
 140  0
         sp.setBorder(new TitledBorder("Over here"));
 141  0
         labeled.add(sp);
 142  0
         pane.add(labeled);
 143   
 
 144  0
         String[][] data = new String[][] {
 145   
             { "0 one", "0 two", "0 three", "0 four" },
 146   
             { "1 one", "1 two", "1 three", "1 four" },
 147   
             { "2 one", "2 two", "2 three", "2 four" },
 148   
             { "3 one", "3 two", "3 three", "3 four" },
 149   
             { "4 one", "4 two", "4 three", "4 four" },
 150   
             { "5 one", "5 two", "5 three", "5 four" },
 151   
         };
 152  0
         String[] names = { "one", "two", "three", "four" };
 153  0
         JTable table = new JTable(data, names);
 154  0
         table.setPreferredScrollableViewportSize(new Dimension(200, myTree.getPreferredSize().height));
 155  0
         JScrollPane scroll = new JScrollPane(table);
 156  0
         pane.add(scroll);
 157   
 
 158  0
         JTextArea ta = new JTextArea("Four score and seven hundred years ago, our forebears extended claws reaching from the innermost mind to the outer limits", 10, 20);
 159  0
         ta.setFocusAccelerator('b');
 160  0
         ta.setToolTipText("<html>This is some <b>HTML</b> tooltip<br>text to look at</html>");
 161  0
         ta.setLineWrap(true);
 162  0
         ta.setWrapStyleWord(true);
 163  0
         pane.add(new JScrollPane(ta));
 164   
 
 165  0
         JTabbedPane tp = new JTabbedPane();
 166   
         // 1.4 only
 167   
         //tp.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
 168  0
         for (int i=0;i < 10;i++) {
 169  0
             tp.add("tab " + i, new JLabel("Contents " + i
 170   
                                           + "                             "));
 171   
         }
 172  0
         pane.add(tp);
 173   
 
 174  0
         frame.setContentPane(pane);
 175  0
         JMenuBar menubar = new JMenuBar();
 176  0
         menubar.setName("My Menu Bar");
 177  0
         JMenu menu = new JMenu("File");
 178   
 
 179  0
         JMenuItem mitem = new JMenuItem("Item 1");
 180  0
         KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_I,
 181   
                                               KeyEvent.ALT_MASK);
 182  0
         mitem.setAccelerator(ks);
 183  0
         menu.add(mitem);
 184  0
         mitem = new JMenuItem("Open");
 185  0
         ks = KeyStroke.getKeyStroke(KeyEvent.VK_O, KeyEvent.META_MASK);
 186  0
         mitem.setAccelerator(ks);
 187  0
         mitem.addActionListener(new ActionListener() {
 188  0
             public void actionPerformed(ActionEvent e) {
 189  0
                 new JFileChooser().showOpenDialog(null);
 190   
             }
 191   
         });
 192  0
         menu.add(mitem);
 193   
 
 194  0
         JMenu submenu = new JMenu("File submenu"); 
 195  0
         menu.add(submenu);
 196  0
         mitem = new JMenuItem("Quit");
 197  0
         ks = KeyStroke.getKeyStroke(KeyEvent.VK_Q, KeyEvent.CTRL_MASK);
 198  0
         mitem.addActionListener(new ActionListener() {
 199  0
             public void actionPerformed(ActionEvent e) {
 200  0
                 System.exit(0);
 201   
             }
 202   
         });
 203  0
         menu.add(mitem);
 204   
 
 205  0
         mitem = new JMenuItem("Submenu item");
 206  0
         mitem.addActionListener(al);
 207  0
         submenu.add(mitem);
 208   
 
 209  0
         JMenu menu2 = new JMenu("Edit");
 210  0
         mitem = new JMenuItem("Copy");
 211  0
         menu2.add(mitem);
 212   
 
 213  0
         menubar.add(menu);
 214  0
         menubar.add(menu2);
 215  0
         frame.setJMenuBar(menubar);
 216  0
         frame.pack();
 217  0
         frame.setSize(400, 400);
 218  0
         frame.show();
 219   
     }
 220   
 
 221   
 }
 222   
 
 223   
 class CustomTextField extends JTextField {
 224  0
     public CustomTextField(String contents) {
 225  0
         super(contents);
 226   
     }
 227  0
     public String getText() { 
 228  0
         return super.getText();
 229   
     }
 230   
 }
 231   
 
 232   
 class DropLabel extends JLabel {
 233   
     /** Target received drag. */
 234   
     public volatile boolean dragEntered = false;
 235   
     /** Target accepted the drop. */
 236   
     public volatile boolean dropAccepted = false;
 237   
     private DropTarget dropTarget = null;
 238   
     private DropTargetListener dtl = null;
 239   
     private boolean acceptDrops = false;
 240   
     private Color oldColor = null;
 241  0
     public DropLabel(String name) { this(name, true); }
 242  0
     public DropLabel(String name, boolean accept) {
 243  0
         super(name);
 244  0
         setName("DropLabel");
 245  0
         acceptDrops = accept;
 246  0
         dtl = new DropTargetListener() {
 247  0
                 public void dragEnter(DropTargetDragEvent e) {
 248  0
                     dragEntered = true;
 249  0
                     if (acceptDrops) {
 250  0
                         oldColor = getForeground();
 251  0
                         setForeground(Color.blue);
 252  0
                         paintImmediately(getBounds());
 253   
                     }
 254   
                 }
 255  0
                 public void dragOver(DropTargetDragEvent e) {
 256  0
                     if (acceptDrops)
 257  0
                         e.acceptDrag(e.getDropAction());
 258   
                 }
 259  0
                 public void dragExit(DropTargetEvent e) {
 260  0
                     if (acceptDrops) {
 261  0
                         setForeground(oldColor);
 262  0
                         paintImmediately(getBounds());
 263   
                     }
 264   
                 }
 265  0
                 public void dropActionChanged(DropTargetDragEvent e) {
 266  0
                     if (acceptDrops)
 267  0
                         e.acceptDrag(e.getDropAction());
 268   
                 }
 269  0
                 public void drop(DropTargetDropEvent e) {
 270  0
                     if (acceptDrops) {
 271  0
                         e.acceptDrop(e.getDropAction());
 272  0
                         e.dropComplete(true);
 273  0
                         dropAccepted = true;
 274  0
                         setForeground(oldColor);
 275  0
                         paintImmediately(getBounds());
 276   
                     }
 277   
                 }
 278   
             };
 279  0
         dropTarget = new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE,
 280   
                                     dtl, true);
 281   
     }
 282   
 
 283   
 }
 284   
 
 285   
 class DragLabel extends DropLabel {
 286   
     private class DragData implements Transferable {
 287  0
         public DataFlavor[] getTransferDataFlavors() {
 288  0
             return new DataFlavor[] {
 289   
                 DataFlavor.stringFlavor
 290   
             };
 291   
         }
 292  0
         public boolean isDataFlavorSupported(DataFlavor flavor) {
 293  0
             return true;
 294   
         }
 295  0
         public Object getTransferData(DataFlavor flavor) {
 296  0
             return getName();
 297   
         }
 298   
     }
 299   
 
 300   
     /** Drag gesture was recognized. */
 301   
     public volatile boolean dragStarted = false;
 302   
     /** Drag has left the building, er, Component. */
 303   
     public volatile boolean dragExited = false;
 304   
     /** Source registered a successful drop. */
 305   
     public volatile boolean dropSuccessful = false;
 306   
     /** Source got an indication the drag ended. */
 307   
     public volatile boolean dragEnded = false;
 308   
     public Exception exception = null;
 309   
     private DragGestureListener dgl = null;
 310   
     private DragSourceListener dsl = null;
 311   
     private DragSource dragSource = null;
 312   
     private int acceptedActions = DnDConstants.ACTION_COPY_OR_MOVE;
 313   
     private Color oldColor = null;
 314  0
     public DragLabel(String name) { this(name, true); }
 315  0
     public DragLabel(String name, final boolean acceptDrops) { 
 316  0
         super(name, acceptDrops);
 317  0
         setName("DragLabel (" + name + ")");
 318  0
         dragSource = DragSource.getDefaultDragSource();
 319  0
         dgl = new DragGestureListener() {
 320  0
                 public void dragGestureRecognized(DragGestureEvent e) {
 321  0
                     if ((e.getDragAction() & acceptedActions) == 0)
 322  0
                         return;
 323  0
                     dragStarted = true;
 324  0
                     try {
 325  0
                         e.startDrag(acceptDrops
 326   
                                     ? DragSource.DefaultCopyDrop
 327   
                                     : DragSource.DefaultCopyNoDrop,
 328   
                                     new DragData(), dsl);
 329  0
                         oldColor = getForeground();
 330  0
                         setForeground(Color.red);
 331  0
                         paintImmediately(getBounds());
 332   
                     }
 333   
                     catch(InvalidDnDOperationException idoe) {
 334  0
                         exception = idoe;
 335   
                     }
 336   
                 }
 337   
             };
 338  0
         dsl = new DragSourceListener() {
 339  0
                 public void dragDropEnd(DragSourceDropEvent e) {
 340  0
                     dropSuccessful = e.getDropSuccess();
 341  0
                     dragEnded = true;
 342  0
                     setForeground(oldColor);
 343  0
                     paintImmediately(getBounds());
 344   
                 }
 345  0
                 public void dragEnter(DragSourceDragEvent e) {
 346   
                 }
 347  0
                 public void dragOver(DragSourceDragEvent e) {
 348   
                 }
 349  0
                 public void dragExit(DragSourceEvent e) {
 350  0
                     dragExited = true;
 351   
                 }
 352  0
                 public void dropActionChanged(DragSourceDragEvent e) {
 353   
                 }
 354   
             };
 355  0
         dragSource.
 356   
             createDefaultDragGestureRecognizer(this, acceptedActions, dgl);
 357   
     }
 358   
 }
 359   
 
 360   
 class DropTree extends JTree {
 361   
     /** Target received drag. */
 362   
     public volatile boolean dragEntered = false;
 363   
     /** Target accepted the drop. */
 364   
     public volatile boolean dropAccepted = false;
 365   
     private DropTarget dropTarget = null;
 366   
     private DropTargetListener dtl = null;
 367   
     private int dropRow = -1;
 368  0
     public DropTree() {
 369  0
         setName("DropTree");
 370  0
         setCellRenderer(new DefaultTreeCellRenderer() {
 371   
             private Font originalFont;
 372   
             private Color originalColor;
 373  0
             public Component getTreeCellRendererComponent(JTree tree,
 374   
                                                           Object value,
 375   
                                                           boolean sel,
 376   
                                                           boolean exp,
 377   
                                                           boolean leaf,
 378   
                                                           int row, 
 379   
                                                           boolean focus) {
 380  0
                 Component c = super.
 381   
                     getTreeCellRendererComponent(tree, value, sel, exp,
 382   
                                                  leaf, row, focus);
 383  0
                 if (c instanceof JLabel) {
 384  0
                     JLabel label = (JLabel)c;
 385  0
                     if (originalFont == null) {
 386  0
                         originalFont = label.getFont();
 387  0
                         originalColor = label.getForeground();
 388   
                     }
 389  0
                     if (row == dropRow) {
 390  0
                         label.setForeground(Color.blue);
 391  0
                         label.setFont(label.getFont().deriveFont(Font.BOLD));
 392   
                     }
 393   
                     else {
 394  0
                         label.setForeground(originalColor);
 395  0
                         label.setFont(originalFont);
 396   
                     }
 397   
                 }
 398  0
                 return c;
 399   
             }
 400   
         });
 401  0
         dtl = new DropTargetListener() {
 402  0
             public void dragEnter(DropTargetDragEvent e) {
 403  0
                 dragEntered = true;
 404  0
                 Point where = e.getLocation();
 405  0
                 int row = getClosestRowForLocation(where.x, where.y);
 406  0
                 dropRow = row;
 407  0
                 if (row != -1) 
 408  0
                     paintImmediately(getRowBounds(row));
 409   
             }
 410  0
             public void dragOver(DropTargetDragEvent e) {
 411  0
                 e.acceptDrag(e.getDropAction());
 412  0
                 Point where = e.getLocation();
 413  0
                 int last = dropRow;
 414  0
                 dropRow = getClosestRowForLocation(where.x, where.y);
 415  0
                 if (last != -1)
 416  0
                     paintImmediately(getRowBounds(last));
 417  0
                 if (dropRow != -1) 
 418  0
                     paintImmediately(getRowBounds(dropRow));
 419   
             }
 420  0
             public void dragExit(DropTargetEvent e) {
 421  0
                 if (dropRow != -1) {
 422  0
                     int repaint = dropRow;
 423  0
                     dropRow = -1;
 424  0
                     paintImmediately(getRowBounds(repaint));
 425   
                 }
 426   
             }
 427  0
             public void dropActionChanged(DropTargetDragEvent e) {
 428  0
                 e.acceptDrag(e.getDropAction());
 429   
             }
 430  0
             public void drop(DropTargetDropEvent e) {
 431  0
                 e.acceptDrop(e.getDropAction());
 432  0
                 e.dropComplete(true);
 433  0
                 dropAccepted = true;
 434  0
                 if (dropRow != -1) {
 435  0
                     int repaint = dropRow;
 436  0
                     dropRow = -1;
 437  0
                     paintImmediately(getRowBounds(repaint));
 438   
                 }
 439   
             }
 440   
         };
 441  0
         dropTarget = new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE,
 442   
                                     dtl, true);
 443   
     }
 444   
     
 445   
 }
 446