PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/macros/Misc/Display_Abbreviations.bsh

#
Unknown | 391 lines | 370 code | 21 blank | 0 comment | 0 complexity | b10707e4d9689c3d1ee7db2f43ce5a9d 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. * Display_abbreviations.bsh - a BeanShell macro script for the
  3. * jEdit text editor - displays all defined abbreviations
  4. * Copyright (C) 2001 John Gellene
  5. * email: jgellene@nyc.rr.com
  6. * http://community.jedit.org
  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. * $Id: Display_Abbreviations.bsh 5224 2005-06-08 22:33:27Z orutherfurd $
  23. *
  24. * requires JDK 1.2, jEdit3.0
  25. *
  26. * Notes on use:
  27. *
  28. * This macro will display a sorted list of all defined abbreviations
  29. * in a dialog. A combo box lists all eidting modes for which abbreviations
  30. * are currently defined, as well as the "global" abbreviation set.
  31. *
  32. * Pressing a letter key will cause the table to scroll to the first row
  33. * with a label beginning with the letter (or the imeediately preceding row if
  34. * no item begins with that letter). The table is read-only; the dialog is
  35. * dismissed by clicking "OK" or pressing Esc or Enter.
  36. *
  37. * The macro has two global constants defined to permit customization of the
  38. * script's behavior. STARTING_SET contains the name of the abbreviation set
  39. * that will be first displayed. If the other variable, EXCLUDE_EMPTY_SETS,
  40. * is set to true, the drop-down menu and the results of clicking the
  41. * "Write All" button will only include abbreviations sets containing
  42. * entries.
  43. *
  44. * The table being displayed at any time is not updated if changes are made to
  45. * the abbreviation set in the "Global Options" dialog. If EXCLUDE_EMPTY_SETS
  46. * is set to true, the drop-down list will not by updated. Clsoing the dialog
  47. * and running the macro again will obtain current data.
  48. *
  49. *
  50. * Checked for jEdit 4.0 API
  51. *
  52. */
  53. import javax.swing.table.*;
  54. /*
  55. * getActiveSets()
  56. * This returns an array with the names of the abbreviation sets
  57. * (beginning with "global"). If EXCLUDE_EMPTY_SETS is set to true, only sets
  58. * with abbreviations are included.
  59. */
  60. Object[] getActiveSets()
  61. {
  62. Mode[] modes = jEdit.getModes();
  63. Vector setVector = new Vector(modes.length + 1);
  64. setVector.addElement("global");
  65. for(int i = 0; i < modes.length; i++)
  66. {
  67. String name = modes[i].getName();
  68. if(EXCLUDE_EMPTY_SETS)
  69. {
  70. Hashtable ht = Abbrevs.getModeAbbrevs().get(name);
  71. if( ht == null || ht.isEmpty())
  72. continue;
  73. }
  74. setVector.addElement(name);
  75. }
  76. Object[] sets = new Object[setVector.size()];
  77. setVector.copyInto(sets);
  78. return sets;
  79. }
  80. /*
  81. * makeTableDataForMode()
  82. * This extracts the abbreviations from the named set. If extraction is
  83. * successful, the vector named by the first parameter will have its
  84. * elements removed before the variable is set to the newly created vector.
  85. */
  86. Vector makeTableDataForMode(Vector v, String name)
  87. {
  88. if(name.equals(currentSet))
  89. return v;
  90. Hashtable htable = null;
  91. if(name.equals("global"))
  92. {
  93. htable = Abbrevs.getGlobalAbbrevs();
  94. }
  95. else
  96. {
  97. Hashtable modeAbbrevs = Abbrevs.getModeAbbrevs();
  98. htable = modeAbbrevs.get(name);
  99. }
  100. if(htable != null)
  101. {
  102. Enumeration abbrevEnum = htable.keys();
  103. Enumeration expandEnum = htable.elements();
  104. Vector newData = new Vector(20, 5);
  105. while(abbrevEnum.hasMoreElements())
  106. {
  107. Vector row = new Vector(2);
  108. row.addElement(abbrevEnum.nextElement());
  109. row.addElement(expandEnum.nextElement());
  110. newData.addElement(row);
  111. }
  112. MiscUtilities.quicksort(newData,
  113. new MiscUtilities.StringICaseCompare());
  114. currentSet = name;
  115. if( v != null)
  116. v.removeAllElements();
  117. v = newData;
  118. }
  119. return v;
  120. }
  121. /*
  122. * showAbbrevs()
  123. * This is the macro's principal method.
  124. */
  125. void showAbbrevs()
  126. {
  127. currentSet = null;
  128. data = makeTableDataForMode(data, STARTING_SET);
  129. if(data.size() == 0)
  130. {
  131. STARTING_SET = "global";
  132. data = makeTableDataForMode(data, STARTING_SET);
  133. }
  134. Vector columnNames = new Vector(2);
  135. columnNames.addElement(new String("Abbreviation"));
  136. columnNames.addElement(new String("Expansion"));
  137. table = new JTable();
  138. table.setModel(new DefaultTableModel(data, columnNames));
  139. table.setRowSelectionAllowed(true);
  140. /* The next line prevents the table from being edited.
  141. * The normal approach in Java would be to subclass the TableModel
  142. * associated with the JTable and define TableModel.isCellEditable()
  143. * to return false. However, BeanShell does not allow conventional
  144. * class creation, and the desired behavior cannot be achieved using
  145. * its scripted object feature.
  146. */
  147. table.setDefaultEditor(Object.class, null);
  148. if(table.getRowCount() != 0)
  149. table.setRowSelectionInterval(0,0);
  150. tablePane = new JScrollPane(table);
  151. tablePane.setPreferredSize(new Dimension(450, 300));
  152. combo = new JComboBox(abbrevSets);
  153. Dimension dim = combo.getPreferredSize();
  154. dim.width = Math.max(dim.width, 120);
  155. combo.setPreferredSize(dim);
  156. combo.setSelectedItem(STARTING_SET);
  157. comboPanel = new JPanel(new FlowLayout());
  158. comboPanel.add(new JLabel("Abbreviation set:"));
  159. comboPanel.add(combo);
  160. close = new JButton("Close");
  161. write_set = new JButton("Write set");
  162. write_all = new JButton("Write all");
  163. buttonPanel = new JPanel(new FlowLayout());
  164. buttonPanel.add(write_set);
  165. buttonPanel.add(write_all);
  166. buttonPanel.add(close);
  167. close.addActionListener(this);
  168. write_set.addActionListener(this);
  169. write_all.addActionListener(this);
  170. combo.addActionListener(this);
  171. void actionPerformed(e)
  172. {
  173. Component source = e.getSource();
  174. if(source == close)
  175. dialog.hide();
  176. else if(source == write_set)
  177. writeTableToNewBuffer(super.data, (String)combo.getSelectedItem());
  178. else if(source == write_all)
  179. writeAllToNewBuffer();
  180. else if(source == combo)
  181. {
  182. super.data = makeTableDataForMode(super.data, (String)combo.getSelectedItem());
  183. if( data != null)
  184. {
  185. DefaultTableModel model = (DefaultTableModel)table.getModel();
  186. model.setDataVector(super.data, columnNames);
  187. }
  188. }
  189. }
  190. // workaround required by Swing bug; scheduled to be fixed in JDK 1.4
  191. combo.getComponent(0).addKeyListener(this);
  192. table.addKeyListener(this);
  193. write_set.addKeyListener(this);
  194. write_all.addKeyListener(this);
  195. close.addKeyListener(this);
  196. void keyPressed(e)
  197. {
  198. if(combo.isPopupVisible()) return;
  199. if(e.getKeyCode() == KeyEvent.VK_ESCAPE ||
  200. e.getKeyCode() == KeyEvent.VK_ENTER)
  201. {
  202. dialog.hide();
  203. }
  204. else if(e.getSource() != combo)
  205. {
  206. char ch = e.getKeyChar();
  207. if(Character.isLetter(ch))
  208. {
  209. e.consume();
  210. row = findFirstItem(ch);
  211. /* The next few lines set the last visible row
  212. * of the table so that you can look ahead of
  213. * the selected row.
  214. */
  215. visibleRows =
  216. table.getVisibleRect().height / table.getRowHeight();
  217. oldRow = table.getSelectedRow();
  218. table.setRowSelectionInterval(row,row);
  219. if (visibleRows > 5 && row - oldRow > visibleRows - 3)
  220. {
  221. row = Math.min( super.data.size() - 1, row + 3);
  222. }
  223. table.scrollRectToVisible(table.getCellRect(row,0,true));
  224. }
  225. }
  226. }
  227. /*
  228. * Having these members of KeyListener implemented as no-ops
  229. * will speedup execution. Otherwise BeanShell throws an
  230. * exception that jEdit handles internally.
  231. * Under BeanShell 1.2, implementation of these methods is
  232. * required.
  233. */
  234. void keyReleased(e) {}
  235. void keyTyped(e) {}
  236. /*
  237. * findFirstItem()
  238. * A simple linear search for the table entry that begins with the
  239. * given letter. It returns the first row with an entry beginning with
  240. * the letter, or the immdediately preceding row if there is no match
  241. * on the letter.
  242. *
  243. */
  244. int findFirstItem(char ch)
  245. {
  246. ch = Character.toUpperCase(ch);
  247. int row = 0;
  248. for(int i = 0; i < data.size(); ++i)
  249. {
  250. String name = ((Vector)data.elementAt(i)).elementAt(0);
  251. char ch_test = Character.toUpperCase(name.charAt(0));
  252. if( ch_test > ch) break;
  253. else
  254. {
  255. row = i;
  256. if( ch_test == ch) break;
  257. }
  258. }
  259. return row;
  260. }
  261. title = "Abbreviation list";
  262. dialog = new JDialog(view, title, false);
  263. c = dialog.getContentPane();
  264. c.add(tablePane, "Center");
  265. c.add(comboPanel, "North");
  266. c.add(buttonPanel, "South");
  267. dialog.getRootPane().setDefaultButton(close);
  268. dialog.pack();
  269. dialog.setLocationRelativeTo(view);
  270. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  271. dialog.show();
  272. }
  273. /*
  274. * The next four methods deal with writing the abbreviation sets to a new
  275. * text buffer.
  276. */
  277. void writeTableToNewBuffer(Vector v, String setName)
  278. {
  279. Log.log(Log.DEBUG, BeanShell.class,
  280. "calling writeTableToNewBuffer for " + setName);
  281. writeHeader();
  282. Log.log(Log.DEBUG, BeanShell.class, "size of vector = "
  283. + String.valueOf(v.size()));
  284. writeSet(v, setName);
  285. }
  286. // saved the currentSet and deleted it for makeTableDataForMode()-invocation
  287. // because otherwise the currently in the Dialog displayed set has no abbrevs
  288. // in the output written to the buffer.
  289. void writeAllToNewBuffer()
  290. {
  291. writeHeader();
  292. Vector vOut = new Vector();
  293. savedCurrentSet = currentSet;
  294. for( int i = 0; i < abbrevSets.length; ++i)
  295. {
  296. currentSet = null;
  297. String setName = (String)abbrevSets[i];
  298. vOut = makeTableDataForMode(vOut, setName);
  299. writeSet(vOut, setName);
  300. textArea.setSelectedText("\n\n");
  301. }
  302. currentSet = savedCurrentSet;
  303. }
  304. void writeHeader()
  305. {
  306. jEdit.newFile(view);
  307. textArea.setSelectedText("jEdit Abbreviation Table\n\n");
  308. }
  309. /*
  310. * This truncates the definition text at 17 characters and the expansion text
  311. * at 58.
  312. */
  313. void writeSet(Vector v, String setName)
  314. {
  315. textArea.setSelectedText("Abbreviation set: " + setName + "\n");
  316. textArea.setSelectedText("Abbreviation Expansion\n\n");
  317. if(v.size() == 0)
  318. textArea.setSelectedText("<< No abbreviations >>\n");
  319. else for( int i = 0; i < v.size(); ++i)
  320. {
  321. StringBuffer sb = new StringBuffer(85);
  322. spaceString = " ";
  323. char[] space = spaceString.toCharArray();
  324. Vector row = (Vector)v.elementAt(i);
  325. abbrevName = (String)row.elementAt(0);
  326. if(abbrevName == null) continue;
  327. if(abbrevName.length() > 17)
  328. abbrevName = abbrevName.substring(0, 14) + "...";
  329. sb.append(abbrevName);
  330. sb.append(space, 0, 18 - (abbrevName.length()));
  331. expansion = row.elementAt(1);
  332. if(shortcut1 != null)
  333. {
  334. if(expansion.length() > 58)
  335. expansion = expansion.substring(0, 55) + "...";
  336. sb.append(expansion);
  337. }
  338. sb.append('\n');
  339. textArea.setSelectedText(sb.toString());
  340. }
  341. }
  342. /*
  343. * main routine, including definition of global variables
  344. */
  345. STARTING_SET = "global";
  346. EXCLUDE_EMPTY_SETS = true;
  347. abbrevSets = getActiveSets();
  348. currentSet = null;
  349. Vector data = new Vector(20, 5);
  350. showAbbrevs();
  351. /*
  352. Macro index data (in DocBook format)
  353. <listitem>
  354. <para><filename>Display_Abbreviations.bsh</filename></para>
  355. <abstract><para>
  356. Displays the abbreviations registered for each of jEdit's
  357. editing modes.
  358. </para></abstract>
  359. <para>
  360. The macro provides a read-only view of the abbreviations
  361. contained in the <quote>Abbreviations</quote> option pane. Pressing
  362. a letter key will scroll the table to the first entry beginning with
  363. that letter. A further option is provided to write a selected mode's
  364. abbreviations or all abbreviations in a text buffer for printing as a
  365. reference. Notes in the source code listing point out some display options
  366. that are configured by modifying global variables.
  367. </para>
  368. </listitem>
  369. */
  370. // end Display_Abbreviations.bsh