PageRenderTime 21ms CodeModel.GetById 10ms app.highlight 8ms RepoModel.GetById 1ms 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
  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 */
 17package mpv5.ui.dialogs;
 18
 19import java.awt.Component;
 20import java.awt.Dimension;
 21import java.awt.Frame;
 22import java.awt.GraphicsEnvironment;
 23import java.util.ArrayList;
 24import java.util.List;
 25import java.util.Vector;
 26import javax.swing.Icon;
 27import javax.swing.JOptionPane;
 28import javax.swing.JScrollPane;
 29import javax.swing.JTextArea;
 30import javax.swing.border.EmptyBorder;
 31import mpv5.db.common.Context;
 32import mpv5.db.common.DatabaseObject;
 33import mpv5.globals.Messages;
 34
 35/**
 36 *
 37 *  
 38 */
 39public class Popup {
 40
 41    public static String WARN = Messages.WARNING.getValue();
 42    public static String ERROR = Messages.ERROR_OCCURED.getValue();
 43    public static String NOTICE = Messages.NOTICE.getValue();
 44    public static String GENERAL_ERROR = Messages.ERROR_OCCURED.getValue();
 45    /**
 46     * This identifier is the parent container opf popups, to keep them all in one screen
 47     */
 48    public static Component identifier = new Frame(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration());
 49
 50    /**
 51     * Prompts the user with a text box
 52     * @param message
 53     * @return
 54     */
 55    public static String Enter_Value(Object... message) {
 56        String text = "";
 57        for (int i = 0; i < message.length; i++) {
 58            if (message[i] != null) {
 59                text += message[i].toString();
 60            }
 61        }
 62        return JOptionPane.showInputDialog(identifier, String.valueOf(text));
 63    }
 64
 65    /**
 66     * A Y_N_dialog
 67     * @param text
 68     * @return
 69     */
 70    public static boolean Y_N_dialog(Object text) {
 71        return Y_N_dialog(text, Messages.ARE_YOU_SURE.toString());
 72    }
 73
 74    /**
 75     * A Y_N_dialog
 76     * @param text
 77     * @param label 
 78     * @return
 79     */
 80    public static boolean Y_N_dialog(Object text, Object label) {
 81        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) {
 82            return true;
 83        } else {
 84            return false;
 85        }
 86    }
 87
 88    /**
 89     * A Y_N_dialog
 90     * @param parent
 91     * @param text
 92     * @param label
 93     * @return
 94     */
 95    public static boolean Y_N_dialog(Component parent, Object text, Object label) {
 96        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) {
 97            return true;
 98        } else {
 99            return false;
100        }
101    }
102
103    /**
104     * A Ok or Cancel -dialog
105     * @param text
106     * @param label
107     * @return
108     */
109    public static boolean OK_dialog(Object text, String label) {
110        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) {
111            return true;
112        } else {
113            return false;
114        }
115    }
116
117    /**
118     * Shows a notice
119     * @param parent
120     * @param text
121     */
122    public static void notice(Component parent, Object text) {
123        JOptionPane.showMessageDialog(parent, prepareText(String.valueOf(text)), Popup.NOTICE, JOptionPane.INFORMATION_MESSAGE);
124    }
125
126    /**
127     * Shows a warning
128     * @param parent
129     * @param text
130     */
131    public static void warn(Component parent, Object text) {
132        JOptionPane.showMessageDialog(parent, prepareText(String.valueOf(text)), Popup.WARN, JOptionPane.WARNING_MESSAGE);
133    }
134
135    /**
136     * Shows an error
137     * @param parent
138     * @param text
139     */
140    public static void error(Component parent, Object text) {
141        JOptionPane.showMessageDialog(parent, prepareText(String.valueOf(text)), Popup.ERROR, JOptionPane.ERROR_MESSAGE);
142    }
143
144    /**
145     * A convenience method to show a notice popup with
146     * the mainframe as parent
147     * @param text
148     */
149    public static void warn(Object text) {
150        JOptionPane.showMessageDialog(identifier, prepareText(String.valueOf(text)), Popup.WARN, JOptionPane.INFORMATION_MESSAGE);
151    }
152
153    /**
154     * A convenience method to show a notice popup with
155     * the mainframe as parent
156     * @param text
157     */
158    public static void notice(Object text) {
159        JOptionPane.showMessageDialog(identifier, prepareText(String.valueOf(text)), Popup.NOTICE, JOptionPane.INFORMATION_MESSAGE);
160    }
161
162    /**
163     * A convenience method to show a notice popup with
164     * the mainframe as parent
165     * @param text
166     * @param boxWidth
167     * @param boxLength
168     */
169    public static void notice(Object text, int boxWidth, int boxLength) {
170        JOptionPane.showMessageDialog(identifier, prepareText(String.valueOf(text), boxWidth, boxLength), Popup.NOTICE, JOptionPane.INFORMATION_MESSAGE);
171    }
172
173    /**
174     * A convenience method to show an error popup with
175     * the mainframe as parent
176     * @param x
177     */
178    public static void error(Exception x) {
179//        x.printStackTrace();
180        error(identifier, x.getLocalizedMessage());
181    }
182
183    private static Object prepareText(String s) {
184        return prepareText(s, 300, 80);
185    }
186
187    private static Object prepareText(String s, int boxWidth, int boxLength) {
188        JTextArea text = new JTextArea(s);
189        JScrollPane scroll = new JScrollPane(text);
190        text.setLineWrap(true);
191        text.setWrapStyleWord(true);
192        text.setEditable(false);
193        text.setBorder(new EmptyBorder(1, 1, 1, 1));
194        text.setBackground(JOptionPane.getRootFrame().getBackground());
195        scroll.setBackground(JOptionPane.getRootFrame().getBackground());
196        scroll.setBorder(new EmptyBorder(0, 0, 0, 0));
197        scroll.setPreferredSize(new Dimension(boxWidth, boxLength));
198        return scroll;
199    }
200
201    /**
202     * Show a selection box
203     * @param objects
204     * @param message
205     * @return the selected object or null
206     */
207    public static DatabaseObject SelectValue(ArrayList<DatabaseObject> objects, Object message) {
208        return (DatabaseObject) JOptionPane.showInputDialog(identifier, message, "",
209                JOptionPane.PLAIN_MESSAGE, (Icon) null, objects.toArray(), objects.get(0));
210    }
211
212    /**
213     * Show a search box
214     * @param t
215     * @return the selected object or null
216     */
217    public static DatabaseObject SelectValue(Context t) {
218        return Search2.showSearchFor(t);
219//        return null;
220    }
221
222    /**
223     * 
224     * @param list
225     * @param message
226     */
227    public static void notice(List list, Object message) {
228        String t = message.toString() + "\n";
229
230        for (int i = 0; i < list.size(); i++) {
231            Object l = list.get(i);
232            if (l instanceof DatabaseObject) {
233                DatabaseObject databaseObject = (DatabaseObject) l;
234                t += databaseObject.__getCname() + " [" + databaseObject.getDbIdentity() + "]" + "\n";
235            } else {
236                t += list.get(i) + "\n";
237            }
238        }
239        Popup.notice(t, 300, list.size() * 5 + 100);
240    }
241
242    private Popup() {
243    }
244}