PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/ui/dialogs/Popup.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 244 lines | 115 code | 22 blank | 107 comment | 15 complexity | 86ef382f9111894acac1731eed959d64 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. * This file is part of YaBS.
  3. *
  4. * YaBS is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * YaBS is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with YaBS. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package mpv5.ui.dialogs;
  18. import java.awt.Component;
  19. import java.awt.Dimension;
  20. import java.awt.Frame;
  21. import java.awt.GraphicsEnvironment;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.Vector;
  25. import javax.swing.Icon;
  26. import javax.swing.JOptionPane;
  27. import javax.swing.JScrollPane;
  28. import javax.swing.JTextArea;
  29. import javax.swing.border.EmptyBorder;
  30. import mpv5.db.common.Context;
  31. import mpv5.db.common.DatabaseObject;
  32. import mpv5.globals.Messages;
  33. /**
  34. *
  35. *
  36. */
  37. public class Popup {
  38. public static String WARN = Messages.WARNING.getValue();
  39. public static String ERROR = Messages.ERROR_OCCURED.getValue();
  40. public static String NOTICE = Messages.NOTICE.getValue();
  41. public static String GENERAL_ERROR = Messages.ERROR_OCCURED.getValue();
  42. /**
  43. * This identifier is the parent container opf popups, to keep them all in one screen
  44. */
  45. public static Component identifier = new Frame(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration());
  46. /**
  47. * Prompts the user with a text box
  48. * @param message
  49. * @return
  50. */
  51. public static String Enter_Value(Object... message) {
  52. String text = "";
  53. for (int i = 0; i < message.length; i++) {
  54. if (message[i] != null) {
  55. text += message[i].toString();
  56. }
  57. }
  58. return JOptionPane.showInputDialog(identifier, String.valueOf(text));
  59. }
  60. /**
  61. * A Y_N_dialog
  62. * @param text
  63. * @return
  64. */
  65. public static boolean Y_N_dialog(Object text) {
  66. return Y_N_dialog(text, Messages.ARE_YOU_SURE.toString());
  67. }
  68. /**
  69. * A Y_N_dialog
  70. * @param text
  71. * @param label
  72. * @return
  73. */
  74. public static boolean Y_N_dialog(Object text, Object label) {
  75. if (JOptionPane.showConfirmDialog(identifier, prepareText(String.valueOf(text)), label.toString(), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, new javax.swing.ImageIcon(new Popup().getClass().getResource("/mpv5/resources/images/32/warning.png"))) == JOptionPane.YES_OPTION) {
  76. return true;
  77. } else {
  78. return false;
  79. }
  80. }
  81. /**
  82. * A Y_N_dialog
  83. * @param parent
  84. * @param text
  85. * @param label
  86. * @return
  87. */
  88. public static boolean Y_N_dialog(Component parent, Object text, Object label) {
  89. if (JOptionPane.showConfirmDialog(parent, prepareText(String.valueOf(text)), label.toString(), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, new javax.swing.ImageIcon(new Popup().getClass().getResource("/mpv5/resources/images/32/warning.png"))) == JOptionPane.YES_OPTION) {
  90. return true;
  91. } else {
  92. return false;
  93. }
  94. }
  95. /**
  96. * A Ok or Cancel -dialog
  97. * @param text
  98. * @param label
  99. * @return
  100. */
  101. public static boolean OK_dialog(Object text, String label) {
  102. if (JOptionPane.showConfirmDialog(identifier, prepareText(String.valueOf(text)), label, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, new javax.swing.ImageIcon(new Popup().getClass().getResource("/mpv5/resources/images/32/warning.png"))) == JOptionPane.OK_OPTION) {
  103. return true;
  104. } else {
  105. return false;
  106. }
  107. }
  108. /**
  109. * Shows a notice
  110. * @param parent
  111. * @param text
  112. */
  113. public static void notice(Component parent, Object text) {
  114. JOptionPane.showMessageDialog(parent, prepareText(String.valueOf(text)), Popup.NOTICE, JOptionPane.INFORMATION_MESSAGE);
  115. }
  116. /**
  117. * Shows a warning
  118. * @param parent
  119. * @param text
  120. */
  121. public static void warn(Component parent, Object text) {
  122. JOptionPane.showMessageDialog(parent, prepareText(String.valueOf(text)), Popup.WARN, JOptionPane.WARNING_MESSAGE);
  123. }
  124. /**
  125. * Shows an error
  126. * @param parent
  127. * @param text
  128. */
  129. public static void error(Component parent, Object text) {
  130. JOptionPane.showMessageDialog(parent, prepareText(String.valueOf(text)), Popup.ERROR, JOptionPane.ERROR_MESSAGE);
  131. }
  132. /**
  133. * A convenience method to show a notice popup with
  134. * the mainframe as parent
  135. * @param text
  136. */
  137. public static void warn(Object text) {
  138. JOptionPane.showMessageDialog(identifier, prepareText(String.valueOf(text)), Popup.WARN, JOptionPane.INFORMATION_MESSAGE);
  139. }
  140. /**
  141. * A convenience method to show a notice popup with
  142. * the mainframe as parent
  143. * @param text
  144. */
  145. public static void notice(Object text) {
  146. JOptionPane.showMessageDialog(identifier, prepareText(String.valueOf(text)), Popup.NOTICE, JOptionPane.INFORMATION_MESSAGE);
  147. }
  148. /**
  149. * A convenience method to show a notice popup with
  150. * the mainframe as parent
  151. * @param text
  152. * @param boxWidth
  153. * @param boxLength
  154. */
  155. public static void notice(Object text, int boxWidth, int boxLength) {
  156. JOptionPane.showMessageDialog(identifier, prepareText(String.valueOf(text), boxWidth, boxLength), Popup.NOTICE, JOptionPane.INFORMATION_MESSAGE);
  157. }
  158. /**
  159. * A convenience method to show an error popup with
  160. * the mainframe as parent
  161. * @param x
  162. */
  163. public static void error(Exception x) {
  164. // x.printStackTrace();
  165. error(identifier, x.getLocalizedMessage());
  166. }
  167. private static Object prepareText(String s) {
  168. return prepareText(s, 300, 80);
  169. }
  170. private static Object prepareText(String s, int boxWidth, int boxLength) {
  171. JTextArea text = new JTextArea(s);
  172. JScrollPane scroll = new JScrollPane(text);
  173. text.setLineWrap(true);
  174. text.setWrapStyleWord(true);
  175. text.setEditable(false);
  176. text.setBorder(new EmptyBorder(1, 1, 1, 1));
  177. text.setBackground(JOptionPane.getRootFrame().getBackground());
  178. scroll.setBackground(JOptionPane.getRootFrame().getBackground());
  179. scroll.setBorder(new EmptyBorder(0, 0, 0, 0));
  180. scroll.setPreferredSize(new Dimension(boxWidth, boxLength));
  181. return scroll;
  182. }
  183. /**
  184. * Show a selection box
  185. * @param objects
  186. * @param message
  187. * @return the selected object or null
  188. */
  189. public static DatabaseObject SelectValue(ArrayList<DatabaseObject> objects, Object message) {
  190. return (DatabaseObject) JOptionPane.showInputDialog(identifier, message, "",
  191. JOptionPane.PLAIN_MESSAGE, (Icon) null, objects.toArray(), objects.get(0));
  192. }
  193. /**
  194. * Show a search box
  195. * @param t
  196. * @return the selected object or null
  197. */
  198. public static DatabaseObject SelectValue(Context t) {
  199. return Search2.showSearchFor(t);
  200. // return null;
  201. }
  202. /**
  203. *
  204. * @param list
  205. * @param message
  206. */
  207. public static void notice(List list, Object message) {
  208. String t = message.toString() + "\n";
  209. for (int i = 0; i < list.size(); i++) {
  210. Object l = list.get(i);
  211. if (l instanceof DatabaseObject) {
  212. DatabaseObject databaseObject = (DatabaseObject) l;
  213. t += databaseObject.__getCname() + " [" + databaseObject.getDbIdentity() + "]" + "\n";
  214. } else {
  215. t += list.get(i) + "\n";
  216. }
  217. }
  218. Popup.notice(t, 300, list.size() * 5 + 100);
  219. }
  220. private Popup() {
  221. }
  222. }