KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > gui > cmd > FieldClipping


1 package jimm.datavision.gui.cmd;
2 import jimm.datavision.Report;
3 import jimm.datavision.SectionArea;
4 import jimm.datavision.Section;
5 import jimm.datavision.field.Field;
6 import jimm.datavision.gui.Designer;
7 import jimm.datavision.gui.FieldWidget;
8 import jimm.datavision.gui.SectionWidget;
9
10 /**
11  * A field clipping gets copied to the clipboard when a field widget is
12  * cut. It contains not only a field widget but also the widget's original
13  * section area.
14  *
15  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
16  */

17 public class FieldClipping implements Pasteable {
18
19 protected Field origField;
20 protected FieldWidget newWidget;
21 protected SectionWidget origSectionWidget;
22 protected SectionArea sectionArea;
23 protected SectionResizeCommand sectionResizeCommand;
24
25 public FieldClipping(FieldWidget fw) {
26     origField = fw.getField();
27
28     // Remember the original section widget so we can attempt to put it
29
// there again, even if this field widget gets moved in the mean time.
30
origSectionWidget = fw.getSectionWidget();
31
32     // In case the original section no longer exists, or we are pasting
33
// into a different report, remember the area of the section so we
34
// can paste into the same area later.
35
sectionArea = origSectionWidget.getSectionArea();
36 }
37
38 public void paste(Designer designer) {
39     Report pasteReport = designer.getReport();
40     Report origReport = origSectionWidget.getReport();
41     SectionWidget sw = null;
42
43     if (pasteReport == origReport) {
44     if (pasteReport.contains(origSectionWidget.getSection()))
45         sw = origSectionWidget;
46     else
47         sw = sectionWidgetBySectionArea(designer);
48     }
49     else { // Different reports; go by section area
50
sw = sectionWidgetBySectionArea(designer);
51     }
52
53
54     Field newField = (Field)origField.clone();
55     newWidget = newField.makeWidget(sw);
56
57     sectionResizeCommand = new SectionResizeCommand(sw);
58     newWidget.moveToSection(sw); // Possibly resizes section
59
sectionResizeCommand.perform();
60
61     designer.select(newWidget, true, false);
62
63     // Don't need to call Designer.enableMenuItems because each call
64
// to Designer.select already does that.
65
}
66
67 protected SectionWidget sectionWidgetBySectionArea(Designer designer) {
68     Report report = designer.getReport();
69     Section s = report.getFirstSectionByArea(sectionArea.getArea());
70     if (s == null) // Can be null if area was group, for example
71
// Will not be null; there is always at least one report header section
72
s = report.getFirstSectionByArea(SectionArea.REPORT_HEADER);
73     return designer.findSectionWidgetFor(s);
74 }
75
76 public void undo(Designer designer) {
77     newWidget.doDelete(); // Widget deletes itself and field from report
78
sectionResizeCommand.undo();
79     designer.enableMenuItems();
80 }
81
82 }
83
Popular Tags