001: /*
002: * PluginsProvider.java - Plugins menu
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2003 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.menu;
024:
025: import javax.swing.*;
026: import java.util.*;
027: import org.gjt.sp.jedit.*;
028: import org.gjt.sp.util.Log;
029:
030: public class PluginsProvider implements DynamicMenuProvider {
031: //{{{ updateEveryTime() method
032: public boolean updateEveryTime() {
033: return false;
034: } //}}}
035:
036: //{{{ update() method
037: public void update(JMenu menu) {
038: // We build a set of lists, each list contains plugin menu
039: // items that begin with a given letter.
040: int count = 0;
041:
042: List[] letters = new List[26];
043: for (int i = 0; i < letters.length; i++) {
044: letters[i] = new ArrayList();
045: }
046:
047: Vector pluginMenuItems = new Vector();
048:
049: PluginJAR[] pluginArray = jEdit.getPluginJARs();
050: for (int i = 0; i < pluginArray.length; i++) {
051: PluginJAR jar = pluginArray[i];
052: EditPlugin plugin = jar.getPlugin();
053: if (plugin == null)
054: continue;
055:
056: JMenuItem menuItem = plugin.createMenuItems();
057: if (menuItem != null) {
058: addToLetterMap(letters, menuItem);
059: count++;
060: }
061: //{{{ old API
062: else if (jEdit.getProperty("plugin."
063: + plugin.getClassName() + ".activate") == null) {
064: try {
065: pluginMenuItems.clear();
066: plugin.createMenuItems(pluginMenuItems);
067:
068: Iterator iter = pluginMenuItems.iterator();
069: while (iter.hasNext()) {
070: addToLetterMap(letters, (JMenuItem) iter.next());
071: count++;
072: }
073: } catch (Throwable t) {
074: Log
075: .log(Log.ERROR, this ,
076: "Error creating menu items"
077: + " for plugin");
078: Log.log(Log.ERROR, this , t);
079: }
080: } //}}}
081: }
082:
083: if (count == 0) {
084: JMenuItem menuItem = new JMenuItem(jEdit
085: .getProperty("no-plugins.label"));
086: menuItem.setEnabled(false);
087: menu.add(menuItem);
088: return;
089: }
090:
091: // Sort each letter
092: for (int i = 0; i < letters.length; i++) {
093: List list = letters[i];
094: Collections.sort(list, new MiscUtilities.MenuItemCompare());
095: }
096:
097: int maxItems = jEdit.getIntegerProperty("menu.spillover", 20);
098:
099: // if less than 20 items, put them directly in the menu
100: if (count <= maxItems) {
101: for (int i = 0; i < letters.length; i++) {
102: Iterator iter = letters[i].iterator();
103: while (iter.hasNext()) {
104: menu.add((JMenuItem) iter.next());
105: }
106: }
107:
108: return;
109: }
110:
111: // Collect blocks of up to maxItems of consecutive letters
112: count = 0;
113: char first = 'A';
114: JMenu submenu = new JMenu();
115: menu.add(submenu);
116:
117: for (int i = 0; i < letters.length; i++) {
118: List letter = letters[i];
119:
120: if (count + letter.size() > maxItems && count != 0) {
121: char last = (char) (i + 'A' - 1);
122: if (last == first)
123: submenu.setText(String.valueOf(first));
124: else
125: submenu.setText(first + " - " + last);
126: first = (char) (i + 'A');
127: count = 0;
128: submenu = null;
129: }
130:
131: Iterator iter = letter.iterator();
132: while (iter.hasNext()) {
133: if (submenu == null) {
134: submenu = new JMenu();
135: menu.add(submenu);
136: }
137: submenu.add((JMenuItem) iter.next());
138: }
139:
140: count += letter.size();
141: }
142:
143: if (submenu != null) {
144: char last = 'Z';
145: if (last == first)
146: submenu.setText(String.valueOf(first));
147: else
148: submenu.setText(first + " - " + last);
149: }
150: } //}}}
151:
152: //{{{ addToLetterMap() method
153: private void addToLetterMap(List[] letters, JMenuItem item) {
154: char ch = item.getText().charAt(0);
155: ch = Character.toUpperCase(ch);
156: if (ch < 'A' || ch > 'Z') {
157: Log.log(Log.ERROR, this , "Plugin menu item label must "
158: + "begin with A - Z, or a - z: " + item.getText());
159: } else
160: letters[ch - 'A'].add(item);
161: } //}}}
162: }
|