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.

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