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
32 // Post to newsgroup objects
33 private NewsGroup
[] subscribedNewsgroups
= null;
34 private XScriptContext xscriptcontext
= null;
36 private final int FRAMEX
= 300;
37 private final int FRAMEY
= 300;
38 private final int TEXTBOXWIDTH
= 300;
39 private final int TEXTBOXHEIGHT
= 24;
40 private final int TEXTAREAHEIGHT
= 70;
41 private final int BUTTONWIDTH
= 80;
42 private final int BUTTONHEIGHT
= 30;
44 private PostNewsgroup window
= null;
45 private JComboBox newsgroupComboBox
= null;
46 private JTextField hostTextField
= null;
47 private JTextField replyTextField
= null;
48 private JTextField subjectTextField
= null;
49 private JTextArea commentTextArea
= null;
50 private JRadioButton officeHtmlButton
= null;
51 private JRadioButton officeButton
= null;
52 private JRadioButton htmlButton
= null;
53 private JButton postButton
= null;
54 private JButton cancelButton
= null;
56 // JFrame for launch progress dialog
57 private StatusWindow statusWindow
= null;
58 private String statusLine
= "";
61 private final String newsgroupText
= "Newsgroup name";
62 private final String hostText
= "Newsgroup host/server name";
63 private final String replyText
= "Email address to reply to";
64 private final String subjectText
= "Subject title for the mail";
65 private final String commentText
= "Additional comment on mail";
66 private final String officeHtmlText
= "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
)
78 // create mailcap and mimetypes files (fix for classloader problem)
79 MimeConfiguration
.createFiles( xscriptcontext
);
81 this.setTitle( "Post Document To Newsgroup" );
82 this.setLocation( FRAMEX
, FRAMEY
);
84 this.addFocusListener( new FocusAdapter()
86 public void focusGained( FocusEvent event
)
88 System
.out
.println( "Focus gained" );
89 window
.update( window
.getGraphics() );
92 public void focusLost( FocusEvent event
)
94 System
.out
.println( "Focus lost" );
98 Container container
= getContentPane();
99 container
.setLayout( new GridBagLayout() );;
100 GridBagConstraints constraints
= new GridBagConstraints();
101 constraints
.fill
= GridBagConstraints
.BOTH
;
103 JPanel labelPanel
= constructLabelPanel();
104 JPanel textPanel
= constructTextPanel();
105 JPanel optionPanel
= constructOptionPanel();
106 JPanel buttonPanel
= constructButtonPanel();
108 constraints
.gridx
= 0;
109 constraints
.gridy
= 0;
110 constraints
.gridwidth
= 1;
111 constraints
.gridheight
= 3;
112 constraints
.insets
= new Insets( 15, 15, 5, 5 );
113 container
.add( labelPanel
, constraints
);
115 constraints
.gridx
= 1;
116 constraints
.gridy
= 0;
117 constraints
.gridwidth
= 4;
118 constraints
.gridheight
= 3;
119 constraints
.insets
= new Insets( 15, 5, 5, 15 );
120 container
.add( textPanel
, constraints
);
122 constraints
.gridx
= 0;
123 constraints
.gridy
= 3;
124 constraints
.gridwidth
= 5;
125 constraints
.gridheight
= 1;
126 constraints
.insets
= new Insets( 5, 15, 5, 15 );
127 container
.add( optionPanel
, constraints
);
129 constraints
.gridx
= 0;
130 constraints
.gridy
= 4;
131 constraints
.gridwidth
= 5;
132 constraints
.gridheight
= 1;
133 constraints
.insets
= new Insets( 5, 5, 5, 5 );
134 container
.add( buttonPanel
, constraints
);
137 this.setResizable( false );
138 this.setVisible( true );
142 private JPanel
constructLabelPanel()
144 JLabel newsgroupLabel
= new JLabel( "Newsgroup:" );
145 JLabel hostLabel
= new JLabel( "Host:" );
146 JLabel replyLabel
= new JLabel( "Reply:" );
147 JLabel subjectLabel
= new JLabel( "Subject:" );
148 JLabel commentLabel
= new JLabel( "Comment:" );
150 newsgroupLabel
.setToolTipText( newsgroupText
);
151 hostLabel
.setToolTipText( hostText
);
152 replyLabel
.setToolTipText( replyText
);
153 subjectLabel
.setToolTipText( subjectText
);
154 commentLabel
.setToolTipText( commentText
);
156 JPanel newsgroupPanel
= new JPanel();
157 newsgroupPanel
.setLayout( new BorderLayout() );
158 newsgroupPanel
.add( newsgroupLabel
, "West" );
159 JPanel hostPanel
= new JPanel();
160 hostPanel
.setLayout( new BorderLayout() );
161 hostPanel
.add( hostLabel
, "West" );
162 JPanel replyPanel
= new JPanel();
163 replyPanel
.setLayout( new BorderLayout() );
164 replyPanel
.add( replyLabel
, "West" );
165 JPanel subjectPanel
= new JPanel();
166 subjectPanel
.setLayout( new BorderLayout() );
167 subjectPanel
.add( subjectLabel
, "West" );
168 JPanel commentPanel
= new JPanel();
169 commentPanel
.setLayout( new BorderLayout() );
170 commentPanel
.add( commentLabel
, "West" );
171 JPanel emptyPanel
= new JPanel();
173 final int labelWidth
= 80;
174 newsgroupPanel
.setPreferredSize( new Dimension( labelWidth
, TEXTBOXHEIGHT
) );
175 hostPanel
.setPreferredSize( new Dimension( labelWidth
, TEXTBOXHEIGHT
) );
176 replyPanel
.setPreferredSize( new Dimension( labelWidth
, TEXTBOXHEIGHT
) );
177 subjectPanel
.setPreferredSize( new Dimension( labelWidth
, TEXTBOXHEIGHT
) );
178 commentPanel
.setPreferredSize( new Dimension( labelWidth
, TEXTBOXHEIGHT
) );
180 JPanel panel
= new JPanel();
181 panel
.setLayout( new GridBagLayout() );
182 GridBagConstraints constraints
= new GridBagConstraints();
183 constraints
.fill
= GridBagConstraints
.BOTH
;
184 constraints
.insets
= new Insets( 5, 5, 5, 5 );
186 constraints
.gridx
= 0;
187 constraints
.gridy
= 0;
188 constraints
.gridwidth
= 1;
189 constraints
.gridheight
= 1;
190 constraints
.weightx
= constraints
.weighty
= 0.0;
191 panel
.add( newsgroupPanel
, constraints
);
193 constraints
.gridx
= 0;
194 constraints
.gridy
= 1;
195 constraints
.gridwidth
= 1;
196 constraints
.gridheight
= 1;
197 panel
.add( hostPanel
, constraints
);
199 constraints
.gridx
= 0;
200 constraints
.gridy
= 2;
201 constraints
.gridwidth
= 1;
202 constraints
.gridheight
= 1;
203 panel
.add( replyPanel
, constraints
);
205 constraints
.gridx
= 0;
206 constraints
.gridy
= 3;
207 constraints
.gridwidth
= 1;
208 constraints
.gridheight
= 1;
209 panel
.add( subjectPanel
, constraints
);
211 constraints
.gridx
= 0;
212 constraints
.gridy
= 4;
213 constraints
.gridwidth
= 1;
214 constraints
.gridheight
= 1;
215 panel
.add( commentPanel
, constraints
);
217 constraints
.gridx
= 0;
218 constraints
.gridy
= 5;
219 constraints
.gridwidth
= 1;
220 constraints
.gridheight
= 1;
221 constraints
.weightx
= constraints
.weighty
= 1.0;
222 panel
.add( emptyPanel
, constraints
);
228 private JPanel
constructTextPanel()
230 hostTextField
= new JTextField();
231 hostTextField
.setPreferredSize( new Dimension( TEXTBOXWIDTH
, TEXTBOXHEIGHT
) );
232 hostTextField
.setToolTipText( hostText
);
233 hostTextField
.setBorder( new EtchedBorder() );
235 //optionPanel.setBorder( new TitledBorder( new EtchedBorder(), "Document Format" ) );
236 newsgroupComboBox
= getNewsgroupCombo();
238 replyTextField
= new JTextField();
239 replyTextField
.setPreferredSize( new Dimension( TEXTBOXWIDTH
, TEXTBOXHEIGHT
) );
240 replyTextField
.setToolTipText( replyText
);
241 replyTextField
.setBorder( new EtchedBorder() );
243 subjectTextField
= new JTextField();
244 subjectTextField
.setPreferredSize( new Dimension( TEXTBOXWIDTH
, TEXTBOXHEIGHT
) );
245 subjectTextField
.setToolTipText( subjectText
);
246 subjectTextField
.setBorder( new EtchedBorder() );
248 commentTextArea
= new JTextArea();
249 commentTextArea
.setPreferredSize( new Dimension( TEXTBOXWIDTH
, TEXTAREAHEIGHT
) );
250 commentTextArea
.setToolTipText( commentText
);
251 commentTextArea
.setBorder( new EtchedBorder() );
253 JPanel panel
= new JPanel();
254 panel
.setLayout( new GridBagLayout() );
255 GridBagConstraints constraints
= new GridBagConstraints();
256 constraints
.fill
= GridBagConstraints
.BOTH
;
257 constraints
.insets
= new Insets( 5, 5, 5, 5 );
259 constraints
.gridx
= 0;
260 constraints
.gridy
= 0;
261 constraints
.gridwidth
= 1;
262 constraints
.gridheight
= 1;
263 panel
.add( newsgroupComboBox
, constraints
);
265 constraints
.gridx
= 0;
266 constraints
.gridy
= 1;
267 constraints
.gridwidth
= 1;
268 constraints
.gridheight
= 1;
269 panel
.add( hostTextField
, constraints
);
271 constraints
.gridx
= 0;
272 constraints
.gridy
= 2;
273 constraints
.gridwidth
= 1;
274 constraints
.gridheight
= 1;
275 panel
.add( replyTextField
, constraints
);
277 constraints
.gridx
= 0;
278 constraints
.gridy
= 3;
279 constraints
.gridwidth
= 1;
280 constraints
.gridheight
= 1;
281 panel
.add( subjectTextField
, constraints
);
283 constraints
.gridx
= 0;
284 constraints
.gridy
= 4;
285 constraints
.gridwidth
= 1;
286 constraints
.gridheight
= 2;
287 panel
.add( commentTextArea
, constraints
);
293 private JComboBox
getNewsgroupCombo()
295 newsgroupComboBox
= new JComboBox();
296 //newsgroupComboBox.setBorder( new EtchedBorder() );
298 newsgroupComboBox
.addActionListener(new ActionListener()
300 public void actionPerformed(ActionEvent e
)
302 // when newsgroup is selected
303 if( subscribedNewsgroups
!= null )
305 int position
= newsgroupComboBox
.getSelectedIndex();
308 hostTextField
.setText( subscribedNewsgroups
[ position
].getHostName() );
309 newsgroupComboBox
.setToolTipText( "Newsgroup name: " + subscribedNewsgroups
[ position
].getNewsgroupName() + " (Host name: " + subscribedNewsgroups
[ position
].getHostName() + ")" );
315 NewsGroup groupToSend
= null;
316 SubscribedNewsgroups newsgroups
= new SubscribedNewsgroups();
317 subscribedNewsgroups
= newsgroups
.getNewsGroups();
319 // Test for no .mozilla or no subscribed newsgroups
320 // subscribedNewsgroups = null;
322 if( subscribedNewsgroups
== null )
324 //System.out.println( "Couldn't find any subscibed newsgroups in .mozilla" );
325 JOptionPane
.showMessageDialog( window
, "No subscribed newsgroups found in mozilla/netscape profile \nPlease enter newsgroup and host name",
326 "Newsgroups Information", JOptionPane
.INFORMATION_MESSAGE
);
330 // Copy all newsgroups into a vector for comparison
331 // Alter entries (to include host name) if duplication is found
332 ArrayList vector
= new ArrayList( subscribedNewsgroups
.length
);
333 for(int i
=0; i
< subscribedNewsgroups
.length
; i
++ )
335 vector
.add( subscribedNewsgroups
[i
].getNewsgroupName() );
338 for(int i
=0; i
< subscribedNewsgroups
.length
; i
++ )
340 // check if combo box already has a newsgroup with same name
341 // then add host name to differentiate
342 for(int j
=0; j
< subscribedNewsgroups
.length
; j
++ )
344 if( j
!= i
&& subscribedNewsgroups
[j
].getNewsgroupName().equalsIgnoreCase( subscribedNewsgroups
[i
].getNewsgroupName() ) )
346 vector
.set( j
, subscribedNewsgroups
[j
].getNewsgroupName() + " (" + subscribedNewsgroups
[j
].getHostName() + ")" );
347 vector
.set( i
, subscribedNewsgroups
[i
].getNewsgroupName() + " (" + subscribedNewsgroups
[i
].getHostName() + ")" );
351 // Copy converted newsgroups from vector to combo box
352 for(int i
=0; i
< subscribedNewsgroups
.length
; i
++ )
354 newsgroupComboBox
.addItem( vector
.elementAt(i
) );
358 newsgroupComboBox
.setPreferredSize( new Dimension( TEXTBOXWIDTH
, TEXTBOXHEIGHT
) );
359 newsgroupComboBox
.setEditable( true );
361 return newsgroupComboBox
;
366 private JPanel
constructOptionPanel()
368 officeHtmlButton
= new JRadioButton( "Office and HTML", true );
369 officeHtmlButton
.setToolTipText( officeHtmlText
);
371 officeButton
= new JRadioButton( "Office" );
372 officeButton
.setToolTipText( officeText
);
374 htmlButton
= new JRadioButton( "HTML" );
375 htmlButton
.setToolTipText( htmlText
);
377 JRadioButton
[] rbuttons
= { officeHtmlButton
, officeButton
, htmlButton
};
378 ButtonGroup radioButtonGroup
= new ButtonGroup();
379 for( int i
=0; i
< rbuttons
.length
; i
++ )
381 radioButtonGroup
.add( rbuttons
[i
] );
384 JPanel optionPanel
= new JPanel();
385 //optionPanel.setLayout( new GridLayout( 1, 3, 20, 0 ) );
386 optionPanel
.setBorder( new TitledBorder( new EtchedBorder(), "Document Format" ) );
387 optionPanel
.setLayout( new GridBagLayout() );
388 GridBagConstraints constraints
= new GridBagConstraints();
389 constraints
.fill
= GridBagConstraints
.BOTH
;
391 constraints
.gridx
= 0;
392 constraints
.gridy
= 0;
393 constraints
.gridwidth
= 1;
394 constraints
.gridheight
= 1;
395 constraints
.insets
= new Insets( 5, 5, 5, 30 );
396 optionPanel
.add( officeHtmlButton
, constraints
);
398 constraints
.gridx
= 1;
399 constraints
.gridy
= 0;
400 constraints
.gridwidth
= 1;
401 constraints
.gridheight
= 1;
402 constraints
.insets
= new Insets( 5, 20, 5, 30 );
403 optionPanel
.add( officeButton
, constraints
);
405 constraints
.gridx
= 2;
406 constraints
.gridy
= 0;
407 constraints
.gridwidth
= 1;
408 constraints
.gridheight
= 1;
409 constraints
.insets
= new Insets( 5, 20, 5, 5 );
410 optionPanel
.add( htmlButton
, constraints
);
417 public boolean sendingActions()
423 // Create status window
424 StatusWindow statusWindow
= new StatusWindow( window
, "Posting to Newsgroup", FRAMEX
, FRAMEY
);
426 statusWindow
.setVisible( true );
427 //statusWindow.requestFocusInWindow();
428 statusLine
= "Ready to send...";
429 statusWindow
.setStatus( 0, statusLine
);
431 // Get the boolean values for HTML/Office document
432 // params: ( XScriptContext, StatusWindow, html document, office document )
434 boolean html
= false;
435 boolean office
= false;
436 if( officeHtmlButton
.isSelected() ) { html
= true; office
= true; }
437 if( officeButton
.isSelected() ) { office
= true; html
= false; }
438 if( htmlButton
.isSelected() ) { html
= true; office
= false; }
440 OfficeAttachment officeAttach
= new OfficeAttachment( xscriptcontext
, 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();
453 if( subscribedNewsgroups
== null || position
== -1 )
455 host
= hostTextField
.getText();
456 group
= newsgroupComboBox
.getSelectedItem().toString();
460 //int position = newsgroupComboBox.getSelectedIndex();
461 host
= subscribedNewsgroups
[ position
].getHostName();
462 group
= subscribedNewsgroups
[ position
].getNewsgroupName();
465 statusLine
= "Creating sender object";
466 statusWindow
.setStatus( 3, statusLine
);
467 Sender sender
= new Sender( statusWindow
, officeAttach
, replyto
, subject
, comment
, host
, group
);
468 if( !sender
.sendMail() )
470 //System.out.println( "Should end here (?)" );
471 statusWindow
.enableCancelButton( true );
472 officeAttach
.cleanUpOnError();
476 statusLine
= "Send is complete";
477 statusWindow
.setStatus( 14, statusLine
);
481 //System.out.println( "Non valid data" );
488 private JPanel
constructButtonPanel()
490 Action postAction
= new AbstractAction() {
491 public void actionPerformed( ActionEvent event
) {
497 Action cancelAction
= new AbstractAction() {
498 public void actionPerformed( ActionEvent event
) {
499 // cancelling actions
504 postButton
= new JButton();
505 postButton
.setAction( postAction
);
506 postButton
.setToolTipText( postText
);
507 postButton
.setText( "Post" );
508 postButton
.setPreferredSize( new Dimension( BUTTONWIDTH
+ 20, BUTTONHEIGHT
) );
510 cancelButton
= new JButton();
511 cancelButton
.setAction( cancelAction
);
512 cancelButton
.setToolTipText( cancelText
);
513 cancelButton
.setText( "Cancel" );
514 cancelButton
.setPreferredSize( new Dimension( BUTTONWIDTH
+ 20, BUTTONHEIGHT
) );
516 JSeparator sep
= new JSeparator( SwingConstants
.HORIZONTAL
);
518 JPanel buttonPanel
= new JPanel();
519 buttonPanel
.setLayout( new GridBagLayout() );
520 GridBagConstraints constraints
= new GridBagConstraints();
521 constraints
.fill
= GridBagConstraints
.BOTH
;
522 constraints
.insets
= new Insets( 5, 5, 5, 5 );
524 JPanel emptyPanel1
= new JPanel();
525 emptyPanel1
.setPreferredSize( new Dimension( BUTTONWIDTH
, BUTTONHEIGHT
) );
527 JPanel emptyPanel2
= new JPanel();
528 emptyPanel2
.setPreferredSize( new Dimension( BUTTONWIDTH
, BUTTONHEIGHT
) );
530 constraints
.gridx
= 0;
531 constraints
.gridy
= 0;
532 constraints
.gridwidth
= 4;
533 constraints
.gridheight
= 1;
534 buttonPanel
.add( sep
, constraints
);
536 constraints
.gridx
= 0;
537 constraints
.gridy
= 1;
538 constraints
.gridwidth
= 1;
539 constraints
.gridheight
= 1;
540 buttonPanel
.add( emptyPanel1
, constraints
);
542 constraints
.gridx
= 1;
543 constraints
.gridy
= 1;
544 constraints
.gridwidth
= 1;
545 constraints
.gridheight
= 1;
546 buttonPanel
.add( emptyPanel2
, constraints
);
548 constraints
.gridx
= 2;
549 constraints
.gridy
= 1;
550 constraints
.gridwidth
= 1;
551 constraints
.gridheight
= 1;
552 buttonPanel
.add( postButton
, constraints
);
554 constraints
.gridx
= 3;
555 constraints
.gridy
= 1;
556 constraints
.gridwidth
= 1;
557 constraints
.gridheight
= 1;
558 constraints
.insets
= new Insets( 5, 5, 5, 0 );
559 buttonPanel
.add( cancelButton
, constraints
);
565 public void enableButtons( boolean enable
)
569 postButton
.setEnabled( true );
570 cancelButton
.setEnabled( true );
574 postButton
.setEnabled( false );
575 cancelButton
.setEnabled( false );
580 private boolean isValidData()
582 // newsgroupComboBox must not be blank (format? dots and whitespace)
583 String newsgroupString
= "";
584 int position
= newsgroupComboBox
.getSelectedIndex();
585 if( subscribedNewsgroups
== null || position
== -1 )
587 newsgroupString
= newsgroupComboBox
.getSelectedItem().toString();
591 //int position = newsgroupComboBox.getSelectedIndex();
592 newsgroupString
= subscribedNewsgroups
[ position
].getNewsgroupName();
594 if( newsgroupString
.length() == 0 )
596 //System.out.println( "Please enter a newsgroup name" );
597 newsgroupComboBox
.requestFocus();
598 JOptionPane
.showMessageDialog( window
, "Please enter a newsgroup name", "Input Error", JOptionPane
.ERROR_MESSAGE
);
603 // hostTextField must not be blank (format?)
604 String hostString
= hostTextField
.getText();
605 if( hostString
.length() == 0 )
607 //System.out.println( "Please enter a hostname" );
608 hostTextField
.requestFocus();
609 JOptionPane
.showMessageDialog( window
, "Please enter a hostname", "Input Error", JOptionPane
.ERROR_MESSAGE
);
614 // replyTextField must have <string>@<string>.<string>
615 // (string at least 2 chars long)
616 // consider <s>.<s>@<s>.<s>.<s> format? (array of dot positons?)
617 String replyString
= replyTextField
.getText();
618 int atPos
= replyString
.indexOf( "@" );
619 int dotPos
= replyString
.lastIndexOf( "." );
620 int length
= replyString
.length();
621 //System.out.println( "length: " + length + "\n atPos: " + atPos + "\n dotPos: " + dotPos );
622 if( length
== 0 || atPos
== -1 || dotPos
== -1 || atPos
< 2 || dotPos
< atPos
|| dotPos
+ 2 == length
|| atPos
+ 2 == dotPos
|| atPos
!= replyString
.lastIndexOf( "@" ) || replyString
.indexOf(" ") != -1 )
624 //System.out.println( "Please enter a valid reply to email address" );
625 replyTextField
.requestFocus();
626 JOptionPane
.showMessageDialog( window
, "Please enter a valid reply to email address", "Input Error", JOptionPane
.ERROR_MESSAGE
);
631 // subjectTextField must not be blank?
632 String subjectString
= subjectTextField
.getText();
633 if( subjectString
.length() == 0 )
635 //System.out.println( "Please enter subject title" );
636 subjectTextField
.requestFocus();
637 JOptionPane
.showMessageDialog( window
, "Please enter subject title", "Input Error", JOptionPane
.ERROR_MESSAGE
);