📄 rtextl10ngenplugin.java
字号:
package org.fife.rtext.plugins.rtextl10ngen;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.swing.*;
import javax.swing.event.*;
import org.fife.rtext.*;
import org.fife.io.UnicodeReader;
import org.fife.io.UnicodeWriter;
import org.fife.ui.app.*;
/**
* Plugin that aids in the creation/maintenance of an RText localization.
*
* @author Robert Futrell
* @version 0.1
*/
public class RTextL10nGenPlugin extends WizardPlugin implements ActionListener {
private RText rtext;
private String name;
private ResourceBundle msg;
//private RTextL10nGenOptionPanel optionPanel;
private Icon pluginIcon;
private int mode;
private static final int BUF_SIZE = 1024;
private static final int MODE_CREATE = 0;
private static final int MODE_PACKAGE = 1;
private static final String BUNDLE_NAME =
"org.fife.rtext.plugins.rtextl10ngen.RTextL10nGen";
private static final String CREATE_PLAYPEN = "CreatePlaypen";
private static final String ICON_FILE =
"org/fife/rtext/plugins/rtextl10ngen.gif";
private static final String PACKAGE_PLAYPEN = "PackagePlaypen";
private static final String VERSION = "0.9.4.0";
/*****************************************************************************/
/**
* Constructor.
*
* @param app The RText instance.
*/
public RTextL10nGenPlugin(AbstractPluggableGUIApplication app) {
super(app);
this.rtext = (RText)app;
ClassLoader cl = this.getClass().getClassLoader();
URL url = cl.getResource(ICON_FILE);
if (url!=null)
pluginIcon = new ImageIcon(url);
msg = ResourceBundle.getBundle(BUNDLE_NAME, Locale.getDefault(), cl);
this.name = msg.getString("Plugin.Name");
}
/*****************************************************************************/
/**
* Called when the user clicks on one of the plugin's menu items.
*
* @param e The event.
*/
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
if (CREATE_PLAYPEN.equals(actionCommand)) {
mode = MODE_CREATE;
runWizard();
}
else if (PACKAGE_PLAYPEN.equals(actionCommand)) {
mode = MODE_PACKAGE;
runWizard();
}
}
/*****************************************************************************/
/**
* This method should be overridden to initialize all panels in this
* wizard via <code>addInfoPanel</code>. Note that you only need to add
* the "step" panels in this way, not the "introduction" or "successful"
* panels.
*/
protected void createPanels() {
String prefix = getPrefix();
String desc = msg.getString(prefix + "PPen.Desc");
switch (mode) {
case MODE_CREATE:
addInfoPanel(new GetPlaypenPanel(desc, msg));
addInfoPanel(new GetLocalePanel(msg));
addInfoPanel(new VerifyCreatePlaypenPanel(msg));
break;
case MODE_PACKAGE:
addInfoPanel(new GetPlaypenPanel(desc, msg));
addInfoPanel(new VerifyPackagePlaypenPanel(msg));
break;
}
}
/*****************************************************************************/
/**
* Ensures the directory tree for the given file exists. If it does not
* exists, it is created.
*
* @param file The file.
* @return Whether or not the directory tree for this file exists or was
* successfully created.
*/
protected boolean ensureDirectoryExistsFor(File file) {
File parentDir = file.getParentFile();
if (parentDir!=null && !parentDir.exists()) {
if (!ensureDirectoryExistsFor(parentDir))
return false;
return parentDir.mkdir();
}
return true; // Parent directory already exists.
}
/*****************************************************************************/
/**
* Extracts properties files for the given locale from the currently
* running RText.jar to the specified playpen directory. The extracted
* versions will be in Unicode (UTF-16), not ASCII, for easy editing.
*
* @param playpen The directory into which to extract the properties
* files.
* @param locale The locale of the properties files to extract.
* @return Whether or not the extraction was successful.
* @throws IOException If an IO error occurs.
*/
public boolean extractPropFiles(File playpen, Locale locale) throws IOException {
String code = locale.toString();
String installLoc = rtext.getInstallLocation();
File[] pluginJars = new File(installLoc, "plugins").listFiles();
JarFile[] jarFiles = new JarFile[pluginJars.length + 2];
jarFiles[0] = new JarFile(new File(installLoc, "RText.jar"));
jarFiles[1] = new JarFile(new File(installLoc, "lnfs/OfficeLnFs.jar"));
for (int i=0; i<pluginJars.length; i++)
jarFiles[2+i] = new JarFile(pluginJars[i]);
// Loop through all Jar files.
for (int i=0; i<jarFiles.length; i++) {
JarFile jarFile = jarFiles[i];
// Loop through all entries, looking for *.properties.
for (Enumeration e=jarFile.entries(); e.hasMoreElements(); ) {
ZipEntry entry = (ZipEntry)e.nextElement();
String name = entry.getName();
// If this entry is an English *.properties file...
if (name.matches(".*/[^_]+\\.properties")) {
// Get the name for the localized version.
name = name.substring(0,name.length()-11) + "_" +
code + ".properties";
// If there's already a *_xx.properties in the jar,
// extract it, not the English version.
ZipEntry localizedEntry = jarFile.getEntry(name);
if (localizedEntry!=null) { // File already in zip...
entry = localizedEntry;
}
File newFile = new File(playpen, name);
ensureDirectoryExistsFor(newFile);
writeUtf16LE(jarFile.getInputStream(entry), newFile);
}
}
jarFile.close();
}
return true;
}
/*****************************************************************************/
/**
* Returns the author of the plugin.
*
* @return The author.
*/
public String getAuthor() {
return "Robert Futrell";
}
/*****************************************************************************/
/**
* Returns the icon to display beside the name of this plugin in the
* application's interface.
*
* @return The icon for this plugin.
*/
public Icon getIcon() {
return pluginIcon;
}
/*****************************************************************************/
/**
* Returns the "introduction panel;" that is, the initial information
* displayed to the user explain what this wizard will do. For example,
* <p>
*
* <pre>This wizard will walk you through creating a simple Java
* class.</pre>
*
* @param dialog The dialog in which this information will be displayed.
* @return The panel.
* @see #getInfoPanel
* @see #getWizardSuccessfulPanel
*/
protected WizardDialogInfoPanel getIntroductionPanel(
WizardPluginDialog dialog) {
String prefix = getPrefix();
return new IntroPanel(msg.getString(prefix + "Intro.Header"),
msg.getString(prefix + "Intro.Desc"));
}
/*****************************************************************************/
/**
* Returns the menu items for this plugin.
*
* @return The menu for this plugin.
*/
public JMenu getMenu() {
JMenu menu = new JMenu(getName());
JMenuItem item = new JMenuItem(msg.getString("Plugin.Menu.Create"));
item.setActionCommand(CREATE_PLAYPEN);
item.addActionListener(this);
menu.add(item);
item = new JMenuItem(msg.getString("Plugin.Menu.Package"));
item.setActionCommand(PACKAGE_PLAYPEN);
item.addActionListener(this);
menu.add(item);
return menu;
}
/*****************************************************************************/
/**
* Returns the name of the plugin.
*
* @return The plugin name.
*/
public String getName() {
return name;
}
/*****************************************************************************/
/**
* Returns an options panel for use in an Options dialog. This panel
* should contain all options pertaining to this plugin.
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -