1 | /**
|
---|
2 | * DataFrameWidget.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.items.widgets;
|
---|
20 |
|
---|
21 | import java.util.Collection;
|
---|
22 | import java.util.HashSet;
|
---|
23 |
|
---|
24 | import javax.swing.JComponent;
|
---|
25 |
|
---|
26 | import org.expeditee.core.Colour;
|
---|
27 | import org.expeditee.core.Point;
|
---|
28 | import org.expeditee.gio.EcosystemManager;
|
---|
29 | import org.expeditee.gio.GraphicsManager;
|
---|
30 | import org.expeditee.gui.DisplayController;
|
---|
31 | import org.expeditee.gui.Frame;
|
---|
32 | import org.expeditee.gui.FrameIO;
|
---|
33 | import org.expeditee.gui.FrameObserver;
|
---|
34 | import org.expeditee.items.Item;
|
---|
35 | import org.expeditee.items.ItemParentStateChangedEvent;
|
---|
36 | import org.expeditee.items.Text;
|
---|
37 |
|
---|
38 | public abstract class DataFrameWidget extends SwingWidget implements
|
---|
39 | FrameObserver {
|
---|
40 |
|
---|
41 | private boolean _needsUpdating;
|
---|
42 |
|
---|
43 | protected Collection<Frame> _subjects = new HashSet<Frame>();
|
---|
44 |
|
---|
45 | protected Frame _dataFrame;
|
---|
46 |
|
---|
47 | protected DataFrameWidget(Text source, JComponent component, int minWidth,
|
---|
48 | int maxWidth, int minHeight, int maxHeight) {
|
---|
49 | this(source, component, minWidth, minWidth, maxWidth, minHeight,
|
---|
50 | minHeight, maxHeight);
|
---|
51 | }
|
---|
52 |
|
---|
53 | public DataFrameWidget(Text source, JComponent component, int minWidth,
|
---|
54 | int defaultWidth, int maxWidth, int minHeight, int defaultHeight,
|
---|
55 | int maxHeight) {
|
---|
56 | super(source, component, minWidth, maxWidth, minHeight, maxHeight);
|
---|
57 | setSize(defaultWidth, defaultHeight);
|
---|
58 |
|
---|
59 | String link = source.getAbsoluteLink();
|
---|
60 | _dataFrame = link != null ? FrameIO.LoadFrame(link) : null;
|
---|
61 | if (_dataFrame != null) {
|
---|
62 | addSubject(_dataFrame);
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | protected void setDataFrame(Frame newDataFrame) {
|
---|
67 | _dataFrame = newDataFrame;
|
---|
68 | }
|
---|
69 |
|
---|
70 | public Frame getDataFrame() {
|
---|
71 | if (_dataFrame == null && getSource().getLink() != null) {
|
---|
72 | _dataFrame = FrameIO.LoadFrame(getSource().getAbsoluteLink());
|
---|
73 | if (_dataFrame != null) {
|
---|
74 | addSubject(_dataFrame);
|
---|
75 | } else {
|
---|
76 | /**
|
---|
77 | * If the dataFrame has not been saved yet because it has just
|
---|
78 | * been created via TDFC this widget needs to be marked as
|
---|
79 | * changed so it will be refreshed when the user goes back
|
---|
80 | */
|
---|
81 | assert (false);
|
---|
82 | // _needsUpdating = true;
|
---|
83 | // update();
|
---|
84 | }
|
---|
85 | }
|
---|
86 | return _dataFrame;
|
---|
87 | }
|
---|
88 |
|
---|
89 | protected void clearSubjects() {
|
---|
90 | for (Frame frame : _subjects) {
|
---|
91 | frame.removeObserver(this);
|
---|
92 | }
|
---|
93 | _subjects.clear();
|
---|
94 | }
|
---|
95 |
|
---|
96 | public void removeSubject(Frame frame) {
|
---|
97 | assert (frame != null);
|
---|
98 | _subjects.remove(frame);
|
---|
99 | frame.removeObserver(this);
|
---|
100 | // Reset the dataFrame if it is being removed from the cache to avoid
|
---|
101 | // memory leaks
|
---|
102 | if (frame == _dataFrame)
|
---|
103 | _dataFrame = null;
|
---|
104 | }
|
---|
105 |
|
---|
106 | public void addSubject(Frame frame) {
|
---|
107 | assert (frame != null);
|
---|
108 | _subjects.add(frame);
|
---|
109 | frame.addObserver(this);
|
---|
110 | }
|
---|
111 |
|
---|
112 | public boolean needsRefresh() {
|
---|
113 | return _needsUpdating;
|
---|
114 | }
|
---|
115 |
|
---|
116 | public void refresh() {
|
---|
117 | _needsUpdating = false;
|
---|
118 | }
|
---|
119 |
|
---|
120 | public void update() {
|
---|
121 | Frame parent = getParentFrame();
|
---|
122 | if (parent != null && parent == DisplayController.getCurrentFrame()) {
|
---|
123 | refresh();
|
---|
124 | }
|
---|
125 |
|
---|
126 | _needsUpdating = true;
|
---|
127 | }
|
---|
128 |
|
---|
129 | @Override
|
---|
130 | public void setLink(String link, Text linker) {
|
---|
131 | String newLink = Item.convertToAbsoluteLink(link);
|
---|
132 | String oldLink = getSource().getAbsoluteLink();
|
---|
133 | if ((newLink == null && oldLink == null)
|
---|
134 | || (newLink != null && newLink.equals(oldLink)))
|
---|
135 | return;
|
---|
136 | super.setLink(link, linker);
|
---|
137 | clearSubjects();
|
---|
138 | setDataFrame(null);
|
---|
139 | if (oldLink == null) {
|
---|
140 | _needsUpdating = true;
|
---|
141 | // Need to refresh imediately so that the data appears when adding a
|
---|
142 | // link to a graph
|
---|
143 | refresh();
|
---|
144 | } else {
|
---|
145 | refresh();
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | protected void onParentStateChanged(int eventType) {
|
---|
151 |
|
---|
152 | switch (eventType) {
|
---|
153 |
|
---|
154 | case ItemParentStateChangedEvent.EVENT_TYPE_ADDED:
|
---|
155 | case ItemParentStateChangedEvent.EVENT_TYPE_ADDED_VIA_OVERLAY:
|
---|
156 | case ItemParentStateChangedEvent.EVENT_TYPE_SHOWN:
|
---|
157 | case ItemParentStateChangedEvent.EVENT_TYPE_SHOWN_VIA_OVERLAY:
|
---|
158 | if (needsRefresh()) {
|
---|
159 | refresh();
|
---|
160 | }
|
---|
161 | break;
|
---|
162 |
|
---|
163 | case ItemParentStateChangedEvent.EVENT_TYPE_REMOVED:
|
---|
164 | case ItemParentStateChangedEvent.EVENT_TYPE_REMOVED_VIA_OVERLAY:
|
---|
165 | case ItemParentStateChangedEvent.EVENT_TYPE_HIDDEN:
|
---|
166 | break;
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | protected void paintInFreeSpace()
|
---|
171 | {
|
---|
172 | super.paintInFreeSpace();
|
---|
173 | GraphicsManager g = EcosystemManager.getGraphicsManager();
|
---|
174 | g.drawString(this.getClass().getSimpleName(), new Point(getX() + 10, getY() + 20), ((Text) getSource()).getFont(), Colour.WHITE);
|
---|
175 |
|
---|
176 | }
|
---|
177 | }
|
---|