Source code: org/gjt/sp/jedit/gui/MacrosMenu.java
1 /*
2 * MacrosMenu.java - Macros menu
3 * Copyright (C) 1999, 2000, 2001 Slava Pestov
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 package org.gjt.sp.jedit.gui;
21
22 import javax.swing.*;
23 import java.util.Collections;
24 import java.util.Vector;
25 import org.gjt.sp.jedit.msg.MacrosChanged;
26 import org.gjt.sp.jedit.*;
27
28 public class MacrosMenu extends EnhancedMenu implements EBComponent
29 {
30 public MacrosMenu()
31 {
32 super("macros");
33 updateMacrosMenu();
34 }
35
36 public void addNotify()
37 {
38 super.addNotify();
39 EditBus.addToBus(this);
40 }
41
42 public void removeNotify()
43 {
44 super.removeNotify();
45 EditBus.removeFromBus(this);
46 }
47
48 public void handleMessage(EBMessage msg)
49 {
50 if(msg instanceof MacrosChanged)
51 updateMacrosMenu();
52 }
53
54 public void init()
55 {
56 super.init();
57 updateMacrosMenu();
58 }
59
60 private void updateMacrosMenu()
61 {
62 if(!initialized)
63 return;
64
65 // Because the macros menu contains normal items as
66 // well as dynamically-generated stuff, we are careful
67 // to only remove the dynamic crap here...
68 for(int i = getMenuComponentCount() - 1; i >= 0; i--)
69 {
70 if(getMenuComponent(i) instanceof JSeparator)
71 break;
72 else
73 remove(i);
74 }
75
76 int count = getMenuComponentCount();
77
78 Vector macroVector = Macros.getMacroHierarchy();
79 createMacrosMenu(this,macroVector,0);
80
81 if(count == getMenuComponentCount())
82 add(GUIUtilities.loadMenuItem("no-macros"));
83 }
84
85 private void createMacrosMenu(JMenu menu, Vector vector, int start)
86 {
87 Vector menuItems = new Vector();
88
89 for(int i = start; i < vector.size(); i++)
90 {
91 Object obj = vector.elementAt(i);
92 if(obj instanceof Macros.Macro)
93 {
94 Macros.Macro macro = (Macros.Macro)obj;
95 menuItems.add(new EnhancedMenuItem(macro.getLabel(),macro));
96 }
97 else if(obj instanceof Vector)
98 {
99 Vector subvector = (Vector)obj;
100 String name = (String)subvector.elementAt(0);
101 JMenu submenu = new JMenu(name);
102 createMacrosMenu(submenu,subvector,1);
103 if(submenu.getMenuComponentCount() != 0)
104 menuItems.add(submenu);
105 }
106 }
107
108 Collections.sort(menuItems,new MiscUtilities.MenuItemCompare());
109 for(int i = 0; i < menuItems.size(); i++)
110 {
111 menu.add((JMenuItem)menuItems.get(i));
112 }
113 }
114 }