PageRenderTime 213ms CodeModel.GetById 205ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/gui/MacrosMenu.java

#
Java | 101 lines | 69 code | 11 blank | 21 comment | 11 complexity | 4eef65adf233cce1a00a0d7e48e87fb9 MD5 | raw file
  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
 20package org.gjt.sp.jedit.gui;
 21
 22import javax.swing.*;
 23import java.awt.event.*;
 24import java.util.Vector;
 25import org.gjt.sp.jedit.msg.MacrosChanged;
 26import org.gjt.sp.jedit.*;
 27
 28public 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	private void updateMacrosMenu()
 55	{
 56		// Because the macros menu contains normal items as
 57		// well as dynamically-generated stuff, we are careful
 58		// to only remove the dynamic crap here...
 59		for(int i = getMenuComponentCount() - 1; i >= 0; i--)
 60		{
 61			if(getMenuComponent(i) instanceof JSeparator)
 62				break;
 63			else
 64				remove(i);
 65		}
 66
 67		int count = getMenuComponentCount();
 68
 69		Vector macroVector = Macros.getMacroHierarchy();
 70		createMacrosMenu(this,macroVector,0);
 71
 72		if(count == getMenuComponentCount())
 73			add(GUIUtilities.loadMenuItem("no-macros"));
 74	}
 75
 76	private void createMacrosMenu(JMenu menu, Vector vector, int start)
 77	{
 78		for(int i = start; i < vector.size(); i++)
 79		{
 80			Object obj = vector.elementAt(i);
 81			if(obj instanceof Macros.Macro)
 82			{
 83				Macros.Macro macro = (Macros.Macro)obj;
 84				menu.add(new EnhancedMenuItem(macro.getLabel(),macro));
 85			}
 86			else if(obj instanceof Vector)
 87			{
 88				Vector subvector = (Vector)obj;
 89				String name = (String)subvector.elementAt(0);
 90				JMenu submenu = new JMenu(name);
 91				createMacrosMenu(submenu,subvector,1);
 92				if(submenu.getMenuComponentCount() == 0)
 93				{
 94					submenu.add(GUIUtilities.loadMenuItem(
 95						"no-macros"));
 96				}
 97				menu.add(submenu);
 98			}
 99		}
100	}
101}