001: package example;
002:
003: import java.awt.*;
004: import java.awt.datatransfer.*;
005: import java.awt.dnd.*;
006: import java.awt.event.*;
007:
008: import javax.swing.*;
009: import javax.swing.table.*;
010: import javax.swing.border.*;
011: import javax.swing.tree.*;
012:
013: // TODO: Organize the layout with labels/tooltips/descriptions of what
014: // each one represents. Illustrate all aspects of recording a Swing UI.
015: //
016: // Component types and actions
017: // Focus accelerators
018: // Menu items
019: // Static/dynamic popup menus
020: // Tooltips
021: // Menu/button accelerators
022: // Mnemonics
023: // Tab-based keyboard traversal
024: // Drag/drop
025:
026: public class MyCode {
027:
028: private static class PopupAdapter extends MouseAdapter {
029: private boolean dynamic;
030: private JPopupMenu cachedMenu = null;
031: private int invokes = 0;
032:
033: public PopupAdapter(boolean dynamic) {
034: this .dynamic = dynamic;
035: }
036:
037: /** Some platforms popup here... */
038: public void mousePressed(MouseEvent ev) {
039: maybePopup(ev);
040: }
041:
042: /** And some platforms popup here... */
043: public void mouseReleased(MouseEvent ev) {
044: maybePopup(ev);
045: }
046:
047: /** And just in case... */
048: public void mouseClicked(MouseEvent ev) {
049: maybePopup(ev);
050: }
051:
052: private void maybePopup(MouseEvent ev) {
053: if (ev.isPopupTrigger()) {
054: JPopupMenu menu = getPopupMenu();
055: menu.pack();
056: menu.show((Component) ev.getSource(), ev.getX(), ev
057: .getY());
058: }
059: }
060:
061: private JPopupMenu getPopupMenu() {
062: JPopupMenu menu = cachedMenu;
063: if (menu == null) {
064: menu = new JPopupMenu();
065: if (dynamic) {
066: menu.add(new JMenuItem("Invoked " + ++invokes
067: + " times"));
068: menu.add(new JSeparator());
069: }
070: menu.add(new JMenuItem("Black"));
071: menu.add(new JMenuItem("Blue"));
072: menu.add(new JMenuItem("Orange"));
073: JMenu submenu = new JMenu("Other");
074: submenu.add(new JMenuItem("White"));
075: submenu.add(new JMenuItem("Green"));
076: menu.add(submenu);
077: if (!dynamic)
078: cachedMenu = menu;
079: }
080: return menu;
081: }
082: }
083:
084: public static void setSystemProperty(String key, String value) {
085: System.setProperty(key, value);
086: }
087:
088: public static void main(String[] args) {
089:
090: final JFrame frame = new JFrame("My Code");/* {
091: // This will cause a NPE in the hierarchy browser
092: private String name = "My Code Frame";
093: public String getName() { return name.toString(); }
094: };*/
095: JPanel pane = new JPanel();
096: pane.setName("My Pane");
097: JLabel label = new JLabel("Static");
098: label.addMouseListener(new PopupAdapter(false));
099: pane.add(label);
100: label = new JLabel("Dynamic");
101: label.addMouseListener(new PopupAdapter(true));
102: pane.add(label);
103: JButton button = new JButton("Button");
104: button.addActionListener(new ActionListener() {
105: public void actionPerformed(ActionEvent ae) {
106: JOptionPane.showMessageDialog(frame,
107: "My Dialog Message");
108: }
109: });
110: pane.add(button);
111: JTextField tf = new CustomTextField("Text field");
112: tf.setFocusAccelerator('a');
113: tf.setName("My Text Field");
114: pane.add(tf);
115: JComboBox cb = new JComboBox();
116: for (int i = 0; i < 20; i++)
117: cb.addItem("Combo " + i);
118: pane.add(cb);
119: //pane.add(new JSpinner());
120:
121: frame.addWindowListener(new WindowAdapter() {
122: public void windowClosing(WindowEvent e) {
123: System.exit(0);
124: }
125: });
126:
127: ActionListener al = new ActionListener() {
128: public void actionPerformed(ActionEvent ev) {
129: //System.out.println("Action: " + ev.getActionCommand());
130: }
131: };
132:
133: String[] myListData = { "zero", "one", "two", "three", "four",
134: "five", "six", "seven", "eight" };
135: JList myList = new JList(myListData);
136: myList.setToolTipText("This is a list");
137: myList.setName("My List");
138: myList.setVisibleRowCount(4);
139: JScrollPane myScrollPane = new JScrollPane(myList);
140: myScrollPane.setName("My ScrollPane");
141: pane.add(myScrollPane);
142:
143: DragLabel dl = new DragLabel("Drag me");
144: dl
145: .setToolTipText("You can drag this label onto the tree to the right");
146: JPanel labeled = new JPanel(new BorderLayout());
147: labeled.add(dl, BorderLayout.WEST);
148: JTree myTree = new DropTree();
149: myTree.addMouseListener(new PopupAdapter(true));
150: myTree.setEditable(true);
151: myTree.setVisibleRowCount(4);
152: JScrollPane sp = new JScrollPane(myTree);
153: sp.setBorder(new TitledBorder("Over here"));
154: labeled.add(sp);
155: pane.add(labeled);
156:
157: final Object[][] data = new Object[][] {
158: { "0 one", "0 two", "0 three", Boolean.TRUE },
159: { "1 one", "1 two", "1 three", Boolean.FALSE },
160: { "2 one", "2 two", "2 three", Boolean.TRUE },
161: { "3 one", "3 two", "3 three", Boolean.TRUE },
162: { "4 one", "4 two", "4 three", Boolean.TRUE },
163: { "5 one", "5 two", "5 three", Boolean.FALSE }, };
164: String[] names = { "one", "two", "three", "four" };
165: TableModel model = new DefaultTableModel(data, names) {
166: public Class getColumnClass(int col) {
167: return col == 3 ? Boolean.class : String.class;
168: }
169: };
170: JTable table = new JTable(model);
171: table.setPreferredScrollableViewportSize(new Dimension(200,
172: myTree.getPreferredSize().height));
173: JScrollPane scroll = new JScrollPane(table);
174: pane.add(scroll);
175:
176: JTextArea ta = new JTextArea(
177: "Four score and seven hundred years ago, our forebears extended claws reaching from the innermost mind to the outer limits",
178: 10, 20);
179: ta.setFocusAccelerator('b');
180: ta
181: .setToolTipText("<html>This is some <b>HTML</b> tooltip<br>text to look at</html>");
182: ta.setLineWrap(true);
183: ta.setWrapStyleWord(true);
184: pane.add(new JScrollPane(ta));
185:
186: JTabbedPane tp = new JTabbedPane();
187: // 1.4 only
188: //tp.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
189: for (int i = 0; i < 10; i++) {
190: tp.add("tab " + i, new JLabel("Contents " + i
191: + " "));
192: }
193: pane.add(tp);
194: pane.add(new JCheckBox("check box"));
195:
196: frame.setContentPane(pane);
197: JMenuBar menubar = new JMenuBar();
198: menubar.setName("My Menu Bar");
199: JMenu menu = new JMenu("File");
200:
201: JMenuItem mitem = new JMenuItem("Item 1");
202: KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_I,
203: KeyEvent.ALT_MASK);
204: mitem.setAccelerator(ks);
205: menu.add(mitem);
206: mitem = new JMenuItem("Open");
207: ks = KeyStroke.getKeyStroke(KeyEvent.VK_O, KeyEvent.META_MASK);
208: mitem.setAccelerator(ks);
209: mitem.addActionListener(new ActionListener() {
210: public void actionPerformed(ActionEvent e) {
211: new JFileChooser().showOpenDialog(null);
212: }
213: });
214: menu.add(mitem);
215:
216: JMenu submenu = new JMenu("File submenu");
217: menu.add(submenu);
218: mitem = new JMenuItem("Quit");
219: ks = KeyStroke.getKeyStroke(KeyEvent.VK_Q, KeyEvent.CTRL_MASK);
220: mitem.addActionListener(new ActionListener() {
221: public void actionPerformed(ActionEvent e) {
222: System.exit(0);
223: }
224: });
225: menu.add(mitem);
226:
227: mitem = new JMenuItem("Submenu item");
228: mitem.addActionListener(al);
229: submenu.add(mitem);
230:
231: JMenu menu2 = new JMenu("Edit");
232: mitem = new JMenuItem("Copy");
233: menu2.add(mitem);
234:
235: menubar.add(menu);
236: menubar.add(menu2);
237: frame.setJMenuBar(menubar);
238: frame.pack();
239: frame.setSize(400, 400);
240: frame.setVisible(true);
241: }
242:
243: }
244:
245: class CustomTextField extends JTextField {
246: public CustomTextField(String contents) {
247: super (contents);
248: }
249:
250: public String getText() {
251: return super .getText();
252: }
253: }
254:
255: class DropLabel extends JLabel {
256: /** Target received drag. */
257: public volatile boolean dragEntered = false;
258: /** Target accepted the drop. */
259: public volatile boolean dropAccepted = false;
260: private DropTargetListener dtl = null;
261: private boolean acceptDrops = false;
262: private Color oldColor = null;
263:
264: public DropLabel(String name) {
265: this (name, true);
266: }
267:
268: public DropLabel(String name, boolean accept) {
269: super (name);
270: setName("DropLabel");
271: acceptDrops = accept;
272: dtl = new DropTargetListener() {
273: public void dragEnter(DropTargetDragEvent e) {
274: dragEntered = true;
275: if (acceptDrops) {
276: oldColor = getForeground();
277: setForeground(Color.blue);
278: paintImmediately(getBounds());
279: }
280: }
281:
282: public void dragOver(DropTargetDragEvent e) {
283: if (acceptDrops)
284: e.acceptDrag(e.getDropAction());
285: }
286:
287: public void dragExit(DropTargetEvent e) {
288: if (acceptDrops) {
289: setForeground(oldColor);
290: paintImmediately(getBounds());
291: }
292: }
293:
294: public void dropActionChanged(DropTargetDragEvent e) {
295: if (acceptDrops)
296: e.acceptDrag(e.getDropAction());
297: }
298:
299: public void drop(DropTargetDropEvent e) {
300: if (acceptDrops) {
301: e.acceptDrop(e.getDropAction());
302: e.dropComplete(true);
303: dropAccepted = true;
304: setForeground(oldColor);
305: paintImmediately(getBounds());
306: }
307: }
308: };
309: new DropTarget(this , DnDConstants.ACTION_COPY_OR_MOVE, dtl,
310: true);
311: }
312:
313: }
314:
315: class DragLabel extends DropLabel {
316: private class DragData implements Transferable {
317: public DataFlavor[] getTransferDataFlavors() {
318: return new DataFlavor[] { DataFlavor.stringFlavor };
319: }
320:
321: public boolean isDataFlavorSupported(DataFlavor flavor) {
322: return true;
323: }
324:
325: public Object getTransferData(DataFlavor flavor) {
326: return getName();
327: }
328: }
329:
330: /** Drag gesture was recognized. */
331: public volatile boolean dragStarted = false;
332: /** Drag has left the building, er, Component. */
333: public volatile boolean dragExited = false;
334: /** Source registered a successful drop. */
335: public volatile boolean dropSuccessful = false;
336: /** Source got an indication the drag ended. */
337: public volatile boolean dragEnded = false;
338: public Exception exception = null;
339: private DragGestureListener dgl = null;
340: private DragSourceListener dsl = null;
341: private DragSource dragSource = null;
342: private int acceptedActions = DnDConstants.ACTION_COPY_OR_MOVE;
343: private Color oldColor = null;
344:
345: public DragLabel(String name) {
346: this (name, true);
347: }
348:
349: public DragLabel(String name, final boolean acceptDrops) {
350: super (name, acceptDrops);
351: setName("DragLabel (" + name + ")");
352: dragSource = DragSource.getDefaultDragSource();
353: dgl = new DragGestureListener() {
354: public void dragGestureRecognized(DragGestureEvent e) {
355: if ((e.getDragAction() & acceptedActions) == 0)
356: return;
357: dragStarted = true;
358: try {
359: e.startDrag(
360: acceptDrops ? DragSource.DefaultCopyDrop
361: : DragSource.DefaultCopyNoDrop,
362: new DragData(), dsl);
363: oldColor = getForeground();
364: setForeground(Color.red);
365: paintImmediately(getBounds());
366: } catch (InvalidDnDOperationException idoe) {
367: exception = idoe;
368: }
369: }
370: };
371: dsl = new DragSourceListener() {
372: public void dragDropEnd(DragSourceDropEvent e) {
373: dropSuccessful = e.getDropSuccess();
374: dragEnded = true;
375: setForeground(oldColor);
376: paintImmediately(getBounds());
377: }
378:
379: public void dragEnter(DragSourceDragEvent e) {
380: }
381:
382: public void dragOver(DragSourceDragEvent e) {
383: }
384:
385: public void dragExit(DragSourceEvent e) {
386: dragExited = true;
387: }
388:
389: public void dropActionChanged(DragSourceDragEvent e) {
390: }
391: };
392: dragSource.createDefaultDragGestureRecognizer(this ,
393: acceptedActions, dgl);
394: }
395: }
396:
397: class DropTree extends JTree {
398: /** Target received drag. */
399: public volatile boolean dragEntered = false;
400: /** Target accepted the drop. */
401: public volatile boolean dropAccepted = false;
402: private DropTargetListener dtl = null;
403: private int dropRow = -1;
404:
405: public DropTree() {
406: setName("DropTree");
407: setCellRenderer(new DefaultTreeCellRenderer() {
408: private Font originalFont;
409: private Color originalColor;
410:
411: public Component getTreeCellRendererComponent(JTree tree,
412: Object value, boolean sel, boolean exp,
413: boolean leaf, int row, boolean focus) {
414: Component c = super .getTreeCellRendererComponent(tree,
415: value, sel, exp, leaf, row, focus);
416: if (c instanceof JLabel) {
417: JLabel label = (JLabel) c;
418: if (originalFont == null) {
419: originalFont = label.getFont();
420: originalColor = label.getForeground();
421: }
422: if (row == dropRow) {
423: label.setForeground(Color.blue);
424: label.setFont(label.getFont().deriveFont(
425: Font.BOLD));
426: } else {
427: label.setForeground(originalColor);
428: label.setFont(originalFont);
429: }
430: }
431: return c;
432: }
433: });
434: dtl = new DropTargetListener() {
435: public void dragEnter(DropTargetDragEvent e) {
436: dragEntered = true;
437: Point where = e.getLocation();
438: int row = getClosestRowForLocation(where.x, where.y);
439: dropRow = row;
440: if (row != -1)
441: paintImmediately(getRowBounds(row));
442: }
443:
444: public void dragOver(DropTargetDragEvent e) {
445: e.acceptDrag(e.getDropAction());
446: Point where = e.getLocation();
447: int last = dropRow;
448: dropRow = getClosestRowForLocation(where.x, where.y);
449: if (last != -1)
450: paintImmediately(getRowBounds(last));
451: if (dropRow != -1)
452: paintImmediately(getRowBounds(dropRow));
453: }
454:
455: public void dragExit(DropTargetEvent e) {
456: if (dropRow != -1) {
457: int repaint = dropRow;
458: dropRow = -1;
459: paintImmediately(getRowBounds(repaint));
460: }
461: }
462:
463: public void dropActionChanged(DropTargetDragEvent e) {
464: e.acceptDrag(e.getDropAction());
465: }
466:
467: public void drop(DropTargetDropEvent e) {
468: e.acceptDrop(e.getDropAction());
469: e.dropComplete(true);
470: dropAccepted = true;
471: if (dropRow != -1) {
472: int repaint = dropRow;
473: dropRow = -1;
474: paintImmediately(getRowBounds(repaint));
475: }
476: }
477: };
478: new DropTarget(this , DnDConstants.ACTION_COPY_OR_MOVE, dtl,
479: true);
480: }
481:
482: }
|