1 | /**
|
---|
2 | * ArraySetting.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.setting;
|
---|
20 |
|
---|
21 | import java.util.Arrays;
|
---|
22 |
|
---|
23 | import org.expeditee.core.Representable;
|
---|
24 | import org.expeditee.gui.Frame;
|
---|
25 | import org.expeditee.gui.FrameIO;
|
---|
26 | import org.expeditee.items.Text;
|
---|
27 |
|
---|
28 | public abstract class ArraySetting<T> extends VariableSetting {
|
---|
29 |
|
---|
30 | protected T[] _default;
|
---|
31 | protected T[] _value;
|
---|
32 |
|
---|
33 | public ArraySetting(String tooltip, String name, T[] value) {
|
---|
34 | super(tooltip, name);
|
---|
35 | _default = Arrays.copyOf(value, value.length);
|
---|
36 | _value = value;
|
---|
37 | }
|
---|
38 |
|
---|
39 | public T getSafe(int i) {
|
---|
40 |
|
---|
41 | if(i < _value.length){
|
---|
42 |
|
---|
43 | return _value[i];
|
---|
44 | }
|
---|
45 | else{
|
---|
46 | return _default[i];
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | public T[] get() {
|
---|
51 | return _value;
|
---|
52 | }
|
---|
53 |
|
---|
54 | public void set(T[] value) {
|
---|
55 | _value = value;
|
---|
56 | }
|
---|
57 |
|
---|
58 | public abstract boolean setSetting(Text text);
|
---|
59 |
|
---|
60 | public void setDefault(T[] value) {
|
---|
61 | _default = Arrays.copyOf(value, value.length);
|
---|
62 | }
|
---|
63 |
|
---|
64 | public void reset() {
|
---|
65 | _value = Arrays.copyOf(_default, _default.length);
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public Text generateRepresentation(String label, String frameset) {
|
---|
70 | Text t = new Text(label);
|
---|
71 |
|
---|
72 | if (_value.length > 0) {
|
---|
73 | Frame arrayFrame = FrameIO.CreateFrame(frameset, null, null);
|
---|
74 | arrayFrame.getTitleItem().delete();
|
---|
75 |
|
---|
76 | int x = 100;
|
---|
77 | int y = 100;
|
---|
78 | for (T v: _value) {
|
---|
79 | if (v == null) continue;
|
---|
80 | Text representation;
|
---|
81 | if (v instanceof Representable) {
|
---|
82 | representation = ((Representable) v).generateRepresentation("Array Entry", "");
|
---|
83 | representation.setPosition(x, y);
|
---|
84 | representation.setID(arrayFrame.getNextItemID());
|
---|
85 | arrayFrame.addItem(representation);
|
---|
86 | } else {
|
---|
87 | representation = arrayFrame.addText(x, y, v.toString(), null);
|
---|
88 | }
|
---|
89 | y += representation.getBoundsHeight();
|
---|
90 | }
|
---|
91 | FrameIO.ForceSaveFrame(arrayFrame);
|
---|
92 | t.setLink(arrayFrame.getName());
|
---|
93 | }
|
---|
94 |
|
---|
95 | return t;
|
---|
96 | }
|
---|
97 | }
|
---|