You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileBrowser.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing.components;
  18. import com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField;
  19. import com.dmdirc.config.prefs.PreferencesSetting;
  20. import com.dmdirc.util.collections.ListenerList;
  21. import java.awt.event.ActionEvent;
  22. import java.awt.event.ActionListener;
  23. import java.awt.event.KeyListener;
  24. import javax.swing.JButton;
  25. import javax.swing.JFileChooser;
  26. import javax.swing.JPanel;
  27. import net.miginfocom.swing.MigLayout;
  28. /**
  29. * File browser component.
  30. *
  31. * @since 0.6.3
  32. */
  33. public class FileBrowser extends JPanel implements ActionListener {
  34. /** A version number for this class. */
  35. private static final long serialVersionUID = 1;
  36. /** Text field to show path of chosen file. */
  37. private final ValidatingJTextField pathField;
  38. /** File browsing window. */
  39. private final JFileChooser fileChooser = new JFileChooser();
  40. /** Our listeners. */
  41. private final ListenerList listeners = new ListenerList();
  42. /**
  43. * Creates a new File Browser.
  44. *
  45. * @param iconManager Icon Manager to get icons from
  46. * @param setting The setting to create the component for
  47. * @param type The type of filechooser we want (Files/Directories/Both)
  48. */
  49. public FileBrowser(final IconManager iconManager,
  50. final PreferencesSetting setting, final int type) {
  51. fileChooser.setFileSelectionMode(type);
  52. final JButton browseButton = new JButton("Browse");
  53. browseButton.addActionListener(this);
  54. pathField
  55. = new ValidatingJTextField(iconManager, setting.getValue(), setting.getValidator());
  56. setLayout(new MigLayout("ins 0, fill"));
  57. add(pathField, "growx, pushx, sgy all");
  58. add(browseButton, "sgy all");
  59. }
  60. /**
  61. * {@inheritDoc}.
  62. *
  63. * @param e Action event
  64. */
  65. @Override
  66. public void actionPerformed(final ActionEvent e) {
  67. fileChooser.showOpenDialog(this);
  68. if (fileChooser.getSelectedFile() != null) {
  69. pathField.setText(fileChooser.getSelectedFile().getAbsolutePath());
  70. }
  71. fireActionEvent();
  72. }
  73. /**
  74. * Adds an action listener to this file browser. Action listeners are notified whenever the path
  75. * changes.
  76. *
  77. * @param l The listener to be added
  78. */
  79. public void addActionListener(final ActionListener l) {
  80. listeners.add(ActionListener.class, l);
  81. }
  82. /**
  83. * {@inheritDoc}.
  84. *
  85. * @param l The listener to be added
  86. */
  87. @Override
  88. public void addKeyListener(final KeyListener l) {
  89. pathField.addKeyListener(l);
  90. }
  91. /**
  92. * Returns the current path selected by this file browser.
  93. *
  94. * @return Path selected by this file browser
  95. */
  96. public String getPath() {
  97. return pathField.getText();
  98. }
  99. /**
  100. * Informs all action listeners that an action has occurred.
  101. */
  102. protected void fireActionEvent() {
  103. for (ActionListener listener : listeners.get(ActionListener.class)) {
  104. listener.actionPerformed(new ActionEvent(this, 1, getPath()));
  105. }
  106. }
  107. }