6
6
* %%
7
7
* Redistribution and use in source and binary forms, with or without
8
8
* modification, are permitted provided that the following conditions are met:
9
- *
9
+ *
10
10
* 1. Redistributions of source code must retain the above copyright notice,
11
11
* this list of conditions and the following disclaimer.
12
12
* 2. Redistributions in binary form must reproduce the above copyright notice,
13
13
* this list of conditions and the following disclaimer in the documentation
14
14
* and/or other materials provided with the distribution.
15
- *
15
+ *
16
16
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
17
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
18
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
26
* POSSIBILITY OF SUCH DAMAGE.
27
27
* #L%
28
28
*/
29
- package bdv .ui .appearance ;
29
+ package bdv .ui .settings ;
30
30
31
31
import bdv .tools .brightness .ColorIcon ;
32
+
32
33
import java .awt .Color ;
33
34
import java .awt .Insets ;
34
35
import java .awt .event .ActionEvent ;
38
39
import java .util .Arrays ;
39
40
import java .util .List ;
40
41
import java .util .Objects ;
41
- import java .util .Vector ;
42
42
import java .util .function .BooleanSupplier ;
43
43
import java .util .function .Consumer ;
44
44
import java .util .function .IntConsumer ;
45
45
import java .util .function .IntSupplier ;
46
46
import java .util .function .Supplier ;
47
- import java . util . stream . Collectors ;
47
+
48
48
import javax .swing .JButton ;
49
49
import javax .swing .JCheckBox ;
50
50
import javax .swing .JColorChooser ;
57
57
* Helpers for building settings pages:
58
58
* Checkboxes, color-selection icons, ...
59
59
*/
60
- // TODO: Polish a bit and make public.
61
- // This is a modified version of the StyleElements class from Mastodon.
62
- // Currently it's only used in AppearanceSettingsPage.
60
+ // TODO: This is a modified version of the StyleElements class from Mastodon.
63
61
// Eventually this should be unified with the Mastodon one and reused.
64
- class StyleElements
62
+ public class StyleElements
65
63
{
66
64
public static Separator separator ()
67
65
{
@@ -115,10 +113,21 @@ public static < T extends Enum< T > > ComboBoxElement< T > comboBoxElement( fina
115
113
final Supplier < T > get , final Consumer < T > set ,
116
114
final T [] entries )
117
115
{
118
- final List < ComboBoxEntry < T > > list = Arrays .stream ( entries )
119
- .map ( v -> new ComboBoxEntry <>( v , v .toString () ) )
120
- .collect ( Collectors .toList () );
121
- return comboBoxElement ( label , get , set , list );
116
+ final String [] entryLabels = new String [entries .length ];
117
+ Arrays .setAll ( entryLabels , i -> entries [ i ].toString () );
118
+ return comboBoxElement ( label , get , set , entries , entryLabels );
119
+ }
120
+
121
+ public static < T extends Enum < T > > ComboBoxElement < T > comboBoxElement ( final String label ,
122
+ final Supplier < T > get , final Consumer < T > set ,
123
+ final T [] entries ,
124
+ final String [] entryLabels )
125
+ {
126
+ if ( entries .length != entryLabels .length )
127
+ throw new IllegalArgumentException ( "lengths of entries and entryLabels arrays do not match" );
128
+ final ComboBoxEntry < T >[] cbentries = new ComboBoxEntry [ entries .length ];
129
+ Arrays .setAll ( cbentries , i -> new ComboBoxEntry <>( entries [ i ], entryLabels [ i ] ) );
130
+ return comboBoxElement ( label , get , set , Arrays .asList ( cbentries ) );
122
131
}
123
132
124
133
public static < T > ComboBoxElement < T > comboBoxElement ( final String label ,
@@ -269,7 +278,7 @@ public void update()
269
278
public abstract void set ( boolean b );
270
279
}
271
280
272
- static class ComboBoxEntry < T >
281
+ public static class ComboBoxEntry < T >
273
282
{
274
283
private final T value ;
275
284
@@ -350,7 +359,6 @@ public List< ComboBoxEntry< T > > entries()
350
359
public static JCheckBox linkedCheckBox ( final BooleanElement element , final String label )
351
360
{
352
361
final JCheckBox checkbox = new JCheckBox ( label , element .get () );
353
- checkbox .setFocusable ( false );
354
362
checkbox .addActionListener ( ( e ) -> element .set ( checkbox .isSelected () ) );
355
363
element .onSet ( b -> {
356
364
if ( b != checkbox .isSelected () )
@@ -416,11 +424,11 @@ public void actionPerformed( final ActionEvent arg0 )
416
424
return button ;
417
425
}
418
426
427
+ @ SuppressWarnings ( "unchecked" )
419
428
public static < T > JComboBox < ComboBoxEntry < T > > linkedComboBox ( final ComboBoxElement < T > element )
420
429
{
421
- Vector < ComboBoxEntry < T > > vector = new Vector <>();
422
- vector .addAll ( element .entries () );
423
- final JComboBox < ComboBoxEntry < T > > comboBox = new JComboBox <>( vector );
430
+ final ComboBoxEntry < T >[] cbentries = element .entries .toArray ( new ComboBoxEntry [ 0 ] );
431
+ final JComboBox < ComboBoxEntry < T > > comboBox = new JComboBox <>( cbentries );
424
432
comboBox .setEditable ( false );
425
433
comboBox .addItemListener ( e -> {
426
434
if ( e .getStateChange () == ItemEvent .SELECTED )
@@ -430,7 +438,7 @@ public static < T > JComboBox< ComboBoxEntry< T > > linkedComboBox( final ComboB
430
438
}
431
439
} );
432
440
final Consumer < T > setEntryForValue = value -> {
433
- for ( ComboBoxEntry < T > entry : vector )
441
+ for ( ComboBoxEntry < T > entry : cbentries )
434
442
if ( Objects .equals ( entry .value (), value ) )
435
443
{
436
444
comboBox .setSelectedItem ( entry );
0 commit comments