Skip to content

Commit c63183f

Browse files
committed
WIP implement URI widget
1 parent 2770bbf commit c63183f

File tree

5 files changed

+318
-1
lines changed

5 files changed

+318
-1
lines changed

src/main/java/org/scijava/ui/swing/widget/SwingFileWidget.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ public static List<File> filterFiles(final List<File> list,
321321

322322
// -- Helper classes --
323323

324-
private class FileTransferHandler extends TransferHandler {
324+
public static class FileTransferHandler extends TransferHandler {
325325

326326
private final String style;
327327

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
* #%L
3+
* SciJava UI components for Java Swing.
4+
* %%
5+
* Copyright (C) 2010 - 2023 SciJava 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+
30+
package org.scijava.ui.swing.widget;
31+
32+
import org.scijava.plugin.Parameter;
33+
import org.scijava.plugin.Plugin;
34+
import org.scijava.ui.UIService;
35+
import org.scijava.widget.FileWidget;
36+
import org.scijava.widget.InputWidget;
37+
import org.scijava.widget.WidgetModel;
38+
39+
import javax.swing.*;
40+
import javax.swing.event.DocumentEvent;
41+
import javax.swing.event.DocumentListener;
42+
import java.awt.event.ActionEvent;
43+
import java.awt.event.ActionListener;
44+
import java.io.File;
45+
import java.net.URI;
46+
import java.net.URISyntaxException;
47+
48+
/**
49+
* Swing implementation of file selector widget.
50+
*
51+
* @author Curtis Rueden
52+
*/
53+
@Plugin(type = InputWidget.class)
54+
public class SwingURIWidget extends SwingInputWidget<URI> implements
55+
URIWidget<JPanel>, ActionListener, DocumentListener
56+
{
57+
58+
@Parameter
59+
private UIService uiService;
60+
61+
private JTextField uriTextField;
62+
private JButton browse;
63+
64+
// -- InputWidget methods --
65+
66+
@Override
67+
public URI getValue() {
68+
final String text = uriTextField.getText();
69+
if (text.isEmpty()) {
70+
return null;
71+
}
72+
File file = new File(text);
73+
try
74+
{
75+
if (file.exists() || file.isAbsolute()) {
76+
return file.toURI();
77+
}
78+
return new URI(text);
79+
} catch ( URISyntaxException e )
80+
{
81+
return null;
82+
}
83+
}
84+
85+
// -- WrapperPlugin methods --
86+
87+
@Override
88+
public void set(final WidgetModel model) {
89+
super.set(model);
90+
91+
uriTextField = new JTextField(16);
92+
uriTextField.setDragEnabled(true);
93+
final String style = model.getItem().getWidgetStyle();
94+
uriTextField.setTransferHandler(new SwingFileWidget.FileTransferHandler(style));
95+
setToolTip( uriTextField );
96+
getComponent().add( uriTextField );
97+
uriTextField.getDocument().addDocumentListener(this);
98+
99+
getComponent().add(Box.createHorizontalStrut(3));
100+
101+
browse = new JButton("Browse");
102+
setToolTip(browse);
103+
getComponent().add(browse);
104+
browse.addActionListener(this);
105+
106+
refreshWidget();
107+
}
108+
109+
// -- Typed methods --
110+
111+
@Override
112+
public boolean supports(final WidgetModel model) {
113+
return super.supports(model) && model.isType(URI.class);
114+
}
115+
116+
// -- ActionListener methods --
117+
118+
@Override
119+
public void actionPerformed(final ActionEvent e) {
120+
File file = new File(uriTextField.getText());
121+
122+
if (!file.isDirectory()) {
123+
file = file.getParentFile();
124+
}
125+
126+
// display file chooser in appropriate mode
127+
final WidgetModel model = get();
128+
final String style;
129+
if (model.isStyle(FileWidget.DIRECTORY_STYLE)) {
130+
style = FileWidget.DIRECTORY_STYLE;
131+
}
132+
else if (model.isStyle(FileWidget.SAVE_STYLE)) {
133+
style = FileWidget.SAVE_STYLE;
134+
}
135+
else {
136+
style = FileWidget.OPEN_STYLE;
137+
}
138+
file = uiService.chooseFile(file, style);
139+
if (file == null) return;
140+
141+
uriTextField.setText(file.getAbsolutePath());
142+
}
143+
144+
// -- DocumentListener methods --
145+
146+
@Override
147+
public void changedUpdate(final DocumentEvent e) {
148+
updateModel();
149+
}
150+
151+
@Override
152+
public void insertUpdate(final DocumentEvent e) {
153+
updateModel();
154+
}
155+
156+
@Override
157+
public void removeUpdate(final DocumentEvent e) {
158+
updateModel();
159+
}
160+
161+
// -- AbstractUIInputWidget methods ---
162+
163+
@Override
164+
public void doRefresh() {
165+
// final String text = get().getText();
166+
// if (text.equals( uriTextField.getText())) return; // no change
167+
// uriTextField.setText(text);
168+
}
169+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.scijava.ui.swing.widget;
2+
3+
/*
4+
* #%L
5+
* SciJava Common shared library for SciJava software.
6+
* %%
7+
* Copyright (C) 2009 - 2024 SciJava developers.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
import org.scijava.widget.InputWidget;
33+
34+
import java.net.URI;
35+
36+
/**
37+
* Widget interface for file selectors.
38+
*
39+
* @author Curtis Rueden
40+
*/
41+
public interface URIWidget<U> extends InputWidget<URI, U>
42+
{
43+
44+
/**
45+
* Widget style for file opener dialogs.
46+
*
47+
* @see org.scijava.plugin.Parameter#style()
48+
*/
49+
String OPEN_STYLE = "open";
50+
51+
/**
52+
* Widget style for file saver dialogs.
53+
*
54+
* @see org.scijava.plugin.Parameter#style()
55+
*/
56+
String SAVE_STYLE = "save";
57+
58+
/**
59+
* Widget style for directory chooser dialogs.
60+
*
61+
* @see org.scijava.plugin.Parameter#style()
62+
*/
63+
String DIRECTORY_STYLE = "directory";
64+
65+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.scijava.ui.swing;
2+
3+
import java.io.File;
4+
import java.net.MalformedURLException;
5+
import java.net.URI;
6+
import java.net.URISyntaxException;
7+
import java.net.URL;
8+
9+
public class URITest
10+
{
11+
public static void main( String[] args ) throws URISyntaxException, MalformedURLException
12+
{
13+
File f = new File("tischer");
14+
System.out.println(f.toURI());
15+
System.out.println( new URI( "file", "/tischer/test.txt" , null).toString() );
16+
URI uri = new URI( "file:/path/to/local/file.txt" );
17+
System.out.println( uri.getScheme() );
18+
URL url = uri.toURL();
19+
System.out.println(url.getFile());
20+
if ( uri.getScheme().equals( "file" ) )
21+
{
22+
File file = new File( uri.toURL().getPath() );
23+
System.out.println( file.getAbsolutePath() );
24+
}
25+
}
26+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*-
2+
* #%L
3+
* SciJava UI components for Java Swing.
4+
* %%
5+
* Copyright (C) 2010 - 2023 SciJava 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+
package org.scijava.ui.swing.widget;
30+
31+
import org.scijava.Context;
32+
import org.scijava.command.Command;
33+
import org.scijava.command.CommandService;
34+
import org.scijava.plugin.Parameter;
35+
import org.scijava.ui.UIService;
36+
import org.scijava.widget.FileListWidget;
37+
import org.scijava.widget.FileWidget;
38+
39+
import java.net.URI;
40+
41+
public class SwingURIWidgetDemo implements Command {
42+
43+
44+
@Parameter(style = FileListWidget.FILES_AND_DIRECTORIES, persist = false)
45+
private URI uri;
46+
47+
@Override
48+
public void run() {
49+
System.out.println(uri);
50+
}
51+
52+
public static void main(final String... args) throws Exception {
53+
Context context = new Context();
54+
context.service(UIService.class).showUI();
55+
context.service(CommandService.class).run( SwingURIWidgetDemo.class, true);
56+
}
57+
}

0 commit comments

Comments
 (0)