Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/gjt/sp/jedit/menu/PluginsProvider.java


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  
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    //{{{ updateEveryTime() method
33    public boolean updateEveryTime()
34    {
35      return false;
36    } //}}}
37  
38    //{{{ update() method
39    public void update(JMenu menu)
40    {
41      // We build a set of lists, each list contains plugin menu
42      // items that begin with a given letter.
43      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        //{{{ old API
68        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        } //}}}
93      }
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     // Sort each letter
105     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 less than 20 items, put them directly in the menu
115     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     // Collect blocks of up to maxItems of consecutive letters
130     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)(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   } //}}}
174 
175   //{{{ addToLetterMap() method
176   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   } //}}}
189 }