public HdfsResourceTypePresenter() { _hostnameField = WidgetFactory.createTextField("hostname", 10); _hostnameField.setText("localhost"); _portField = WidgetFactory.createTextField("port", 4); _portField.setDocument(new NumberDocument(false, false)); _portField.setText("9000"); _pathTextField = WidgetFactory.createTextField("path", 12); _pathTextField.setText("/"); final DCDocumentListener documentListener = new DCDocumentListener() { @Override protected void onChange(DocumentEvent event) { onInputChanged(); } }; _hostnameField.getDocument().addDocumentListener(documentListener); _portField.getDocument().addDocumentListener(documentListener); _pathTextField.getDocument().addDocumentListener(documentListener); _panel = DCPanel.flow(Alignment.LEFT, 2, 0, _hostnameField, _portField, _pathTextField); }
/** * A widget for selecting a file(name). It will be represented as a textfield with a browse button. * * <p>It is preferred to use this widget's "big brother" implementation, {@link ResourceSelector} * which will work with any type of {@link Resource}, not just files (e.g. {@link FileResource} and * others). */ public final class FilenameTextField extends DCPanel implements ResourceTypePresenter<FileResource> { private static final long serialVersionUID = 1L; private final JXTextField _textField = WidgetFactory.createTextField("Filename"); private final JButton _browseButton = WidgetFactory.createDefaultButton("Browse", IconUtils.ACTION_BROWSE); private final List<FileSelectionListener> _fileSelectionListeners = new ArrayList<>(); private final List<ResourceTypePresenter.Listener> _resourceListeners = new ArrayList<>(); private final List<FileFilter> _chooseableFileFilters = new ArrayList<>(); private volatile FileFilter _selectedFileFilter; private volatile File _directory; private boolean _textFieldUpdating = false; private int _fileSelectionMode = JFileChooser.FILES_ONLY; /** * @param directory * @param fileOpenDialog true if browse dialog should be an "open file" dialog or false if it * should be a "save file" dialog. */ public FilenameTextField(File directory, final boolean fileOpenDialog) { super(); _directory = directory; setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); add(_textField); add(Box.createHorizontalStrut(4)); add(_browseButton); _browseButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { final DCFileChooser fileChooser; if (_directory == null) { fileChooser = new DCFileChooser(); } else { fileChooser = new DCFileChooser(_directory); } WidgetUtils.centerOnScreen(fileChooser); for (FileFilter filter : _chooseableFileFilters) { fileChooser.addChoosableFileFilter(filter); } fileChooser.setFileSelectionMode(_fileSelectionMode); if (_selectedFileFilter != null) { if (!_chooseableFileFilters.contains(_selectedFileFilter)) { _chooseableFileFilters.add(_selectedFileFilter); } fileChooser.setFileFilter(_selectedFileFilter); } int result; if (fileOpenDialog) { result = fileChooser.showOpenDialog(FilenameTextField.this); } else { result = fileChooser.showSaveDialog(FilenameTextField.this); } if (result == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); boolean accepted = true; if (fileOpenDialog) { accepted = file.exists(); } if (accepted) { setFile(file); if (file.isDirectory()) { _directory = file; } else { _directory = file.getParentFile(); } notifyListeners(file); } } } }); _textField .getDocument() .addDocumentListener( new DCDocumentListener() { @Override protected void onChange(DocumentEvent event) { _textFieldUpdating = true; try { final File file = getFile(); if (file == null) { final String text = _textField.getText(); notifyListeners(text); } else { notifyListeners(file); } } finally { _textFieldUpdating = false; } } }); } private void notifyListeners(String text) { for (Listener listener : _resourceListeners) { listener.onPathEntered(this, text); } } private void notifyListeners(final File file) { for (FileSelectionListener listener : _fileSelectionListeners) { listener.onSelected(this, file); } final Resource fileResource = new FileResource(file); for (Listener listener : _resourceListeners) { listener.onResourceSelected(this, fileResource); } } public File getDirectory() { return _directory; } public JButton getBrowseButton() { return _browseButton; } public JXTextField getTextField() { return _textField; } public String getFilename() { return _textField.getText(); } @Override public void setEnabled(boolean enabled) { super.setEnabled(enabled); _browseButton.setEnabled(enabled); _textField.setEnabled(enabled); } public void setFilename(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); } public void setFile(File file) { try { setFilename(file.getCanonicalPath()); } catch (IOException e1) { // ignore setFilename(file.getAbsolutePath()); } } public File getFile() { String filename = getFilename(); if (StringUtils.isNullOrEmpty(filename)) { return null; } return new File(filename); } public void addFileSelectionListener(FileSelectionListener listener) { _fileSelectionListeners.add(listener); } public void removeFileSelectionListener(FileSelectionListener listener) { _fileSelectionListeners.remove(listener); } @Override public void addChoosableFileFilter(FileFilter filter) { _chooseableFileFilters.add(filter); } @Override public void setSelectedFileFilter(FileFilter filter) { _selectedFileFilter = filter; } @Override public void removeChoosableFileFilter(FileFilter filter) { _chooseableFileFilters.remove(filter); } public void setFileSelectionMode(int fileSelectionMode) { _fileSelectionMode = fileSelectionMode; } public int getFileSelectionMode() { return _fileSelectionMode; } @Override public JComponent getWidget() { return this; } @Override public FileResource getResource() { final File file = getFile(); if (file == null) { return null; } return new FileResource(file); } @Override public void setResource(FileResource resource) { setFile(resource.getFile()); } @Override public void addListener(org.datacleaner.widgets.ResourceTypePresenter.Listener listener) { _resourceListeners.add(listener); } @Override public void removeListener(org.datacleaner.widgets.ResourceTypePresenter.Listener listener) { _resourceListeners.remove(listener); } }