2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package org
.libreoffice
.example
.java_scripts
;
22 import java
.awt
.event
.*;
24 import javax
.swing
.border
.*;
25 import java
.util
.Vector
;
26 import com
.sun
.star
.script
.framework
.runtime
.XScriptContext
;
29 public class PostNewsgroup
extends JFrame
{
31 // Post to newsgroup objects
32 private NewsGroup
[] subscribedNewsgroups
= null;
33 private XScriptContext xscriptcontext
= null;
35 private final int FRAMEX
= 300;
36 private final int FRAMEY
= 300;
37 private final int TEXTBOXWIDTH
= 300;
38 private final int TEXTBOXHEIGHT
= 24;
39 private final int TEXTAREAHEIGHT
= 70;
40 private final int BUTTONWIDTH
= 80;
41 private final int BUTTONHEIGHT
= 30;
43 private PostNewsgroup window
= null;
44 private JComboBox newsgroupComboBox
= null;
45 private JTextField hostTextField
= null;
46 private JTextField replyTextField
= null;
47 private JTextField subjectTextField
= null;
48 private JTextArea commentTextArea
= null;
49 private JRadioButton officeHtmlButton
= null;
50 private JRadioButton officeButton
= null;
51 private JRadioButton htmlButton
= null;
52 private JButton postButton
= null;
53 private JButton cancelButton
= null;
55 // JFrame for launch progress dialog
56 private StatusWindow statusWindow
= null;
57 private String statusLine
= "";
60 private final String newsgroupText
= "Newsgroup name";
61 private final String hostText
= "Newsgroup host/server name";
62 private final String replyText
= "Email address to reply to";
63 private final String subjectText
= "Subject title for the mail";
64 private final String commentText
= "Additional comment on mail";
65 private final String officeHtmlText
=
66 "Post as both Office and HTML attachments";
67 private final String officeText
= "Post as Office attachment only";
68 private final String htmlText
= "Post as HTML attachment only";
69 private final String postText
= "Post to newsgroup";
70 private final String cancelText
= "Cancel post to newsgroup";
73 public void post(XScriptContext xsc
) {
77 // create mailcap and mimetypes files (fix for classloader problem)
78 MimeConfiguration
.createFiles(xscriptcontext
);
80 this.setTitle("Post Document To Newsgroup");
81 this.setLocation(FRAMEX
, FRAMEY
);
83 this.addFocusListener(new FocusAdapter() {
84 public void focusGained(FocusEvent event
) {
85 System
.out
.println("Focus gained");
86 window
.update(window
.getGraphics());
89 public void focusLost(FocusEvent event
) {
90 System
.out
.println("Focus lost");
94 Container container
= getContentPane();
95 container
.setLayout(new GridBagLayout());
96 GridBagConstraints constraints
= new GridBagConstraints();
97 constraints
.fill
= GridBagConstraints
.BOTH
;
99 JPanel labelPanel
= constructLabelPanel();
100 JPanel textPanel
= constructTextPanel();
101 JPanel optionPanel
= constructOptionPanel();
102 JPanel buttonPanel
= constructButtonPanel();
104 constraints
.gridx
= 0;
105 constraints
.gridy
= 0;
106 constraints
.gridwidth
= 1;
107 constraints
.gridheight
= 3;
108 constraints
.insets
= new Insets(15, 15, 5, 5);
109 container
.add(labelPanel
, constraints
);
111 constraints
.gridx
= 1;
112 constraints
.gridy
= 0;
113 constraints
.gridwidth
= 4;
114 constraints
.gridheight
= 3;
115 constraints
.insets
= new Insets(15, 5, 5, 15);
116 container
.add(textPanel
, constraints
);
118 constraints
.gridx
= 0;
119 constraints
.gridy
= 3;
120 constraints
.gridwidth
= 5;
121 constraints
.gridheight
= 1;
122 constraints
.insets
= new Insets(5, 15, 5, 15);
123 container
.add(optionPanel
, constraints
);
125 constraints
.gridx
= 0;
126 constraints
.gridy
= 4;
127 constraints
.gridwidth
= 5;
128 constraints
.gridheight
= 1;
129 constraints
.insets
= new Insets(5, 5, 5, 5);
130 container
.add(buttonPanel
, constraints
);
133 this.setResizable(false);
134 this.setVisible(true);
138 private JPanel
constructLabelPanel() {
139 JLabel newsgroupLabel
= new JLabel("Newsgroup:");
140 JLabel hostLabel
= new JLabel("Host:");
141 JLabel replyLabel
= new JLabel("Reply:");
142 JLabel subjectLabel
= new JLabel("Subject:");
143 JLabel commentLabel
= new JLabel("Comment:");
145 newsgroupLabel
.setToolTipText(newsgroupText
);
146 hostLabel
.setToolTipText(hostText
);
147 replyLabel
.setToolTipText(replyText
);
148 subjectLabel
.setToolTipText(subjectText
);
149 commentLabel
.setToolTipText(commentText
);
151 JPanel newsgroupPanel
= new JPanel();
152 newsgroupPanel
.setLayout(new BorderLayout());
153 newsgroupPanel
.add(newsgroupLabel
, "West");
154 JPanel hostPanel
= new JPanel();
155 hostPanel
.setLayout(new BorderLayout());
156 hostPanel
.add(hostLabel
, "West");
157 JPanel replyPanel
= new JPanel();
158 replyPanel
.setLayout(new BorderLayout());
159 replyPanel
.add(replyLabel
, "West");
160 JPanel subjectPanel
= new JPanel();
161 subjectPanel
.setLayout(new BorderLayout());
162 subjectPanel
.add(subjectLabel
, "West");
163 JPanel commentPanel
= new JPanel();
164 commentPanel
.setLayout(new BorderLayout());
165 commentPanel
.add(commentLabel
, "West");
166 JPanel emptyPanel
= new JPanel();
168 final int labelWidth
= 80;
169 newsgroupPanel
.setPreferredSize(new Dimension(labelWidth
, TEXTBOXHEIGHT
));
170 hostPanel
.setPreferredSize(new Dimension(labelWidth
, TEXTBOXHEIGHT
));
171 replyPanel
.setPreferredSize(new Dimension(labelWidth
, TEXTBOXHEIGHT
));
172 subjectPanel
.setPreferredSize(new Dimension(labelWidth
, TEXTBOXHEIGHT
));
173 commentPanel
.setPreferredSize(new Dimension(labelWidth
, TEXTBOXHEIGHT
));
175 JPanel panel
= new JPanel();
176 panel
.setLayout(new GridBagLayout());
177 GridBagConstraints constraints
= new GridBagConstraints();
178 constraints
.fill
= GridBagConstraints
.BOTH
;
179 constraints
.insets
= new Insets(5, 5, 5, 5);
181 constraints
.gridx
= 0;
182 constraints
.gridy
= 0;
183 constraints
.gridwidth
= 1;
184 constraints
.gridheight
= 1;
185 constraints
.weightx
= constraints
.weighty
= 0.0;
186 panel
.add(newsgroupPanel
, constraints
);
188 constraints
.gridx
= 0;
189 constraints
.gridy
= 1;
190 constraints
.gridwidth
= 1;
191 constraints
.gridheight
= 1;
192 panel
.add(hostPanel
, constraints
);
194 constraints
.gridx
= 0;
195 constraints
.gridy
= 2;
196 constraints
.gridwidth
= 1;
197 constraints
.gridheight
= 1;
198 panel
.add(replyPanel
, constraints
);
200 constraints
.gridx
= 0;
201 constraints
.gridy
= 3;
202 constraints
.gridwidth
= 1;
203 constraints
.gridheight
= 1;
204 panel
.add(subjectPanel
, constraints
);
206 constraints
.gridx
= 0;
207 constraints
.gridy
= 4;
208 constraints
.gridwidth
= 1;
209 constraints
.gridheight
= 1;
210 panel
.add(commentPanel
, constraints
);
212 constraints
.gridx
= 0;
213 constraints
.gridy
= 5;
214 constraints
.gridwidth
= 1;
215 constraints
.gridheight
= 1;
216 constraints
.weightx
= constraints
.weighty
= 1.0;
217 panel
.add(emptyPanel
, constraints
);
223 private JPanel
constructTextPanel() {
224 hostTextField
= new JTextField();
225 hostTextField
.setPreferredSize(new Dimension(TEXTBOXWIDTH
, TEXTBOXHEIGHT
));
226 hostTextField
.setToolTipText(hostText
);
227 hostTextField
.setBorder(new EtchedBorder());
229 //optionPanel.setBorder( new TitledBorder( new EtchedBorder(), "Document Format" ) );
230 newsgroupComboBox
= getNewsgroupCombo();
232 replyTextField
= new JTextField();
233 replyTextField
.setPreferredSize(new Dimension(TEXTBOXWIDTH
, TEXTBOXHEIGHT
));
234 replyTextField
.setToolTipText(replyText
);
235 replyTextField
.setBorder(new EtchedBorder());
237 subjectTextField
= new JTextField();
238 subjectTextField
.setPreferredSize(new Dimension(TEXTBOXWIDTH
, TEXTBOXHEIGHT
));
239 subjectTextField
.setToolTipText(subjectText
);
240 subjectTextField
.setBorder(new EtchedBorder());
242 commentTextArea
= new JTextArea();
243 commentTextArea
.setPreferredSize(new Dimension(TEXTBOXWIDTH
, TEXTAREAHEIGHT
));
244 commentTextArea
.setToolTipText(commentText
);
245 commentTextArea
.setBorder(new EtchedBorder());
247 JPanel panel
= new JPanel();
248 panel
.setLayout(new GridBagLayout());
249 GridBagConstraints constraints
= new GridBagConstraints();
250 constraints
.fill
= GridBagConstraints
.BOTH
;
251 constraints
.insets
= new Insets(5, 5, 5, 5);
253 constraints
.gridx
= 0;
254 constraints
.gridy
= 0;
255 constraints
.gridwidth
= 1;
256 constraints
.gridheight
= 1;
257 panel
.add(newsgroupComboBox
, constraints
);
259 constraints
.gridx
= 0;
260 constraints
.gridy
= 1;
261 constraints
.gridwidth
= 1;
262 constraints
.gridheight
= 1;
263 panel
.add(hostTextField
, constraints
);
265 constraints
.gridx
= 0;
266 constraints
.gridy
= 2;
267 constraints
.gridwidth
= 1;
268 constraints
.gridheight
= 1;
269 panel
.add(replyTextField
, constraints
);
271 constraints
.gridx
= 0;
272 constraints
.gridy
= 3;
273 constraints
.gridwidth
= 1;
274 constraints
.gridheight
= 1;
275 panel
.add(subjectTextField
, constraints
);
277 constraints
.gridx
= 0;
278 constraints
.gridy
= 4;
279 constraints
.gridwidth
= 1;
280 constraints
.gridheight
= 2;
281 panel
.add(commentTextArea
, constraints
);
287 private JComboBox
getNewsgroupCombo() {
288 newsgroupComboBox
= new JComboBox();
289 //newsgroupComboBox.setBorder( new EtchedBorder() );
291 newsgroupComboBox
.addActionListener(new ActionListener() {
292 public void actionPerformed(ActionEvent e
) {
293 // when newsgroup is selected
294 if (subscribedNewsgroups
!= null) {
295 int position
= newsgroupComboBox
.getSelectedIndex();
297 if (position
!= -1) {
298 hostTextField
.setText(subscribedNewsgroups
[ position
].getHostName());
299 newsgroupComboBox
.setToolTipText("Newsgroup name: " +
300 subscribedNewsgroups
[ position
].getNewsgroupName() + " (Host name: " +
301 subscribedNewsgroups
[ position
].getHostName() + ")");
307 NewsGroup groupToSend
= null;
308 SubscribedNewsgroups newsgroups
= new SubscribedNewsgroups();
309 subscribedNewsgroups
= newsgroups
.getNewsGroups();
311 // Test for no .mozilla or no subscribed newsgroups
312 // subscribedNewsgroups = null;
314 if (subscribedNewsgroups
== null) {
315 JOptionPane
.showMessageDialog(window
,
316 "No subscribed newsgroups found in mozilla/netscape profile \nPlease enter newsgroup and host name",
317 "Newsgroups Information", JOptionPane
.INFORMATION_MESSAGE
);
319 // Copy all newsgroups into a vector for comparison
320 // Alter entries (to include host name) if duplication is found
321 ArrayList vector
= new ArrayList(subscribedNewsgroups
.length
);
323 for (int i
= 0; i
< subscribedNewsgroups
.length
; i
++) {
324 vector
.add(subscribedNewsgroups
[i
].getNewsgroupName());
328 for (int i
= 0; i
< subscribedNewsgroups
.length
; i
++) {
329 // check if combo box already has a newsgroup with same name
330 // then add host name to differentiate
331 for (int j
= 0; j
< subscribedNewsgroups
.length
; j
++) {
333 && subscribedNewsgroups
[j
].getNewsgroupName().equalsIgnoreCase(
334 subscribedNewsgroups
[i
].getNewsgroupName())) {
335 vector
.set(j
, subscribedNewsgroups
[j
].getNewsgroupName() + " (" +
336 subscribedNewsgroups
[j
].getHostName() + ")");
337 vector
.set(i
, subscribedNewsgroups
[i
].getNewsgroupName() + " (" +
338 subscribedNewsgroups
[i
].getHostName() + ")");
343 // Copy converted newsgroups from vector to combo box
344 for (int i
= 0; i
< subscribedNewsgroups
.length
; i
++) {
345 newsgroupComboBox
.addItem(vector
.elementAt(i
));
349 newsgroupComboBox
.setPreferredSize(new Dimension(TEXTBOXWIDTH
, TEXTBOXHEIGHT
));
350 newsgroupComboBox
.setEditable(true);
352 return newsgroupComboBox
;
357 private JPanel
constructOptionPanel() {
358 officeHtmlButton
= new JRadioButton("Office and HTML", true);
359 officeHtmlButton
.setToolTipText(officeHtmlText
);
361 officeButton
= new JRadioButton("Office");
362 officeButton
.setToolTipText(officeText
);
364 htmlButton
= new JRadioButton("HTML");
365 htmlButton
.setToolTipText(htmlText
);
367 JRadioButton
[] rbuttons
= { officeHtmlButton
, officeButton
, htmlButton
};
368 ButtonGroup radioButtonGroup
= new ButtonGroup();
370 for (int i
= 0; i
< rbuttons
.length
; i
++) {
371 radioButtonGroup
.add(rbuttons
[i
]);
374 JPanel optionPanel
= new JPanel();
375 optionPanel
.setBorder(new TitledBorder(new EtchedBorder(), "Document Format"));
376 optionPanel
.setLayout(new GridBagLayout());
377 GridBagConstraints constraints
= new GridBagConstraints();
378 constraints
.fill
= GridBagConstraints
.BOTH
;
380 constraints
.gridx
= 0;
381 constraints
.gridy
= 0;
382 constraints
.gridwidth
= 1;
383 constraints
.gridheight
= 1;
384 constraints
.insets
= new Insets(5, 5, 5, 30);
385 optionPanel
.add(officeHtmlButton
, constraints
);
387 constraints
.gridx
= 1;
388 constraints
.gridy
= 0;
389 constraints
.gridwidth
= 1;
390 constraints
.gridheight
= 1;
391 constraints
.insets
= new Insets(5, 20, 5, 30);
392 optionPanel
.add(officeButton
, constraints
);
394 constraints
.gridx
= 2;
395 constraints
.gridy
= 0;
396 constraints
.gridwidth
= 1;
397 constraints
.gridheight
= 1;
398 constraints
.insets
= new Insets(5, 20, 5, 5);
399 optionPanel
.add(htmlButton
, constraints
);
406 public boolean sendingActions() {
410 // Create status window
411 StatusWindow statusWindow
= new StatusWindow(window
, "Posting to Newsgroup",
414 statusWindow
.setVisible(true);
415 statusLine
= "Ready to send...";
416 statusWindow
.setStatus(0, statusLine
);
418 // Get the boolean values for HTML/Office document
419 // params: ( XScriptContext, StatusWindow, html document, office document )
421 boolean html
= false;
422 boolean office
= false;
424 if (officeHtmlButton
.isSelected()) {
429 if (officeButton
.isSelected()) {
434 if (htmlButton
.isSelected()) {
439 OfficeAttachment officeAttach
= new OfficeAttachment(xscriptcontext
,
440 statusWindow
, html
, office
);
442 statusLine
= "Getting user input";
443 statusWindow
.setStatus(2, statusLine
);
444 // Get replyto, subject, comment from textboxes
445 String replyto
= replyTextField
.getText();
446 String subject
= subjectTextField
.getText();
447 String comment
= commentTextArea
.getText();
449 // Get newsgroup from combo box (corresponding position)
452 int position
= newsgroupComboBox
.getSelectedIndex();
454 if (subscribedNewsgroups
== null || position
== -1) {
455 host
= hostTextField
.getText();
456 group
= newsgroupComboBox
.getSelectedItem().toString();
458 host
= subscribedNewsgroups
[ position
].getHostName();
459 group
= subscribedNewsgroups
[ position
].getNewsgroupName();
462 statusLine
= "Creating sender object";
463 statusWindow
.setStatus(3, statusLine
);
464 Sender sender
= new Sender(statusWindow
, officeAttach
, replyto
, subject
,
465 comment
, host
, group
);
467 if (!sender
.sendMail()) {
468 statusWindow
.enableCancelButton(true);
469 officeAttach
.cleanUpOnError();
473 statusLine
= "Send is complete";
474 statusWindow
.setStatus(14, statusLine
);
483 private JPanel
constructButtonPanel() {
484 Action postAction
= new AbstractAction() {
485 public void actionPerformed(ActionEvent event
) {
491 Action cancelAction
= new AbstractAction() {
492 public void actionPerformed(ActionEvent event
) {
493 // cancelling actions
498 postButton
= new JButton();
499 postButton
.setAction(postAction
);
500 postButton
.setToolTipText(postText
);
501 postButton
.setText("Post");
502 postButton
.setPreferredSize(new Dimension(BUTTONWIDTH
+ 20, BUTTONHEIGHT
));
504 cancelButton
= new JButton();
505 cancelButton
.setAction(cancelAction
);
506 cancelButton
.setToolTipText(cancelText
);
507 cancelButton
.setText("Cancel");
508 cancelButton
.setPreferredSize(new Dimension(BUTTONWIDTH
+ 20, BUTTONHEIGHT
));
510 JSeparator sep
= new JSeparator(SwingConstants
.HORIZONTAL
);
512 JPanel buttonPanel
= new JPanel();
513 buttonPanel
.setLayout(new GridBagLayout());
514 GridBagConstraints constraints
= new GridBagConstraints();
515 constraints
.fill
= GridBagConstraints
.BOTH
;
516 constraints
.insets
= new Insets(5, 5, 5, 5);
518 JPanel emptyPanel1
= new JPanel();
519 emptyPanel1
.setPreferredSize(new Dimension(BUTTONWIDTH
, BUTTONHEIGHT
));
521 JPanel emptyPanel2
= new JPanel();
522 emptyPanel2
.setPreferredSize(new Dimension(BUTTONWIDTH
, BUTTONHEIGHT
));
524 constraints
.gridx
= 0;
525 constraints
.gridy
= 0;
526 constraints
.gridwidth
= 4;
527 constraints
.gridheight
= 1;
528 buttonPanel
.add(sep
, constraints
);
530 constraints
.gridx
= 0;
531 constraints
.gridy
= 1;
532 constraints
.gridwidth
= 1;
533 constraints
.gridheight
= 1;
534 buttonPanel
.add(emptyPanel1
, constraints
);
536 constraints
.gridx
= 1;
537 constraints
.gridy
= 1;
538 constraints
.gridwidth
= 1;
539 constraints
.gridheight
= 1;
540 buttonPanel
.add(emptyPanel2
, constraints
);
542 constraints
.gridx
= 2;
543 constraints
.gridy
= 1;
544 constraints
.gridwidth
= 1;
545 constraints
.gridheight
= 1;
546 buttonPanel
.add(postButton
, constraints
);
548 constraints
.gridx
= 3;
549 constraints
.gridy
= 1;
550 constraints
.gridwidth
= 1;
551 constraints
.gridheight
= 1;
552 constraints
.insets
= new Insets(5, 5, 5, 0);
553 buttonPanel
.add(cancelButton
, constraints
);
559 public void enableButtons(boolean enable
) {
561 postButton
.setEnabled(true);
562 cancelButton
.setEnabled(true);
564 postButton
.setEnabled(false);
565 cancelButton
.setEnabled(false);
570 private boolean isValidData() {
571 // newsgroupComboBox must not be blank (format? dots and whitespace)
572 String newsgroupString
= "";
573 int position
= newsgroupComboBox
.getSelectedIndex();
575 if (subscribedNewsgroups
== null || position
== -1) {
576 newsgroupString
= newsgroupComboBox
.getSelectedItem().toString();
578 newsgroupString
= subscribedNewsgroups
[ position
].getNewsgroupName();
581 if (newsgroupString
.length() == 0) {
582 newsgroupComboBox
.requestFocus();
583 JOptionPane
.showMessageDialog(window
, "Please enter a newsgroup name",
584 "Input Error", JOptionPane
.ERROR_MESSAGE
);
589 // hostTextField must not be blank (format?)
590 String hostString
= hostTextField
.getText();
592 if (hostString
.length() == 0) {
593 hostTextField
.requestFocus();
594 JOptionPane
.showMessageDialog(window
, "Please enter a hostname", "Input Error",
595 JOptionPane
.ERROR_MESSAGE
);
600 // replyTextField must have <string>@<string>.<string>
601 // (string at least 2 chars long)
602 // consider <s>.<s>@<s>.<s>.<s> format? (array of dot positions?)
603 String replyString
= replyTextField
.getText();
604 int atPos
= replyString
.indexOf("@");
605 int dotPos
= replyString
.lastIndexOf(".");
606 int length
= replyString
.length();
608 if (length
== 0 || atPos
== -1 || dotPos
== -1 || atPos
< 2 || dotPos
< atPos
609 || dotPos
+ 2 == length
|| atPos
+ 2 == dotPos
610 || atPos
!= replyString
.lastIndexOf("@") || replyString
.indexOf(" ") != -1) {
611 replyTextField
.requestFocus();
612 JOptionPane
.showMessageDialog(window
,
613 "Please enter a valid reply to email address", "Input Error",
614 JOptionPane
.ERROR_MESSAGE
);
619 // subjectTextField must not be blank?
620 String subjectString
= subjectTextField
.getText();
622 if (subjectString
.length() == 0) {
623 subjectTextField
.requestFocus();
624 JOptionPane
.showMessageDialog(window
, "Please enter subject title",
625 "Input Error", JOptionPane
.ERROR_MESSAGE
);