01: package jimm.datavision.gui;
02:
03: import jimm.datavision.ErrorHandler;
04: import jimm.datavision.gui.cmd.NewDraggedFieldCommand;
05: import java.awt.Color;
06: import java.awt.Component;
07: import java.awt.dnd.*;
08: import java.awt.datatransfer.*;
09: import javax.swing.JPanel;
10:
11: /**
12: * This is the panel that holds {@link FieldWidget}s within a {@link
13: * SectionWidget}. It can handle dragged fields.
14: *
15: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
16: */
17: public class SectionFieldPanel extends JPanel implements
18: DropTargetListener {
19:
20: protected SectionWidget sectionWidget;
21:
22: /**
23: * Constructor.
24: *
25: * @param sw the section widget
26: */
27: public SectionFieldPanel(SectionWidget sw) {
28: super ();
29: sectionWidget = sw;
30: new DropTarget(this , // component
31: DnDConstants.ACTION_COPY_OR_MOVE, // actions
32: this ); // DropTargetListener
33: }
34:
35: /**
36: * Accepts fields dragged from the field picker window.
37: */
38: public void drop(DropTargetDropEvent e) {
39: try {
40: DataFlavor stringFlavor = DataFlavor.stringFlavor;
41: Transferable tr = e.getTransferable();
42: if (e.isDataFlavorSupported(stringFlavor)) {
43: addField(e, (String) tr.getTransferData(stringFlavor));
44: e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
45: e.dropComplete(true);
46: } else {
47: e.rejectDrop();
48: }
49: } catch (Exception ex) {
50: ErrorHandler.error(ex);
51: }
52: }
53:
54: public void dragEnter(DropTargetDragEvent e) {
55: }
56:
57: public void dragExit(DropTargetEvent e) {
58: }
59:
60: public void dragOver(DropTargetDragEvent e) {
61: }
62:
63: public void dropActionChanged(DropTargetDragEvent e) {
64: }
65:
66: /**
67: * Creates and adds a new field. The type and contents of the field are
68: * determined by the string that was dropped. If the field is a
69: * <code>ColumnField</code> and this is a detail section, add a text title
70: * field as well.
71: *
72: * @param e the drop event
73: * @param dropString the string received from the field picker window
74: */
75: protected void addField(DropTargetDropEvent e, String dropString) {
76: sectionWidget.performCommand(new NewDraggedFieldCommand(
77: sectionWidget, dropString, e));
78: }
79:
80: /**
81: * Performs visual changes that reflect the "hidden" state of the section.
82: * Called by the command that edits the suppression proc of a section.
83: *
84: * @param isHidden new suppressed state
85: */
86: public void setHidden(boolean isHidden) {
87: Color c = isHidden ? SectionWidget.SUPPRESSED_COLOR
88: : SectionWidget.NORMAL_COLOR;
89: setBackground(c);
90: Component[] kids = getComponents();
91: for (int i = 0; i < kids.length; ++i)
92: kids[i].setBackground(c);
93: }
94:
95: }
|