Skip to content

Commit 12da664

Browse files
committed
Add copy&paste of BDV state as JSON
1 parent f93c3f6 commit 12da664

20 files changed

+2094
-8
lines changed

src/main/java/bdv/BigDataViewer.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
import bdv.tools.brightness.RealARGBColorConverterSetup;
7474
import bdv.tools.brightness.SetupAssignments;
7575
import bdv.tools.crop.CropDialog;
76+
import bdv.tools.links.LinkActions;
77+
import bdv.tools.links.PasteSettings;
7678
import bdv.tools.links.ResourceManager;
7779
import bdv.tools.links.resource.SpimDataMinimalFileResource;
7880
import bdv.tools.links.resource.SpimDataSetupSourceResource;
@@ -86,6 +88,9 @@
8688
import bdv.ui.keymap.Keymap;
8789
import bdv.ui.keymap.KeymapManager;
8890
import bdv.ui.keymap.KeymapSettingsPage;
91+
import bdv.ui.links.LinkCard;
92+
import bdv.ui.links.LinkSettingsManager;
93+
import bdv.ui.links.LinkSettingsPage;
8994
import bdv.viewer.ConverterSetups;
9095
import bdv.viewer.NavigationActions;
9196
import bdv.viewer.SourceAndConverter;
@@ -140,6 +145,8 @@ public class BigDataViewer
140145

141146
private final AppearanceManager appearanceManager;
142147

148+
private final LinkSettingsManager linkSettingsManager;
149+
143150
private final ResourceManager resourceManager;
144151

145152
protected final PreferencesDialog preferencesDialog;
@@ -422,8 +429,10 @@ public BigDataViewer(
422429
{
423430
final KeymapManager optionsKeymapManager = options.values.getKeymapManager();
424431
final AppearanceManager optionsAppearanceManager = options.values.getAppearanceManager();
432+
final LinkSettingsManager optionsLinkSettingsManager = options.values.getLinkSettingsManager();
425433
keymapManager = optionsKeymapManager != null ? optionsKeymapManager : new KeymapManager( configDir );
426434
appearanceManager = optionsAppearanceManager != null ? optionsAppearanceManager : new AppearanceManager( configDir );
435+
linkSettingsManager = optionsLinkSettingsManager != null ? optionsLinkSettingsManager : new LinkSettingsManager( configDir );
427436
resourceManager = options.values.getResourceManager();
428437

429438
InputTriggerConfig inputTriggerConfig = options.values.getInputTriggerConfig();
@@ -512,6 +521,7 @@ public boolean accept( final File f )
512521
preferencesDialog = new PreferencesDialog( viewerFrame, keymap, new String[] { KeyConfigContexts.BIGDATAVIEWER } );
513522
preferencesDialog.addPage( new AppearanceSettingsPage( "Appearance", appearanceManager ) );
514523
preferencesDialog.addPage( new KeymapSettingsPage( "Keymap", this.keymapManager, this.keymapManager.getCommandDescriptions() ) );
524+
preferencesDialog.addPage( new LinkSettingsPage( "Links", linkSettingsManager ) );
515525
appearanceManager.appearance().updateListeners().add( viewerFrame::repaint );
516526
appearanceManager.addLafComponent( fileChooser );
517527
SwingUtilities.invokeLater(() -> appearanceManager.updateLookAndFeel());
@@ -524,9 +534,17 @@ public boolean accept( final File f )
524534
bdvActions.install( viewerFrame.getKeybindings(), "bdv" );
525535
BigDataViewerActions.install( bdvActions, this );
526536

537+
final Actions linkActions = new Actions( inputTriggerConfig, "bdv" );
538+
linkActions.install( viewerFrame.getKeybindings(), "links" );
539+
final PasteSettings pasteSettings = linkSettingsManager.linkSettings().pasteSettings();
540+
LinkActions.install( linkActions, viewerFrame.getViewerPanel(), viewerFrame.getConverterSetups(), pasteSettings, resourceManager );
541+
542+
LinkCard.install( linkSettingsManager.linkSettings(), viewerFrame.getCardPanel() );
543+
527544
keymap.updateListeners().add( () -> {
528545
navigationActions.updateKeyConfig( keymap.getConfig() );
529546
bdvActions.updateKeyConfig( keymap.getConfig() );
547+
linkActions.updateKeyConfig( keymap.getConfig() );
530548
viewerFrame.getTransformBehaviours().updateKeyConfig( keymap.getConfig() );
531549
} );
532550

@@ -682,6 +700,11 @@ public AppearanceManager getAppearanceManager()
682700
return appearanceManager;
683701
}
684702

703+
public LinkSettingsManager getLinkSettingsManager()
704+
{
705+
return linkSettingsManager;
706+
}
707+
685708
public ResourceManager getResourceManager()
686709
{
687710
return resourceManager;
Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
package bdv.tools.links;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.stream.IntStream;
7+
8+
import bdv.tools.brightness.ConverterSetup;
9+
import bdv.tools.JsonUtils.TypedList;
10+
import bdv.util.Bounds;
11+
import bdv.viewer.ConverterSetups;
12+
import bdv.viewer.DisplayMode;
13+
import bdv.viewer.Interpolation;
14+
import bdv.viewer.SourceAndConverter;
15+
import bdv.viewer.ViewerState;
16+
import net.imglib2.Dimensions;
17+
import net.imglib2.FinalDimensions;
18+
import net.imglib2.Point;
19+
import net.imglib2.realtransform.AffineTransform3D;
20+
import net.imglib2.type.numeric.ARGBType;
21+
22+
class BdvPropertiesV0
23+
{
24+
private final AffineTransform3D transform;
25+
private final int timepoint;
26+
private final DisplayMode displaymode;
27+
private final Interpolation interpolation;
28+
private final TypedList< ResourceSpec< ? > > sourceSpecs;
29+
private final TypedList< ResourceConfig > sourceConfigs;
30+
private final List< SourceConverterConfig > converterConfigs;
31+
private final int currentSourceIndex;
32+
private final int[] activeSourceIndices;
33+
private final long[] panelsize;
34+
private final long[] mousepos;
35+
36+
private final Anchor anchor;
37+
38+
public enum Anchor
39+
{
40+
CENTER( "center" ),
41+
MOUSE( "mouse" );
42+
43+
private final String label;
44+
45+
Anchor( final String label )
46+
{
47+
this.label = label;
48+
}
49+
50+
@Override
51+
public String toString()
52+
{
53+
return label;
54+
}
55+
56+
public static Anchor fromString( final String string )
57+
{
58+
for ( final Anchor value : values() )
59+
if ( value.toString().equals( string ) )
60+
return value;
61+
return null;
62+
}
63+
}
64+
65+
public BdvPropertiesV0()
66+
{
67+
transform = new AffineTransform3D();
68+
timepoint = 0;
69+
displaymode = DisplayMode.SINGLE;
70+
interpolation = Interpolation.NLINEAR;
71+
sourceSpecs = new TypedList<>();
72+
sourceConfigs = new TypedList<>();
73+
converterConfigs = new ArrayList<>();
74+
currentSourceIndex = -1;
75+
activeSourceIndices = new int[ 0 ];
76+
panelsize = new long[ 2 ];
77+
mousepos = new long[ 2 ];
78+
anchor = Anchor.CENTER;
79+
}
80+
81+
public BdvPropertiesV0(
82+
final ViewerState state,
83+
final ConverterSetups converterSetups,
84+
final Dimensions panelsize,
85+
final Point mousepos,
86+
final ResourceManager resources )
87+
{
88+
this.transform = state.getViewerTransform();
89+
this.timepoint = state.getCurrentTimepoint();
90+
this.displaymode = state.getDisplayMode();
91+
this.interpolation = state.getInterpolation();
92+
this.sourceSpecs = getSourceSpecs( state.getSources(), resources );
93+
this.sourceConfigs = getSourceConfigs( sourceSpecs.list(), resources );
94+
this.converterConfigs = getSourceConverterConfigs( state.getSources(), converterSetups );
95+
this.currentSourceIndex = getCurrentSourceIndex( state );
96+
this.activeSourceIndices = getActiveSourceIndices( state );
97+
this.panelsize = panelsize.dimensionsAsLongArray();
98+
this.mousepos = mousepos.positionAsLongArray();
99+
this.anchor = Anchor.CENTER;
100+
}
101+
102+
private static int getCurrentSourceIndex( final ViewerState state )
103+
{
104+
return state.getSources().indexOf( state.getCurrentSource() );
105+
}
106+
107+
private static int[] getActiveSourceIndices( final ViewerState state )
108+
{
109+
final List< SourceAndConverter< ? > > sources = state.getSources();
110+
return IntStream.range( 0, sources.size() ).filter( i -> state.isSourceActive( sources.get( i ) ) ).toArray();
111+
}
112+
113+
private static List< SourceConverterConfig > getSourceConverterConfigs( final List< SourceAndConverter< ? > > sources, final ConverterSetups converterSetups )
114+
{
115+
List< SourceConverterConfig > configs = new ArrayList<>();
116+
for ( SourceAndConverter< ? > soc : sources )
117+
{
118+
final ConverterSetup setup = converterSetups.getConverterSetup( soc );
119+
final boolean hasColor = setup.supportsColor();
120+
final int color = setup.getColor().get();
121+
final double min = setup.getDisplayRangeMin();
122+
final double max = setup.getDisplayRangeMax();
123+
final Bounds bounds = converterSetups.getBounds().getBounds( setup );
124+
final double minBound = bounds.getMinBound();
125+
final double maxBound = bounds.getMaxBound();
126+
configs.add( new SourceConverterConfig( hasColor, color, min, max, minBound, maxBound ) );
127+
}
128+
return configs;
129+
}
130+
131+
private static TypedList< ResourceSpec< ? > > getSourceSpecs( final List< SourceAndConverter< ? > > sources, final ResourceManager resources )
132+
{
133+
List< ResourceSpec< ? > > sourceSpecs = new ArrayList<>();
134+
for ( final SourceAndConverter< ? > soc : sources )
135+
{
136+
sourceSpecs.add( resources.getResourceSpec( soc ) );
137+
}
138+
return new TypedList<>( sourceSpecs );
139+
}
140+
141+
private static TypedList< ResourceConfig > getSourceConfigs( final List< ResourceSpec< ? > > sourceSpecs, final ResourceManager resources )
142+
{
143+
final List< ResourceConfig > sourceConfigs = new ArrayList<>();
144+
for ( ResourceSpec< ? > spec : sourceSpecs ) {
145+
sourceConfigs.add( spec.getConfig( resources ) );
146+
}
147+
return new TypedList<>( sourceConfigs );
148+
}
149+
150+
public AffineTransform3D transform()
151+
{
152+
return transform;
153+
}
154+
155+
public int timepoint()
156+
{
157+
return timepoint;
158+
}
159+
160+
public Dimensions panelsize()
161+
{
162+
return FinalDimensions.wrap( panelsize );
163+
}
164+
165+
public Point mousepos()
166+
{
167+
return Point.wrap( mousepos );
168+
}
169+
170+
public Anchor getAnchor()
171+
{
172+
return anchor;
173+
}
174+
175+
public DisplayMode displaymode()
176+
{
177+
return displaymode;
178+
}
179+
180+
public Interpolation interpolation()
181+
{
182+
return interpolation;
183+
}
184+
185+
public List< ResourceSpec< ? > > sourceSpecs()
186+
{
187+
return sourceSpecs.list();
188+
}
189+
190+
public List< ResourceConfig > sourceConfigs()
191+
{
192+
return sourceConfigs.list();
193+
}
194+
195+
public List< SourceConverterConfig > converterConfigs()
196+
{
197+
return converterConfigs;
198+
}
199+
200+
public int currentSourceIndex()
201+
{
202+
return currentSourceIndex;
203+
}
204+
205+
public int[] activeSourceIndices()
206+
{
207+
return activeSourceIndices;
208+
}
209+
210+
@Override
211+
public String toString()
212+
{
213+
return "BdvPropertiesV0{" +
214+
"transform=" + transform +
215+
", timepoint=" + timepoint +
216+
", displaymode=" + displaymode +
217+
", interpolation=" + interpolation +
218+
", sourceSpecs =" + sourceSpecs +
219+
", sourceConfigs =" + sourceConfigs +
220+
", converterConfigs=" + converterConfigs +
221+
", currentSourceIndex=" + currentSourceIndex +
222+
", activeSourceIndices=" + Arrays.toString( activeSourceIndices ) +
223+
", panelsize=" + Arrays.toString( panelsize ) +
224+
", mousepos=" + Arrays.toString( mousepos ) +
225+
", anchor=" + anchor +
226+
'}';
227+
}
228+
229+
public static class SourceConverterConfig
230+
{
231+
final boolean hasColor;
232+
final int color;
233+
final double min;
234+
final double max;
235+
final double minBound;
236+
final double maxBound;
237+
238+
public SourceConverterConfig( final boolean hasColor, final int color, final double min, final double max, final double minBound, final double maxBound )
239+
{
240+
this.hasColor = hasColor;
241+
this.color = color;
242+
this.min = min;
243+
this.max = max;
244+
this.minBound = minBound;
245+
this.maxBound = maxBound;
246+
}
247+
248+
public double rangeMin() {
249+
return min;
250+
}
251+
252+
public double rangeMax() {
253+
return max;
254+
}
255+
256+
public ARGBType color() {
257+
return new ARGBType( color );
258+
}
259+
260+
public Bounds bounds() {
261+
return new Bounds( minBound, maxBound );
262+
}
263+
264+
@Override
265+
public String toString()
266+
{
267+
return "SourceConverterConfig{" +
268+
"hasColor=" + hasColor +
269+
", color=" + String.format("#%08x", color) +
270+
", min=" + min +
271+
", max=" + max +
272+
", minBound=" + minBound +
273+
", maxBound=" + maxBound +
274+
'}';
275+
}
276+
}
277+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package bdv.tools.links;
2+
3+
import java.awt.Toolkit;
4+
import java.awt.datatransfer.Clipboard;
5+
import java.awt.datatransfer.DataFlavor;
6+
import java.awt.datatransfer.StringSelection;
7+
import java.awt.datatransfer.Transferable;
8+
import java.awt.datatransfer.UnsupportedFlavorException;
9+
import java.io.IOException;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
class ClipboardUtils
14+
{
15+
private static final Logger LOG = LoggerFactory.getLogger( ClipboardUtils.class );
16+
17+
static void copyToClipboard( final String string )
18+
{
19+
final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
20+
clipboard.setContents( new StringSelection( string ), null );
21+
}
22+
23+
static String getFromClipboard()
24+
{
25+
final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
26+
try
27+
{
28+
Transferable transferable = clipboard.getContents(DataFlavor.stringFlavor);
29+
if ( transferable != null )
30+
{
31+
return ( String ) transferable.getTransferData( DataFlavor.stringFlavor );
32+
}
33+
}
34+
catch ( UnsupportedFlavorException | IOException e )
35+
{
36+
LOG.debug( "Unable to retrieve string from system clipboard", e );
37+
}
38+
return null;
39+
}
40+
}

0 commit comments

Comments
 (0)