Skip to content

Commit 9745a44

Browse files
committed
Move StyleElements to package bdv.ui.settings, make public
1 parent ae1c027 commit 9745a44

File tree

4 files changed

+55
-38
lines changed

4 files changed

+55
-38
lines changed

src/main/java/bdv/ui/appearance/AppearanceIO.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939

4040
import javax.swing.UIManager.LookAndFeelInfo;
4141

42+
import org.slf4j.Logger;
43+
import org.slf4j.LoggerFactory;
4244
import org.yaml.snakeyaml.DumperOptions;
4345
import org.yaml.snakeyaml.LoaderOptions;
4446
import org.yaml.snakeyaml.Yaml;
@@ -58,6 +60,8 @@
5860
*/
5961
public class AppearanceIO
6062
{
63+
private static final Logger LOG = LoggerFactory.getLogger( AppearanceIO.class );
64+
6165
public static Appearance load( final String filename ) throws IOException
6266
{
6367
final FileReader input = new FileReader( filename );
@@ -182,7 +186,7 @@ public Object construct( final Node node )
182186
}
183187
catch( final Exception e )
184188
{
185-
e.printStackTrace();
189+
LOG.info( "Error constructing Appearance", e );
186190
}
187191
return null;
188192
}

src/main/java/bdv/ui/appearance/AppearanceManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ void save( final String filename )
162162
catch ( final Exception e )
163163
{
164164
e.printStackTrace();
165-
System.out.println( "Error while reading appearance settings file " + filename + ". Using defaults." );
165+
System.out.println( "Error while writing appearance settings file " + filename + "." );
166166
}
167167
}
168168

src/main/java/bdv/ui/appearance/AppearanceSettingsPage.java

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030

3131
import bdv.ui.settings.ModificationListener;
3232
import bdv.ui.settings.SettingsPage;
33+
import bdv.ui.settings.StyleElements;
3334
import bdv.util.Prefs;
34-
import bdv.ui.appearance.StyleElements.ComboBoxEntry;
35+
import bdv.ui.settings.StyleElements.ComboBoxEntry;
3536
import java.util.ArrayList;
3637
import java.util.Arrays;
3738
import java.util.List;
@@ -44,14 +45,14 @@
4445
import net.miginfocom.swing.MigLayout;
4546
import org.scijava.listeners.Listeners;
4647

47-
import static bdv.ui.appearance.StyleElements.booleanElement;
48-
import static bdv.ui.appearance.StyleElements.cbentry;
49-
import static bdv.ui.appearance.StyleElements.colorElement;
50-
import static bdv.ui.appearance.StyleElements.comboBoxElement;
51-
import static bdv.ui.appearance.StyleElements.linkedCheckBox;
52-
import static bdv.ui.appearance.StyleElements.linkedColorButton;
53-
import static bdv.ui.appearance.StyleElements.linkedComboBox;
54-
import static bdv.ui.appearance.StyleElements.separator;
48+
import static bdv.ui.settings.StyleElements.booleanElement;
49+
import static bdv.ui.settings.StyleElements.cbentry;
50+
import static bdv.ui.settings.StyleElements.colorElement;
51+
import static bdv.ui.settings.StyleElements.comboBoxElement;
52+
import static bdv.ui.settings.StyleElements.linkedCheckBox;
53+
import static bdv.ui.settings.StyleElements.linkedColorButton;
54+
import static bdv.ui.settings.StyleElements.linkedComboBox;
55+
import static bdv.ui.settings.StyleElements.separator;
5556

5657
/**
5758
* Preferences page for changing {@link Appearance}.
@@ -131,7 +132,7 @@ public AppearancePanel( final Appearance appearance )
131132
lafs.add( cbentry( feel, feel.getName() ) );
132133

133134
final List< StyleElements.StyleElement > styleElements = Arrays.asList(
134-
comboBoxElement( "look-and-feel", appearance::lookAndFeel, appearance::setLookAndFeel, lafs ),
135+
comboBoxElement( "look-and-feel:", appearance::lookAndFeel, appearance::setLookAndFeel, lafs ),
135136
separator(),
136137
booleanElement( "show scalebar", appearance::showScaleBar, appearance::setShowScaleBar ),
137138
booleanElement( "show scalebar in movies", appearance::showScaleBarInMovie, appearance::setShowScaleBarInMovie ),
@@ -140,7 +141,8 @@ public AppearancePanel( final Appearance appearance )
140141
separator(),
141142
booleanElement( "show minimap", appearance::showMultibox, appearance::setShowMultibox ),
142143
booleanElement( "show source info", appearance::showTextOverlay, appearance::setShowTextOverlay ),
143-
comboBoxElement( "source name position", appearance::sourceNameOverlayPosition, appearance::setSourceNameOverlayPosition, Prefs.OverlayPosition.values() )
144+
separator(),
145+
comboBoxElement( "source name position:", appearance::sourceNameOverlayPosition, appearance::setSourceNameOverlayPosition, Prefs.OverlayPosition.values() )
144146
);
145147

146148
final JColorChooser colorChooser = new JColorChooser();
@@ -156,22 +158,25 @@ public void visit( final StyleElements.Separator separator )
156158
@Override
157159
public void visit( final StyleElements.ColorElement element )
158160
{
159-
add( new JLabel( element.getLabel() ), "r" );
160-
add( linkedColorButton( element, colorChooser ), "l, wrap" );
161+
JPanel row = new JPanel(new MigLayout( "insets 0, fillx", "[r][l]", "" ));
162+
row.add( linkedColorButton( element, colorChooser ), "l" );
163+
row.add( new JLabel( element.getLabel() ), "l, growx" );
164+
add( row, "l, span 2, wrap" );
161165
}
162166

163167
@Override
164168
public void visit( final StyleElements.BooleanElement element )
165169
{
166-
add( new JLabel( element.getLabel() ), "r" );
167-
add( linkedCheckBox( element ), "l, wrap" );
170+
add( linkedCheckBox( element, element.getLabel() ), "l, span 2, wrap" );
168171
}
169172

170173
@Override
171174
public void visit( final StyleElements.ComboBoxElement< ? > element )
172175
{
173-
add( new JLabel( element.getLabel() ), "r" );
174-
add( linkedComboBox( element ), "l, wrap" );
176+
JPanel row = new JPanel(new MigLayout( "insets 0, fillx", "[r][l]", "" ));
177+
row.add( new JLabel( element.getLabel() ), "l" );
178+
row.add( linkedComboBox( element ), "l, growx" );
179+
add( row, "l, span 2, wrap" );
175180
}
176181
} ) );
177182

src/main/java/bdv/ui/appearance/StyleElements.java renamed to src/main/java/bdv/ui/settings/StyleElements.java

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
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
@@ -26,9 +26,10 @@
2626
* POSSIBILITY OF SUCH DAMAGE.
2727
* #L%
2828
*/
29-
package bdv.ui.appearance;
29+
package bdv.ui.settings;
3030

3131
import bdv.tools.brightness.ColorIcon;
32+
3233
import java.awt.Color;
3334
import java.awt.Insets;
3435
import java.awt.event.ActionEvent;
@@ -38,13 +39,12 @@
3839
import java.util.Arrays;
3940
import java.util.List;
4041
import java.util.Objects;
41-
import java.util.Vector;
4242
import java.util.function.BooleanSupplier;
4343
import java.util.function.Consumer;
4444
import java.util.function.IntConsumer;
4545
import java.util.function.IntSupplier;
4646
import java.util.function.Supplier;
47-
import java.util.stream.Collectors;
47+
4848
import javax.swing.JButton;
4949
import javax.swing.JCheckBox;
5050
import javax.swing.JColorChooser;
@@ -57,11 +57,9 @@
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

Comments
 (0)