1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package davmail.ui;
20
21 import davmail.BundleMessage;
22 import davmail.ui.browser.DesktopBrowser;
23 import davmail.ui.tray.DavGatewayTray;
24
25 import javax.swing.*;
26 import java.awt.*;
27 import java.awt.event.ActionEvent;
28
29
30
31
32 public class NotificationDialog extends JDialog {
33
34 protected boolean sendNotification;
35 protected boolean hasRecipients;
36
37 protected JTextField toField;
38 protected JTextField ccField;
39 protected JTextField subjectField;
40 protected JEditorPane bodyField;
41
42 protected void addRecipientComponent(JPanel panel, String label, JTextField textField, String toolTipText) {
43 JLabel fieldLabel = new JLabel(label);
44 fieldLabel.setHorizontalAlignment(SwingConstants.RIGHT);
45 fieldLabel.setVerticalAlignment(SwingConstants.CENTER);
46 JPanel innerPanel = new JPanel();
47 innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.X_AXIS));
48 innerPanel.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
49 innerPanel.setAlignmentX(Component.RIGHT_ALIGNMENT);
50 innerPanel.add(fieldLabel);
51 innerPanel.add(textField);
52 panel.add(innerPanel);
53 if (toolTipText != null) {
54 fieldLabel.setToolTipText(toolTipText);
55 textField.setToolTipText(toolTipText);
56 }
57 }
58
59
60
61
62
63
64
65
66 public NotificationDialog(String subject, String description) {
67 this(null, null, subject, description);
68 }
69
70
71
72
73
74
75
76
77
78 public NotificationDialog(String to, String cc, String subject, String description) {
79 hasRecipients = to != null || cc != null;
80 setModal(true);
81 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
82 setTitle(BundleMessage.format("UI_CALDAV_NOTIFICATION"));
83 try {
84 setIconImages(DavGatewayTray.getFrameIcons());
85 } catch (NoSuchMethodError error) {
86 DavGatewayTray.debug(new BundleMessage("LOG_UNABLE_TO_SET_ICON_IMAGE"));
87 }
88
89 JPanel mainPanel = new JPanel();
90
91 mainPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("F1"),
92 "help");
93 mainPanel.getActionMap().put("help", new AbstractAction() {
94 public void actionPerformed(ActionEvent e) {
95 DesktopBrowser.browse("https://davmail.sourceforge.net");
96 }
97 });
98
99 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
100 mainPanel.add(getRecipientsPanel());
101 mainPanel.add(getBodyPanel(description));
102
103 JPanel recipientsPanel = getRecipientsPanel();
104 if (to != null) {
105 toField.setText(to);
106 }
107 if (cc != null) {
108 ccField.setText(cc);
109 }
110 if (subject != null) {
111 subjectField.setText(subject);
112 }
113 add(BorderLayout.NORTH, recipientsPanel);
114 JPanel bodyPanel = getBodyPanel(description);
115 add(BorderLayout.CENTER, bodyPanel);
116 bodyField.setPreferredSize(recipientsPanel.getPreferredSize());
117
118 JPanel buttonPanel = new JPanel();
119 JButton cancel = new JButton(BundleMessage.format("UI_BUTTON_CANCEL"));
120 JButton send = new JButton(BundleMessage.format("UI_BUTTON_SEND"));
121
122 send.addActionListener(evt -> {
123 sendNotification = true;
124 setVisible(false);
125 });
126
127 cancel.addActionListener(evt -> {
128
129 setVisible(false);
130 });
131
132 buttonPanel.add(send);
133 buttonPanel.add(cancel);
134
135 add(BorderLayout.SOUTH, buttonPanel);
136
137 pack();
138 setResizable(true);
139
140 setLocation(getToolkit().getScreenSize().width / 2 -
141 getSize().width / 2,
142 getToolkit().getScreenSize().height / 2 -
143 getSize().height / 2);
144 setAlwaysOnTop(true);
145 setVisible(true);
146 }
147
148 protected JPanel getRecipientsPanel() {
149 JPanel recipientsPanel = new JPanel();
150 recipientsPanel.setLayout(new BoxLayout(recipientsPanel, BoxLayout.Y_AXIS));
151 recipientsPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
152
153 if (hasRecipients) {
154 toField = new JTextField("", 40);
155 addRecipientComponent(recipientsPanel, BundleMessage.format("UI_TO"), toField,
156 BundleMessage.format("UI_TO_HELP"));
157 ccField = new JTextField("", 40);
158 addRecipientComponent(recipientsPanel, BundleMessage.format("UI_CC"), ccField,
159 BundleMessage.format("UI_CC_HELP"));
160 }
161 subjectField = new JTextField("", 40);
162 if (!hasRecipients) {
163 subjectField.setEditable(false);
164 }
165 addRecipientComponent(recipientsPanel, BundleMessage.format("UI_SUBJECT"), subjectField,
166 BundleMessage.format("UI_SUBJECT_HELP"));
167 return recipientsPanel;
168 }
169
170 protected JPanel getBodyPanel(String description) {
171 JPanel bodyPanel = new JPanel();
172 bodyPanel.setLayout(new BoxLayout(bodyPanel, BoxLayout.Y_AXIS));
173 bodyPanel.setBorder(BorderFactory.createTitledBorder(BundleMessage.format("UI_NOTIFICATION_BODY")));
174
175 bodyField = new JTextPane();
176 bodyField.setText(description);
177
178
179
180
181 bodyPanel.add(new JScrollPane(bodyField));
182 return bodyPanel;
183 }
184
185
186
187
188
189
190 public boolean getSendNotification() {
191 return sendNotification;
192 }
193
194
195
196
197
198
199 public String getTo() {
200 return toField.getText();
201 }
202
203
204
205
206
207
208 public String getCc() {
209 return ccField.getText();
210 }
211
212
213
214
215
216
217 public String getSubject() {
218 return subjectField.getText();
219 }
220
221
222
223
224
225
226 public String getBody() {
227 return bodyField.getText();
228 }
229 }