/plugins/Tags/tags/tags-1_1/tags/TagsOptionsPanel.java

#
Java | 526 lines | 380 code | 77 blank | 69 comment | 51 complexity | f5694a1741ba2c87b975dde9c98d98bc MD5 | raw file

✨ Summary
  1. /*
  2. * TagsOptionsPanel.java
  3. * Copyright (c) 2001, 2002 Kenrick Drew
  4. * kdrew@earthlink.net
  5. *
  6. * This file is part of TagsPlugin
  7. *
  8. * TagsPlugin 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. * TagsPlugin 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. * $Id: TagsOptionsPanel.java 7600 2003-12-30 18:46:24Z orutherfurd $
  23. */
  24. package tags;
  25. //{{{ imports
  26. import java.io.*;
  27. import java.lang.*;
  28. import java.lang.System.*;
  29. import java.util.*;
  30. import java.awt.*;
  31. import java.awt.event.*;
  32. import javax.swing.*;
  33. import javax.swing.event.*;
  34. import javax.swing.table.*;
  35. import org.gjt.sp.jedit.jEdit;
  36. import org.gjt.sp.jedit.View;
  37. import org.gjt.sp.jedit.GUIUtilities;
  38. import org.gjt.sp.jedit.browser.VFSBrowser;
  39. import org.gjt.sp.jedit.AbstractOptionPane;
  40. import org.gjt.sp.util.Log;
  41. import org.gjt.sp.jedit.gui.KeyEventWorkaround;
  42. //}}}
  43. public class TagsOptionsPanel extends AbstractOptionPane {
  44. //{{{ delcarations
  45. protected JCheckBox openDialogsUnderCursorCheckBox_;
  46. protected JCheckBox extendThroughMemberOpCheckBox_;
  47. protected JCheckBox searchInParentDirs_;
  48. protected JCheckBox useCurrentBufTagFileCheckBox_;
  49. protected JLabel curBufFileNameLabel_;
  50. protected JTextField curBufFileNameField_;
  51. protected JCheckBox searchAllFilesCheckBox_;
  52. protected JScrollPane pane_;
  53. protected JTable table_;
  54. protected TagIndexFileTableModel tableModel_;
  55. protected Vector tagIndexFilesInfo_;
  56. protected JButton addButton_;
  57. protected JButton removeButton_;
  58. protected JButton moveUpButton_;
  59. protected JButton moveDownButton_;
  60. protected TagFiles tagFiles;
  61. //}}}
  62. //{{{ TagsOptionsPanel construtor
  63. public TagsOptionsPanel()
  64. {
  65. super("tags");
  66. } //}}}
  67. //{{{ _init() method
  68. public void _init()
  69. {
  70. tagFiles = TagsPlugin.getTagFiles();
  71. int numFiles = tagFiles.size();
  72. TagFile tf = null;
  73. boolean foundCurBufIndexFile = false;
  74. tagIndexFilesInfo_ = new Vector(5);
  75. for (int i = 0; i < numFiles; i++)
  76. {
  77. tf = tagFiles.get(i);
  78. foundCurBufIndexFile = foundCurBufIndexFile || tf.currentDirIndexFile;
  79. if (tf != null)
  80. {
  81. Log.log(Log.DEBUG, this, tf.toDebugString());
  82. tagIndexFilesInfo_.add(tf);
  83. }
  84. }
  85. setup();
  86. /* Previously the user didn't have the ability to change the order of the
  87. * current buffer's tag index file in the search (it was always done first).
  88. * Now that we allow it, if it wasn't already in the list add it.
  89. */
  90. if (!foundCurBufIndexFile &&
  91. jEdit.getBooleanProperty(
  92. "options.tags.tag-search-current-buff-tag-file"))
  93. { /* as if user check box */
  94. useCurBufTagFileListener_.actionPerformed(null);
  95. }
  96. tf = null;
  97. } //}}}
  98. //{{{ _save() method
  99. public void _save() {
  100. Tags.setSearchAllTagFiles(searchAllFilesCheckBox_.isSelected());
  101. Tags.setUseCurrentBufTagFile(useCurrentBufTagFileCheckBox_.isSelected());
  102. jEdit.setProperty("options.tags.current-buffer-file-name",
  103. curBufFileNameField_.getText());
  104. jEdit.setBooleanProperty("options.tags.open-dialogs-under-cursor",
  105. openDialogsUnderCursorCheckBox_.isSelected());
  106. jEdit.setBooleanProperty("options.tags.tag-extends-through-dot",
  107. extendThroughMemberOpCheckBox_.isSelected());
  108. Log.log(Log.DEBUG, this, "Search in parent dir: " + searchInParentDirs_.isSelected());
  109. jEdit.setBooleanProperty("options.tags.tags-search-parent-dir",
  110. searchInParentDirs_.isSelected());
  111. Log.log(Log.DEBUG, this, "After setting: " +
  112. jEdit.getBooleanProperty("options.tags.tags-search-parent-dir"));
  113. // remove all tag index files and reload with the ones from the pane
  114. Log.log(Log.DEBUG, this, "Tag index files (from pane):");
  115. tagFiles.clear();
  116. int numTagFiles = tagIndexFilesInfo_.size();
  117. TagFile tf = null;
  118. for (int i = 0; i < numTagFiles; i++)
  119. {
  120. tf = (TagFile) tagIndexFilesInfo_.get(i);
  121. if ( tf.path == null
  122. || (tf.path.length() == 0 && tf.currentDirIndexFile))
  123. {
  124. // User entered nothing for file name
  125. Tags.setUseCurrentBufTagFile(false);
  126. }
  127. else if (tf != null)
  128. {
  129. Log.log(Log.DEBUG, this, tf.toDebugString());
  130. tagFiles.add(tf);
  131. }
  132. }
  133. tagFiles.save();
  134. tf = null;
  135. } //}}}
  136. //{{{ setup() method
  137. protected void setup()
  138. {
  139. // Open dialog under cursor
  140. openDialogsUnderCursorCheckBox_ = new JCheckBox(jEdit.getProperty(
  141. "options.tags.open-dialogs-under-cursor.label"));
  142. openDialogsUnderCursorCheckBox_.setSelected(jEdit.getBooleanProperty(
  143. "options.tags.open-dialogs-under-cursor"));
  144. addComponent(openDialogsUnderCursorCheckBox_);
  145. // tags through member operator
  146. extendThroughMemberOpCheckBox_ = new JCheckBox(jEdit.getProperty(
  147. "options.tags.tag-extends-through-dot.label"));
  148. extendThroughMemberOpCheckBox_.setSelected(jEdit.getBooleanProperty(
  149. "options.tags.tag-extends-through-dot"));
  150. addComponent(extendThroughMemberOpCheckBox_);
  151. // search all tag index files
  152. searchAllFilesCheckBox_ = new JCheckBox(jEdit.getProperty(
  153. "options.tags.tag-search-all-files.label"));
  154. searchAllFilesCheckBox_.setSelected(Tags.getSearchAllTagFiles());
  155. addComponent(searchAllFilesCheckBox_);
  156. // Use current buffer's tag index file
  157. useCurrentBufTagFileCheckBox_ = new JCheckBox(jEdit.getProperty(
  158. "options.tags.tag-search-current-buff-tag-file.label"));
  159. useCurrentBufTagFileCheckBox_.setSelected(Tags.getUseCurrentBufTagFile());
  160. useCurrentBufTagFileCheckBox_.addActionListener(useCurBufTagFileListener_);
  161. addComponent(useCurrentBufTagFileCheckBox_);
  162. // Current buffer's tag index file name
  163. curBufFileNameLabel_ = new JLabel(jEdit.getProperty(
  164. "options.tags.current-buffer-file-name.label"));
  165. curBufFileNameField_ = new JTextField(jEdit.getProperty(
  166. "options.tags.current-buffer-file-name"), 20);
  167. curBufFileNameField_.getDocument().addDocumentListener(
  168. curBufFilenameListener_);
  169. curBufFileNameLabel_.setLabelFor(curBufFileNameField_);
  170. JPanel p = new JPanel(new BorderLayout(5,0));
  171. p.add(curBufFileNameLabel_, BorderLayout.WEST);
  172. p.add(curBufFileNameField_, BorderLayout.CENTER);
  173. addComponent(p);
  174. // Search in parent dirs
  175. searchInParentDirs_ = new JCheckBox(jEdit.getProperty(
  176. "options.tags.tags-search-parent-dirs.label"));
  177. searchInParentDirs_.setSelected(Tags.getSearchInParentDirs());
  178. addComponent(searchInParentDirs_);
  179. // tag index file table
  180. p = new JPanel(new BorderLayout(5,5));
  181. p.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
  182. tableModel_ = new TagIndexFileTableModel();
  183. table_ = new JTable(tableModel_);
  184. pane_ = new JScrollPane(table_, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  185. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  186. table_.getSelectionModel().addListSelectionListener(listSelectionListener_);
  187. table_.setPreferredScrollableViewportSize(new Dimension(250, 110));
  188. TableColumnModel columnModel = table_.getColumnModel();
  189. columnModel.getColumn(1).setMinWidth(50);
  190. columnModel.getColumn(1).setMaxWidth(80);
  191. columnModel.getColumn(1).setPreferredWidth(60);
  192. p.add(pane_, BorderLayout.CENTER);
  193. // tag index file list buttons
  194. JPanel bp = new JPanel(new GridLayout(1,0,5,5));
  195. addButton_ = new JButton(
  196. jEdit.getProperty("options.tags.tag-search-files-add.label"));
  197. addButton_.addActionListener(addButtonListener_);
  198. addButton_.setMnemonic(KeyEvent.VK_A);
  199. bp.add(addButton_);
  200. removeButton_ = new JButton(
  201. jEdit.getProperty("options.tags.tag-search-files-remove.label"));
  202. removeButton_.addActionListener(removeButtonListener_);
  203. removeButton_.setMnemonic(KeyEvent.VK_R);
  204. bp.add(removeButton_);
  205. moveUpButton_ = new JButton(
  206. jEdit.getProperty("options.tags.tag-search-files-move-up.label"));
  207. moveUpButton_.addActionListener(moveUpButtonListener_);
  208. moveUpButton_.setMnemonic(KeyEvent.VK_U);
  209. bp.add(moveUpButton_);
  210. moveDownButton_ = new JButton(
  211. jEdit.getProperty("options.tags.tag-search-files-move-down.label"));
  212. moveDownButton_.addActionListener(moveDownButtonListener_);
  213. moveDownButton_.setMnemonic(KeyEvent.VK_D);
  214. bp.add(moveDownButton_);
  215. p.add(bp, BorderLayout.SOUTH);
  216. addComponent(p);
  217. // Update the GUI
  218. updateGUI();
  219. p = bp = null;
  220. } //}}}
  221. protected ActionListener addButtonListener_ = new ActionListener() {
  222. //{{{ +actionPerformed(ActionEvent) : void
  223. public void actionPerformed(ActionEvent e) {
  224. int selectedRow = table_.getSelectedRow();
  225. if (selectedRow == -1)
  226. selectedRow = 0;
  227. String newTagFiles[] = null;
  228. View view = GUIUtilities.getView(TagsOptionsPanel.this);
  229. newTagFiles = GUIUtilities.showVFSFileDialog(view, null,
  230. VFSBrowser.OPEN_DIALOG,
  231. false);
  232. if (newTagFiles != null)
  233. {
  234. for (int i = 0; i < newTagFiles.length; i++)
  235. {
  236. tagIndexFilesInfo_.add(selectedRow,
  237. new TagFile(newTagFiles[i]));
  238. }
  239. tableModel_.fireTableRowsInserted(selectedRow,
  240. selectedRow + newTagFiles.length);
  241. table_.getSelectionModel().clearSelection();
  242. table_.getSelectionModel().setSelectionInterval(selectedRow,
  243. selectedRow);
  244. }
  245. updateGUI();
  246. }//}}}
  247. };
  248. protected ActionListener removeButtonListener_ = new ActionListener()
  249. {
  250. //{{{ +actionPerformed(ActionEvent) : void
  251. public void actionPerformed(ActionEvent e)
  252. {
  253. int selectedRows[] = table_.getSelectedRows();
  254. TagFile tf = null;
  255. for (int i = selectedRows.length - 1; i >= 0; i--)
  256. {
  257. tf = (TagFile) tagIndexFilesInfo_.get(i);
  258. if (tf.currentDirIndexFile)
  259. {
  260. useCurrentBufTagFileCheckBox_.setSelected(false);
  261. }
  262. tagIndexFilesInfo_.removeElementAt(selectedRows[i]);
  263. }
  264. tf = null;
  265. if (selectedRows.length != 0)
  266. tableModel_.fireTableRowsDeleted(selectedRows[0],
  267. selectedRows[selectedRows.length - 1]);
  268. int newSize = tagIndexFilesInfo_.size();
  269. if (newSize != 0)
  270. {
  271. int index = selectedRows[0];
  272. if (index > (newSize - 1))
  273. index = newSize - 1;
  274. table_.getSelectionModel().clearSelection();
  275. table_.getSelectionModel().setSelectionInterval(index, index);
  276. }
  277. updateGUI();
  278. }//}}}
  279. };
  280. protected ActionListener moveUpButtonListener_ = new ActionListener() {
  281. //{{{ +actionPerformed(ActionEvent) : void
  282. public void actionPerformed(ActionEvent e) {
  283. moveItem(-1);
  284. updateGUI();
  285. }//}}}
  286. };
  287. protected ActionListener moveDownButtonListener_ = new ActionListener() {
  288. //{{{ +actionPerformed(ActionEvent) : void
  289. public void actionPerformed(ActionEvent e) {
  290. moveItem(+1);
  291. updateGUI();
  292. }//}}}
  293. };
  294. //{{{ #moveItem(int) : void
  295. protected void moveItem(int indexDir) {
  296. int selectedRow = table_.getSelectedRow();
  297. int newIndex = 0;
  298. Object element = tagIndexFilesInfo_.get(selectedRow);
  299. newIndex = selectedRow + indexDir;
  300. tagIndexFilesInfo_.removeElementAt(selectedRow);
  301. tagIndexFilesInfo_.add(newIndex, element);
  302. if (indexDir > 0)
  303. tableModel_.fireTableRowsUpdated(selectedRow, selectedRow + indexDir);
  304. else
  305. tableModel_.fireTableRowsUpdated(selectedRow + indexDir, selectedRow);
  306. table_.getSelectionModel().clearSelection();
  307. table_.getSelectionModel().setSelectionInterval(newIndex, newIndex);
  308. }//}}}
  309. protected ListSelectionListener listSelectionListener_ =
  310. new ListSelectionListener() {
  311. //{{{ +valueChanged(ListSelectionEvent) : void
  312. public void valueChanged(ListSelectionEvent e) {
  313. updateGUI();
  314. }//}}}
  315. };
  316. protected ActionListener useCurBufTagFileListener_ = new ActionListener() {
  317. //{{{ +actionPerformed(ActionEvent) : void
  318. public void actionPerformed(ActionEvent e)
  319. {
  320. updateGUI();
  321. if (useCurrentBufTagFileCheckBox_.isSelected()) // add
  322. {
  323. String path = curBufFileNameField_.getText();
  324. path = path.trim();
  325. TagFile tf = new TagFile(path);
  326. tf.currentDirIndexFile = true;
  327. tagIndexFilesInfo_.add(0, tf);
  328. tableModel_.fireTableRowsInserted(0, 0);
  329. table_.getSelectionModel().clearSelection();
  330. tf = null;
  331. }
  332. else // remove
  333. {
  334. int numTagIndexFiles = tagIndexFilesInfo_.size();
  335. TagFile tf = null;
  336. for (int i = 0; i < numTagIndexFiles; i++)
  337. {
  338. tf = (TagFile) tagIndexFilesInfo_.get(i);
  339. if (tf.currentDirIndexFile)
  340. {
  341. tagIndexFilesInfo_.removeElementAt(i);
  342. tableModel_.fireTableRowsDeleted(i, i);
  343. break;
  344. }
  345. }
  346. }
  347. }//}}}
  348. };
  349. protected DocumentListener curBufFilenameListener_ = new DocumentListener()
  350. {
  351. //{{{ +changedUpdate(DocumentEvent) : void
  352. public void changedUpdate(DocumentEvent e) { update(e); }//}}}
  353. //{{{ +insertUpdate(DocumentEvent) : void
  354. public void insertUpdate(DocumentEvent e) { update(e); }//}}}
  355. //{{{ +removeUpdate(DocumentEvent) : void
  356. public void removeUpdate(DocumentEvent e) { update(e); }//}}}
  357. //{{{ +update(DocumentEvent) : void
  358. public void update(DocumentEvent e)
  359. {
  360. String name = curBufFileNameField_.getText();
  361. name = name.trim();
  362. int numTagIndexFiles = tagIndexFilesInfo_.size();
  363. TagFile tf = null;
  364. for (int i = 0; i < numTagIndexFiles; i++)
  365. {
  366. tf = (TagFile) tagIndexFilesInfo_.get(i);
  367. if (tf.currentDirIndexFile)
  368. {
  369. Log.log(Log.DEBUG, this, tf.path + " > " + name);
  370. TagFile newTagFile = new TagFile(name);
  371. newTagFile.currentDirIndexFile = true;
  372. tagIndexFilesInfo_.removeElementAt(i);
  373. tagIndexFilesInfo_.add(i, newTagFile);
  374. tableModel_.fireTableRowsUpdated(i, i);
  375. newTagFile = null;
  376. }
  377. }
  378. name = null;
  379. tf = null;
  380. }//}}}
  381. };
  382. //{{{ #updateGUI() : void
  383. protected void updateGUI() {
  384. int selectedRows[] = table_.getSelectedRows();
  385. int selectionCount = (selectedRows != null) ? selectedRows.length : 0;
  386. int numItems = tagIndexFilesInfo_.size();
  387. removeButton_.setEnabled(selectionCount != 0 && numItems != 0);
  388. moveUpButton_.setEnabled(selectionCount == 1 && selectedRows[0] != 0 &&
  389. numItems != 0);
  390. moveDownButton_.setEnabled(selectionCount == 1 &&
  391. selectedRows[0] != (numItems - 1) &&
  392. numItems != 0);
  393. boolean selected = useCurrentBufTagFileCheckBox_.isSelected();
  394. curBufFileNameLabel_.setEnabled(selected);
  395. curBufFileNameField_.setEnabled(selected);
  396. }//}}}
  397. class TagIndexFileTableModel extends AbstractTableModel
  398. {
  399. //{{{ +getColumnCount() : int
  400. public int getColumnCount() { return 2; }//}}};
  401. //{{{ +getRowCount() : int
  402. public int getRowCount() { return tagIndexFilesInfo_.size(); }//}}}
  403. //{{{ +getColumnName(int) : String
  404. public String getColumnName(int col)
  405. {
  406. if (col == 0)
  407. return jEdit.getProperty(
  408. "options.tags.tag-index-file-path-column.label");
  409. else
  410. return jEdit.getProperty(
  411. "options.tags.tag-index-file-enabled-column.label");
  412. }//}}}
  413. //{{{ +getValueAt(int, int) : Object
  414. public Object getValueAt(int r, int c)
  415. {
  416. TagFile tf = (TagFile) tagIndexFilesInfo_.get(r);
  417. return (c == 0) ? (Object) tf.getPath() : (Object) new Boolean(tf.isEnabled());
  418. }//}}}
  419. //{{{ +getColumnClass(int) : Class
  420. public Class getColumnClass(int c)
  421. {
  422. return getValueAt(0, c).getClass();
  423. }//}}}
  424. //{{{ isCellEditable(int, int) method
  425. public boolean isCellEditable(int r, int c)
  426. {
  427. // don't allow user to enable/disable current
  428. // buffer dir index file using the checkbox
  429. // as it just muddles things up
  430. if(c == 1)
  431. {
  432. TagFile tf = (TagFile)tagIndexFilesInfo_.get(r);
  433. return !tf.isCurrentDirIndexFile();
  434. }
  435. return false;
  436. } //}}}
  437. //{{{ setValueAt(Object, int, int) : void
  438. public void setValueAt(Object v, int r, int c)
  439. {
  440. if (c == 1)
  441. {
  442. TagFile tf = (TagFile) tagIndexFilesInfo_.get(r);
  443. tf.setEnabled(((Boolean) v).booleanValue());
  444. tf = null;
  445. }
  446. } //}}}
  447. }
  448. }
  449. // :collapseFolds=1:noTabs=true:lineSeparator=\r\n:tabSize=2:indentSize=2:deepIndent=false:folding=explicit: