|
| 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 | +} |
0 commit comments