1 package com.melloware.jukes.util;
2
3 import java.awt.Component;
4 import java.awt.Container;
5 import java.awt.Window;
6
7 import javax.swing.Icon;
8 import javax.swing.JComponent;
9 import javax.swing.JDialog;
10 import javax.swing.JOptionPane;
11 import javax.swing.JPanel;
12
13 import com.jgoodies.uif.application.Application;
14 import com.jgoodies.uif.util.ResourceUtils;
15 import com.melloware.jukes.gui.tool.Resources;
16
17
18
19
20
21
22
23
24 public final class MessageUtil {
25
26 private static final String MESSAGE_TITLE = ResourceUtils.getString("messages.message.title");
27 private static final String WARNING_TITLE = ResourceUtils.getString("messages.warning.title");
28 private static final String ERROR_TITLE = ResourceUtils.getString("messages.error.title");
29 private static final String CONFIRMATION_TITLE = ResourceUtils.getString("messages.confirmation.title");
30 private static final String INPUT_TITLE = ResourceUtils.getString("messages.input.title");
31 private static final String CONFIRM_DELETE_MSG = ResourceUtils.getString("messages.confirmDelete");
32 private static final String CONFIRM_UPDATE_MSG = ResourceUtils.getString("messages.confirmUpdate");
33 private static final String CONFIRM_BACKUP_MSG = ResourceUtils.getString("messages.confirmBackup");
34 private static final String CANCEL_MSG = ResourceUtils.getString("messages.confirmCancelAndDiscardChanges");
35 private static final String CLOSE_MSG = ResourceUtils.getString("messages.confirmClose");
36
37
38
39
40 private MessageUtil() {
41 super();
42 }
43
44 public static boolean confirmBackup(Component parent) {
45 return promptYesNo(getParentWindow(parent), CONFIRM_BACKUP_MSG);
46 }
47
48 public static boolean confirmCancelAndDiscardChanges(Component parent) {
49 return promptYesNo(getParentWindow(parent), CANCEL_MSG);
50 }
51
52 public static boolean confirmClose(Component parent) {
53 return promptYesNo(getParentWindow(parent), CLOSE_MSG);
54 }
55
56
57
58 public static boolean confirmDelete(Component parent) {
59 return promptYesNo(getParentWindow(parent), CONFIRM_DELETE_MSG);
60 }
61
62 public static boolean confirmUpdate(Component parent) {
63 return promptYesNo(getParentWindow(parent), CONFIRM_UPDATE_MSG);
64 }
65
66
67 public static boolean promptOkCancel(Component parent, String message) {
68 return promptOkCancel(parent, message, JOptionPane.QUESTION_MESSAGE);
69 }
70
71
72 public static boolean promptOkCancel(Component parent, String message, int icon) {
73 int answer = showConfirmDialog(getParentWindow(parent), message, WARNING_TITLE, JOptionPane.OK_CANCEL_OPTION,
74 icon);
75 return (answer == JOptionPane.OK_OPTION);
76 }
77
78 public static boolean promptwarn(Component parent, String message) {
79 int answer = showConfirmDialog(getParentWindow(parent), message, WARNING_TITLE, JOptionPane.YES_NO_OPTION,
80 JOptionPane.WARNING_MESSAGE);
81 return (answer == JOptionPane.YES_OPTION);
82 }
83
84
85 public static boolean promptYesNo(Component parent, String message) {
86 int answer = showConfirmDialog(getParentWindow(parent), message, CONFIRMATION_TITLE, JOptionPane.YES_NO_OPTION,
87 JOptionPane.QUESTION_MESSAGE);
88 return (answer == JOptionPane.YES_OPTION);
89 }
90
91
92 public static int promptYesNoCancel(Component parent, String message) {
93 int answer = showConfirmDialog(getParentWindow(parent), message, INPUT_TITLE, JOptionPane.YES_NO_CANCEL_OPTION,
94 JOptionPane.QUESTION_MESSAGE);
95 return ((answer == JOptionPane.CLOSED_OPTION) ? JOptionPane.CANCEL_OPTION : answer);
96 }
97
98 public static int showConfirmDialog(Component parentComponent,
99 Object message,
100 String title,
101 int optionType,
102 int messageType) {
103 return showDialog(parentComponent, message, title, optionType, messageType, null, null, null);
104 }
105
106 public static void showError(JPanel panel) {
107 showMessageDialog(null, panel, ERROR_TITLE, JOptionPane.ERROR_MESSAGE);
108 }
109
110 public static void showError(Component parent, String message) {
111 showMessageDialog(getParentWindow(parent), message, ERROR_TITLE, JOptionPane.ERROR_MESSAGE);
112 }
113
114 public static void showError(Component parent, JPanel panel) {
115 showMessageDialog(getParentWindow(parent), panel, ERROR_TITLE, JOptionPane.ERROR_MESSAGE);
116 }
117
118 public static void showInformation(Component parent, String message) {
119 showMessageDialog(getParentWindow(parent), message, MESSAGE_TITLE, JOptionPane.INFORMATION_MESSAGE);
120 }
121
122
123
124 public static void showMessage(Component parent, String message) {
125 showMessageDialog(getParentWindow(parent), message, MESSAGE_TITLE, JOptionPane.PLAIN_MESSAGE);
126 }
127
128 public static void showMessageDialog(Component parentComponent, Object message, String title, int messageType) {
129 showDialog(parentComponent, message, title, JOptionPane.DEFAULT_OPTION, messageType, null, null, null);
130 }
131
132 public static void showSuccess(Component parent) {
133 showMessageDialog(getParentWindow(parent), Resources.getString("messages.save.success"), MESSAGE_TITLE,
134 JOptionPane.INFORMATION_MESSAGE);
135 }
136
137 public static void showTaskCompleted(Component parent) {
138 showMessageDialog(getParentWindow(parent), Resources.getString("messages.taskCompleted"), MESSAGE_TITLE,
139 JOptionPane.INFORMATION_MESSAGE);
140 }
141
142 public static void showwarn(Component parent, String message) {
143 showMessageDialog(getParentWindow(parent), message, WARNING_TITLE, JOptionPane.WARNING_MESSAGE);
144 }
145
146
147
148 private static Window getParentWindow(Component c) {
149 Window w = GuiUtil.findComponentOwnerFrame(c);
150 return ((w == null) ? Application.getDefaultParentFrame() : w);
151 }
152
153
154
155
156
157
158
159 private static void setVerifyInputWhenFocusTarget(Component c, boolean b) {
160 if (c != null) {
161 if (c instanceof JComponent) {
162 ((JComponent)c).setVerifyInputWhenFocusTarget(b);
163 }
164 if (c instanceof Container) {
165 Component[] components = ((Container)c).getComponents();
166 for (int i = 0; i < components.length; i++) {
167 setVerifyInputWhenFocusTarget(components[i], b);
168 }
169 }
170 }
171 }
172
173 private static int showDialog(Component parentComponent,
174 Object message,
175 String title,
176 int optionType,
177 int messageType,
178 Icon icon,
179 Object[] options,
180 Object initialValue) {
181 JOptionPane pane = new JOptionPane(message, messageType, optionType, icon, options, initialValue);
182
183 pane.setInitialValue(initialValue);
184 pane.setComponentOrientation(((parentComponent == null) ? JOptionPane.getRootFrame() : parentComponent).getComponentOrientation());
185
186 JDialog dialog = pane.createDialog(parentComponent, title);
187 setVerifyInputWhenFocusTarget(dialog.getRootPane(), false);
188
189 pane.selectInitialValue();
190 dialog.setVisible(true);
191 dialog.dispose();
192
193 Object selectedValue = pane.getValue();
194
195 if (selectedValue == null) {
196 return JOptionPane.CLOSED_OPTION;
197 }
198 if (options == null) {
199 if (selectedValue instanceof Integer) {
200 return ((Integer)selectedValue).intValue();
201 }
202 return JOptionPane.CLOSED_OPTION;
203 }
204 for (int counter = 0, maxCounter = options.length; counter < maxCounter; counter++) {
205 if (options[counter].equals(selectedValue)) {
206 return counter;
207 }
208 }
209 return JOptionPane.CLOSED_OPTION;
210 }
211
212 }