All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.datacleaner.widgets.AbstractResourceTextField Maven / Gradle / Ivy

/**
 * DataCleaner (community edition)
 * Copyright (C) 2014 Free Software Foundation, Inc.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.datacleaner.widgets;

import java.util.ArrayList;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.event.DocumentEvent;
import javax.swing.filechooser.FileFilter;

import org.apache.metamodel.util.Resource;
import org.datacleaner.panels.DCPanel;
import org.datacleaner.util.DCDocumentListener;
import org.datacleaner.util.IconUtils;
import org.datacleaner.util.WidgetFactory;
import org.datacleaner.util.WidgetUtils;
import org.jdesktop.swingx.JXTextField;


public abstract class AbstractResourceTextField extends DCPanel
        implements ResourceTypePresenter {

    private static final long serialVersionUID = 1L;

    protected final JXTextField _textField = WidgetFactory.createTextField("Filename");
    protected final JButton _browseButton = WidgetFactory.createDefaultButton("Browse", IconUtils.ACTION_BROWSE);
    protected final List _resourceListeners = new ArrayList<>();
    protected final List _choosableFileFilters = new ArrayList<>();
    protected volatile FileFilter _selectedFileFilter;
    protected boolean _textFieldUpdating = false;
    protected int _fileSelectionMode = JFileChooser.FILES_ONLY;

    public AbstractResourceTextField() {
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        add(_textField);
        add(Box.createHorizontalStrut(WidgetUtils.DEFAULT_PADDING));
        add(_browseButton);

        _textField.getDocument().addDocumentListener(new DCDocumentListener() {
            @Override
            protected void onChange(final DocumentEvent event) {
                _textFieldUpdating = true;
                _textField.setToolTipText(_textField.getText().isEmpty() ? "Filename" : _textField.getText());
                try {
                    final String text = _textField.getText();
                    notifyListeners(text);
                } finally {
                    _textFieldUpdating = false;
                }
            }
        });

    }

    protected void notifyListeners(final String text) {
        final Resource resource = getResource();
        for (final Listener listener : _resourceListeners) {
            listener.onPathEntered(this, text);
            if (resource != null) {
                listener.onResourceSelected(this, resource);
            }
        }
    }

    public JButton getBrowseButton() {
        return _browseButton;
    }

    public JXTextField getTextField() {
        return _textField;
    }

    public String getFilename() {
        return _textField.getText();
    }

    public void setFilename(final String filename) {
        if (_textFieldUpdating) {
            // ignore this event - it's a call back from listeners that reacted
            // to a text field change.
            return;
        }
        _textField.setText(filename);
    }

    @Override
    public void setEnabled(final boolean enabled) {
        super.setEnabled(enabled);

        _browseButton.setEnabled(enabled);
        _textField.setEnabled(enabled);
    }

    @Override
    public void addChoosableFileFilter(final FileFilter filter) {
        _choosableFileFilters.add(filter);
    }

    @Override
    public void setSelectedFileFilter(final FileFilter filter) {
        _selectedFileFilter = filter;
    }

    @Override
    public void removeChoosableFileFilter(final FileFilter filter) {
        _choosableFileFilters.remove(filter);
    }

    public int getFileSelectionMode() {
        return _fileSelectionMode;
    }

    public void setFileSelectionMode(final int fileSelectionMode) {
        _fileSelectionMode = fileSelectionMode;
    }

    @Override
    public JComponent getWidget() {
        return this;
    }

    @Override
    public void addListener(final Listener listener) {
        _resourceListeners.add(listener);
    }

    @Override
    public void removeListener(final Listener listener) {
        _resourceListeners.remove(listener);
    }

    @Override
    public abstract R getResource();

    @Override
    public abstract void setResource(R resource);

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy