PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/CommonControls/common/gui/pathbuilder/PathBuilder.java

#
Java | 590 lines | 306 code | 78 blank | 206 comment | 72 complexity | 0fb7c0eb17fffa43c7a1d1b9c2e82592 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. package common.gui.pathbuilder;
  2. /*
  3. * PathBuilder.java
  4. * Part of the JSwat plugin for the jEdit text editor
  5. * Copyright (C) 2001 David Taylor
  6. * dtaylo11@bigpond.net.au
  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. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. import java.awt.*;
  23. import java.awt.event.*;
  24. import java.io.File;
  25. import java.util.*;
  26. import javax.swing.*;
  27. import javax.swing.event.ListSelectionListener;
  28. import javax.swing.event.ListSelectionEvent;
  29. import javax.swing.filechooser.*;
  30. import javax.swing.table.*;
  31. import javax.swing.text.*;
  32. import org.gjt.sp.jedit.jEdit;
  33. /** A component that allows a user to build a
  34. * classpath by selecting directories files using a filesystem
  35. * browser.<p>
  36. */
  37. public class PathBuilder extends JPanel implements ActionListener, ListSelectionListener {
  38. /**
  39. * The table that displays the path elements.<p>
  40. */
  41. private JTable pathElementTable;
  42. /**
  43. * The table model that holds the path elements.<p>
  44. */
  45. private PathElementTableModel pathElementModel;
  46. /**
  47. * A panel to hold the buttons.<p>
  48. */
  49. private JPanel btnPanel;
  50. /**
  51. * The button to add an element.<p>
  52. */
  53. private JButton addElement;
  54. /**
  55. * The button to remove an element.<p>
  56. */
  57. private JButton removeElement;
  58. /**
  59. * The button to move an element towards the front of the path.<p>
  60. */
  61. private JButton moveUp;
  62. /**
  63. * The button to move an element towards the end of the path.<p>
  64. */
  65. private JButton moveDown;
  66. /**
  67. * Whether the move buttons are enabled or not.<p>
  68. */
  69. private boolean moveButtonsEnabled = true;
  70. /**
  71. * Whether to enabled multi-selection in the file chooser or not.<p>
  72. */
  73. private boolean multiSelectionEnabled = false;
  74. /**
  75. * The elements of the path.<p>
  76. */
  77. private Vector elements;
  78. /**
  79. * The initial directory to show in the file dialog.<p>
  80. */
  81. private String startDirectory;
  82. /**
  83. * The file selection mode. By default it is FILES_AND_DIRECTORIES.<p>
  84. */
  85. private int fileSelectionMode;
  86. /**
  87. * A list of external action listeners added by users of this class.
  88. */
  89. private java.util.List<ActionListener> actionListeners = null;
  90. /**
  91. * A file filter to set on the file chooser.<p>
  92. */
  93. private FileFilter filter;
  94. private static final String PROPS_PREFIX = "common.gui.pathbuilder";
  95. private String addButtonText;
  96. private String removeButtonText;
  97. private String moveUpButtonText;
  98. private String moveDownButtonText;
  99. private String fileDialogTitle;
  100. private String fileDialogAction;
  101. /**
  102. * Creates a new PathBuilder. Title at the top of the path table will be
  103. * "Classpath Elements".
  104. *
  105. */
  106. public PathBuilder() {
  107. this("Classpath Elements");
  108. }
  109. /**
  110. * Creates a new PathBuilder.<p>
  111. *
  112. * @param title The title to display at the top of the path table.
  113. */
  114. public PathBuilder(String title) {
  115. super(new BorderLayout());
  116. elements = new Vector();
  117. pathElementModel = new PathElementTableModel(title);
  118. addButtonText = jEdit.getProperty(PROPS_PREFIX + ".addButtonText");
  119. removeButtonText = jEdit.getProperty(PROPS_PREFIX + ".removeButtonText");
  120. moveUpButtonText = jEdit.getProperty(PROPS_PREFIX + ".moveUpButtonText");
  121. moveDownButtonText = jEdit.getProperty(PROPS_PREFIX + ".moveDownButtonText");
  122. fileDialogTitle = jEdit.getProperty(PROPS_PREFIX + ".fileDialogTitle");
  123. fileDialogAction = jEdit.getProperty(PROPS_PREFIX + ".fileDialogAction");
  124. addElement = new JButton(addButtonText);
  125. addElement.addActionListener(this);
  126. removeElement = new JButton(removeButtonText);
  127. removeElement.addActionListener(this);
  128. moveUp = new JButton(moveUpButtonText);
  129. moveUp.addActionListener(this);
  130. moveDown = new JButton(moveDownButtonText);
  131. moveDown.addActionListener(this);
  132. btnPanel = new JPanel();
  133. btnPanel.setBorder(BorderFactory.createEmptyBorder(6, 0, 0, 0));
  134. btnPanel.add(addElement);
  135. btnPanel.add(removeElement);
  136. btnPanel.add(moveUp);
  137. btnPanel.add(moveDown);
  138. add(btnPanel, BorderLayout.SOUTH);
  139. removeElement.setEnabled(false);
  140. moveUp.setEnabled(false);
  141. moveDown.setEnabled(false);
  142. pathElementTable = new JTable(pathElementModel);
  143. JScrollPane tableScroller = new JScrollPane(pathElementTable);
  144. add(tableScroller, BorderLayout.CENTER);
  145. pathElementTable.getSelectionModel().addListSelectionListener(this);
  146. if(elements.size() > 0)
  147. pathElementTable.setRowSelectionInterval(0, 0);
  148. fileSelectionMode = JFileChooser.FILES_AND_DIRECTORIES;
  149. }
  150. /**
  151. * Set the text of the add element button.<p>
  152. *
  153. * @param text the String to display on the add element button.
  154. */
  155. public void setAddButtonText(String text) {
  156. //addButtonText = text;
  157. addElement.setText(text);
  158. }
  159. /**
  160. * Set the text of the remove element button.<p>
  161. *
  162. * @param text the String to display on the remove element button.
  163. */
  164. public void setRemoveButtonText(String text) {
  165. //removeButtonText = text;
  166. removeElement.setText(text);
  167. }
  168. /**
  169. * Set the text of the move up button.<p>
  170. *
  171. * @param text the String to display on the move up button.
  172. */
  173. public void setMoveUpButtonText(String text) {
  174. //moveUpButtonText = text;
  175. moveUp.setText(text);
  176. }
  177. /**
  178. * Set the text of the move down button.<p>
  179. *
  180. * @param text the String to display on the move down button.
  181. */
  182. public void setMoveDownButtonText(String text) {
  183. //moveDownButtonText = text;
  184. moveDown.setText(text);
  185. }
  186. /**
  187. * Set a file selection mode to customise type of files can be selected.<p>
  188. *
  189. * @param filter the filter to use.
  190. */
  191. public void setFileSelectionMode(int fsm) {
  192. this.fileSelectionMode = fsm;
  193. }
  194. /**
  195. * Set a filter to customise what files are displayed.<p>
  196. *
  197. * @param filter the filter to use.
  198. */
  199. public void setFileFilter(FileFilter filter) {
  200. this.filter = filter;
  201. }
  202. /**
  203. * Sets the initial directory to be displayed by the file dialog.<p>
  204. *
  205. * @param startDirectory the initial directory to be displayed by the
  206. * file dialog.
  207. */
  208. public void setStartDirectory(String startDirectory) {
  209. this.startDirectory = startDirectory;
  210. }
  211. /**
  212. * Sets the title of the file dialog.<p>
  213. *
  214. * @param fileDialogTitle the title of the file dialog.
  215. */
  216. public void setFileDialogTitle(String fileDialogTitle) {
  217. this.fileDialogTitle = fileDialogTitle;
  218. }
  219. /**
  220. * Sets the label of the file dialog "approve" button.<p>
  221. *
  222. * @param fileDialogAction the label of the file dialog "approve" button.
  223. */
  224. public void setFileDialogAction(String fileDialogAction) {
  225. this.fileDialogAction = fileDialogAction;
  226. }
  227. /**
  228. * Set the path to be displayed in the list box.<p>
  229. *
  230. * @param path the current path elements, separated by
  231. * File.pathSeparator.
  232. */
  233. public void setPath(String path) {
  234. int size = elements.size();
  235. if(size > 0)
  236. {
  237. elements.clear();
  238. pathElementModel.fireTableRowsDeleted(0, size - 1);
  239. }
  240. StringTokenizer st = new StringTokenizer(path, File.pathSeparator);
  241. while(st.hasMoreTokens()) {
  242. elements.addElement(st.nextToken());
  243. }
  244. if(elements.size() > 0) {
  245. pathElementModel.fireTableRowsInserted(0, elements.size() - 1);
  246. pathElementTable.setRowSelectionInterval(0, 0);
  247. }
  248. }
  249. /**
  250. * Set the path to be displayed in the list box.<p>
  251. *
  252. * @param path an array of the current path elements.
  253. */
  254. public void setPathArray(String[] path) {
  255. int size = elements.size();
  256. if ( size > 0 ) {
  257. elements.clear();
  258. pathElementModel.fireTableRowsDeleted(0, size - 1);
  259. }
  260. for(int i = 0; i < path.length; i++)
  261. elements.addElement(path[i]);
  262. if(elements.size() > 0) {
  263. pathElementModel.fireTableRowsInserted(0, elements.size() - 1);
  264. pathElementTable.setRowSelectionInterval(0, 0);
  265. }
  266. }
  267. /**
  268. * Returns the path built using this PathBuilder as a single String,
  269. * with the elements of the path separated by File.pathSeparator.<p>
  270. *
  271. * @return the path built using this PathBuilder.
  272. */
  273. public String getPath() {
  274. StringBuffer sb = new StringBuffer();
  275. for(int i = 0; i < elements.size(); i++) {
  276. sb.append((String)elements.elementAt(i));
  277. if(i < (elements.size() - 1))
  278. sb.append(File.pathSeparator);
  279. }
  280. return sb.toString();
  281. }
  282. /**
  283. * Returns the path built using this PathBuilder as an array of
  284. * Strings.<p>
  285. *
  286. * @return the path built using this PathBuilder.
  287. */
  288. public String[] getPathArray() {
  289. String[] pathArray = new String[elements.size()];
  290. for(int i = 0; i < elements.size(); i++)
  291. pathArray[i] = (String)elements.elementAt(i);
  292. return pathArray;
  293. }
  294. /**
  295. * Returns the last directory selected in the file chooser dialog
  296. *
  297. * @return the last selected directory
  298. */
  299. public String getStartDirectory() {
  300. return startDirectory;
  301. }
  302. /**
  303. * Enable or disable the move buttons.<p>
  304. *
  305. * @param enabled true to enabled the move up and move down buttons,
  306. * false to hide them.
  307. */
  308. public void setMoveButtonsEnabled(boolean enabled) {
  309. if(enabled == true && moveButtonsEnabled == false) {
  310. moveButtonsEnabled = true;
  311. btnPanel.add(moveUp);
  312. btnPanel.add(moveDown);
  313. }
  314. else if(enabled == false && moveButtonsEnabled == true) {
  315. moveButtonsEnabled = false;
  316. btnPanel.remove(moveDown);
  317. btnPanel.remove(moveUp);
  318. }
  319. }
  320. /**
  321. * Enable or disable multiple file selection in the file chooser.<p>
  322. *
  323. * @param multiSelectionEnabled true to enable multiple file selection,
  324. * false to disable it.
  325. */
  326. public void setMultiSelectionEnabled(boolean multiSelectionEnabled) {
  327. this.multiSelectionEnabled = multiSelectionEnabled;
  328. }
  329. /**
  330. * Listen to specific GUI events.
  331. *
  332. * @param evt the GUI event.
  333. */
  334. public void actionPerformed(ActionEvent evt) {
  335. int row;
  336. Object source = evt.getSource();
  337. if(source.equals(addElement)) {
  338. JFileChooser chooser;
  339. if(startDirectory != null)
  340. chooser = new JFileChooser(startDirectory);
  341. else
  342. chooser = new JFileChooser();
  343. chooser.setFileSelectionMode(fileSelectionMode);
  344. if(multiSelectionEnabled == true)
  345. chooser.setMultiSelectionEnabled(true);
  346. if(filter != null)
  347. chooser.addChoosableFileFilter(filter);
  348. chooser.setDialogTitle(fileDialogTitle);
  349. int returnVal = chooser.showDialog(null, fileDialogAction);
  350. if(returnVal == JFileChooser.APPROVE_OPTION) {
  351. if(multiSelectionEnabled == true) {
  352. File[] files = chooser.getSelectedFiles();
  353. for(int i = 0; i < files.length; i++)
  354. pathElementModel.add(files[i].getPath());
  355. } else
  356. pathElementModel.add(chooser.getSelectedFile().getPath());
  357. if(elements.size() == 1)
  358. pathElementTable.setRowSelectionInterval(0, 0);
  359. startDirectory = chooser.getCurrentDirectory().getPath();
  360. }
  361. }
  362. else if(source.equals(removeElement)) {
  363. row = pathElementTable.getSelectedRow();
  364. if(row >= 0) {
  365. pathElementModel.remove(row);
  366. }
  367. }
  368. else if(source.equals(moveUp)) {
  369. row = pathElementTable.getSelectedRow();
  370. if(row >= 1) {
  371. pathElementModel.moveUp(row);
  372. }
  373. }
  374. else if(source.equals(moveDown)) {
  375. row = pathElementTable.getSelectedRow();
  376. if(row < (elements.size() - 1)) {
  377. pathElementModel.moveDown(row);
  378. }
  379. }
  380. int tableSize = elements.size();
  381. if(tableSize < 1 && removeElement.isEnabled())
  382. removeElement.setEnabled(false);
  383. else if(tableSize > 0 && removeElement.isEnabled() != true)
  384. removeElement.setEnabled(true);
  385. // update the move up/down buttons
  386. valueChanged(null);
  387. // notify external action listeners
  388. if (actionListeners != null) {
  389. for (ActionListener actionListener : actionListeners) {
  390. actionListener.actionPerformed(new ActionEvent(this, 0, ""));
  391. }
  392. }
  393. }
  394. /**
  395. * Handle list selection events.<p>
  396. *
  397. * @param evt the list selection event.
  398. */
  399. public void valueChanged(ListSelectionEvent evt) {
  400. int row = pathElementTable.getSelectedRow();
  401. int tableSize = elements.size();
  402. if(tableSize < 1 && removeElement.isEnabled())
  403. removeElement.setEnabled(false);
  404. else if(tableSize > 0 && removeElement.isEnabled() != true)
  405. removeElement.setEnabled(true);
  406. if(tableSize < 1) {
  407. moveUp.setEnabled(false);
  408. moveDown.setEnabled(false);
  409. return;
  410. }
  411. if(row < 1) {
  412. moveUp.setEnabled(false);
  413. if(tableSize > 1 && moveDown.isEnabled() != true)
  414. moveDown.setEnabled(true);
  415. }
  416. else if(row == (tableSize - 1)) {
  417. moveDown.setEnabled(false);
  418. if(moveUp.isEnabled() != true)
  419. moveUp.setEnabled(true);
  420. }
  421. else {
  422. moveUp.setEnabled(true);
  423. moveDown.setEnabled(true);
  424. }
  425. }
  426. /**
  427. * Users of this class may add action listeners to be notified when
  428. * paths are changed. Each action listener is called when ever the
  429. * 'add', 'remove', or 'move' buttons are pressed.
  430. */
  431. public void addActionListener(ActionListener listener) {
  432. if (actionListeners == null) {
  433. actionListeners = new ArrayList<ActionListener>();
  434. }
  435. actionListeners.add(listener);
  436. }
  437. /**
  438. * A simple table model of the classpathElementTable.<p>
  439. */
  440. class PathElementTableModel extends AbstractTableModel {
  441. private String title = "";
  442. public PathElementTableModel(String title) {
  443. this.title = title;
  444. }
  445. public int getRowCount() {
  446. return elements.size();
  447. }
  448. public int getColumnCount() {
  449. return 1;
  450. }
  451. public String getColumnName(int column) {
  452. return title;
  453. }
  454. public Object getValueAt(int row, int column) {
  455. return elements.elementAt(row);
  456. }
  457. /**
  458. * Add an element to the path model.<p>
  459. *
  460. * @param value the path element to be added.
  461. */
  462. protected void add(String value) {
  463. int rows = elements.size();
  464. elements.addElement(value);
  465. fireTableRowsInserted(rows, rows);
  466. }
  467. /**
  468. * Remove an element from the path model.<p>
  469. *
  470. * @param row the index of the element to remove.
  471. */
  472. protected void remove(int row) {
  473. elements.removeElementAt(row);
  474. fireTableRowsDeleted(row, row);
  475. if(elements.size() > 0) {
  476. if(elements.size() > row) {
  477. pathElementTable.setRowSelectionInterval(row, row);
  478. }
  479. else {
  480. row = elements.size() - 1;
  481. pathElementTable.setRowSelectionInterval(row, row);
  482. }
  483. }
  484. }
  485. /**
  486. * Move an element up (towards the front of) the path.<p>
  487. *
  488. * @param row the element to be moved.
  489. */
  490. protected void moveUp(int row) {
  491. Object a = elements.elementAt(row);
  492. Object b = elements.elementAt(row - 1);
  493. elements.setElementAt(a, row - 1);
  494. elements.setElementAt(b, row);
  495. fireTableRowsUpdated(row - 1, row);
  496. pathElementTable.setRowSelectionInterval(row - 1, row - 1);
  497. }
  498. /**
  499. * Move an element down (towards the end of) the path.<p>
  500. *
  501. * @param row the element to be moved.
  502. */
  503. protected void moveDown(int row) {
  504. Object a = elements.elementAt(row);
  505. Object b = elements.elementAt(row + 1);
  506. elements.setElementAt(a, row + 1);
  507. elements.setElementAt(b, row);
  508. fireTableRowsUpdated(row, row + 1);
  509. pathElementTable.setRowSelectionInterval(row + 1, row + 1);
  510. }
  511. }
  512. }