[919] | 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 |
|
---|
[214] | 19 | package org.expeditee.items.widgets;
|
---|
| 20 |
|
---|
| 21 | import java.util.Collection;
|
---|
| 22 | import java.util.HashSet;
|
---|
| 23 |
|
---|
| 24 | import javax.swing.JComponent;
|
---|
| 25 |
|
---|
[1102] | 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;
|
---|
[214] | 31 | import org.expeditee.gui.Frame;
|
---|
| 32 | import org.expeditee.gui.FrameIO;
|
---|
| 33 | import org.expeditee.gui.FrameObserver;
|
---|
[220] | 34 | import org.expeditee.items.Item;
|
---|
| 35 | import org.expeditee.items.ItemParentStateChangedEvent;
|
---|
[214] | 36 | import org.expeditee.items.Text;
|
---|
| 37 |
|
---|
[1102] | 38 | public abstract class DataFrameWidget extends SwingWidget implements
|
---|
[214] | 39 | FrameObserver {
|
---|
| 40 |
|
---|
[220] | 41 | private boolean _needsUpdating;
|
---|
[214] | 42 |
|
---|
| 43 | protected Collection<Frame> _subjects = new HashSet<Frame>();
|
---|
[220] | 44 |
|
---|
[214] | 45 | protected Frame _dataFrame;
|
---|
| 46 |
|
---|
[220] | 47 | protected DataFrameWidget(Text source, JComponent component, int minWidth,
|
---|
| 48 | int maxWidth, int minHeight, int maxHeight) {
|
---|
[214] | 49 | this(source, component, minWidth, minWidth, maxWidth, minHeight,
|
---|
| 50 | minHeight, maxHeight);
|
---|
| 51 | }
|
---|
[220] | 52 |
|
---|
[214] | 53 | public DataFrameWidget(Text source, JComponent component, int minWidth,
|
---|
| 54 | int defaultWidth, int maxWidth, int minHeight, int defaultHeight,
|
---|
| 55 | int maxHeight) {
|
---|
[228] | 56 | super(source, component, minWidth, maxWidth, minHeight, maxHeight);
|
---|
[214] | 57 | setSize(defaultWidth, defaultHeight);
|
---|
[220] | 58 |
|
---|
[214] | 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 {
|
---|
[313] | 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 | */
|
---|
[376] | 81 | assert (false);
|
---|
| 82 | // _needsUpdating = true;
|
---|
[313] | 83 | // update();
|
---|
[214] | 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 | }
|
---|
[220] | 111 |
|
---|
[214] | 112 | public boolean needsRefresh() {
|
---|
| 113 | return _needsUpdating;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | public void refresh() {
|
---|
| 117 | _needsUpdating = false;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | public void update() {
|
---|
[294] | 121 | Frame parent = getParentFrame();
|
---|
[1102] | 122 | if (parent != null && parent == DisplayController.getCurrentFrame()) {
|
---|
[294] | 123 | refresh();
|
---|
| 124 | }
|
---|
[313] | 125 |
|
---|
[214] | 126 | _needsUpdating = true;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[220] | 129 | @Override
|
---|
[352] | 130 | public void setLink(String link, Text linker) {
|
---|
[220] | 131 | String newLink = Item.convertToAbsoluteLink(link);
|
---|
| 132 | String oldLink = getSource().getAbsoluteLink();
|
---|
| 133 | if ((newLink == null && oldLink == null)
|
---|
| 134 | || (newLink != null && newLink.equals(oldLink)))
|
---|
[214] | 135 | return;
|
---|
[352] | 136 | super.setLink(link, linker);
|
---|
[220] | 137 | clearSubjects();
|
---|
| 138 | setDataFrame(null);
|
---|
[313] | 139 | if (oldLink == null) {
|
---|
| 140 | _needsUpdating = true;
|
---|
[376] | 141 | // Need to refresh imediately so that the data appears when adding a
|
---|
| 142 | // link to a graph
|
---|
| 143 | refresh();
|
---|
[313] | 144 | } else {
|
---|
| 145 | refresh();
|
---|
| 146 | }
|
---|
[214] | 147 | }
|
---|
[220] | 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 | }
|
---|
[286] | 169 |
|
---|
[1102] | 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);
|
---|
[286] | 175 |
|
---|
| 176 | }
|
---|
[214] | 177 | }
|
---|