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