PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/CommonControls/common/gui/VFSPathFileList.java

#
Java | 157 lines | 113 code | 14 blank | 30 comment | 17 complexity | e8a334b7341a3ec1d344927e19ef491c MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * jEdit - Programmer's Text Editor
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright Š 2011 Matthieu Casanova
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. package common.gui;
  22. import java.awt.event.ActionEvent;
  23. import java.awt.event.ActionListener;
  24. import java.io.File;
  25. import java.util.ArrayList;
  26. import java.util.Collection;
  27. import java.util.StringTokenizer;
  28. import javax.swing.BorderFactory;
  29. import javax.swing.Box;
  30. import javax.swing.BoxLayout;
  31. import javax.swing.DefaultListModel;
  32. import javax.swing.JButton;
  33. import javax.swing.JList;
  34. import javax.swing.JPanel;
  35. import javax.swing.JScrollPane;
  36. import org.gjt.sp.jedit.GUIUtilities;
  37. import org.gjt.sp.jedit.TextUtilities;
  38. import org.gjt.sp.jedit.browser.VFSBrowser;
  39. import org.gjt.sp.jedit.jEdit;
  40. /**
  41. * A component that contains a JList with a list of path,
  42. * and two buttons add and remove to add paths.
  43. * This can be used in option panes.
  44. * The property given in the constructor contains the list of paths
  45. * separated by the File.pathSeparator
  46. * Calling save() will save the paths to that property.
  47. *
  48. * @author Matthieu Casanova
  49. */
  50. public class VFSPathFileList extends JPanel
  51. {
  52. private JList searchList;
  53. private DefaultListModel model;
  54. private JButton add;
  55. private JButton remove;
  56. private String pathProperty;
  57. private JScrollPane scroll;
  58. public VFSPathFileList()
  59. {
  60. setBorder(BorderFactory.createEtchedBorder());
  61. setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
  62. model = new DefaultListModel();
  63. searchList = new JList(model);
  64. searchList.setPrototypeCellValue("a");
  65. add = new JButton(jEdit.getProperty("common.add"));
  66. remove = new JButton(jEdit.getProperty("common.remove"));
  67. ActionListener actionListener = new MyActionListener();
  68. add.addActionListener(actionListener);
  69. remove.addActionListener(actionListener);
  70. Box box = Box.createHorizontalBox();
  71. box.add(add);
  72. box.add(Box.createHorizontalStrut(6));
  73. box.add(remove);
  74. box.add(Box.createHorizontalGlue());
  75. scroll = new JScrollPane(searchList);
  76. add(scroll);
  77. add(box);
  78. }
  79. public VFSPathFileList(String pathProperty)
  80. {
  81. this();
  82. this.pathProperty = pathProperty;
  83. scroll.setBorder(BorderFactory.createTitledBorder(jEdit.getProperty(pathProperty + ".label")));
  84. String property = jEdit.getProperty(pathProperty, "");
  85. StringTokenizer tokenizer = new StringTokenizer(property, File.pathSeparator);
  86. while (tokenizer.hasMoreTokens())
  87. {
  88. String s = tokenizer.nextToken();
  89. addPath(s);
  90. }
  91. }
  92. public void save()
  93. {
  94. if (pathProperty != null)
  95. {
  96. jEdit.setProperty(pathProperty, getPaths());
  97. }
  98. }
  99. public String getPaths()
  100. {
  101. int size = model.size();
  102. Collection<String> paths = new ArrayList<String>();
  103. for (int i = 0; i < size; i++)
  104. {
  105. String path = (String) model.getElementAt(i);
  106. paths.add(path);
  107. }
  108. return TextUtilities.join(paths, File.pathSeparator);
  109. }
  110. private void addPath(String path)
  111. {
  112. if (path != null && !path.isEmpty() && !model.contains(path))
  113. model.addElement(path);
  114. }
  115. private class MyActionListener implements ActionListener
  116. {
  117. private String lastBrowserDialog;
  118. @Override
  119. public void actionPerformed(ActionEvent e)
  120. {
  121. if (e.getSource() == add)
  122. {
  123. String[] strings = GUIUtilities
  124. .showVFSFileDialog(jEdit.getActiveView(), lastBrowserDialog,
  125. VFSBrowser.CHOOSE_DIRECTORY_DIALOG, true);
  126. if (strings != null)
  127. {
  128. for (String string : strings)
  129. {
  130. lastBrowserDialog = string;
  131. addPath(string);
  132. }
  133. }
  134. }
  135. if (e.getSource() == remove)
  136. {
  137. int selectedIndex = searchList.getSelectedIndex();
  138. if (selectedIndex != -1)
  139. model.remove(selectedIndex);
  140. }
  141. }
  142. }
  143. }