Index: src/test/FrameMain.java =================================================================== --- src/test/FrameMain.java (Revision 0) +++ src/test/FrameMain.java (Revision 0) @@ -0,0 +1,60 @@ + +import java.awt.event.WindowEvent; +import java.awt.event.WindowListener; + +import javax.swing.JButton; +import javax.swing.JComponent; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.KeyStroke; + +public final class FrameMain extends JFrame implements WindowListener +{ + private JPanel main = new JPanel(); + + public static void main(String[] args) + { + new FrameMain(); + } + + public FrameMain() + { + super.setTitle("Xstream KeyStroke bug"); + super.setLocation(300, 300); + super.setSize(200, 200); + super.addWindowListener(this); + + JButton button = new JButton("Foo"); + button.setAction(new ActionFoo(this)); + this.main.add(button); + + KeyStroke key = null; + //key = KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_DOWN_MASK, true); + key = Settings.getInstance().getAcceleratorMap().get(ActionFoo.class); + System.out.println(key); + + this.main.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(key, ActionFoo.class.getSimpleName()); + this.main.getActionMap().put(ActionFoo.class.getSimpleName(), new ActionFoo(this)); + + super.setContentPane(this.main); + super.setVisible(true); + } + + public void windowClosing(final WindowEvent event) + { + Settings.getInstance().writeSettings(); + System.exit(0); + } + public void windowIconified(final WindowEvent event) + { /* nothing */ } + public void windowOpened(final WindowEvent event) + { /* nothing */ } + public void windowClosed(final WindowEvent event) + { /* nothing */ } + public void windowDeiconified(final WindowEvent event) + { /* nothing */ } + public void windowActivated(final WindowEvent event) + { /* nothing */ } + public void windowDeactivated(final WindowEvent event) + { /* nothing */ } +} Index: src/test/Settings.java =================================================================== --- src/test/Settings.java (Revision 0) +++ src/test/Settings.java (Revision 0) @@ -0,0 +1,102 @@ + +import java.awt.event.InputEvent; +import java.awt.event.KeyEvent; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; +import java.util.HashMap; + +import javax.swing.KeyStroke; + +import com.thoughtworks.xstream.XStream; + +final class Settings implements Serializable +{ + private static Settings instance = null; + + private HashMap, KeyStroke> acceleratorMap = null; + + public static Settings getInstance() + { + if (instance == null) + { + try { + instance = readSettings("settings.obj"); //$NON-NLS-1$ + } + catch (Exception e) { + e.printStackTrace(); + instance = new Settings(); + } + } + return instance; + } + + private Settings() + { + this.setDefaultAccelerators(); + } + + /** + * Sets the default accelerators for each action used in this program. + */ + public void setDefaultAccelerators() + { + if (this.acceleratorMap == null) + this.acceleratorMap = new HashMap, KeyStroke>(); + else + this.acceleratorMap.clear(); + + this.acceleratorMap.put(ActionFoo.class, KeyStroke.getKeyStroke(KeyEvent.VK_B, 0, true)); + } + + public void writeSettings() + { + try { + this.write("settings.obj"); //$NON-NLS-1$ + } + catch (Exception e) { + e.printStackTrace(); + } + } + + public HashMap, KeyStroke> getAcceleratorMap() + { + return this.acceleratorMap; + } + + private final void write(final String file) throws IOException + { + XStream xstream = new XStream(); + xstream.toXML(this, new FileOutputStream(file)); + } + public static Settings readSettings(final String file) throws IOException + { + XStream xstream = new XStream(); + return (Settings) xstream.fromXML(new FileInputStream(file)); + } + /* + private final void write(final String file) throws IOException + { + ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file)); + try { + oos.writeObject(this); + } + finally { + oos.close(); + } + } + private static Settings readSettings(final String file) throws IOException, ClassNotFoundException + { + ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file)); + try { + return (Settings) ois.readObject(); + } + finally { + ois.close(); + } + } + */ +} Index: src/test/ActionFoo.java =================================================================== --- src/test/ActionFoo.java (Revision 0) +++ src/test/ActionFoo.java (Revision 0) @@ -0,0 +1,20 @@ + +import java.awt.event.ActionEvent; + +import javax.swing.AbstractAction; +import javax.swing.JOptionPane; + +final class ActionFoo extends AbstractAction +{ + private FrameMain parent; + + public ActionFoo(FrameMain parent) + { + this.parent = parent; + } + + public void actionPerformed(ActionEvent arg0) + { + JOptionPane.showMessageDialog(this.parent, "Foo"); + } +}