PageRenderTime 71ms CodeModel.GetById 52ms app.highlight 10ms RepoModel.GetById 7ms app.codeStats 0ms

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

#
Java | 192 lines | 96 code | 32 blank | 64 comment | 5 complexity | c42d58dbd1783faff786cd82260de6b2 MD5 | raw file
  1/*
  2 * CommandoCommand.java - Commando command wrapper
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 1999, 2000, 2001, 2002 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
 26import java.io.BufferedReader;
 27import java.io.File;
 28import java.io.FileReader;
 29import java.io.IOException;
 30import java.io.InputStreamReader;
 31import java.io.Reader;
 32import java.net.URL;
 33import java.util.regex.Matcher;
 34import java.util.regex.Pattern;
 35
 36import org.gjt.sp.jedit.EditAction;
 37import org.gjt.sp.jedit.View;
 38import org.gjt.sp.jedit.jEdit;
 39import org.gjt.sp.util.StringList;
 40
 41
 42// }}}
 43
 44
 45/**
 46 * An EditAction which is intended to be used in the Console Commando.
 47 * Associated with an .xml file which may be inside a jar, or may be in the
 48 * user dir.
 49 * 
 50 */
 51public class CommandoCommand extends EditAction
 52{
 53
 54	// {{{ isUser()
 55	/**
 56	 * 
 57	 * @return true if userdefined
 58	 */
 59	public boolean isUser()
 60	{
 61		return (url == null);
 62	}
 63
 64	/**
 65	 * 
 66	 * @return true for user commands that override a command 
 67	 *     with the same name in the jar. 
 68	 */
 69	public boolean isOverriding() 
 70	{
 71		if (!isUser()) return false;
 72		String defaultCommands = jEdit.getProperty("commando.default");
 73		StringList sl = StringList.split(defaultCommands, " ");
 74		String cmdName = name.replace("commando.", "");
 75		return sl.contains(cmdName);
 76	}
 77	// }}}
 78
 79	// {{{ create()
 80	public static CommandoCommand create(URL url)
 81	{
 82		String l = shortLabel(url.getPath());
 83		CommandoCommand retval = new CommandoCommand(l, url.getPath());
 84		retval.url = url;
 85		return retval;
 86	}
 87
 88	public static CommandoCommand create(String path)
 89	{
 90		String l = shortLabel(path);
 91		File f = new File(path);
 92		if (f.canRead())
 93		{
 94			return new CommandoCommand(l, path);
 95		}
 96		else
 97			throw new RuntimeException("path: " + path + " abs: " + f.getAbsolutePath());
 98	}
 99
100	// }}}
101
102	// {{{ shortLabel
103	/**
104	 * @return the short label - for button text
105	 */
106	public String getShortLabel()
107	{
108		return label;
109	}
110
111	/**
112	 * @param path
113	 *                an absolute path to a resource
114	 * @return the short label on for a button text
115	 */
116
117	static String shortLabel(String path)
118	{
119		Matcher m = p.matcher(path);
120		m.find();
121		String name = m.group(1);
122		name = name.replace('_', ' ');
123		return name;
124	}
125
126	// }}}
127
128	// {{{ Constructor (private )
129
130	private CommandoCommand(String shortLabel, String path)
131	{
132		super("commando." + shortLabel);
133		label = shortLabel;
134		// Log.log(Log.WARNING, this, "New command: " + label + " path:
135		// " + path);
136		this.path = path;
137		this.propertyPrefix = getName() + '.';
138		jEdit.setTemporaryProperty(getName() + ".label", label);
139	}
140
141	// }}}
142
143	// {{{ getPropertyPrefix() method
144	public String getPropertyPrefix()
145	{
146		return propertyPrefix;
147	} // }}}
148
149	// {{{ invoke() method
150	public void invoke(View view)
151	{
152		new CommandoDialog(view, getName());
153	} // }}}
154
155	// {{{ getCode() method
156	public String getCode()
157	{
158		return "new console.commando.CommandoDialog(view,\"" + getName() + "\");";
159	} // }}}
160
161	// {{{ openStream() method
162	protected Reader openStream() throws IOException
163	{
164		if (url != null)
165		{
166			return new BufferedReader(new InputStreamReader(url.openStream()));
167		}
168		else
169		{
170			return new BufferedReader(new FileReader(path));
171		}
172	} // }}}
173
174	// {{{ Private members
175	private URL url = null;
176
177	private String label;
178
179	private String path;
180
181	private String propertyPrefix;
182
183	// }}}
184
185	// {{{ static private members
186	private static final String pattern = "([^\\\\\\./]+)\\.xml$";
187
188	private static final Pattern p = Pattern.compile(pattern);
189
190	// }}}
191
192}