PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/menu/PluginsProvider.java

#
Java | 189 lines | 138 code | 21 blank | 30 comment | 28 complexity | 4ba09aece922d68fe603986cef316e81 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. * PluginsProvider.java - Plugins menu
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2003 Slava Pestov
  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. package org.gjt.sp.jedit.menu;
  23. import javax.swing.*;
  24. import java.util.*;
  25. import org.gjt.sp.jedit.*;
  26. import org.gjt.sp.util.Log;
  27. public class PluginsProvider implements DynamicMenuProvider
  28. {
  29. //{{{ updateEveryTime() method
  30. public boolean updateEveryTime()
  31. {
  32. return false;
  33. } //}}}
  34. //{{{ update() method
  35. public void update(JMenu menu)
  36. {
  37. // We build a set of lists, each list contains plugin menu
  38. // items that begin with a given letter.
  39. int count = 0;
  40. List[] letters = new List[26];
  41. for(int i = 0; i < letters.length; i++)
  42. {
  43. letters[i] = new ArrayList();
  44. }
  45. Vector pluginMenuItems = new Vector();
  46. PluginJAR[] pluginArray = jEdit.getPluginJARs();
  47. for(int i = 0; i < pluginArray.length; i++)
  48. {
  49. PluginJAR jar = pluginArray[i];
  50. EditPlugin plugin = jar.getPlugin();
  51. if(plugin == null)
  52. continue;
  53. JMenuItem menuItem = plugin.createMenuItems();
  54. if(menuItem != null)
  55. {
  56. addToLetterMap(letters,menuItem);
  57. count++;
  58. }
  59. //{{{ old API
  60. else if(jEdit.getProperty("plugin."
  61. + plugin.getClassName()
  62. + ".activate") == null)
  63. {
  64. try
  65. {
  66. pluginMenuItems.clear();
  67. plugin.createMenuItems(pluginMenuItems);
  68. Iterator iter = pluginMenuItems.iterator();
  69. while(iter.hasNext())
  70. {
  71. addToLetterMap(letters,
  72. (JMenuItem)iter.next());
  73. count++;
  74. }
  75. }
  76. catch(Throwable t)
  77. {
  78. Log.log(Log.ERROR,this,
  79. "Error creating menu items"
  80. + " for plugin");
  81. Log.log(Log.ERROR,this,t);
  82. }
  83. } //}}}
  84. }
  85. if(count == 0)
  86. {
  87. JMenuItem menuItem = new JMenuItem(
  88. jEdit.getProperty("no-plugins.label"));
  89. menuItem.setEnabled(false);
  90. menu.add(menuItem);
  91. return;
  92. }
  93. // Sort each letter
  94. for(int i = 0; i < letters.length; i++)
  95. {
  96. List list = letters[i];
  97. Collections.sort(list,new MiscUtilities
  98. .MenuItemCompare());
  99. }
  100. int maxItems = jEdit.getIntegerProperty("menu.spillover",20);
  101. // if less than 20 items, put them directly in the menu
  102. if(count <= maxItems)
  103. {
  104. for(int i = 0; i < letters.length; i++)
  105. {
  106. Iterator iter = letters[i].iterator();
  107. while(iter.hasNext())
  108. {
  109. menu.add((JMenuItem)iter.next());
  110. }
  111. }
  112. return;
  113. }
  114. // Collect blocks of up to maxItems of consecutive letters
  115. count = 0;
  116. char first = 'A';
  117. JMenu submenu = new JMenu();
  118. menu.add(submenu);
  119. for(int i = 0; i < letters.length; i++)
  120. {
  121. List letter = letters[i];
  122. if(count + letter.size() > maxItems && count != 0)
  123. {
  124. char last = (char)(i + 'A' - 1);
  125. if(last == first)
  126. submenu.setText(String.valueOf(first));
  127. else
  128. submenu.setText(first + " - " + last);
  129. first = (char)(char)(i + 'A');
  130. count = 0;
  131. submenu = null;
  132. }
  133. Iterator iter = letter.iterator();
  134. while(iter.hasNext())
  135. {
  136. if(submenu == null)
  137. {
  138. submenu = new JMenu();
  139. menu.add(submenu);
  140. }
  141. submenu.add((JMenuItem)iter.next());
  142. }
  143. count += letter.size();
  144. }
  145. if(submenu != null)
  146. {
  147. char last = 'Z';
  148. if(last == first)
  149. submenu.setText(String.valueOf(first));
  150. else
  151. submenu.setText(first + " - " + last);
  152. }
  153. } //}}}
  154. //{{{ addToLetterMap() method
  155. private void addToLetterMap(List[] letters, JMenuItem item)
  156. {
  157. char ch = item.getText().charAt(0);
  158. ch = Character.toUpperCase(ch);
  159. if(ch < 'A' || ch > 'Z')
  160. {
  161. Log.log(Log.ERROR,this,"Plugin menu item label must "
  162. + "begin with A - Z, or a - z: "
  163. + item.getText());
  164. }
  165. else
  166. letters[ch - 'A'].add(item);
  167. } //}}}
  168. }