1 22 23 package org.gjt.sp.jedit.menu; 24 25 import javax.swing.*; 26 import java.util.*; 27 import org.gjt.sp.jedit.*; 28 import org.gjt.sp.util.Log; 29 30 public class PluginsProvider implements DynamicMenuProvider 31 { 32 public boolean updateEveryTime() 34 { 35 return false; 36 } 38 public void update(JMenu menu) 40 { 41 int count = 0; 44 45 List[] letters = new List[26]; 46 for(int i = 0; i < letters.length; i++) 47 { 48 letters[i] = new ArrayList(); 49 } 50 51 Vector pluginMenuItems = new Vector(); 52 53 PluginJAR[] pluginArray = jEdit.getPluginJARs(); 54 for(int i = 0; i < pluginArray.length; i++) 55 { 56 PluginJAR jar = pluginArray[i]; 57 EditPlugin plugin = jar.getPlugin(); 58 if(plugin == null) 59 continue; 60 61 JMenuItem menuItem = plugin.createMenuItems(); 62 if(menuItem != null) 63 { 64 addToLetterMap(letters,menuItem); 65 count++; 66 } 67 else if(jEdit.getProperty("plugin." 69 + plugin.getClassName() 70 + ".activate") == null) 71 { 72 try 73 { 74 pluginMenuItems.clear(); 75 plugin.createMenuItems(pluginMenuItems); 76 77 Iterator iter = pluginMenuItems.iterator(); 78 while(iter.hasNext()) 79 { 80 addToLetterMap(letters, 81 (JMenuItem)iter.next()); 82 count++; 83 } 84 } 85 catch(Throwable t) 86 { 87 Log.log(Log.ERROR,this, 88 "Error creating menu items" 89 + " for plugin"); 90 Log.log(Log.ERROR,this,t); 91 } 92 } } 94 95 if(count == 0) 96 { 97 JMenuItem menuItem = new JMenuItem( 98 jEdit.getProperty("no-plugins.label")); 99 menuItem.setEnabled(false); 100 menu.add(menuItem); 101 return; 102 } 103 104 for(int i = 0; i < letters.length; i++) 106 { 107 List list = letters[i]; 108 Collections.sort(list,new MiscUtilities 109 .MenuItemCompare()); 110 } 111 112 int maxItems = jEdit.getIntegerProperty("menu.spillover",20); 113 114 if(count <= maxItems) 116 { 117 for(int i = 0; i < letters.length; i++) 118 { 119 Iterator iter = letters[i].iterator(); 120 while(iter.hasNext()) 121 { 122 menu.add((JMenuItem)iter.next()); 123 } 124 } 125 126 return; 127 } 128 129 count = 0; 131 char first = 'A'; 132 JMenu submenu = new JMenu(); 133 menu.add(submenu); 134 135 for(int i = 0; i < letters.length; i++) 136 { 137 List letter = letters[i]; 138 139 if(count + letter.size() > maxItems && count != 0) 140 { 141 char last = (char)(i + 'A' - 1); 142 if(last == first) 143 submenu.setText(String.valueOf(first)); 144 else 145 submenu.setText(first + " - " + last); 146 first = (char)(i + 'A'); 147 count = 0; 148 submenu = null; 149 } 150 151 Iterator iter = letter.iterator(); 152 while(iter.hasNext()) 153 { 154 if(submenu == null) 155 { 156 submenu = new JMenu(); 157 menu.add(submenu); 158 } 159 submenu.add((JMenuItem)iter.next()); 160 } 161 162 count += letter.size(); 163 } 164 165 if(submenu != null) 166 { 167 char last = 'Z'; 168 if(last == first) 169 submenu.setText(String.valueOf(first)); 170 else 171 submenu.setText(first + " - " + last); 172 } 173 } 175 private void addToLetterMap(List[] letters, JMenuItem item) 177 { 178 char ch = item.getText().charAt(0); 179 ch = Character.toUpperCase(ch); 180 if(ch < 'A' || ch > 'Z') 181 { 182 Log.log(Log.ERROR,this,"Plugin menu item label must " 183 + "begin with A - Z, or a - z: " 184 + item.getText()); 185 } 186 else 187 letters[ch - 'A'].add(item); 188 } } 190 | Popular Tags |