PageRenderTime 77ms CodeModel.GetById 16ms app.highlight 40ms RepoModel.GetById 1ms app.codeStats 1ms

/src/mpv5/ui/dialogs/BigPopup.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 363 lines | 253 code | 37 blank | 73 comment | 44 complexity | 28278dfe72f69200b82ef5ece7d8f546 MD5 | raw file
  1/*
  2 * To change this template, choose Tools | Templates
  3 * and open the template in the editor.
  4 */
  5package mpv5.ui.dialogs;
  6
  7import java.awt.BorderLayout;
  8import java.awt.Dimension;
  9import java.awt.Point;
 10import java.awt.event.KeyEvent;
 11import java.awt.event.KeyListener;
 12import java.util.HashMap;
 13import javax.swing.JComponent;
 14import javax.swing.JDialog;
 15import javax.swing.JPanel;
 16import mpv5.YabsViewProxy;
 17import mpv5.globals.Messages;
 18import mpv5.ui.misc.Position;
 19
 20/**
 21 *
 22 *  
 23 */
 24public class BigPopup {
 25
 26    private static HashMap<JPanel, JDialog> contents = new HashMap<JPanel, JDialog>();
 27
 28    /**
 29     * Creates a new popup FRAME
 30     * @param content
 31     */
 32    public static void showPopup(JPanel content) {
 33        if (!contents.containsKey(content)) {
 34            final JDialog window = new JDialog(YabsViewProxy.instance().getIdentifierFrame(), Messages.YABS.getValue());
 35            window.getContentPane().setLayout(new BorderLayout());
 36            window.getContentPane().add(content, BorderLayout.CENTER);
 37            window.pack();
 38            window.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
 39            window.addKeyListener(new KeyListener() {
 40
 41                @Override
 42                public void keyTyped(KeyEvent e) {
 43                    if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
 44                        window.dispose();
 45                    }
 46                }
 47
 48                @Override
 49                public void keyPressed(KeyEvent e) {
 50                    if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
 51                        window.dispose();
 52                    }
 53                }
 54
 55                @Override
 56                public void keyReleased(KeyEvent e) {
 57                }
 58            });
 59
 60            window.setLocationRelativeTo(mpv5.YabsViewProxy.instance().getIdentifierFrame());
 61            window.setVisible(true);
 62            contents.put(content, window);
 63        } else {
 64            try {
 65                show(content);
 66                setOnTop(content);
 67            } catch (Exception exception) {
 68            }
 69        }
 70    }
 71
 72    /**
 73     * Creates a new popup FRAME
 74     * @param content
 75     * @param title
 76     * @param locationOnScreen
 77     */
 78    public static void showPopup(JPanel content, String title, Point locationOnScreen) {
 79        if (!contents.containsKey(content)) {
 80            final JDialog window = new JDialog(YabsViewProxy.instance().getIdentifierFrame(), Messages.YABS.getValue());
 81            window.getContentPane().setLayout(new BorderLayout());
 82            window.getContentPane().add(content, BorderLayout.CENTER);
 83            window.pack();
 84            window.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
 85            window.setTitle(title);
 86            window.addKeyListener(new KeyListener() {
 87
 88                @Override
 89                public void keyTyped(KeyEvent e) {
 90                    if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
 91                        window.dispose();
 92                    }
 93                }
 94
 95                @Override
 96                public void keyPressed(KeyEvent e) {
 97                    if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
 98                        window.dispose();
 99                    }
100                }
101
102                @Override
103                public void keyReleased(KeyEvent e) {
104                }
105            });
106
107            window.setLocation(locationOnScreen);
108            window.setVisible(true);
109            contents.put(content, window);
110        } else {
111            try {
112                show(content);
113                setOnTop(content);
114            } catch (Exception exception) {
115            }
116        }
117    }
118
119    /**
120     * Creates a new popup FRAME
121     * @param parent
122     * @param content
123     * @param title
124     * @param state
125     * @param width optional
126     */
127    public static void showPopup(JComponent parent, JPanel content, String title, Integer height, Integer width) {
128        if (!contents.containsKey(content)) {
129            final JDialog window = new JDialog(YabsViewProxy.instance().getIdentifierFrame(), Messages.YABS.getValue());
130            window.getContentPane().setLayout(new BorderLayout());
131            window.getContentPane().add(content, BorderLayout.CENTER);
132            window.pack();
133            window.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
134            window.setTitle(title);
135            window.addKeyListener(new KeyListener() {
136
137                @Override
138                public void keyTyped(KeyEvent e) {
139                    if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
140                        window.dispose();
141                    }
142                }
143
144                @Override
145                public void keyPressed(KeyEvent e) {
146                    if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
147                        window.dispose();
148                    }
149                }
150
151                @Override
152                public void keyReleased(KeyEvent e) {
153                }
154            });
155            if (width != null) {
156                window.setPreferredSize(new Dimension(width, height));
157                window.setSize(new Dimension(width, height));
158            }
159//            window.setExtendedState(state);
160            new Position(window);
161            window.setVisible(true);
162            contents.put(content, window);
163        } else {
164            try {
165                show(content);
166                setOnTop(content);
167            } catch (Exception exception) {
168            }
169        }
170    }
171
172    /**
173     * Creates a new popup FRAME
174     * @param parent
175     * @param content
176     * @param title
177     * @param alwaysOnTop make the popup modal
178     */
179    public static void showPopup(JComponent parent, JPanel content, String title, boolean alwaysOnTop) {
180
181        if (!contents.containsKey(content)) {
182            final JDialog window = new JDialog(YabsViewProxy.instance().getIdentifierFrame(), Messages.YABS.getValue());
183            window.getContentPane().setLayout(new BorderLayout());
184            window.getContentPane().add(content, BorderLayout.CENTER);
185            window.pack();
186            window.setAlwaysOnTop(alwaysOnTop);
187            window.setModal(alwaysOnTop);
188//            if (alwaysOnTop) {
189//                window.setModal(alwaysOnTop);
190//            }
191            window.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
192            window.setTitle(title);
193            window.addKeyListener(new KeyListener() {
194
195                @Override
196                public void keyTyped(KeyEvent e) {
197                    if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
198                        window.dispose();
199                    }
200                }
201
202                @Override
203                public void keyPressed(KeyEvent e) {
204                    if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
205                        window.dispose();
206                    }
207                }
208
209                @Override
210                public void keyReleased(KeyEvent e) {
211                }
212            });
213            new Position(window);
214            window.setVisible(true);
215            contents.put(content, window);
216        } else {
217            try {
218                show(content);
219                if (alwaysOnTop) {
220                    setOnTop(content);
221                }
222            } catch (Exception exception) {
223            }
224        }
225    }
226
227    /**
228     * Creates a new popup FRAME
229     * @param parent
230     * @param content
231     * @param title
232     */
233    public static void showPopup(JComponent parent, JPanel content, String title) {
234        showPopup(parent, content, title, false);
235    }
236
237    /**
238     * Closes the popup with the given content
239     * @param panel
240     *   @throws Exception If the panel is not known
241     */
242    public static void close(JPanel panel) throws Exception {
243        if (contents.containsKey(panel)) {
244            contents.get(panel).dispose();
245            contents.remove(panel);
246        } else {
247            throw new Exception("Can not use a panel which has never been shown before.");
248        }
249    }
250
251    /**
252     * Pack the popup with the given content
253     * @param panel
254     *  @throws Exception If the panel is not known
255     */
256    public static void pack(JPanel panel) throws Exception {
257        if (contents.containsKey(panel)) {
258            contents.get(panel).pack();
259        } else {
260            throw new Exception("Can not use a panel which has never been shown before.");
261        }
262    }
263
264    /**
265     * Sets the popup with the given content on top
266     * @param panel
267     *  @throws Exception If the panel is not known
268     */
269    public static void setOnTop(JPanel panel) throws Exception {
270
271        if (contents.containsKey(panel)) {
272            show(panel);
273            contents.get(panel).setAlwaysOnTop(true);
274            contents.get(panel).validate();
275        } else {
276            throw new Exception("Can not use a panel which has never been shown before.");
277        }
278    }
279
280    /**
281     * Hides the popup with the given content
282     * @param panel
283     *  @throws Exception If the panel is not known
284     */
285    public static void hide(JPanel panel) throws Exception {
286        if (contents.containsKey(panel)) {
287            contents.get(panel).setVisible(false);
288        } else {
289            throw new Exception("Can not use a panel which has never been shown before.");
290        }
291    }
292
293    /**
294     * Shows the popup with the given content
295     * @param panel
296     * @throws Exception If the panel is not known
297     */
298    public static void show(JPanel panel) throws Exception {
299        if (contents.containsKey(panel)) {
300            contents.get(panel).setVisible(true);
301            contents.get(panel).validate();
302        } else {
303            throw new Exception("Can not use a panel which has never been shown before.");
304        }
305    }
306
307    /**
308     * Creates the popup with the given content, without showing it
309     * @param content
310     * @param title 
311     */
312    public static void create(JPanel content, String title) {
313        final JDialog window = new JDialog(YabsViewProxy.instance().getIdentifierFrame(), Messages.YABS.getValue());
314        window.getContentPane().setLayout(new BorderLayout());
315        window.getContentPane().add(content, BorderLayout.CENTER);
316        window.pack();
317
318        window.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
319        window.setTitle(title);
320        window.addKeyListener(new KeyListener() {
321
322            @Override
323            public void keyTyped(KeyEvent e) {
324                if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
325                    window.dispose();
326                }
327            }
328
329            @Override
330            public void keyPressed(KeyEvent e) {
331                if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
332                    window.dispose();
333                }
334            }
335
336            @Override
337            public void keyReleased(KeyEvent e) {
338            }
339        });
340        Position position = new Position(window);
341        contents.put(content, window);
342    }
343
344    public static void setLocation(JPanel panel, Point location) {
345        if (contents.containsKey(panel)) {
346            contents.get(panel).setLocation(location);
347        }
348    }
349    
350    public static void setLocationBottomLeft(JPanel panel) {
351       if (contents.containsKey(panel)) {
352           Position position = new Position(contents.get(panel));
353           position.bottomLeft();
354        } 
355    }
356
357    public static void setLocationBottomRight(JPanel panel) {
358        if (contents.containsKey(panel)) {
359           Position position = new Position(contents.get(panel));
360           position.bottomRight();
361        } 
362    }
363}