1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hivegui.message; |
16 | |
17 | import java.awt.Window; |
18 | |
19 | import javax.swing.JDialog; |
20 | import javax.swing.JFrame; |
21 | import javax.swing.JOptionPane; |
22 | |
23 | import org.apache.commons.logging.Log; |
24 | import org.apache.commons.logging.LogFactory; |
25 | import org.jdesktop.application.ApplicationContext; |
26 | import org.jdesktop.application.Resource; |
27 | import org.jdesktop.application.ResourceMap; |
28 | |
29 | import net.sourceforge.hivegui.component.ActiveWindowHolder; |
30 | |
31 | public class MessageFactory |
32 | { |
33 | static private final Log _logger = LogFactory.getLog(MessageFactory.class); |
34 | |
35 | public MessageFactory(ApplicationContext context) |
36 | { |
37 | _map = context.getResourceMap(getClass()); |
38 | } |
39 | |
40 | public UserChoice message(String id, Object... args) |
41 | { |
42 | try |
43 | { |
44 | _map.injectFields(this, id); |
45 | String title = String.format(_title, args); |
46 | String message = String.format(_message, args); |
47 | |
48 | JOptionPane pane = new JOptionPane( message, |
49 | _messageType.value(), |
50 | _optionType.value()); |
51 | pane.setName("message-" + id); |
52 | Window parent = ActiveWindowHolder.instance().getActiveWindow(); |
53 | JDialog box; |
54 | if (parent instanceof JFrame) |
55 | { |
56 | box = pane.createDialog(parent, title); |
57 | } |
58 | else if (parent instanceof JDialog) |
59 | { |
60 | box = pane.createDialog(parent, title); |
61 | } |
62 | else |
63 | { |
64 | box = pane.createDialog((JFrame) null, title); |
65 | } |
66 | box.setLocationRelativeTo(parent); |
67 | box.setVisible(true); |
68 | Integer result = (Integer) pane.getValue(); |
69 | if (result == null) |
70 | { |
71 | return UserChoice.CANCEL; |
72 | } |
73 | else |
74 | { |
75 | return UserChoice.get(result.intValue()); |
76 | } |
77 | } |
78 | catch (ResourceMap.InjectFieldException e) |
79 | { |
80 | _logger.error("message() unexisting id <" + id + ">", e); |
81 | return null; |
82 | } |
83 | } |
84 | |
85 | final private ResourceMap _map; |
86 | |
87 | @Resource(key = "messageType") |
88 | private MessageType _messageType; |
89 | @Resource(key = "optionType") |
90 | private OptionType _optionType; |
91 | @Resource(key = "title") |
92 | private String _title; |
93 | @Resource(key = "message") |
94 | private String _message; |
95 | } |