📄 swingcallbackhandler.java
字号:
protected void handleLanguage (final LanguageCallback callback) throws IOException { Locale locale = Locale.getDefault (); Locale[] locales = Locale.getAvailableLocales (); String[] localeNames = new String[locales.length+1]; int defaultIndex = 0; for (int i = 0; i < locales.length; i++) { localeNames[i+1] = locales[i].getDisplayLanguage (locales[i]); String country = locales[i].getDisplayCountry (locales[i]); if (country.length () > 0) localeNames[i+1] += " (" + country + ")"; if (locales[i].equals (locale)) defaultIndex = i; } locales[0] = locale; localeNames[0] = locale.getDisplayLanguage (locale); String country = locale.getDisplayCountry (locale); if (country.length () > 0) localeNames[0] += " (" + country + ")"; ChoiceCallback cb = new ChoiceCallback (messages.getString ("callback.language"), localeNames, 0, false); handleChoice (cb); int selected = cb.getSelectedIndexes ()[0]; if (selected > 0) callback.setLocale (locales[selected - 1]); else callback.setLocale (locale); } protected void handleName (final NameCallback callback) throws IOException { final JDialog dialog = new JDialog (); Container content = dialog.getContentPane (); content.setLayout (new GridBagLayout ()); content.add (new JLabel (callback.getPrompt ()), new GridBagConstraints (0, 0, 1, 1, 0, 0, GridBagConstraints.NORTHEAST, GridBagConstraints.VERTICAL, new Insets (10, 10, 15, 5), 0, 0)); final JTextField name = new JTextField (); name.setColumns (20); String _name; if ((_name = callback.getDefaultName ()) != null) name.setText (_name); content.add (name, new GridBagConstraints (1, 0, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets (10, 5, 15, 10), 0, 0)); ActionListener listener = new ActionListener () { public void actionPerformed (ActionEvent ae) { String cmd = ae.getActionCommand (); if (cmd.equals ("okay")) callback.setName (name.getText ()); dialog.setVisible (false); synchronized (callback) { callback.notify (); } } }; JPanel buttons = new JPanel (); buttons.setLayout (new FlowLayout (FlowLayout.RIGHT)); JButton cancel = new JButton (messages.getString ("callback.cancel")); JButton okay = new JButton (messages.getString ("callback.ok")); cancel.setActionCommand ("cancel"); cancel.addActionListener (listener); buttons.add (cancel); okay.setActionCommand ("okay"); okay.addActionListener (listener); buttons.add (okay); content.add (buttons, new GridBagConstraints (0, 1, 2, 1, 0, 0, GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE, new Insets (0, 10, 10, 10), 0, 0)); dialog.setResizable (false); dialog.pack (); dialog.setVisible (true); dialog.getRootPane ().setDefaultButton (okay); waitForInput (dialog, callback); } protected void handlePassword (final PasswordCallback callback) throws IOException { final JDialog dialog = new JDialog (); Container content = dialog.getContentPane (); content.setLayout (new GridBagLayout ()); content.add (new JLabel (callback.getPrompt ()), new GridBagConstraints (0, 0, 1, 1, 0, 0, GridBagConstraints.NORTHEAST, GridBagConstraints.VERTICAL, new Insets (10, 10, 15, 5), 0, 0)); final JPasswordField password = new JPasswordField (); password.setColumns (20); password.setEchoChar (callback.isEchoOn () ? '\u0000' : '\u2022'); content.add (password, new GridBagConstraints (1, 0, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets (10, 5, 15, 10), 0, 0)); ActionListener listener = new ActionListener () { public void actionPerformed (ActionEvent ae) { String cmd = ae.getActionCommand (); if (cmd.equals ("okay")) callback.setPassword (password.getPassword ()); dialog.setVisible (false); synchronized (callback) { callback.notify (); } } }; JPanel buttons = new JPanel (); buttons.setLayout (new FlowLayout (FlowLayout.RIGHT)); JButton cancel = new JButton (messages.getString ("callback.cancel")); JButton okay = new JButton (messages.getString ("callback.ok")); cancel.setActionCommand ("cancel"); cancel.addActionListener (listener); buttons.add (cancel); okay.setActionCommand ("okay"); okay.addActionListener (listener); buttons.add (okay); content.add (buttons, new GridBagConstraints (0, 1, 2, 1, 0, 0, GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE, new Insets (0, 10, 10, 10), 0, 0)); dialog.setResizable (false); dialog.pack (); dialog.setVisible (true); dialog.getRootPane ().setDefaultButton (okay); waitForInput (dialog, callback); } protected void handleTextInput (final TextInputCallback callback) throws IOException { final JDialog dialog = new JDialog (); Container content = dialog.getContentPane (); content.setLayout (new GridBagLayout ()); content.add (new JLabel (callback.getPrompt ()), new GridBagConstraints (0, 0, 1, 1, 0, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets (10, 10, 15, 5), 0, 0)); final JTextArea text = new JTextArea (24, 80); text.setEditable (true); String _text; if ((_text = callback.getDefaultText ()) != null) text.setText (_text); text.setFont (new Font ("Monospaced", Font.PLAIN, 12)); JScrollPane textPane = new JScrollPane (text, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); content.add (textPane, new GridBagConstraints (0, 1, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets (5, 10, 5, 10), 0, 0)); ActionListener listener = new ActionListener () { public void actionPerformed (ActionEvent ae) { String cmd = ae.getActionCommand (); if (cmd.equals ("okay")) callback.setText (text.getText ()); dialog.setVisible (false); synchronized (callback) { callback.notify (); } } }; JPanel buttons = new JPanel (); buttons.setLayout (new FlowLayout (FlowLayout.RIGHT)); JButton cancel = new JButton (messages.getString ("callback.cancel")); JButton okay = new JButton (messages.getString ("callback.ok")); cancel.setActionCommand ("cancel"); cancel.addActionListener (listener); buttons.add (cancel); okay.setActionCommand ("okay"); okay.addActionListener (listener); buttons.add (okay); content.add (buttons, new GridBagConstraints (0, 2, 1, 1, 0, 0, GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE, new Insets (0, 10, 10, 10), 0, 0)); dialog.setResizable (true); dialog.pack (); dialog.setVisible (true); dialog.getRootPane ().setDefaultButton (okay); waitForInput (dialog, callback); } protected void handleTextOutput (final TextOutputCallback callback) throws IOException { final JDialog dialog = new JDialog (); switch (callback.getMessageType ()) { case TextOutputCallback.ERROR: dialog.setTitle (messages.getString ("callback.error")); break; case TextOutputCallback.WARNING: dialog.setTitle (messages.getString ("callback.warning")); break; case TextOutputCallback.INFORMATION: dialog.setTitle (messages.getString ("callback.information")); break; } Container content = dialog.getContentPane (); content.setLayout (new GridBagLayout ()); final JTextArea text = new JTextArea (24, 80); text.setEditable (false); text.setText (callback.getMessage ()); text.setFont (new Font ("Monospaced", Font.PLAIN, 12)); JScrollPane textPane = new JScrollPane (text, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); content.add (textPane, new GridBagConstraints (0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets (10, 10, 5, 10), 0, 0)); ActionListener listener = new ActionListener () { public void actionPerformed (ActionEvent ae) { dialog.setVisible (false); synchronized (callback) { callback.notify (); } } }; JButton okay = new JButton (messages.getString ("callback.ok")); okay.setActionCommand ("okay"); okay.addActionListener (listener); content.add (okay, new GridBagConstraints (0, 1, 1, 1, 0, 0, GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE, new Insets (0, 10, 10, 10), 0, 0)); dialog.setResizable (true); dialog.pack (); dialog.setVisible (true); dialog.getRootPane ().setDefaultButton (okay); waitForInput (dialog, callback); } private void waitForInput (JDialog dialog, Callback callback) { synchronized (callback) { while (dialog.isVisible ()) { try { callback.wait (1000); } catch (InterruptedException ignored) { } } } dialog.dispose (); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -