001package com.ganteater.ae.desktop.ui;
002
003import java.awt.Component;
004import java.awt.Dimension;
005import java.awt.Graphics;
006import java.awt.event.KeyEvent;
007
008import javax.swing.CellRendererPane;
009import javax.swing.JComponent;
010import javax.swing.JLabel;
011import javax.swing.JPopupMenu;
012import javax.swing.JTextArea;
013import javax.swing.JToolTip;
014import javax.swing.MenuElement;
015import javax.swing.MenuSelectionManager;
016import javax.swing.plaf.ComponentUI;
017import javax.swing.plaf.basic.BasicToolTipUI;
018
019public class DialogPopupMenu extends JPopupMenu {
020
021        private JTextArea editor;
022
023        public DialogPopupMenu(Component component) {
024                super();
025                if (component instanceof JTextArea)
026                        this.editor = (JTextArea) component;
027                setAutoscrolls(true);
028        }
029
030        /**
031         * Field holding the value of serialVersionUID
032         */
033        private static final long serialVersionUID = 829852467458669791L;
034
035        public static class JMultiLineToolTip extends JToolTip {
036                private static final long serialVersionUID = 1L;
037
038                public JMultiLineToolTip() {
039                        updateUI();
040                }
041
042                @Override
043                public void updateUI() {
044                        setUI(MultiLineToolTipUI.createUI(this));
045                }
046
047                public void setColumns(int columns) {
048                        this.columns = columns;
049                        this.fixedwidth = 0;
050                }
051
052                public int getColumns() {
053                        return columns;
054                }
055
056                public void setFixedWidth(int width) {
057                        this.fixedwidth = width;
058                        this.columns = 0;
059                }
060
061                public int getFixedWidth() {
062                        return fixedwidth;
063                }
064
065                protected int columns = 0;
066                protected int fixedwidth = 0;
067        }
068
069        static class MultiLineToolTipUI extends BasicToolTipUI {
070                static MultiLineToolTipUI sharedInstance = new MultiLineToolTipUI();
071
072                private CellRendererPane rendererPane;
073                private JLabel textArea;
074
075                public static ComponentUI createUI(JComponent c) {
076                        return sharedInstance;
077                }
078
079                public MultiLineToolTipUI() {
080                        super();
081                }
082
083                @Override
084                public void installUI(JComponent c) {
085                        super.installUI(c);
086                        rendererPane = new CellRendererPane();
087                        c.add(rendererPane);
088                }
089
090                @Override
091                public void uninstallUI(JComponent c) {
092                        super.uninstallUI(c);
093
094                        c.remove(rendererPane);
095                        rendererPane = null;
096                }
097
098                @Override
099                public void paint(Graphics g, JComponent c) {
100                        Dimension size = c.getSize();
101                        textArea.setBackground(c.getBackground());
102                        rendererPane.paintComponent(g, textArea, c, 1, 1, size.width - 1, size.height - 1, true);
103                }
104
105                @Override
106                public Dimension getPreferredSize(JComponent c) {
107                        String tipText = ((JToolTip) c).getTipText();
108                        if (tipText == null)
109                                return new Dimension(0, 0);
110                        textArea = new JLabel(tipText);
111                        rendererPane.removeAll();
112                        rendererPane.add(textArea);
113                        int width = ((JMultiLineToolTip) c).getFixedWidth();
114                        int columns = ((JMultiLineToolTip) c).getColumns();
115
116                        if (columns > 0) {
117                                textArea.setSize(0, 0);
118                                textArea.setSize(textArea.getPreferredSize());
119                        } else if (width > 0) {
120                                Dimension d = textArea.getPreferredSize();
121                                d.width = width;
122                                d.height++;
123                                textArea.setSize(d);
124                        }
125
126                        Dimension dim = textArea.getPreferredSize();
127
128                        dim.height += 1;
129                        dim.width += 1;
130                        return dim;
131                }
132
133                @Override
134                public Dimension getMinimumSize(JComponent c) {
135                        return getPreferredSize(c);
136                }
137
138                @Override
139                public Dimension getMaximumSize(JComponent c) {
140                        return getPreferredSize(c);
141                }
142        }
143
144        @Override
145        public void processKeyEvent(KeyEvent e, MenuElement[] path, MenuSelectionManager manager) {
146                char keyChar = e.getKeyChar();
147                if (Character.isLetter(keyChar)) {
148                        setVisible(false);
149                        if (editor != null)
150                                editor.insert(new String(new char[] { keyChar }), editor.getCaretPosition());
151                } else
152                        super.processKeyEvent(e, path, manager);
153        }
154
155}