KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > SendFailedDialog


1 package net.suberic.pooka.gui;
2
3 import javax.swing.*;
4 import javax.swing.event.ChangeEvent JavaDoc;
5 import javax.swing.event.ChangeListener JavaDoc;
6 import java.awt.*;
7 import java.util.*;
8
9 import javax.mail.MessagingException JavaDoc;
10 import net.suberic.pooka.*;
11
12 /**
13  * A dialog that lets you choose whehter to cancel the sending or try
14  * another mailserver.
15  */

16 public class SendFailedDialog extends JPanel {
17
18   // the resource for this component.
19
static String JavaDoc P_RESOURCE = "SendFailedDialog";
20
21   // the action commands
22
static String JavaDoc S_ABORT = "abort";
23   static String JavaDoc S_SEND_OTHER_SERVER = "send";
24   static String JavaDoc S_SAVE_TO_OUTBOX = "outbox";
25
26   // the mailserver action commands
27
public static String JavaDoc S_NOTHING = "nothing";
28   public static String JavaDoc S_SESSION_DEFAULT = "session";
29   public static String JavaDoc S_CHANGE_DEFAULT = "change_default";
30
31   // the MessagingException
32
MessagingException JavaDoc mException;
33
34   // the original mailserver
35
OutgoingMailServer mOriginalMailServer;
36
37   // the display panel.
38
JTextArea mMessageDisplay;
39
40   // a JComboBox that shows all available mailservers.
41
JComboBox mMailServerList = null;
42
43   // a JRadioButton that shows the choices of what to do with the failed
44
// send.
45
ButtonGroup mActionButtons = null;
46
47   // a JRadioButton that shows the choices of what to do with the newly
48
// chosen mailserver.
49
ButtonGroup mServerDefaultButtons = null;
50
51   boolean sendButtonSelected = false;
52
53   /**
54    * Creates a new SendFailedDialog.
55    */

56   public SendFailedDialog(OutgoingMailServer pServer, MessagingException JavaDoc me) {
57     mException = me;
58     mOriginalMailServer = pServer;
59   }
60   
61   /**
62    * Configures this component.
63    */

64   public void configureComponent() {
65     this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
66
67     JPanel messagePanel = new JPanel();
68     mMessageDisplay = new JTextArea(Pooka.getProperty("error.MessageUI.sendFailed", "Failed to send Message.") + "\n" + mException.getMessage());
69     JLabel testLabel = new JLabel();
70     
71     mMessageDisplay.setBackground(testLabel.getBackground());
72     mMessageDisplay.setFont(testLabel.getFont());
73     mMessageDisplay.setForeground(testLabel.getForeground());
74     mMessageDisplay.setEditable(false);
75
76     messagePanel.add(mMessageDisplay);
77
78     JPanel buttonPanel = createActionPanel();
79
80     JPanel actionPanel = createServerDefaultPanel();
81
82     mMailServerList = createMailServerList();
83
84     JPanel choicePanel = new JPanel();
85     choicePanel.setBorder(BorderFactory.createEtchedBorder());
86     choicePanel.setLayout(new BoxLayout(choicePanel, BoxLayout.X_AXIS));
87
88     choicePanel.add(buttonPanel);
89
90     Box mailServerListBox = new Box(BoxLayout.Y_AXIS);
91     mailServerListBox.add(Box.createVerticalGlue());
92     mailServerListBox.add(mMailServerList);
93     mailServerListBox.add(Box.createVerticalGlue());
94     choicePanel.add(mailServerListBox);
95     choicePanel.add(actionPanel);
96
97     this.add(messagePanel);
98     this.add(choicePanel);
99     this.add(new net.suberic.util.swing.ExceptionDisplayPanel(Pooka.getProperty("error.showStackTrace", "Stack Trace"), mException));
100
101     reactivatePanels();
102   }
103
104   /**
105    * Creates a JRadioButton to show the available actions.
106    */

107   protected JPanel createActionPanel() {
108     JPanel returnValue = new JPanel();
109     returnValue.setBorder(BorderFactory.createEtchedBorder());
110     returnValue.setLayout(new BoxLayout(returnValue, BoxLayout.Y_AXIS));
111
112     ButtonGroup choices = new ButtonGroup();
113
114     JRadioButton abortButton = new JRadioButton();
115     abortButton.setText(Pooka.getProperty(P_RESOURCE + ".cancel", "Cancel send"));
116     abortButton.setActionCommand(S_ABORT);
117     abortButton.setSelected(true);
118     choices.add(abortButton);
119     returnValue.add(abortButton);
120
121     final JRadioButton sendButton = new JRadioButton();
122     sendButton.setText(Pooka.getProperty(P_RESOURCE + ".send", "Send using another server"));
123     sendButton.setActionCommand(S_SEND_OTHER_SERVER);
124     choices.add(sendButton);
125     returnValue.add(sendButton);
126
127     JRadioButton outboxButton = new JRadioButton();
128     outboxButton.setText(Pooka.getProperty(P_RESOURCE + ".outbox", "Save to outbox"));
129     outboxButton.setActionCommand(S_SAVE_TO_OUTBOX);
130     choices.add(outboxButton);
131     returnValue.add(outboxButton);
132
133     mActionButtons = choices;
134
135     sendButton.addChangeListener(new ChangeListener JavaDoc() {
136     public void stateChanged(ChangeEvent JavaDoc ce) {
137       synchronized(sendButton) {
138         boolean selected = sendButton.isSelected();
139         if (selected != sendButtonSelected) {
140           sendButtonSelected = selected;
141           reactivatePanels();
142         }
143       }
144     }
145       });
146     
147     return returnValue;
148   }
149
150   /**
151    * Creates a JComboBox to show the choices of mailservers.
152    */

153   public JComboBox createMailServerList() {
154     Vector v = Pooka.getOutgoingMailManager().getOutgoingMailServerList();
155     Iterator it = v.iterator();
156     Vector idList = new Vector();
157     while (it.hasNext()) {
158       OutgoingMailServer current = (OutgoingMailServer) it.next();
159       idList.add(current.getItemID());
160     }
161     JComboBox returnValue = new JComboBox(idList);
162     if (mOriginalMailServer != null)
163       returnValue.setSelectedItem(mOriginalMailServer.getItemID());
164     
165     return returnValue;
166   }
167
168   /**
169    * Creates a JRadioButton to show the choices of what to do with the
170    * newly selected mailserver.
171    */

172   public JPanel createServerDefaultPanel() {
173
174     JPanel returnValue = new JPanel();
175     returnValue.setBorder(BorderFactory.createEtchedBorder());
176     returnValue.setLayout(new BoxLayout(returnValue, BoxLayout.Y_AXIS));
177
178     ButtonGroup choices = new ButtonGroup();
179
180     JRadioButton current = new JRadioButton();
181     current.setText(Pooka.getProperty(P_RESOURCE + ".noDefault", "Keep default"));
182     current.setActionCommand(S_NOTHING);
183     current.setSelected(true);
184     choices.add(current);
185     returnValue.add(current);
186
187     current = new JRadioButton();
188     current.setText(Pooka.getProperty(P_RESOURCE + ".defaultThisSession", "Set as default for this session"));
189     current.setActionCommand(S_SESSION_DEFAULT);
190     choices.add(current);
191     returnValue.add(current);
192  
193     current = new JRadioButton();
194     current.setText(Pooka.getProperty(P_RESOURCE + ".defaultPerm", "Set as default"));
195     current.setActionCommand(S_CHANGE_DEFAULT);
196     choices.add(current);
197     returnValue.add(current);
198
199     mServerDefaultButtons = choices;
200     return returnValue;
201
202   }
203
204   /**
205    * Sets the appropriate panels as selected.
206    */

207   public void reactivatePanels() {
208     String JavaDoc actionCommand = mActionButtons.getSelection().getActionCommand();
209
210     if (actionCommand == S_SEND_OTHER_SERVER) {
211       mMailServerList.setEnabled(true);
212       Enumeration elements = mServerDefaultButtons.getElements();
213       while (elements.hasMoreElements()) {
214     AbstractButton ab = (AbstractButton) elements.nextElement();
215     ab.setEnabled(true);
216       }
217     } else {
218       mMailServerList.setEnabled(false);
219       Enumeration elements = mServerDefaultButtons.getElements();
220       while (elements.hasMoreElements()) {
221     AbstractButton ab = (AbstractButton) elements.nextElement();
222     ab.setEnabled(false);
223       }
224     }
225   }
226
227   /**
228    * Whether or not to try a resend, or just fail.
229    */

230   public boolean resendMessage() {
231     if (mActionButtons.getSelection().getActionCommand() == S_SEND_OTHER_SERVER) {
232       return true;
233     } else {
234       return false;
235     }
236   }
237
238   /**
239    * Wheter or not to save this message to the outbox for use later.
240    */

241   public boolean getSaveToOutbox() {
242     
243     if (mActionButtons.getSelection().getActionCommand() == S_SAVE_TO_OUTBOX) {
244       return true;
245     } else {
246       return false;
247     }
248
249   }
250
251   /**
252    * The MailServer selected.
253    */

254   public OutgoingMailServer getMailServer() {
255     String JavaDoc selectedValue = (String JavaDoc) mMailServerList.getSelectedItem();
256     OutgoingMailServer returnValue = Pooka.getOutgoingMailManager().getOutgoingMailServer(selectedValue);
257     return returnValue;
258   }
259
260   /**
261    * What to do with the selected MailServer.
262    */

263   public String JavaDoc getMailServerAction() {
264     String JavaDoc selectedValue = mServerDefaultButtons.getSelection().getActionCommand();
265     return selectedValue;
266   }
267 }
268
269
Popular Tags