📄 getlocalepanel.java
字号:
package org.fife.rtext.plugins.rtextl10ngen;
import java.awt.*;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.fife.RListSelectionModel;
import org.fife.ui.RScrollPane;
import org.fife.ui.app.WizardPluginDialog;
import org.fife.ui.app.WizardDialogInfoPanel;
/**
* Allows the user to select a locale.
*
* @author Robert Futrell
* @version 1.0
*/
class GetLocalePanel extends WizardDialogInfoPanel
implements ListSelectionListener {
private DefaultListModel listModel;
private JList localeList;
private JLabel codeLabel;
private ResourceBundle msg;
/*****************************************************************************/
/**
* Constructor.
*
* @param msg The plugin's resource bundle.
*/
public GetLocalePanel(ResourceBundle msg) {
super(msg.getString("Plugin.Wizard.Create.Locale.Header"));
this.msg = msg;
setLayout(new BorderLayout());
JPanel contentPane = new JPanel();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
JPanel temp = new JPanel(new BorderLayout());
String desc = msg.getString("Plugin.Wizard.Create.Locale.Desc");
temp.add(new JLabel(desc));//, BorderLayout.WEST);
contentPane.add(temp);
contentPane.add(Box.createVerticalStrut(10));
temp = new JPanel(new BorderLayout());
listModel = createListModel();
localeList = new JList(listModel);
localeList.setSelectionModel(new RListSelectionModel());
localeList.addListSelectionListener(this);
localeList.setVisibleRowCount(15);
temp.add(new RScrollPane(localeList), BorderLayout.WEST);
codeLabel = new JLabel("xxxx", JLabel.CENTER);
temp.add(codeLabel);
contentPane.add(temp);
add(contentPane, BorderLayout.NORTH);
localeList.setSelectedIndex(0); // Do after codeLabel is added.
}
/*****************************************************************************/
/**
* Returns the list model containing available locales.
*
* @return The list model.
*/
protected DefaultListModel createListModel() {
// Create a sorted (by display name) list of Locale-supported locales.
Locale[] locales = Locale.getAvailableLocales();
SortableLocale[] sl = new SortableLocale[locales.length];
for (int i=0; i<locales.length; i++) {
sl[i] = new SortableLocale(locales[i]);
}
Arrays.sort(sl);
// If we are running in a JDK or an international JRE, we get many
// more locales, such as Japanese and Chinese ones. If, however,
// we're running in a non-international JRE, we do NOT get many
// locales. We'll check for Japanese and Chinese locales and add
// them in manually if they aren't there.
sl = ensureLocaleAvailable(sl, Locale.SIMPLIFIED_CHINESE);
sl = ensureLocaleAvailable(sl, Locale.TRADITIONAL_CHINESE);
sl = ensureLocaleAvailable(sl, Locale.JAPAN);
sl = ensureLocaleAvailable(sl, Locale.JAPANESE);
sl = ensureLocaleAvailable(sl, Locale.KOREAN);
// Add all locales to the JList model and return it.
DefaultListModel model = new DefaultListModel();
for (int i=0; i<locales.length; i++) {
model.addElement(sl[i]);
}
return model;
}
/*****************************************************************************/
/**
* Checks whether or not the specified locale is in the specified array.
* If it isn't, it is added in the proper location.
*
* @param sl The array of sorted locales.
* @param l The locale to check for.
* @return The (possibly enlarged) sorted locale array.
*/
protected SortableLocale[] ensureLocaleAvailable(SortableLocale[] sl, Locale l) {
SortableLocale temp = new SortableLocale(l);
int index = Arrays.binarySearch(sl, temp);
if (index<-1) {
index = -(index+1);
SortableLocale[] sl2 = new SortableLocale[sl.length+1];
for (int i=0; i<index; i++)
sl2[i] = sl[i];
sl2[index] = temp;
for (int i=index; i<sl.length; i++)
sl2[i+1] = sl[i];
sl = sl2;
}
else {
System.out.println("Locale already available: " + l.getDisplayName());
}
return sl;
}
/*****************************************************************************/
/**
* Returns the locale specified by the user to use for the playpen.
*
* @return The locale.
*/
public Locale getSelectedLocale() {
return ((SortableLocale)localeList.getSelectedValue()).getLocale();
}
/*****************************************************************************/
/**
* Saves input from the user. This method is called when the user clicks
* the "Next" button after giving any input needed for this panel. The
* panel should use the <code>setWizarProperty</code> method of the
* wizard dialog to save entered data.
*
* @param dialog The wizard dialog.
*/
protected void saveUserInput(WizardPluginDialog dialog) {
dialog.setWizardProperty("Locale", getSelectedLocale());
}
/*****************************************************************************/
/**
* Called when the user selects a new locale from the list.
*
* @param e The selection event.
*/
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
String str = msg.getString(
"Plugin.Wizard.Common.Locale.Code.Label");
str = MessageFormat.format(str,
new Object[] { getSelectedLocale().toString() });
codeLabel.setText(str);
}
}
/*****************************************************************************/
/******************* INNER CLASSES *******************************************/
/*****************************************************************************/
/**
* A locale that is comparable to other locales.
*/
static class SortableLocale implements Comparable {
private Locale locale;
public SortableLocale(Locale l) {
this.locale = l;
}
public int compareTo(Object o) {
SortableLocale sl2 = (SortableLocale)o;
return locale.getDisplayName().
compareTo(sl2.locale.getDisplayName());
}
public boolean equals(Object o2) {
return (o2 instanceof SortableLocale) &&
((SortableLocale)o2).locale.getDisplayName().equals(
locale.getDisplayName());
}
public Locale getLocale() {
return locale;
}
public String toString() {
return locale.getDisplayName();
}
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -