Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add URI widget #86

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@

<jdatepicker.version>1.3.2</jdatepicker.version>
<object-inspector.version>0.1</object-inspector.version>
<scijava-common.version>99.0</scijava-common.version>
</properties>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public static List<File> filterFiles(final List<File> list,

// -- Helper classes --

private class FileTransferHandler extends TransferHandler {
public static class FileTransferHandler extends TransferHandler {

private final String style;

Expand Down
174 changes: 174 additions & 0 deletions src/main/java/org/scijava/ui/swing/widget/SwingURIWidget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* #%L
* SciJava UI components for Java Swing.
* %%
* Copyright (C) 2010 - 2023 SciJava developers.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

package org.scijava.ui.swing.widget;

import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.ui.UIService;
import org.scijava.widget.FileWidget;
import org.scijava.widget.InputWidget;
import org.scijava.widget.URIWidget;
import org.scijava.widget.WidgetModel;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;

/**
* Swing implementation of URI selector widget.
*
* @author Christian Tischer
* @author Jan Eglinger
*/
@Plugin(type = InputWidget.class)
public class SwingURIWidget extends SwingInputWidget<URI> implements
URIWidget<JPanel>, ActionListener, DocumentListener
{

@Parameter
private UIService uiService;

private JTextField uriTextField;
private JButton browse;

// -- InputWidget methods --

@Override
public URI getValue() {
final String text = uriTextField.getText();
if (text.isEmpty()) {
return null;
}
File file = new File(text);
try
{
if (file.exists() || file.isAbsolute()) {
return file.toURI();
}
return new URI(text);
} catch ( URISyntaxException e )
{
return null;
}
}

// -- WrapperPlugin methods --

@Override
public void set(final WidgetModel model) {
super.set(model);

uriTextField = new JTextField(16);
uriTextField.setDragEnabled(true);
final String style = model.getItem().getWidgetStyle();
uriTextField.setTransferHandler(new SwingFileWidget.FileTransferHandler(style));
setToolTip( uriTextField );
getComponent().add( uriTextField );
uriTextField.getDocument().addDocumentListener(this);

getComponent().add(Box.createHorizontalStrut(3));

browse = new JButton("Browse");
setToolTip(browse);
getComponent().add(browse);
browse.addActionListener(this);

refreshWidget();
}

// -- Typed methods --

@Override
public boolean supports(final WidgetModel model) {
return super.supports(model) && model.isType(URI.class);
}

// -- ActionListener methods --

@Override
public void actionPerformed(final ActionEvent e) {
File file = new File(uriTextField.getText());

if (!file.isDirectory()) {
file = file.getParentFile();
}

// display file chooser in appropriate mode
final WidgetModel model = get();
final String style;
if (model.isStyle(FileWidget.DIRECTORY_STYLE)) {
style = FileWidget.DIRECTORY_STYLE;
}
else if (model.isStyle(FileWidget.SAVE_STYLE)) {
style = FileWidget.SAVE_STYLE;
}
else {
style = FileWidget.OPEN_STYLE;
}
file = uiService.chooseFile(file, style);
if (file == null) return;

uriTextField.setText(file.getAbsolutePath());
}

// -- DocumentListener methods --

@Override
public void changedUpdate(final DocumentEvent e) {
updateModel();
}

@Override
public void insertUpdate(final DocumentEvent e) {
updateModel();
}

@Override
public void removeUpdate(final DocumentEvent e) {
updateModel();
}

// -- AbstractUIInputWidget methods ---

@Override
public void doRefresh() {
// final String text = get().getText();
// if (text.equals( uriTextField.getText())) return; // no change
// uriTextField.setText(text);
}
}
26 changes: 26 additions & 0 deletions src/test/java/org/scijava/ui/swing/URITest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.scijava.ui.swing;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class URITest
{
public static void main( String[] args ) throws URISyntaxException, MalformedURLException
{
File f = new File("tischer");
System.out.println(f.toURI());
System.out.println( new URI( "file", "/tischer/test.txt" , null).toString() );
URI uri = new URI( "file:/path/to/local/file.txt" );
System.out.println( uri.getScheme() );
URL url = uri.toURL();
System.out.println(url.getFile());
if ( uri.getScheme().equals( "file" ) )
{
File file = new File( uri.toURL().getPath() );
System.out.println( file.getAbsolutePath() );
}
}
}
57 changes: 57 additions & 0 deletions src/test/java/org/scijava/ui/swing/widget/SwingURIWidgetDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*-
* #%L
* SciJava UI components for Java Swing.
* %%
* Copyright (C) 2010 - 2023 SciJava developers.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.scijava.ui.swing.widget;

import org.scijava.Context;
import org.scijava.command.Command;
import org.scijava.command.CommandService;
import org.scijava.plugin.Parameter;
import org.scijava.ui.UIService;
import org.scijava.widget.FileListWidget;
import org.scijava.widget.FileWidget;

import java.net.URI;

public class SwingURIWidgetDemo implements Command {


@Parameter(style = FileListWidget.FILES_AND_DIRECTORIES, persist = false)
private URI uri;

@Override
public void run() {
System.out.println(uri);
}

public static void main(final String... args) throws Exception {
Context context = new Context();
context.service(UIService.class).showUI();
context.service(CommandService.class).run( SwingURIWidgetDemo.class, true);
}
}
Loading