PageRenderTime 133ms CodeModel.GetById 100ms app.highlight 11ms RepoModel.GetById 20ms app.codeStats 0ms

/bundles/plugins-trunk/Console/console/commando/CommandoToolBar.java

#
Java | 182 lines | 106 code | 28 blank | 48 comment | 11 complexity | 6dc705107397d0b98eb6ddc6108d23c0 MD5 | raw file
  1/*
  2 * CommandoToolBar.java - Commando tool bar
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2001 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
 23package console.commando;
 24
 25// {{{ Imports
 26
 27import java.awt.Insets;
 28import java.awt.event.ActionEvent;
 29import java.awt.event.ActionListener;
 30import java.util.HashMap;
 31import java.util.Iterator;
 32
 33import javax.swing.Box;
 34import javax.swing.BoxLayout;
 35import javax.swing.JToolBar;
 36
 37import org.gjt.sp.jedit.EditAction;
 38import org.gjt.sp.jedit.EditBus;
 39import org.gjt.sp.jedit.View;
 40import org.gjt.sp.jedit.jEdit;
 41import org.gjt.sp.jedit.EditBus.EBHandler;
 42import org.gjt.sp.jedit.msg.DynamicMenuChanged;
 43
 44import console.ConsolePlugin;
 45
 46// }}}
 47// {{{ CommandoToolBar class
 48public class CommandoToolBar extends JToolBar
 49{
 50
 51	// {{{ init()
 52
 53	public static void init() {
 54		remove();
 55		View views[]  = jEdit.getViews();
 56
 57		for (int i=0; i<views.length; ++i) {
 58			create(views[i]);
 59		}
 60
 61	}
 62
 63	public static CommandoToolBar create(View view)
 64	{
 65		if (!jEdit.getBooleanProperty("commando.toolbar.enabled"))
 66			return null;
 67		CommandoToolBar tb = new CommandoToolBar(view);
 68		view.addToolBar(tb);
 69		smToolBarMap.put(view, tb);
 70		return tb;
 71	}
 72
 73	// }}}
 74
 75	// {{{ remove()
 76	/** Remove the instance from the all views */
 77	public static void remove()
 78	{
 79		Iterator<View> itr = smToolBarMap.keySet().iterator();
 80		while (itr.hasNext())
 81		{
 82			View v = itr.next();
 83			if (v == null) continue;
 84			CommandoToolBar tb = smToolBarMap.get(v);
 85			if (tb != null) {
 86				v.removeToolBar(tb);
 87			}
 88		}
 89		smToolBarMap.clear();
 90	}
 91	// }}}
 92
 93	/** Remove the instance from the all views */
 94	public static void remove(View v)
 95	{
 96		CommandoToolBar tb = smToolBarMap.get(v);
 97		if (tb != null) {
 98			v.removeToolBar(tb);
 99			smToolBarMap.remove(v);
100		}
101		
102	}
103	// }}}
104
105	
106	
107	// {{{ CommandoToolBar constructor
108	private CommandoToolBar(View dockable)
109	{
110		view = dockable;
111		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
112		setFloatable(true);
113		updateButtons();
114
115	}
116	// }}}
117
118	// {{{ addNotify() method
119	public void addNotify()
120	{
121		super.addNotify();
122		EditBus.addToBus(this);
123	}
124	// }}}
125
126	// {{{ removeNotify() method
127	public void removeNotify()
128	{
129		super.removeNotify();
130		EditBus.removeFromBus(this);
131	}
132	// }}}
133
134	// {{{ handleMessage() method
135	@EBHandler
136	public void handleMessage(DynamicMenuChanged msg)
137	{
138		if (ConsolePlugin.MENU.equals(msg.getMenuName()))
139			updateButtons();
140	} // }}}
141
142	// {{{ updateButtons() method
143	private void updateButtons()
144	{
145		removeAll();
146
147		ActionListener actionHandler = new ActionListener()
148		{
149			public void actionPerformed(ActionEvent evt)
150			{
151				new CommandoDialog(view, evt.getActionCommand());
152			}
153		};
154		// ConsolePlugin.rescanCommands();
155		EditAction[] commands = ConsolePlugin.getCommandoCommands();
156		for (int i = 0; i < commands.length; i++)
157		{
158			CommandoCommand command = (CommandoCommand) commands[i];
159			CommandoButton button = new CommandoButton(command);
160			button.setActionCommand(command.getName());
161			button.addActionListener(actionHandler);
162			button.setRequestFocusEnabled(false);
163			button.setMargin(new Insets(1, 2, 1, 2));
164			add(button);
165		}
166
167		add(Box.createGlue());
168	}
169	// }}}
170
171	// {{{ Data members
172	private View view;
173
174	/**
175	 * For each view, we might add a toolbar.
176	 * This map keeps track of what
177	 * views had toolbars added to them.
178	 */
179	static HashMap<View, CommandoToolBar> smToolBarMap = 
180		new HashMap<View, CommandoToolBar>();
181	// }}}
182} // }}}