7024963: Notepad demo: remove non-translatable resources from Notepad.properties file

Reviewed-by: rupashka
This commit is contained in:
Alexander Scherbatiy 2012-05-04 13:15:49 +04:00
parent e3ab8ba856
commit 045ba7db6b
3 changed files with 92 additions and 200 deletions

View File

@ -39,71 +39,18 @@
import java.awt.BorderLayout; import java.awt.*;
import java.awt.Color; import java.awt.event.*;
import java.awt.Component; import java.beans.*;
import java.awt.Container; import java.io.*;
import java.awt.FileDialog; import java.net.*;
import java.awt.Font; import java.util.*;
import java.awt.Frame; import java.util.logging.*;
import java.awt.Graphics; import javax.swing.*;
import java.awt.Insets; import javax.swing.undo.*;
import java.awt.event.ActionEvent; import javax.swing.text.*;
import java.awt.event.WindowAdapter; import javax.swing.event.*;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.JViewport;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo; import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import javax.swing.text.PlainDocument;
import javax.swing.text.Segment;
import javax.swing.text.TextAction;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
/** /**
@ -115,16 +62,27 @@ import javax.swing.undo.UndoManager;
@SuppressWarnings("serial") @SuppressWarnings("serial")
class Notepad extends JPanel { class Notepad extends JPanel {
private static Properties properties;
private static ResourceBundle resources; private static ResourceBundle resources;
private final static String EXIT_AFTER_PAINT = "-exit"; private final static String EXIT_AFTER_PAINT = "-exit";
private static boolean exitAfterFirstPaint; private static boolean exitAfterFirstPaint;
private static final String[] MENUBAR_KEYS = {"file", "edit", "debug"};
private static final String[] TOOLBAR_KEYS = {"new", "open", "save", "-", "cut", "copy", "paste"};
private static final String[] FILE_KEYS = {"new", "open", "save", "-", "exit"};
private static final String[] EDIT_KEYS = {"cut", "copy", "paste", "-", "undo", "redo"};
private static final String[] DEBUG_KEYS = {"dump", "showElementTree"};
static { static {
try { try {
properties = new Properties();
properties.load(Notepad.class.getResourceAsStream(
"resources/system.properties"));
resources = ResourceBundle.getBundle("resources.Notepad", resources = ResourceBundle.getBundle("resources.Notepad",
Locale.getDefault()); Locale.getDefault());
} catch (MissingResourceException mre) { } catch (MissingResourceException | IOException e) {
System.err.println("resources/Notepad.properties not found"); System.err.println("resources/Notepad.properties "
+ "or resources/system.properties not found");
System.exit(1); System.exit(1);
} }
} }
@ -163,26 +121,22 @@ class Notepad extends JPanel {
// install the command table // install the command table
commands = new HashMap<Object, Action>(); commands = new HashMap<Object, Action>();
Action[] actions = getActions(); Action[] actions = getActions();
for (int i = 0; i < actions.length; i++) { for (Action a : actions) {
Action a = actions[i];
//commands.put(a.getText(Action.NAME), a);
commands.put(a.getValue(Action.NAME), a); commands.put(a.getValue(Action.NAME), a);
} }
JScrollPane scroller = new JScrollPane(); JScrollPane scroller = new JScrollPane();
JViewport port = scroller.getViewport(); JViewport port = scroller.getViewport();
port.add(editor); port.add(editor);
try {
String vpFlag = resources.getString("ViewportBackingStore"); String vpFlag = getProperty("ViewportBackingStore");
if (vpFlag != null) {
Boolean bs = Boolean.valueOf(vpFlag); Boolean bs = Boolean.valueOf(vpFlag);
port.setScrollMode(bs.booleanValue() port.setScrollMode(bs
? JViewport.BACKINGSTORE_SCROLL_MODE ? JViewport.BACKINGSTORE_SCROLL_MODE
: JViewport.BLIT_SCROLL_MODE); : JViewport.BLIT_SCROLL_MODE);
} catch (MissingResourceException ignored) {
// just use the viewport default
} }
menuItems = new HashMap<String, JMenuItem>();
JPanel panel = new JPanel(); JPanel panel = new JPanel();
panel.setLayout(new BorderLayout()); panel.setLayout(new BorderLayout());
panel.add("North", createToolbar()); panel.add("North", createToolbar());
@ -191,8 +145,7 @@ class Notepad extends JPanel {
add("South", createStatusbar()); add("South", createStatusbar());
} }
public static void main(String[] args) { public static void main(String[] args) throws Exception {
try {
if (args.length > 0 && args[0].equals(EXIT_AFTER_PAINT)) { if (args.length > 0 && args[0].equals(EXIT_AFTER_PAINT)) {
exitAfterFirstPaint = true; exitAfterFirstPaint = true;
} }
@ -212,10 +165,6 @@ class Notepad extends JPanel {
frame.setVisible(true); frame.setVisible(true);
} }
}); });
} catch (Throwable t) {
Logger.getLogger(Notepad.class.getName()).log(Level.SEVERE,
"uncaught exception", t);
}
} }
/** /**
@ -274,9 +223,7 @@ class Notepad extends JPanel {
/** /**
* This is the hook through which all menu items are * This is the hook through which all menu items are
* created. It registers the result with the menuitem * created.
* hashtable so that it can be fetched with getMenuItem().
* @see #getMenuItem
*/ */
protected JMenuItem createMenuItem(String cmd) { protected JMenuItem createMenuItem(String cmd) {
JMenuItem mi = new JMenuItem(getResourceString(cmd + labelSuffix)); JMenuItem mi = new JMenuItem(getResourceString(cmd + labelSuffix));
@ -285,7 +232,7 @@ class Notepad extends JPanel {
mi.setHorizontalTextPosition(JButton.RIGHT); mi.setHorizontalTextPosition(JButton.RIGHT);
mi.setIcon(new ImageIcon(url)); mi.setIcon(new ImageIcon(url));
} }
String astr = getResourceString(cmd + actionSuffix); String astr = getProperty(cmd + actionSuffix);
if (astr == null) { if (astr == null) {
astr = cmd; astr = cmd;
} }
@ -298,25 +245,17 @@ class Notepad extends JPanel {
} else { } else {
mi.setEnabled(false); mi.setEnabled(false);
} }
menuItems.put(cmd, mi);
return mi; return mi;
} }
/**
* Fetch the menu item that was created for the given
* command.
* @param cmd Name of the action.
* @returns item created for the given command or null
* if one wasn't created.
*/
protected JMenuItem getMenuItem(String cmd) {
return menuItems.get(cmd);
}
protected Action getAction(String cmd) { protected Action getAction(String cmd) {
return commands.get(cmd); return commands.get(cmd);
} }
protected String getProperty(String key) {
return properties.getProperty(key);
}
protected String getResourceString(String nm) { protected String getResourceString(String nm) {
String str; String str;
try { try {
@ -330,20 +269,11 @@ class Notepad extends JPanel {
protected URL getResource(String key) { protected URL getResource(String key) {
String name = getResourceString(key); String name = getResourceString(key);
if (name != null) { if (name != null) {
URL url = this.getClass().getResource(name); return this.getClass().getResource(name);
return url;
} }
return null; return null;
} }
protected Container getToolbar() {
return toolbar;
}
protected JMenuBar getMenubar() {
return menubar;
}
/** /**
* Create a status bar * Create a status bar
*/ */
@ -368,12 +298,11 @@ class Notepad extends JPanel {
*/ */
private Component createToolbar() { private Component createToolbar() {
toolbar = new JToolBar(); toolbar = new JToolBar();
String[] toolKeys = tokenize(getResourceString("toolbar")); for (String toolKey: TOOLBAR_KEYS) {
for (int i = 0; i < toolKeys.length; i++) { if (toolKey.equals("-")) {
if (toolKeys[i].equals("-")) {
toolbar.add(Box.createHorizontalStrut(5)); toolbar.add(Box.createHorizontalStrut(5));
} else { } else {
toolbar.add(createTool(toolKeys[i])); toolbar.add(createTool(toolKey));
} }
} }
toolbar.add(Box.createHorizontalGlue()); toolbar.add(Box.createHorizontalGlue());
@ -408,7 +337,7 @@ class Notepad extends JPanel {
b.setRequestFocusEnabled(false); b.setRequestFocusEnabled(false);
b.setMargin(new Insets(1, 1, 1, 1)); b.setMargin(new Insets(1, 1, 1, 1));
String astr = getResourceString(key + actionSuffix); String astr = getProperty(key + actionSuffix);
if (astr == null) { if (astr == null) {
astr = key; astr = key;
} }
@ -428,44 +357,18 @@ class Notepad extends JPanel {
return b; return b;
} }
/**
* Take the given string and chop it up into a series
* of strings on whitespace boundaries. This is useful
* for trying to get an array of strings out of the
* resource file.
*/
protected String[] tokenize(String input) {
List<String> v = new ArrayList<String>();
StringTokenizer t = new StringTokenizer(input);
String cmd[];
while (t.hasMoreTokens()) {
v.add(t.nextToken());
}
cmd = new String[v.size()];
for (int i = 0; i < cmd.length; i++) {
cmd[i] = v.get(i);
}
return cmd;
}
/** /**
* Create the menubar for the app. By default this pulls the * Create the menubar for the app. By default this pulls the
* definition of the menu from the associated resource file. * definition of the menu from the associated resource file.
*/ */
protected JMenuBar createMenubar() { protected JMenuBar createMenubar() {
JMenuItem mi;
JMenuBar mb = new JMenuBar(); JMenuBar mb = new JMenuBar();
for(String menuKey: MENUBAR_KEYS){
String[] menuKeys = tokenize(getResourceString("menubar")); JMenu m = createMenu(menuKey);
for (int i = 0; i < menuKeys.length; i++) {
JMenu m = createMenu(menuKeys[i]);
if (m != null) { if (m != null) {
mb.add(m); mb.add(m);
} }
} }
this.menubar = mb;
return mb; return mb;
} }
@ -474,19 +377,32 @@ class Notepad extends JPanel {
* definition of the menu from the associated resource file. * definition of the menu from the associated resource file.
*/ */
protected JMenu createMenu(String key) { protected JMenu createMenu(String key) {
String[] itemKeys = tokenize(getResourceString(key)); JMenu menu = new JMenu(getResourceString(key + labelSuffix));
JMenu menu = new JMenu(getResourceString(key + "Label")); for (String itemKey: getItemKeys(key)) {
for (int i = 0; i < itemKeys.length; i++) { if (itemKey.equals("-")) {
if (itemKeys[i].equals("-")) {
menu.addSeparator(); menu.addSeparator();
} else { } else {
JMenuItem mi = createMenuItem(itemKeys[i]); JMenuItem mi = createMenuItem(itemKey);
menu.add(mi); menu.add(mi);
} }
} }
return menu; return menu;
} }
// get keys for menus
private String[] getItemKeys(String key) {
switch (key) {
case "file":
return FILE_KEYS;
case "edit":
return EDIT_KEYS;
case "debug":
return DEBUG_KEYS;
default:
return null;
}
}
// Yarked from JMenu, ideally this would be public. // Yarked from JMenu, ideally this would be public.
protected PropertyChangeListener createActionChangeListener(JMenuItem b) { protected PropertyChangeListener createActionChangeListener(JMenuItem b) {
return new ActionChangedListener(b); return new ActionChangedListener(b);
@ -516,13 +432,11 @@ class Notepad extends JPanel {
} }
private JTextComponent editor; private JTextComponent editor;
private Map<Object, Action> commands; private Map<Object, Action> commands;
private Map<String, JMenuItem> menuItems;
private JMenuBar menubar;
private JToolBar toolbar; private JToolBar toolbar;
private JComponent status; private JComponent status;
private JFrame elementTreeFrame; private JFrame elementTreeFrame;
protected ElementTreePanel elementTreePanel; protected ElementTreePanel elementTreePanel;
protected FileDialog fileDialog;
/** /**
* Listener for the edits on the current document. * Listener for the edits on the current document.
*/ */
@ -773,10 +687,6 @@ class Notepad extends JPanel {
super(showElementTreeAction); super(showElementTreeAction);
} }
ShowElementTreeAction(String nm) {
super(nm);
}
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (elementTreeFrame == null) { if (elementTreeFrame == null) {
// Create a frame containing an instance of // Create a frame containing an instance of

View File

@ -3,16 +3,6 @@
Title=Notepad Title=Notepad
ElementTreeFrameTitle=Elements ElementTreeFrameTitle=Elements
# The following string should NOT be translated: ViewportBackingStore
ViewportBackingStore=false
# menubar definition
#
# Each of the strings that follow form a key to be
# used to the actual menu definition.
# The following string should NOT be translated: menubar
menubar=file edit debug
# file Menu definition # file Menu definition
# #
@ -24,8 +14,6 @@ menubar=file edit debug
# save -> Notepad.saveAction # save -> Notepad.saveAction
# exit -> Notepad.exitAction # exit -> Notepad.exitAction
# The following string should NOT be translated: file
file=new open save - exit
fileLabel=File fileLabel=File
openLabel=Open openLabel=Open
openImage=resources/open.gif openImage=resources/open.gif
@ -42,38 +30,22 @@ exitLabel=Exit
# copy -> JTextComponent.copyAction # copy -> JTextComponent.copyAction
# paste -> JTextComponent.pasteAction # paste -> JTextComponent.pasteAction
# The following string should NOT be translated: edit
edit=cut copy paste - undo redo
editLabel=Edit editLabel=Edit
cutLabel=Cut cutLabel=Cut
# The following string should NOT be translated: cutAction
cutAction=cut-to-clipboard
cutImage=resources/cut.gif cutImage=resources/cut.gif
copyLabel=Copy copyLabel=Copy
# The following string should NOT be translated: copyAction
copyAction=copy-to-clipboard
copyImage=resources/copy.gif copyImage=resources/copy.gif
pasteLabel=Paste pasteLabel=Paste
# The following string should NOT be translated: pasteAction
pasteAction=paste-from-clipboard
pasteImage=resources/paste.gif pasteImage=resources/paste.gif
undoLabel=Undo undoLabel=Undo
# The following string should NOT be translated: undoAction
undoAction=Undo
redoLabel=Redo redoLabel=Redo
# The following string should NOT be translated: redoAction
redoAction=Redo
# #
# debug Menu definition # debug Menu definition
# #
# The following string should NOT be translated: debug
debug=dump showElementTree
debugLabel=Debug debugLabel=Debug
dumpLabel=Dump model to System.err dumpLabel=Dump model to System.err
# The following string should NOT be translated: dumpAction
dumpAction=dump-model
showElementTreeLabel=Show Elements showElementTreeLabel=Show Elements
# toolbar definition # toolbar definition
@ -83,8 +55,6 @@ showElementTreeLabel=Show Elements
# are of course sharable, and in this case are shared # are of course sharable, and in this case are shared
# with the menu items. # with the menu items.
# The following string should NOT be translated: toolbar
toolbar=new open save - cut copy paste
newTooltip=Create a new file newTooltip=Create a new file
openTooltip=Open a file openTooltip=Open a file
saveTooltip=Save to a file saveTooltip=Save to a file

View File

@ -0,0 +1,12 @@
#
# Non-translatable properties for Notepad example
ViewportBackingStore=false
cutAction=cut-to-clipboard
copyAction=copy-to-clipboard
pasteAction=paste-from-clipboard
undoAction=Undo
redoAction=Redo
dumpAction=dump-model