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.4KB

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