Source code: com/imagero/gui/flowin/InputPopup.java
1 /*
2 * Copyright (c) imagero Andrey Kuznetsov. All Rights Reserved.
3 * http://jgui.imagero.com
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 package com.imagero.gui.flowin;
21
22 import javax.swing.*;
23 import javax.swing.plaf.basic.BasicBorders;
24 import java.awt.*;
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
27 import java.awt.event.FocusAdapter;
28 import java.awt.event.FocusEvent;
29 import java.awt.event.KeyEvent;
30
31 /**
32 * With InputPopup it is possible to show popup with JTextField.
33 * When popup is closed ActionEvent is fired with content of JTextField as action command.
34 * If isCanceled() returns true then contents of action command should be ignored.
35 * Popup is closed if JTextField lose focus or user has pressed escape key - in both cases isCanceled() returns true,
36 * or if user pressed return key - in this case isCanceled() returns false.
37 *
38 * @author Andrey Kuznetsov
39 */
40 public class InputPopup {
41
42 JTextField input = new JTextField(20);
43 JWindow window;
44 Component owner;
45 JLabel label;
46 ActionListener actionListener;
47 boolean canceled;
48
49 public InputPopup(Component owner, String s) {
50 this.owner = owner;
51 label = new JLabel(s);
52 }
53
54 public InputPopup(Component owner, Icon icon, String s) {
55 this.owner = owner;
56 label = new JLabel(s, icon, SwingConstants.LEFT);
57 }
58
59 protected JWindow getWindow() {
60 if (window == null) {
61 window = new JWindow(SwingUtilities.getWindowAncestor(owner));
62 input = new JTextField(20);
63
64 window.getContentPane().setLayout(new BorderLayout());
65 window.getContentPane().add(label, BorderLayout.NORTH);
66 window.getContentPane().add(input);
67 ((JComponent) window.getContentPane()).setBorder(BasicBorders.getInternalFrameBorder());
68 window.pack();
69 input.addFocusListener(new FocusAdapter() {
70 public void focusLost(FocusEvent e) {
71 window.setVisible(false);
72 canceled = true;
73 if(actionListener != null) {
74 actionListener.actionPerformed(new ActionEvent(input, ActionEvent.ACTION_PERFORMED, null));
75 }
76 }
77 });
78 input.addActionListener(new ActionListener() {
79 public void actionPerformed(ActionEvent e) {
80 canceled = false;
81 if(actionListener != null) {
82 actionListener.actionPerformed(e);
83 }
84 window.setVisible(false);
85 }
86 });
87 KeyStroke eks = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
88 InputMap imap = input.getInputMap(JComponent.WHEN_FOCUSED);
89 imap.put(eks, "closeInput");
90 ActionMap amap = input.getActionMap();
91 amap.put("closeInput", new AbstractAction("closeInput") {
92 public void actionPerformed(ActionEvent e) {
93 canceled = true;
94 if(actionListener != null) {
95 actionListener.actionPerformed(new ActionEvent(input, ActionEvent.ACTION_PERFORMED, null));
96 }
97 window.setVisible(false);
98 }
99 });
100 }
101 return window;
102 }
103
104 public void showPopup() {
105 JWindow window = getWindow();
106 window.setVisible(true);
107 window.setLocationRelativeTo(owner);
108 }
109
110 public synchronized void addActionListener(ActionListener l) {
111 actionListener = AWTEventMulticaster.add(actionListener, l);
112 }
113
114 public synchronized void removeActionListener(ActionListener l) {
115 actionListener = AWTEventMulticaster.remove(actionListener, l);
116 }
117
118 public boolean isCanceled() {
119 return canceled;
120 }
121 }