PageRenderTime 65ms CodeModel.GetById 50ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 187 lines | 129 code | 20 blank | 38 comment | 15 complexity | e1a0cb980500afb3e212ccefbd9a658f MD5 | raw file
  1/*
  2 * CommandoButton.java - A button for a command.
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2005 Alan Ezust
  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 */
 22package console.commando;
 23
 24// {{{ imports
 25import java.awt.Color;
 26import java.awt.Component;
 27import java.awt.event.ActionEvent;
 28import java.awt.event.ActionListener;
 29
 30import java.io.File;
 31import java.io.FileWriter;
 32import java.io.IOException;
 33import java.io.Reader;
 34
 35import javax.swing.JButton;
 36import javax.swing.JMenuItem;
 37import javax.swing.JPopupMenu;
 38import javax.swing.SwingUtilities;
 39
 40import org.gjt.sp.jedit.View;
 41import org.gjt.sp.jedit.jEdit;
 42import org.gjt.sp.jedit.browser.VFSBrowser;
 43import org.gjt.sp.jedit.gui.DockableWindowManager;
 44import org.gjt.sp.jedit.io.VFSFile;
 45
 46import console.ConsolePlugin;
 47// }}}
 48
 49/**
 50 * A view for a CommandoCommand
 51 * 
 52 * @author ezust
 53 * 
 54 */
 55public class CommandoButton extends JButton implements ActionListener 
 56{
 57    // {{{ Private data members
 58	private boolean visible;
 59
 60	private CommandoCommand command;
 61
 62	private JPopupMenu contextMenu;
 63
 64	private JMenuItem hide;
 65
 66	private JMenuItem customize;
 67	
 68	private JMenuItem unCustomize;
 69	
 70	// }}}
 71
 72    // {{{ ctor 
 73	public CommandoButton(CommandoCommand command)
 74	{
 75		this.command = command;
 76		init();
 77	}
 78	
 79	void init() {
 80		
 81		setToolTipText(jEdit.getProperty("console.commando.button.tooltiptext"));
 82		String name = command.getLabel();
 83		setText(name);
 84		contextMenu = new JPopupMenu();
 85		visible = jEdit.getBooleanProperty("commando.visible." + name);
 86		setVisible(visible);
 87		if (!command.isUser()) {
 88			hide = new JMenuItem(jEdit.getProperty("commando.hide"));
 89			hide.addActionListener(this);
 90			contextMenu.add(hide);
 91		}
 92		customize = new JMenuItem(jEdit.getProperty("commando.customize"));
 93		customize.addActionListener(this);
 94		contextMenu.add(customize);
 95		if (command.isUser()) {
 96			setBackground(Color.GREEN);
 97			setToolTipText(jEdit.getProperty("console.commando.button.customized.tooltiptext"));
 98			if (command.isOverriding()) {
 99				unCustomize = new JMenuItem(jEdit.getProperty("commando.uncustomize"));
100				unCustomize.setToolTipText(jEdit.getProperty("commando.uncustomize.tooltiptext", new Object[] {name}));
101				unCustomize.addActionListener(this);
102				contextMenu.add(unCustomize);
103			}
104		}
105		setComponentPopupMenu(contextMenu);
106		
107	}
108    // }}}
109    
110	// {{{ unCustomize
111	public void unCustomizeButton() 
112	{
113		String userDir = ConsolePlugin.getUserCommandDirectory();
114		String name = command.getShortLabel() + ".xml";
115		File f = new File(userDir, name);
116		String path = f.getPath();
117		VFSFile vf = new VFSFile(path, path, path, VFSFile.FILE, 0, false);
118		VFSFile[] vfl = new VFSFile[1];
119		vfl[0]=vf;
120		DockableWindowManager dwm = jEdit.getActiveView().getDockableWindowManager();
121		Component comp = dwm.getDockable("vfs.browser");
122		VFSBrowser browser = (VFSBrowser) comp;
123		if (browser == null) {
124			dwm.showDockableWindow("vfs.browser");
125			comp = dwm.getDockable("vfs.browser");
126			browser = (VFSBrowser) comp;
127		}
128		if (browser == null) {
129			return;
130		}
131		browser.delete(vfl);
132		SwingUtilities.invokeLater(new Runnable() {
133			public void run() {	ConsolePlugin.rescanCommands();	}});
134	}
135	
136    // {{{ Customize 
137	public void customize()
138	{
139		String userDir = ConsolePlugin.getUserCommandDirectory();
140		try
141		{
142			String name = command.getShortLabel() + ".xml";
143			File f = new File(userDir, name);
144			if (!f.exists())
145			{
146				Reader reader = command.openStream();
147				FileWriter writer = new FileWriter(f);
148				int bytes;
149				char[] buf = new char[200];
150				while ((bytes = reader.read(buf)) > 0)
151				{
152					writer.write(buf, 0, bytes);
153				}
154				writer.close();
155				reader.close();
156				ConsolePlugin.rescanCommands();
157			}
158			View v = jEdit.getActiveView();
159			jEdit.openFile(v, f.getAbsolutePath());
160		}
161		catch (IOException ioe)
162		{
163			throw new RuntimeException(ioe);
164		}
165	}
166    // }}}
167    
168    // {{{ actionPerformed 
169	public void actionPerformed(ActionEvent e)
170	{
171		if (e.getSource() == hide)
172		{
173			visible = false;
174			jEdit.setBooleanProperty("commando.visible." + getText(), visible);
175			setVisible(visible);
176		}
177		if (e.getSource() == customize)
178		{
179			customize();
180		}
181		if (e.getSource() == unCustomize) {
182			unCustomizeButton();
183		}
184	}
185    // }}}
186
187}