File: PanelController.java

package info (click to toggle)
gpsprune 26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,824 kB
  • sloc: java: 52,154; sh: 25; makefile: 21; python: 15
file content (158 lines) | stat: -rw-r--r-- 4,202 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package tim.prune.function.compress;

import java.awt.Container;
import java.util.ArrayList;

import tim.prune.config.Config;
import tim.prune.data.MarkingData;
import tim.prune.data.Track;
import tim.prune.function.compress.methods.CompressionMethod;

public class PanelController implements MethodPanelContainer
{
	private final Track _track;
	private final CompressionDialog _parentDialog;
	private final ArrayList<MethodPanel> _panels = new ArrayList<>();
	private final Container _container;
	private ParameterValues _values = null;

	/** Constructor */
	public PanelController(Track inTrack, CompressionDialog inParent, Container inContainer)
	{
		_track = inTrack;
		_parentDialog = inParent;
		_container = inContainer;
	}

	/** @return the number of panels */
	public int getNumPanels() {
		return _panels.size();
	}

	/** Add a new method panel to the list, using the given config */
	public void addMethod(Config inConfig)
	{
		for (MethodPanel panel : _panels)
		{
			if (!panel.hasMethodSelected()) {
				return; // Refuse to create a new one because there's already a None
			}
		}
		MethodPanel panel = new MethodPanel(_panels.size(), this, _values, inConfig);
		addPanel(panel);
	}

	/** Add a new method panel to the list, using the given config */
	public void addMethod(CompressionMethod inMethod, Config inConfig)
	{
		MethodPanel panel = new MethodPanel(_panels.size(), this, _values, inConfig);
		panel.setMethod(inMethod);
		addPanel(panel);
	}

	/** a bit of a hack to reserve space in the gui for at least a few panels */
	public void addDummyPanels()
	{
		for (int i=0; i<5; i++) {
			addPanel(new MethodPanel(_panels.size(), this, _values, null));
		}
	}

	/** Called by a panel to move itself up */
	public void movePanelUp(int inIndex) {
		swapPanels(inIndex - 1, inIndex);
	}

	/** Called by a panel to move itself down */
	public void movePanelDown(int inIndex) {
		swapPanels(inIndex, inIndex + 1);
	}

	private void swapPanels(int inLowerIndex, int inHigherIndex)
	{
		if (inLowerIndex < 0 || inHigherIndex >= _panels.size()) {
			return; // move not allowed
		}
		MethodPanel.swapPanels(_panels.get(inLowerIndex), _panels.get(inHigherIndex));
		recalculateAll();
	}

	private void addPanel(MethodPanel inPanel)
	{
		_panels.add(inPanel);
		_container.add(inPanel);
		_container.revalidate();
		_container.repaint();
	}

	public void deletePanel(int inIndex)
	{
		if (inIndex < 0 || _panels.size() <= 1 || inIndex >= _panels.size()) {
			return;
		}
		for (int i=inIndex+1; i<_panels.size(); i++) {
			movePanelUp(i);
		}
		// Now the one to be deleted is at the end
		final MethodPanel lastPanel = _panels.get(_panels.size() - 1);
		_panels.remove(_panels.size() - 1);
		// Remove last one from scrollpanel too
		_container.remove(lastPanel);
		_container.revalidate();
		_container.repaint();
	}

	public void deleteAllPanels()
	{
		while (!_panels.isEmpty())
		{
			_container.remove(0);
			_panels.remove(0);
		}
		_container.revalidate();
		_container.repaint();
	}

	public void refresh()
	{
		for (MethodPanel panel : _panels) {
			panel.refresh();
		}
	}

	public MarkingData recalculateAll()
	{
		int totalDeleted = 0;
		MarkingData markings = new MarkingData(_track);
		for (MethodPanel panel : _panels)
		{
			CompressionMethod method = (panel == null ? null : panel.getCompressionMethod());
			if (method != null)
			{
				TrackDetails details = new TrackDetails(_track);
				int numDeletedByThisMethod = method.preview(_track, details, markings);
				panel.showNumDeleted(numDeletedByThisMethod);
				totalDeleted += numDeletedByThisMethod;
			}
		}
		_parentDialog.informNumPointsDeleted(totalDeleted);
		return markings;
	}

	public MethodList getMethodList()
	{
		MethodList methods = new MethodList();
		for (MethodPanel panel : _panels)
		{
			CompressionMethod method = (panel == null ? null : panel.getCompressionMethod());
			if (method != null){
				methods.add(method);
			}
		}
		return methods;
	}

	public void setParameterValues(ParameterValues inValues) {
		_values = inValues;
	}
}