forked from DSheirer/sdrtrunk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AgencyEditor.java
219 lines (198 loc) · 7.51 KB
/
AgencyEditor.java
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* *****************************************************************************
* Copyright (C) 2014-2022 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
* ****************************************************************************
*/
package io.github.dsheirer.gui.playlist.radioreference;
import io.github.dsheirer.playlist.PlaylistManager;
import io.github.dsheirer.preference.UserPreferences;
import io.github.dsheirer.rrapi.type.Agency;
import io.github.dsheirer.rrapi.type.AgencyInfo;
import io.github.dsheirer.rrapi.type.CountyInfo;
import io.github.dsheirer.service.radioreference.RadioReference;
import io.github.dsheirer.util.ThreadPool;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* Editor for displaying a list of agencies and viewing frequency lists for each agency
*/
public class AgencyEditor extends VBox
{
private static final Logger mLog = LoggerFactory.getLogger(AgencyEditor.class);
private UserPreferences mUserPreferences;
private RadioReference mRadioReference;
private PlaylistManager mPlaylistManager;
private Level mLevel;
private ListView<Agency> mAgencyListView;
private AgencyFrequencyEditor mAgencyFrequencyEditor;
private IntegerProperty mAgencyCountProperty = new SimpleIntegerProperty();
/**
* Constructs an instance
* @param userPreferences
* @param radioReference
* @param level
*/
public AgencyEditor(UserPreferences userPreferences, RadioReference radioReference,
PlaylistManager playlistManager, Level level)
{
mUserPreferences = userPreferences;
mRadioReference = radioReference;
mPlaylistManager = playlistManager;
mLevel = level;
mAgencyCountProperty.bind(Bindings.size(getAgencyListView().getItems()));
setPadding(new Insets(10,10,10,10));
setSpacing(10);
VBox.setVgrow(getAgencyFrequencyEditor(), Priority.ALWAYS);
getChildren().addAll(getAgencyListView(), getAgencyFrequencyEditor());
}
/**
* Sets the list of displayed agencies and clears out any existing agencies. Auto-selects an agency if the user
* has selected that agency before.
*
* Note: this should only be invoked on the FX application thread
*
* @param agencies to display
*/
public void setAgencies(List<Agency> agencies)
{
clear();
if(agencies != null && !agencies.isEmpty())
{
Collections.sort(agencies, new AgencyComparator());
getAgencyListView().getItems().addAll(agencies);
int preferredAgencyId = mUserPreferences.getRadioReferencePreference().getPreferredAgencyId(mLevel);
for(Agency agency: getAgencyListView().getItems())
{
if(agency.getAgencyId() == preferredAgencyId)
{
getAgencyListView().getSelectionModel().select(agency);
getAgencyListView().scrollTo(agency);
return;
}
}
}
}
public void clear()
{
getAgencyListView().getItems().clear();
getAgencyFrequencyEditor().setCategories(null);
}
/**
* Observable count of agencies in the editor
*/
public IntegerProperty agencyCountProperty()
{
return mAgencyCountProperty;
}
private AgencyFrequencyEditor getAgencyFrequencyEditor()
{
if(mAgencyFrequencyEditor == null)
{
mAgencyFrequencyEditor = new AgencyFrequencyEditor(mUserPreferences, mRadioReference, mPlaylistManager, mLevel);
}
return mAgencyFrequencyEditor;
}
private ListView<Agency> getAgencyListView()
{
if(mAgencyListView == null)
{
mAgencyListView = new ListView<>();
mAgencyListView.setPrefHeight(200);
mAgencyListView.setCellFactory(param -> new AgencyListCell());
mAgencyListView.getSelectionModel().selectedItemProperty()
.addListener((observable, oldValue, newValue) -> setAgency(newValue));
mAgencyListView.setPlaceholder(new Label("No agencies available"));
}
return mAgencyListView;
}
private void setAgency(Agency agency)
{
if(agency != null)
{
getAgencyFrequencyEditor().clearAndSetLoading();
mUserPreferences.getRadioReferencePreference().setPreferredAgencyId(agency.getAgencyId(), mLevel);
ThreadPool.CACHED.submit(() -> {
try
{
if (mLevel == Level.COUNTY && agency instanceof CountyAgency) {
final CountyInfo countyInfo = mRadioReference.getService().getCountyInfo(-agency.getAgencyId());
Platform.runLater(() -> getAgencyFrequencyEditor().setCategories(countyInfo.getCategories()));
} else {
final AgencyInfo agencyInfo = mRadioReference.getService().getAgencyInfo(agency);
Platform.runLater(() -> getAgencyFrequencyEditor().setCategories(agencyInfo.getCategories()));
}
}
catch(Throwable t)
{
mLog.error("Error retrieving agency info", t);
Platform.runLater(() -> {
getAgencyFrequencyEditor().setLoading(false);
new RadioReferenceUnavailableAlert(getAgencyListView()).showAndWait();
});
}
});
}
else
{
getAgencyFrequencyEditor().clear();
}
}
public class AgencyListCell extends ListCell<Agency>
{
@Override
protected void updateItem(Agency item, boolean empty)
{
super.updateItem(item, empty);
setText((empty || item == null) ? null : item.getName());
}
}
public class AgencyComparator implements Comparator<Agency>
{
@Override
public int compare(Agency o1, Agency o2)
{
if(o1.getName() == null && o2.getName() == null)
{
return 0;
}
else if(o1.getName() == null || o2 instanceof CountyAgency)
{
return 1;
}
else if(o2.getName() == null || o1 instanceof CountyAgency)
{
return -1;
}
else
{
return o1.getName().compareTo(o2.getName());
}
}
}
}