KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > xjlib > appkit > utils > XJAlert


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32 package org.antlr.xjlib.appkit.utils;
33
34 import org.antlr.xjlib.appkit.app.XJApplication;
35 import org.antlr.xjlib.foundation.XJSystem;
36
37 import javax.swing.*;
38 import java.awt.*;
39
40 public class XJAlert {
41
42     public static final int YES = JOptionPane.YES_OPTION;
43     public static final int NO = JOptionPane.NO_OPTION;
44     public static final int CANCEL = JOptionPane.CANCEL_OPTION;
45
46     public static void display(Component parent, String JavaDoc title, String JavaDoc message) {
47         JOptionPane.showMessageDialog(parent==null?XJApplication.getActiveContainer():parent,
48                 autoAdjustMessage(message), title, JOptionPane.INFORMATION_MESSAGE, null);
49     }
50
51     public static int displayAlertYESNO(Component parent, String JavaDoc title, String JavaDoc message) {
52         return JOptionPane.showConfirmDialog(parent==null?XJApplication.getActiveContainer():parent,
53                 autoAdjustMessage(message),
54                 title,
55                 JOptionPane.YES_NO_OPTION,
56                 JOptionPane.QUESTION_MESSAGE);
57     }
58
59     public static int displayAlertYESNOCANCEL(Component parent, String JavaDoc title, String JavaDoc message) {
60         return JOptionPane.showConfirmDialog(parent==null?XJApplication.getActiveContainer():parent,
61                 autoAdjustMessage(message),
62                 title,
63                 JOptionPane.YES_NO_CANCEL_OPTION,
64                 JOptionPane.QUESTION_MESSAGE);
65     }
66
67     public static int displayAlert(Component parent, String JavaDoc title, String JavaDoc message, String JavaDoc b1, String JavaDoc b2, int def) {
68         return displayCustomAlert(parent, title, message, new String JavaDoc[] { b1, b2}, def);
69     }
70
71     public static int displayAlert(Component parent, String JavaDoc title, String JavaDoc message, String JavaDoc b1, String JavaDoc b2, String JavaDoc b3, int def) {
72         return displayCustomAlert(parent, title, message, new String JavaDoc[] { b1, b2, b3 }, def);
73     }
74
75     public static int displayCustomAlert(Component parent, String JavaDoc title, String JavaDoc message, String JavaDoc[] buttons, int def) {
76         if(XJSystem.isMacOS()) {
77             String JavaDoc [] reverse = new String JavaDoc[buttons.length];
78             for(int i=0; i<buttons.length; i++) {
79                 reverse[i] = buttons[buttons.length-i-1];
80             }
81             buttons = reverse;
82             def = buttons.length-def-1;
83         }
84         int result = JOptionPane.showOptionDialog(parent==null?XJApplication.getActiveContainer():parent,
85                 autoAdjustMessage(message), title, 0, JOptionPane.INFORMATION_MESSAGE, null, buttons, buttons[def]);
86         if(XJSystem.isMacOS()) {
87             return buttons.length-result-1;
88         } else
89             return result;
90     }
91
92     public static final int AUTO_ADJUST_LENGTH = 100;
93
94     public static String JavaDoc autoAdjustMessage(String JavaDoc message) {
95         if(message.length() <= AUTO_ADJUST_LENGTH)
96             return message;
97
98         /** Insert a new line every AUTO_ADJUST_LENGTH characters using counter k.
99          * If a new-line is encountered, the counter k set back to 0.
100          */

101
102         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(message);
103         int i = 0;
104         int k = 0;
105         while(i < sb.length()) {
106             // New line is encountered, reset the counter
107
if(sb.charAt(i) == '\n')
108                 k = 0;
109
110             // If the counter reach the threshold, insert a newline
111
// using the auto-adjust method to avoid breaking a word
112
// in the middle.
113
if(k == AUTO_ADJUST_LENGTH) {
114                 autoAdjustMessageAround(sb, i);
115                 // Increment the absolute index because a newline has been added
116
// in the autoAdjustMessageArround() method.
117
i++;
118
119                 // Reset the counter
120
k = 0;
121             }
122
123             // Increment both the absolute index i and the counter k
124
i++;
125             k++;
126         }
127         return sb.toString();
128     }
129
130     public static void autoAdjustMessageAround(StringBuffer JavaDoc sb, int k) {
131         // Make sure to insert a newline outside of a word. We can break
132
// on a white space, a comma, etc...
133
for(int i = k; i > k-AUTO_ADJUST_LENGTH + 1; i--) {
134             char c = sb.charAt(i);
135             switch(c) {
136                 case ' ':
137                 case ',':
138                 case '.':
139                 case ';':
140                 case ':':
141                 case '/':
142                 case '!':
143                 case '?':
144                     sb.insert(i+1, "\n");
145                     return;
146             }
147         }
148         sb.insert(k, "\n");
149     }
150 }
151
Popular Tags