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