Skip to content

Commit eea6f1f

Browse files
committed
generate url links and process them
1 parent 8a1ee3f commit eea6f1f

File tree

6 files changed

+148
-2
lines changed

6 files changed

+148
-2
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
<package-name>org.bigdataviewer.core</package-name>
143143
<license.licenseName>bsd_2</license.licenseName>
144144
<license.copyrightOwners>BigDataViewer developers.</license.copyrightOwners>
145+
<scijava.jvm.version>11</scijava.jvm.version>
145146

146147
<imglib2.version>7.1.4</imglib2.version>
147148
<imglib2-algorithm.version>0.17.2</imglib2-algorithm.version>
@@ -243,6 +244,11 @@
243244
<groupId>org.slf4j</groupId>
244245
<artifactId>slf4j-simple</artifactId>
245246
</dependency>
247+
<dependency>
248+
<groupId>org.scijava</groupId>
249+
<artifactId>scijava-links</artifactId>
250+
<version>1.0.0</version>
251+
</dependency>
246252

247253
<!-- Test dependencies -->
248254
<dependency>

src/main/java/bdv/BigDataViewer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ public void collapseCardPanel()
878878

879879
public static void main( final String[] args )
880880
{
881-
final String fn = "/Users/pietzsch/workspace/data/111010_weber_resave.xml";
881+
final String fn = "/Users/zouinkhim/Downloads/grid-3d-stitched-h5/dataset.xml";
882882
try
883883
{
884884
System.setProperty( "apple.laf.useScreenMenuBar", "true" );
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*-
2+
* #%L
3+
* BigDataViewer core classes with minimal dependencies.
4+
* %%
5+
* Copyright (C) 2012 - 2025 BigDataViewer developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
import bdv.util.Bdv;
30+
import bdv.util.BdvFunctions;
31+
import bdv.util.BdvOptions;
32+
import bdv.util.BdvStackSource;
33+
import net.imglib2.RandomAccessibleInterval;
34+
import net.imglib2.img.array.ArrayImgs;
35+
import net.imglib2.type.numeric.integer.UnsignedByteType;
36+
import org.janelia.saalfeldlab.n5.N5Reader;
37+
import org.scijava.links.AbstractLinkHandler;
38+
import org.scijava.links.LinkHandler;
39+
import org.scijava.plugin.Parameter;
40+
import org.scijava.plugin.Plugin;
41+
import org.scijava.ui.UIService;
42+
import org.slf4j.Logger;
43+
import org.slf4j.LoggerFactory;
44+
45+
import com.google.gson.JsonParseException;
46+
47+
import javax.swing.*;
48+
49+
import java.io.UnsupportedEncodingException;
50+
import java.net.URI;
51+
import java.net.URLDecoder;
52+
import java.nio.charset.StandardCharsets;
53+
54+
@Plugin(type = LinkHandler.class)
55+
public class BDVLinkHandlerPlugin extends AbstractLinkHandler {
56+
57+
private static final Logger LOG = LoggerFactory.getLogger( LinkActions.class );
58+
59+
public static final String PLUGIN_NAME = "BDV";
60+
61+
@Parameter
62+
private UIService uiService;
63+
64+
@Override
65+
public void handle(final URI uri){
66+
if (!supports(uri)) throw new UnsupportedOperationException("" + uri);
67+
String query = uri.getQuery();
68+
if (query == null || query.isEmpty()) {
69+
// TODO open an empty BDV window
70+
throw new UnsupportedOperationException("Not implemented yet");
71+
} else {
72+
String decoded_query;
73+
try{
74+
decoded_query = URLDecoder.decode(query, StandardCharsets.UTF_8.name());
75+
} catch (UnsupportedEncodingException e) {
76+
LOG.error("Failed to decode JSON from URI: {}", uri, e);
77+
return;
78+
}
79+
try
80+
{
81+
// TODO handle the decoded JSON query
82+
// Links.paste( decoded_query, panel, converterSetups, pasteSettings, resources );
83+
throw new UnsupportedOperationException("Not implemented yet");
84+
}
85+
catch ( final JsonParseException | IllegalArgumentException e )
86+
{
87+
LOG.debug( "pasted JSON is malformed:\n\"{}\"", pastedText, e );
88+
}
89+
}
90+
91+
}
92+
93+
94+
95+
@Override
96+
public boolean supports(final URI uri) {
97+
return super.supports(uri) && PLUGIN_NAME.equals(org.scijava.links.Links.operation(uri));
98+
}
99+
100+
101+
}

src/main/java/bdv/tools/links/LinkActions.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
import static bdv.tools.links.ClipboardUtils.getFromClipboard;
3232

33+
import java.io.UnsupportedEncodingException;
34+
3335
import org.scijava.plugin.Plugin;
3436
import org.scijava.ui.behaviour.io.gui.CommandDescriptionProvider;
3537
import org.scijava.ui.behaviour.io.gui.CommandDescriptions;
@@ -53,9 +55,11 @@ public class LinkActions
5355

5456
public static final String COPY_VIEWER_STATE = "copy viewer state";
5557
public static final String PASTE_VIEWER_STATE = "paste viewer state";
58+
public static final String GENERATE_LINK_VIEWER_STATE = "generate link viewer state";
5659

5760
public static final String[] COPY_VIEWER_STATE_KEYS = new String[] { "ctrl C", "meta C" };
5861
public static final String[] PASTE_VIEWER_STATE_KEYS = new String[] { "ctrl V", "meta V" };
62+
public static final String[] GENERATE_LINK_VIEWER_STATE_KEYS = new String[] { "ctrl L", "meta L" };
5963

6064
/*
6165
* Command descriptions for all provided commands
@@ -73,6 +77,7 @@ public void getCommandDescriptions( final CommandDescriptions descriptions )
7377
{
7478
descriptions.add( COPY_VIEWER_STATE, COPY_VIEWER_STATE_KEYS, "Copy the current viewer state as a string." );
7579
descriptions.add( PASTE_VIEWER_STATE, PASTE_VIEWER_STATE_KEYS, "Paste the current viewer state from a string." );
80+
descriptions.add( GENERATE_LINK_VIEWER_STATE, GENERATE_LINK_VIEWER_STATE_KEYS, "Generate a fiji uri link to the current viewer state and copy it to the clipboard." );
7681
}
7782
}
7883

@@ -85,6 +90,22 @@ private static void copyViewerState(
8590
ClipboardUtils.copyToClipboard( json.toString() );
8691
}
8792

93+
private static void generateLinkViewerState(
94+
final AbstractViewerPanel panel,
95+
final ConverterSetups converterSetups,
96+
final ResourceManager resources )
97+
{
98+
final JsonElement json = Links.copyJson( panel, converterSetups, resources );
99+
try{
100+
final String link = Links.generateLink( json );
101+
ClipboardUtils.copyToClipboard( link);
102+
LOG.debug( "Generated link: {}", link );
103+
}
104+
catch (final UnsupportedEncodingException e) {
105+
LOG.debug( "couldn't generate link from JSON:\n\"{}\"", json, e );
106+
}
107+
}
108+
88109
private static void pasteViewerState(
89110
final AbstractViewerPanel panel,
90111
final ConverterSetups converterSetups,
@@ -133,5 +154,8 @@ public static void install(
133154
COPY_VIEWER_STATE, COPY_VIEWER_STATE_KEYS );
134155
actions.runnableAction( () -> pasteViewerState( panel, converterSetups, pasteSettings, resources ),
135156
PASTE_VIEWER_STATE, PASTE_VIEWER_STATE_KEYS );
157+
actions.runnableAction( () -> generateLinkViewerState( panel, converterSetups, resources ),
158+
GENERATE_LINK_VIEWER_STATE, GENERATE_LINK_VIEWER_STATE_KEYS );
159+
136160
}
137161
}

src/main/java/bdv/tools/links/Links.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
import static bdv.tools.links.PasteSettings.SourceMatchingMethod.BY_INDEX;
77
import static bdv.tools.links.PasteSettings.SourceMatchingMethod.BY_SPEC_LOAD_MISSING;
88

9+
import java.io.UnsupportedEncodingException;
910
import java.lang.reflect.Type;
11+
import java.net.URLDecoder;
12+
import java.nio.charset.StandardCharsets;
1013
import java.util.Arrays;
1114
import java.util.List;
1215

@@ -41,6 +44,8 @@ class Links
4144
{
4245
private static final Logger LOG = LoggerFactory.getLogger( Links.class );
4346

47+
final static String BASE_URL = "fiji://bdv?";
48+
4449
static JsonElement copyJson(
4550
final AbstractViewerPanel panel,
4651
final ConverterSetups converterSetups,
@@ -413,5 +418,11 @@ public JsonElement serialize(
413418
}
414419
}
415420
}
421+
422+
public static String generateLink(JsonElement json) throws UnsupportedEncodingException{
423+
final String jsonString = json.toString();
424+
return BASE_URL + java.net.URLEncoder.encode(jsonString, java.nio.charset.StandardCharsets.UTF_8.name());
425+
}
426+
416427
}
417428

src/main/resources/bdv/ui/keymap/default.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,4 +394,8 @@
394394
- !mapping
395395
action: paste viewer state
396396
contexts: [bdv]
397-
triggers: [ctrl V, meta V]
397+
triggers: [ctrl V, meta V]
398+
- !mapping
399+
action: generate link viewer state
400+
contexts: [bdv]
401+
triggers: [ctrl L, meta L]

0 commit comments

Comments
 (0)