-
Notifications
You must be signed in to change notification settings - Fork 35
/
SetupAssignments.java
365 lines (322 loc) · 13.3 KB
/
SetupAssignments.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* #%L
* BigDataViewer core classes with minimal dependencies.
* %%
* Copyright (C) 2012 - 2022 BigDataViewer developers.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package bdv.tools.brightness;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import net.imglib2.type.numeric.ARGBType;
import org.jdom2.Element;
import mpicbg.spim.data.XmlHelpers;
/**
* Manage a (fixed) set of {@link ConverterSetup}s and (changing) set of
* {@link MinMaxGroup}s, such that the following is always true:
* <ol>
* <li>Every setup is assigned to exactly one group.</li>
* <li>No group is empty.</li>
* </ol>
*
* @author Tobias Pietzsch
*/
@Deprecated
public class SetupAssignments
{
/**
* All {@link ConverterSetup}s managed by this SetupAssignments.
*/
private final ArrayList< ConverterSetup > setups;
/**
* A list of {@link MinMaxGroup}s, such that every {@link #setups setup} is
* contained in exactly one group.
*/
private final ArrayList< MinMaxGroup > minMaxGroups;
/**
* Maps every {@link #setups setup} to the {@link #minMaxGroups group}
* containing it.
*/
private final Map< ConverterSetup, MinMaxGroup > setupToGroup;
private final double fullRangeMin;
private final double fullRangeMax;
private final double defaultRangeMin;
private final double defaultRangeMax;
private static final double minIntervalSize = 0;
public interface UpdateListener
{
void update();
}
private UpdateListener updateListener;
public SetupAssignments( final ArrayList< ConverterSetup > converterSetups, final double defaultRangeMin, final double defaultRangeMax )
{
this( converterSetups, Integer.MIN_VALUE, Integer.MAX_VALUE, defaultRangeMin, defaultRangeMax );
}
/**
*
* @param converterSetups
* @param fullRangeMin
* @param fullRangeMax
* @param defaultRangeMin
* @param defaultRangeMax
*/
public SetupAssignments( final ArrayList< ConverterSetup > converterSetups, final double fullRangeMin, final double fullRangeMax, final double defaultRangeMin, final double defaultRangeMax )
{
setups = new ArrayList<>( converterSetups );
minMaxGroups = new ArrayList<>();
setupToGroup = new HashMap<>();
this.fullRangeMin = fullRangeMin;
this.fullRangeMax = fullRangeMax;
this.defaultRangeMin = defaultRangeMin;
this.defaultRangeMax = defaultRangeMax;
for ( final ConverterSetup setup : setups )
{
final double displayRangeMin = Math.max( defaultRangeMin, Math.min( defaultRangeMax, setup.getDisplayRangeMin() ) );
final double displayRangeMax = Math.max( defaultRangeMin, Math.min( defaultRangeMax, setup.getDisplayRangeMax() ) );
if ( setup.getDisplayRangeMin() != displayRangeMin || setup.getDisplayRangeMax() != displayRangeMax )
setup.setDisplayRange( displayRangeMin, displayRangeMax );
final MinMaxGroup group = new MinMaxGroup( fullRangeMin, fullRangeMax, defaultRangeMin, defaultRangeMax, displayRangeMin, displayRangeMax, minIntervalSize );
minMaxGroups.add( group );
setupToGroup.put( setup, group );
group.addSetup( setup );
}
updateListener = null;
}
/**
* Add the specified setup to the specified group. The setup is removed from
* its previous group. If this previous group is made empty by this, it is
* removed from the list of groups.
*/
public void moveSetupToGroup( final ConverterSetup setup, final MinMaxGroup group )
{
final MinMaxGroup oldGroup = setupToGroup.get( setup );
if ( oldGroup == group )
return;
setupToGroup.put( setup, group );
group.addSetup( setup );
final boolean oldGroupIsEmpty = oldGroup.removeSetup( setup );
if ( oldGroupIsEmpty )
minMaxGroups.remove( oldGroup );
if ( updateListener != null )
updateListener.update();
}
/**
* Remove the specified setup from the specified group. If this group would
* be made empty by this, it is not removed from the group. Otherwise, after
* being removed, a new group is created containing only the specified
* setup, and this new group is added to the list of group. The settings of
* the new group are initialized with the settings of the old group.
*
* @return Whether or not removal was successful (so the corresponding
* checkbox can be re-checked)
*/
public boolean removeSetupFromGroup( final ConverterSetup setup, final MinMaxGroup group )
{
if ( setupToGroup.get( setup ) != group )
return false;
if ( group.setups.size() == 1 )
return false;
final MinMaxGroup newGroup = new MinMaxGroup( group.getFullRangeMin(), group.getFullRangeMax(), group.getRangeMin(), group.getRangeMax(), setup.getDisplayRangeMin(), setup.getDisplayRangeMax(), minIntervalSize );
minMaxGroups.add( newGroup );
setupToGroup.put( setup, newGroup );
newGroup.addSetup( setup );
final boolean groupIsEmpty = group.removeSetup( setup );
if ( groupIsEmpty )
minMaxGroups.remove( group );
if ( updateListener != null )
updateListener.update();
return true;
}
public void setUpdateListener( final UpdateListener l )
{
updateListener = l;
}
/**
* @return the list of {@link MinMaxGroup}s, such that every {@link #setups
* setup} is contained in exactly one group.
*/
public List< MinMaxGroup > getMinMaxGroups()
{
return Collections.unmodifiableList( minMaxGroups );
}
/**
* @return a list of all {@link ConverterSetup}s.
*/
public List< ConverterSetup > getConverterSetups()
{
return Collections.unmodifiableList( setups );
}
/**
* @return the {@link MinMaxGroup} that contains {@code setup}, currently.
*/
public MinMaxGroup getMinMaxGroup( final ConverterSetup setup )
{
return setupToGroup.get( setup );
}
/**
* Add the setup in a new group.
* @param setup
*/
public void addSetup( final ConverterSetup setup )
{
final MinMaxGroup group = new MinMaxGroup( fullRangeMin, fullRangeMax, defaultRangeMin, defaultRangeMax, setup.getDisplayRangeMin(), setup.getDisplayRangeMax(), minIntervalSize );
minMaxGroups.add( group );
setupToGroup.put( setup, group );
group.addSetup( setup );
setups.add( setup );
if ( updateListener != null )
updateListener.update();
}
public void removeSetup( final ConverterSetup setup )
{
final MinMaxGroup group = setupToGroup.get( setup );
if ( group == null )
return;
final boolean groupIsEmpty = group.removeSetup( setup );
if ( groupIsEmpty )
minMaxGroups.remove( group );
setups.remove( setup );
setupToGroup.remove( setup );
if ( updateListener != null )
updateListener.update();
}
/**
* Serialize the state of this {@link SetupAssignments} to XML.
*/
public Element toXml()
{
final Element elem = new Element( "SetupAssignments" );
final Element elemConverterSetups = new Element( "ConverterSetups" );
for ( final ConverterSetup setup : setups )
{
final Element elemConverterSetup = new Element( "ConverterSetup" );
elemConverterSetup.addContent( XmlHelpers.intElement( "id", setup.getSetupId() ) );
elemConverterSetup.addContent( XmlHelpers.doubleElement( "min", setup.getDisplayRangeMin() ) );
elemConverterSetup.addContent( XmlHelpers.doubleElement( "max", setup.getDisplayRangeMax() ) );
elemConverterSetup.addContent( XmlHelpers.intElement( "color", setup.getColor().get() ) );
elemConverterSetup.addContent( XmlHelpers.intElement( "groupId", minMaxGroups.indexOf( setupToGroup.get( setup ) ) ) );
elemConverterSetups.addContent( elemConverterSetup );
}
elem.addContent( elemConverterSetups );
final Element elemMinMaxGroups = new Element( "MinMaxGroups" );
for ( int i = 0; i < minMaxGroups.size(); ++i )
{
final MinMaxGroup group = minMaxGroups.get( i );
final Element elemMinMaxGroup = new Element( "MinMaxGroup" );
elemMinMaxGroup.addContent( XmlHelpers.intElement( "id", i ) );
elemMinMaxGroup.addContent( XmlHelpers.doubleElement( "fullRangeMin", group.getFullRangeMin() ) );
elemMinMaxGroup.addContent( XmlHelpers.doubleElement( "fullRangeMax", group.getFullRangeMax() ) );
elemMinMaxGroup.addContent( XmlHelpers.doubleElement( "rangeMin", group.getRangeMin() ) );
elemMinMaxGroup.addContent( XmlHelpers.doubleElement( "rangeMax", group.getRangeMax() ) );
elemMinMaxGroup.addContent( XmlHelpers.doubleElement( "currentMin", group.getMinBoundedValue().getCurrentValue() ) );
elemMinMaxGroup.addContent( XmlHelpers.doubleElement( "currentMax", group.getMaxBoundedValue().getCurrentValue() ) );
elemMinMaxGroups.addContent( elemMinMaxGroup );
}
elem.addContent( elemMinMaxGroups );
return elem;
}
/**
* Restore the state of this {@link SetupAssignments} from XML. Note, that
* this only restores the assignments of setups to groups and group
* settings. The list of {@link ConverterSetup}s is not restored.
*/
public void restoreFromXml( final Element parent )
{
final Element elemSetupAssignments = parent.getChild( "SetupAssignments" );
if ( elemSetupAssignments == null )
return;
final Element elemConverterSetups = elemSetupAssignments.getChild( "ConverterSetups" );
final List< Element > converterSetupNodes = elemConverterSetups.getChildren( "ConverterSetup" );
if ( converterSetupNodes.size() != setups.size() )
throw new IllegalArgumentException();
final Element elemMinMaxGroups = elemSetupAssignments.getChild( "MinMaxGroups" );
final List< Element > minMaxGroupNodes = elemMinMaxGroups.getChildren( "MinMaxGroup" );
minMaxGroups.clear();
for ( int i = 0; i < minMaxGroupNodes.size(); ++i )
minMaxGroups.add( null );
for ( final Element elem : minMaxGroupNodes )
{
final int id = Integer.parseInt( elem.getChildText( "id" ) );
final double fullRangeMin = Double.parseDouble( elem.getChildText( "fullRangeMin" ) );
final double fullRangeMax = Double.parseDouble( elem.getChildText( "fullRangeMax" ) );
final double rangeMin = Double.parseDouble( elem.getChildText( "rangeMin" ) );
final double rangeMax = Double.parseDouble( elem.getChildText( "rangeMax" ) );
final double currentMin = Double.parseDouble( elem.getChildText( "currentMin" ) );
final double currentMax = Double.parseDouble( elem.getChildText( "currentMax" ) );
minMaxGroups.set( id, new MinMaxGroup( fullRangeMin, fullRangeMax, rangeMin, rangeMax, currentMin, currentMax, minIntervalSize ) );
}
for ( final Element elem : converterSetupNodes )
{
final int id = Integer.parseInt( elem.getChildText( "id" ) );
final double min = Double.parseDouble( elem.getChildText( "min" ) );
final double max = Double.parseDouble( elem.getChildText( "max" ) );
final int color = Integer.parseInt( elem.getChildText( "color" ) );
final int groupId = Integer.parseInt( elem.getChildText( "groupId" ) );
final ConverterSetup setup = getSetupById( id );
setup.setDisplayRange( min, max );
setup.setColor( new ARGBType( color ) );
final MinMaxGroup group = minMaxGroups.get( groupId );
setupToGroup.put( setup, group );
group.addSetup( setup );
}
if ( updateListener != null )
updateListener.update();
}
private ConverterSetup getSetupById( final int id )
{
for ( final ConverterSetup setup : setups )
if ( setup.getSetupId() == id )
return setup;
return null;
}
private static final WeakHashMap< SetupAssignments, Integer > maxIds = new WeakHashMap<>( );
private static final SetupAssignments nullSetupAssignmentsKey = new SetupAssignments( new ArrayList<>(), 0, 1 );
/**
* Get a {@link ConverterSetup#getSetupId() setup id} that is not used in
* the specified {@code setupAssignments}.
*
* @param setupAssignments
* the {@link SetupAssignments} for which to find an unused id.
* May be {@code null}, in which case a new id is returned that
* was not used in any previous call to
* {@code getUnusedSetupId(null)}.
*
* @return a unused setup id
*/
public static synchronized int getUnusedSetupId( SetupAssignments setupAssignments )
{
if ( setupAssignments == null )
setupAssignments = nullSetupAssignmentsKey;
int maxId = maxIds.getOrDefault( setupAssignments, 0 );
for ( final ConverterSetup setup : setupAssignments.getConverterSetups() )
maxId = Math.max( setup.getSetupId(), maxId );
++maxId;
maxIds.put( setupAssignments, maxId );
return maxId;
}
}