View Javadoc
1   /*
2    * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3    * Copyright (C) 2010  Mickael Guessant
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
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   * Edit Caldav scheduling notifications.
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       * Notification dialog to let user edit message body or cancel notification.
61       * Called from EWS => no recipients information
62       *
63       * @param subject     notification subject
64       * @param description notification description
65       */
66      public NotificationDialog(String subject, String description) {
67          this(null, null, subject, description);
68      }
69  
70      /**
71       * Notification dialog to let user edit message body or cancel notification.
72       *
73       * @param to          main recipients
74       * @param cc          copy recipients
75       * @param subject     notification subject
76       * @param description notification description
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          // add help (F1 handler)
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             // nothing to do, just hide
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         // center frame
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         //HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
178         //bodyField.setEditorKit(htmlEditorKit);
179         //bodyField.setContentType("text/html");
180 
181         bodyPanel.add(new JScrollPane(bodyField));
182         return bodyPanel;
183     }
184 
185     /**
186      * Cancel notification flag.
187      *
188      * @return false if user chose to cancel notification
189      */
190     public boolean getSendNotification() {
191         return sendNotification;
192     }
193 
194     /**
195      * Get edited recipients.
196      *
197      * @return recipients string
198      */
199     public String getTo() {
200         return toField.getText();
201     }
202 
203     /**
204      * Get edited copy recipients.
205      *
206      * @return copy recipients string
207      */
208     public String getCc() {
209         return ccField.getText();
210     }
211 
212     /**
213      * Get edited subject.
214      *
215      * @return subject
216      */
217     public String getSubject() {
218         return subjectField.getText();
219     }
220 
221     /**
222      * Get edited body.
223      *
224      * @return edited notification body
225      */
226     public String getBody() {
227         return bodyField.getText();
228     }
229 }