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

Save This Page
Home » hibernate-search-src-20081106 » example » [javadoc | source]