66 * %%
77 * Redistribution and use in source and binary forms, with or without
88 * modification, are permitted provided that the following conditions are met:
9- *
9+ *
1010 * 1. Redistributions of source code must retain the above copyright notice,
1111 * this list of conditions and the following disclaimer.
1212 * 2. Redistributions in binary form must reproduce the above copyright notice,
1313 * this list of conditions and the following disclaimer in the documentation
1414 * and/or other materials provided with the distribution.
15- *
15+ *
1616 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1717 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1818 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2626 * POSSIBILITY OF SUCH DAMAGE.
2727 * #L%
2828 */
29- package bdv .ui .appearance ;
29+ package bdv .ui .settings ;
3030
3131import bdv .tools .brightness .ColorIcon ;
32+
3233import java .awt .Color ;
3334import java .awt .Insets ;
3435import java .awt .event .ActionEvent ;
3839import java .util .Arrays ;
3940import java .util .List ;
4041import java .util .Objects ;
41- import java .util .Vector ;
4242import java .util .function .BooleanSupplier ;
4343import java .util .function .Consumer ;
4444import java .util .function .IntConsumer ;
4545import java .util .function .IntSupplier ;
4646import java .util .function .Supplier ;
47- import java . util . stream . Collectors ;
47+
4848import javax .swing .JButton ;
4949import javax .swing .JCheckBox ;
5050import javax .swing .JColorChooser ;
5757 * Helpers for building settings pages:
5858 * Checkboxes, color-selection icons, ...
5959 */
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.
6361// Eventually this should be unified with the Mastodon one and reused.
64- class StyleElements
62+ public class StyleElements
6563{
6664 public static Separator separator ()
6765 {
@@ -115,10 +113,21 @@ public static < T extends Enum< T > > ComboBoxElement< T > comboBoxElement( fina
115113 final Supplier < T > get , final Consumer < T > set ,
116114 final T [] entries )
117115 {
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 ) );
122131 }
123132
124133 public static < T > ComboBoxElement < T > comboBoxElement ( final String label ,
@@ -269,7 +278,7 @@ public void update()
269278 public abstract void set ( boolean b );
270279 }
271280
272- static class ComboBoxEntry < T >
281+ public static class ComboBoxEntry < T >
273282 {
274283 private final T value ;
275284
@@ -350,7 +359,6 @@ public List< ComboBoxEntry< T > > entries()
350359 public static JCheckBox linkedCheckBox ( final BooleanElement element , final String label )
351360 {
352361 final JCheckBox checkbox = new JCheckBox ( label , element .get () );
353- checkbox .setFocusable ( false );
354362 checkbox .addActionListener ( ( e ) -> element .set ( checkbox .isSelected () ) );
355363 element .onSet ( b -> {
356364 if ( b != checkbox .isSelected () )
@@ -416,11 +424,11 @@ public void actionPerformed( final ActionEvent arg0 )
416424 return button ;
417425 }
418426
427+ @ SuppressWarnings ( "unchecked" )
419428 public static < T > JComboBox < ComboBoxEntry < T > > linkedComboBox ( final ComboBoxElement < T > element )
420429 {
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 );
424432 comboBox .setEditable ( false );
425433 comboBox .addItemListener ( e -> {
426434 if ( e .getStateChange () == ItemEvent .SELECTED )
@@ -430,7 +438,7 @@ public static < T > JComboBox< ComboBoxEntry< T > > linkedComboBox( final ComboB
430438 }
431439 } );
432440 final Consumer < T > setEntryForValue = value -> {
433- for ( ComboBoxEntry < T > entry : vector )
441+ for ( ComboBoxEntry < T > entry : cbentries )
434442 if ( Objects .equals ( entry .value (), value ) )
435443 {
436444 comboBox .setSelectedItem ( entry );
0 commit comments