1 | package org.expeditee.items.widgets;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Graphics;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.HashSet;
|
---|
7 |
|
---|
8 | import javax.swing.JComponent;
|
---|
9 |
|
---|
10 | import org.expeditee.gui.DisplayIO;
|
---|
11 | import org.expeditee.gui.Frame;
|
---|
12 | import org.expeditee.gui.FrameIO;
|
---|
13 | import org.expeditee.gui.FrameObserver;
|
---|
14 | import org.expeditee.items.Item;
|
---|
15 | import org.expeditee.items.ItemParentStateChangedEvent;
|
---|
16 | import org.expeditee.items.Text;
|
---|
17 |
|
---|
18 | public abstract class DataFrameWidget extends InteractiveWidget implements
|
---|
19 | FrameObserver {
|
---|
20 |
|
---|
21 | private boolean _needsUpdating;
|
---|
22 |
|
---|
23 | protected Collection<Frame> _subjects = new HashSet<Frame>();
|
---|
24 |
|
---|
25 | protected Frame _dataFrame;
|
---|
26 |
|
---|
27 | protected DataFrameWidget(Text source, JComponent component, int minWidth,
|
---|
28 | int maxWidth, int minHeight, int maxHeight) {
|
---|
29 | this(source, component, minWidth, minWidth, maxWidth, minHeight,
|
---|
30 | minHeight, maxHeight);
|
---|
31 | }
|
---|
32 |
|
---|
33 | public DataFrameWidget(Text source, JComponent component, int minWidth,
|
---|
34 | int defaultWidth, int maxWidth, int minHeight, int defaultHeight,
|
---|
35 | int maxHeight) {
|
---|
36 | super(source, component, minWidth, maxWidth, minHeight, maxHeight);
|
---|
37 | setSize(defaultWidth, defaultHeight);
|
---|
38 |
|
---|
39 | String link = source.getAbsoluteLink();
|
---|
40 | _dataFrame = link != null ? FrameIO.LoadFrame(link) : null;
|
---|
41 | if (_dataFrame != null) {
|
---|
42 | addSubject(_dataFrame);
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | protected void setDataFrame(Frame newDataFrame) {
|
---|
47 | _dataFrame = newDataFrame;
|
---|
48 | }
|
---|
49 |
|
---|
50 | public Frame getDataFrame() {
|
---|
51 | if (_dataFrame == null && getSource().getLink() != null) {
|
---|
52 | _dataFrame = FrameIO.LoadFrame(getSource().getAbsoluteLink());
|
---|
53 | if (_dataFrame != null) {
|
---|
54 | addSubject(_dataFrame);
|
---|
55 | } else {
|
---|
56 | /**
|
---|
57 | * If the dataFrame has not been saved yet because it has just
|
---|
58 | * been created via TDFC this widget needs to be marked as
|
---|
59 | * changed so it will be refreshed when the user goes back
|
---|
60 | */
|
---|
61 | assert(false);
|
---|
62 | //_needsUpdating = true;
|
---|
63 | // update();
|
---|
64 | }
|
---|
65 | }
|
---|
66 | return _dataFrame;
|
---|
67 | }
|
---|
68 |
|
---|
69 | protected void clearSubjects() {
|
---|
70 | for (Frame frame : _subjects) {
|
---|
71 | frame.removeObserver(this);
|
---|
72 | }
|
---|
73 | _subjects.clear();
|
---|
74 | }
|
---|
75 |
|
---|
76 | public void removeSubject(Frame frame) {
|
---|
77 | assert (frame != null);
|
---|
78 | _subjects.remove(frame);
|
---|
79 | frame.removeObserver(this);
|
---|
80 | // Reset the dataFrame if it is being removed from the cache to avoid
|
---|
81 | // memory leaks
|
---|
82 | if (frame == _dataFrame)
|
---|
83 | _dataFrame = null;
|
---|
84 | }
|
---|
85 |
|
---|
86 | public void addSubject(Frame frame) {
|
---|
87 | assert (frame != null);
|
---|
88 | _subjects.add(frame);
|
---|
89 | frame.addObserver(this);
|
---|
90 | }
|
---|
91 |
|
---|
92 | public boolean needsRefresh() {
|
---|
93 | return _needsUpdating;
|
---|
94 | }
|
---|
95 |
|
---|
96 | public void refresh() {
|
---|
97 | _needsUpdating = false;
|
---|
98 | }
|
---|
99 |
|
---|
100 | public void update() {
|
---|
101 | Frame parent = getParentFrame();
|
---|
102 | if (parent != null && parent == DisplayIO.getCurrentFrame()) {
|
---|
103 | refresh();
|
---|
104 | }
|
---|
105 |
|
---|
106 | _needsUpdating = true;
|
---|
107 | }
|
---|
108 |
|
---|
109 | @Override
|
---|
110 | public void setLink(String link, Text linker) {
|
---|
111 | String newLink = Item.convertToAbsoluteLink(link);
|
---|
112 | String oldLink = getSource().getAbsoluteLink();
|
---|
113 | if ((newLink == null && oldLink == null)
|
---|
114 | || (newLink != null && newLink.equals(oldLink)))
|
---|
115 | return;
|
---|
116 | super.setLink(link, linker);
|
---|
117 | clearSubjects();
|
---|
118 | setDataFrame(null);
|
---|
119 | if (oldLink == null) {
|
---|
120 | _needsUpdating = true;
|
---|
121 | } else {
|
---|
122 | refresh();
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | @Override
|
---|
127 | protected void onParentStateChanged(int eventType) {
|
---|
128 |
|
---|
129 | switch (eventType) {
|
---|
130 |
|
---|
131 | case ItemParentStateChangedEvent.EVENT_TYPE_ADDED:
|
---|
132 | case ItemParentStateChangedEvent.EVENT_TYPE_ADDED_VIA_OVERLAY:
|
---|
133 | case ItemParentStateChangedEvent.EVENT_TYPE_SHOWN:
|
---|
134 | case ItemParentStateChangedEvent.EVENT_TYPE_SHOWN_VIA_OVERLAY:
|
---|
135 | if (needsRefresh()) {
|
---|
136 | refresh();
|
---|
137 | }
|
---|
138 | break;
|
---|
139 |
|
---|
140 | case ItemParentStateChangedEvent.EVENT_TYPE_REMOVED:
|
---|
141 | case ItemParentStateChangedEvent.EVENT_TYPE_REMOVED_VIA_OVERLAY:
|
---|
142 | case ItemParentStateChangedEvent.EVENT_TYPE_HIDDEN:
|
---|
143 | break;
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | protected void paintInFreeSpace(Graphics g) {
|
---|
148 | super.paintInFreeSpace(g);
|
---|
149 | g.setFont(((Text) getSource()).getFont());
|
---|
150 | g.setColor(Color.WHITE);
|
---|
151 | g.drawString(this.getClass().getSimpleName(), getX() + 10, getY() + 20);
|
---|
152 |
|
---|
153 | }
|
---|
154 | }
|
---|