PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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