2 import java
.awt
.event
.*;
4 import javax
.swing
.border
.*;
5 import java
.util
.Vector
;
6 import com
.sun
.star
.script
.framework
.runtime
.XScriptContext
;
9 public class PostNewsgroup
extends JFrame
12 // Post to newsgroup objects
13 private NewsGroup
[] subscribedNewsgroups
= null;
14 private XScriptContext xscriptcontext
= null;
16 private final int FRAMEX
= 300;
17 private final int FRAMEY
= 300;
18 private final int TEXTBOXWIDTH
= 300;
19 private final int TEXTBOXHEIGHT
= 24;
20 private final int TEXTAREAHEIGHT
= 70;
21 private final int BUTTONWIDTH
= 80;
22 private final int BUTTONHEIGHT
= 30;
24 private PostNewsgroup window
= null;
25 private JComboBox newsgroupComboBox
= null;
26 private JTextField hostTextField
= null;
27 private JTextField replyTextField
= null;
28 private JTextField subjectTextField
= null;
29 private JTextArea commentTextArea
= null;
30 private JRadioButton officeHtmlButton
= null;
31 private JRadioButton officeButton
= null;
32 private JRadioButton htmlButton
= null;
33 private JButton postButton
= null;
34 private JButton cancelButton
= null;
36 // JFrame for launch progress dialog
37 private StatusWindow statusWindow
= null;
38 private String statusLine
= "";
41 private final String newsgroupText
= "Newsgroup name";
42 private final String hostText
= "Newsgroup host/server name";
43 private final String replyText
= "Email address to reply to";
44 private final String subjectText
= "Subject title for the mail";
45 private final String commentText
= "Additional comment on mail";
46 private final String officeHtmlText
= "Post as both Office and HTML attachments";
47 private final String officeText
= "Post as Office attachment only";
48 private final String htmlText
= "Post as HTML attachment only";
49 private final String postText
= "Post to newsgroup";
50 private final String cancelText
= "Cancel post to newsgroup";
53 public void post( XScriptContext xsc
)
58 // create mailcap and mimetypes files (fix for classloader problem)
59 MimeConfiguration
.createFiles( xscriptcontext
);
61 this.setTitle( "Post Document To Newsgroup" );
62 this.setLocation( FRAMEX
, FRAMEY
);
64 this.addFocusListener( new FocusAdapter()
66 public void focusGained( FocusEvent event
)
68 System
.out
.println( "Focus gained" );
69 window
.update( window
.getGraphics() );
72 public void focusLost( FocusEvent event
)
74 System
.out
.println( "Focus lost" );
78 Container container
= getContentPane();
79 container
.setLayout( new GridBagLayout() );;
80 GridBagConstraints constraints
= new GridBagConstraints();
81 constraints
.fill
= GridBagConstraints
.BOTH
;
83 JPanel labelPanel
= constructLabelPanel();
84 JPanel textPanel
= constructTextPanel();
85 JPanel optionPanel
= constructOptionPanel();
86 JPanel buttonPanel
= constructButtonPanel();
88 constraints
.gridx
= 0;
89 constraints
.gridy
= 0;
90 constraints
.gridwidth
= 1;
91 constraints
.gridheight
= 3;
92 constraints
.insets
= new Insets( 15, 15, 5, 5 );
93 container
.add( labelPanel
, constraints
);
95 constraints
.gridx
= 1;
96 constraints
.gridy
= 0;
97 constraints
.gridwidth
= 4;
98 constraints
.gridheight
= 3;
99 constraints
.insets
= new Insets( 15, 5, 5, 15 );
100 container
.add( textPanel
, constraints
);
102 constraints
.gridx
= 0;
103 constraints
.gridy
= 3;
104 constraints
.gridwidth
= 5;
105 constraints
.gridheight
= 1;
106 constraints
.insets
= new Insets( 5, 15, 5, 15 );
107 container
.add( optionPanel
, constraints
);
109 constraints
.gridx
= 0;
110 constraints
.gridy
= 4;
111 constraints
.gridwidth
= 5;
112 constraints
.gridheight
= 1;
113 constraints
.insets
= new Insets( 5, 5, 5, 5 );
114 container
.add( buttonPanel
, constraints
);
117 this.setResizable( false );
118 this.setVisible( true );
122 private JPanel
constructLabelPanel()
124 JLabel newsgroupLabel
= new JLabel( "Newsgroup:" );
125 JLabel hostLabel
= new JLabel( "Host:" );
126 JLabel replyLabel
= new JLabel( "Reply:" );
127 JLabel subjectLabel
= new JLabel( "Subject:" );
128 JLabel commentLabel
= new JLabel( "Comment:" );
130 newsgroupLabel
.setToolTipText( newsgroupText
);
131 hostLabel
.setToolTipText( hostText
);
132 replyLabel
.setToolTipText( replyText
);
133 subjectLabel
.setToolTipText( subjectText
);
134 commentLabel
.setToolTipText( commentText
);
136 JPanel newsgroupPanel
= new JPanel();
137 newsgroupPanel
.setLayout( new BorderLayout() );
138 newsgroupPanel
.add( newsgroupLabel
, "West" );
139 JPanel hostPanel
= new JPanel();
140 hostPanel
.setLayout( new BorderLayout() );
141 hostPanel
.add( hostLabel
, "West" );
142 JPanel replyPanel
= new JPanel();
143 replyPanel
.setLayout( new BorderLayout() );
144 replyPanel
.add( replyLabel
, "West" );
145 JPanel subjectPanel
= new JPanel();
146 subjectPanel
.setLayout( new BorderLayout() );
147 subjectPanel
.add( subjectLabel
, "West" );
148 JPanel commentPanel
= new JPanel();
149 commentPanel
.setLayout( new BorderLayout() );
150 commentPanel
.add( commentLabel
, "West" );
151 JPanel emptyPanel
= new JPanel();
153 final int labelWidth
= 80;
154 newsgroupPanel
.setPreferredSize( new Dimension( labelWidth
, TEXTBOXHEIGHT
) );
155 hostPanel
.setPreferredSize( new Dimension( labelWidth
, TEXTBOXHEIGHT
) );
156 replyPanel
.setPreferredSize( new Dimension( labelWidth
, TEXTBOXHEIGHT
) );
157 subjectPanel
.setPreferredSize( new Dimension( labelWidth
, TEXTBOXHEIGHT
) );
158 commentPanel
.setPreferredSize( new Dimension( labelWidth
, TEXTBOXHEIGHT
) );
160 JPanel panel
= new JPanel();
161 panel
.setLayout( new GridBagLayout() );
162 GridBagConstraints constraints
= new GridBagConstraints();
163 constraints
.fill
= GridBagConstraints
.BOTH
;
164 constraints
.insets
= new Insets( 5, 5, 5, 5 );
166 constraints
.gridx
= 0;
167 constraints
.gridy
= 0;
168 constraints
.gridwidth
= 1;
169 constraints
.gridheight
= 1;
170 constraints
.weightx
= constraints
.weighty
= 0.0;
171 panel
.add( newsgroupPanel
, constraints
);
173 constraints
.gridx
= 0;
174 constraints
.gridy
= 1;
175 constraints
.gridwidth
= 1;
176 constraints
.gridheight
= 1;
177 panel
.add( hostPanel
, constraints
);
179 constraints
.gridx
= 0;
180 constraints
.gridy
= 2;
181 constraints
.gridwidth
= 1;
182 constraints
.gridheight
= 1;
183 panel
.add( replyPanel
, constraints
);
185 constraints
.gridx
= 0;
186 constraints
.gridy
= 3;
187 constraints
.gridwidth
= 1;
188 constraints
.gridheight
= 1;
189 panel
.add( subjectPanel
, constraints
);
191 constraints
.gridx
= 0;
192 constraints
.gridy
= 4;
193 constraints
.gridwidth
= 1;
194 constraints
.gridheight
= 1;
195 panel
.add( commentPanel
, constraints
);
197 constraints
.gridx
= 0;
198 constraints
.gridy
= 5;
199 constraints
.gridwidth
= 1;
200 constraints
.gridheight
= 1;
201 constraints
.weightx
= constraints
.weighty
= 1.0;
202 panel
.add( emptyPanel
, constraints
);
208 private JPanel
constructTextPanel()
210 hostTextField
= new JTextField();
211 hostTextField
.setPreferredSize( new Dimension( TEXTBOXWIDTH
, TEXTBOXHEIGHT
) );
212 hostTextField
.setToolTipText( hostText
);
213 hostTextField
.setBorder( new EtchedBorder() );
215 //optionPanel.setBorder( new TitledBorder( new EtchedBorder(), "Document Format" ) );
216 newsgroupComboBox
= getNewsgroupCombo();
218 replyTextField
= new JTextField();
219 replyTextField
.setPreferredSize( new Dimension( TEXTBOXWIDTH
, TEXTBOXHEIGHT
) );
220 replyTextField
.setToolTipText( replyText
);
221 replyTextField
.setBorder( new EtchedBorder() );
223 subjectTextField
= new JTextField();
224 subjectTextField
.setPreferredSize( new Dimension( TEXTBOXWIDTH
, TEXTBOXHEIGHT
) );
225 subjectTextField
.setToolTipText( subjectText
);
226 subjectTextField
.setBorder( new EtchedBorder() );
228 commentTextArea
= new JTextArea();
229 commentTextArea
.setPreferredSize( new Dimension( TEXTBOXWIDTH
, TEXTAREAHEIGHT
) );
230 commentTextArea
.setToolTipText( commentText
);
231 commentTextArea
.setBorder( new EtchedBorder() );
233 JPanel panel
= new JPanel();
234 panel
.setLayout( new GridBagLayout() );
235 GridBagConstraints constraints
= new GridBagConstraints();
236 constraints
.fill
= GridBagConstraints
.BOTH
;
237 constraints
.insets
= new Insets( 5, 5, 5, 5 );
239 constraints
.gridx
= 0;
240 constraints
.gridy
= 0;
241 constraints
.gridwidth
= 1;
242 constraints
.gridheight
= 1;
243 panel
.add( newsgroupComboBox
, constraints
);
245 constraints
.gridx
= 0;
246 constraints
.gridy
= 1;
247 constraints
.gridwidth
= 1;
248 constraints
.gridheight
= 1;
249 panel
.add( hostTextField
, constraints
);
251 constraints
.gridx
= 0;
252 constraints
.gridy
= 2;
253 constraints
.gridwidth
= 1;
254 constraints
.gridheight
= 1;
255 panel
.add( replyTextField
, constraints
);
257 constraints
.gridx
= 0;
258 constraints
.gridy
= 3;
259 constraints
.gridwidth
= 1;
260 constraints
.gridheight
= 1;
261 panel
.add( subjectTextField
, constraints
);
263 constraints
.gridx
= 0;
264 constraints
.gridy
= 4;
265 constraints
.gridwidth
= 1;
266 constraints
.gridheight
= 2;
267 panel
.add( commentTextArea
, constraints
);
273 private JComboBox
getNewsgroupCombo()
275 newsgroupComboBox
= new JComboBox();
276 //newsgroupComboBox.setBorder( new EtchedBorder() );
278 newsgroupComboBox
.addActionListener(new ActionListener()
280 public void actionPerformed(ActionEvent e
)
282 // when newsgroup is selected
283 if( subscribedNewsgroups
!= null )
285 int position
= newsgroupComboBox
.getSelectedIndex();
288 hostTextField
.setText( subscribedNewsgroups
[ position
].getHostName() );
289 newsgroupComboBox
.setToolTipText( "Newsgroup name: " + subscribedNewsgroups
[ position
].getNewsgroupName() + " (Host name: " + subscribedNewsgroups
[ position
].getHostName() + ")" );
295 NewsGroup groupToSend
= null;
296 SubscribedNewsgroups newsgroups
= new SubscribedNewsgroups();
297 subscribedNewsgroups
= newsgroups
.getNewsGroups();
299 // Test for no .mozilla or no subscribed newsgroups
300 // subscribedNewsgroups = null;
302 if( subscribedNewsgroups
== null )
304 //System.out.println( "Couldn't find any subscibed newsgroups in .mozilla" );
305 JOptionPane
.showMessageDialog( window
, "No subscribed newsgroups found in mozilla/netscape profile \nPlease enter newsgroup and host name",
306 "Newsgroups Information", JOptionPane
.INFORMATION_MESSAGE
);
310 // Copy all newsgroups into a vector for comparison
311 // Alter entries (to include host name) if duplication is found
312 Vector vector
= new Vector( subscribedNewsgroups
.length
);
313 for(int i
=0; i
< subscribedNewsgroups
.length
; i
++ )
315 vector
.add( subscribedNewsgroups
[i
].getNewsgroupName() );
318 for(int i
=0; i
< subscribedNewsgroups
.length
; i
++ )
320 // check if combo box already has a newsgroup with same name
321 // then add host name to differentiate
322 for(int j
=0; j
< subscribedNewsgroups
.length
; j
++ )
324 if( j
!= i
&& subscribedNewsgroups
[j
].getNewsgroupName().equalsIgnoreCase( subscribedNewsgroups
[i
].getNewsgroupName() ) )
326 vector
.set( j
, subscribedNewsgroups
[j
].getNewsgroupName() + " (" + subscribedNewsgroups
[j
].getHostName() + ")" );
327 vector
.set( i
, subscribedNewsgroups
[i
].getNewsgroupName() + " (" + subscribedNewsgroups
[i
].getHostName() + ")" );
331 // Copy converted newsgroups from vector to combo box
332 for(int i
=0; i
< subscribedNewsgroups
.length
; i
++ )
334 newsgroupComboBox
.addItem( vector
.elementAt(i
) );
338 newsgroupComboBox
.setPreferredSize( new Dimension( TEXTBOXWIDTH
, TEXTBOXHEIGHT
) );
339 newsgroupComboBox
.setEditable( true );
341 return newsgroupComboBox
;
346 private JPanel
constructOptionPanel()
348 officeHtmlButton
= new JRadioButton( "Office and HTML", true );
349 officeHtmlButton
.setToolTipText( officeHtmlText
);
351 officeButton
= new JRadioButton( "Office" );
352 officeButton
.setToolTipText( officeText
);
354 htmlButton
= new JRadioButton( "HTML" );
355 htmlButton
.setToolTipText( htmlText
);
357 JRadioButton
[] rbuttons
= { officeHtmlButton
, officeButton
, htmlButton
};
358 ButtonGroup radioButtonGroup
= new ButtonGroup();
359 for( int i
=0; i
< rbuttons
.length
; i
++ )
361 radioButtonGroup
.add( rbuttons
[i
] );
364 JPanel optionPanel
= new JPanel();
365 //optionPanel.setLayout( new GridLayout( 1, 3, 20, 0 ) );
366 optionPanel
.setBorder( new TitledBorder( new EtchedBorder(), "Document Format" ) );
367 optionPanel
.setLayout( new GridBagLayout() );
368 GridBagConstraints constraints
= new GridBagConstraints();
369 constraints
.fill
= GridBagConstraints
.BOTH
;
371 constraints
.gridx
= 0;
372 constraints
.gridy
= 0;
373 constraints
.gridwidth
= 1;
374 constraints
.gridheight
= 1;
375 constraints
.insets
= new Insets( 5, 5, 5, 30 );
376 optionPanel
.add( officeHtmlButton
, constraints
);
378 constraints
.gridx
= 1;
379 constraints
.gridy
= 0;
380 constraints
.gridwidth
= 1;
381 constraints
.gridheight
= 1;
382 constraints
.insets
= new Insets( 5, 20, 5, 30 );
383 optionPanel
.add( officeButton
, constraints
);
385 constraints
.gridx
= 2;
386 constraints
.gridy
= 0;
387 constraints
.gridwidth
= 1;
388 constraints
.gridheight
= 1;
389 constraints
.insets
= new Insets( 5, 20, 5, 5 );
390 optionPanel
.add( htmlButton
, constraints
);
397 public boolean sendingActions()
403 // Create status window
404 StatusWindow statusWindow
= new StatusWindow( window
, "Posting to Newsgroup", FRAMEX
, FRAMEY
);
406 statusWindow
.setVisible( true );
407 //statusWindow.requestFocusInWindow();
408 statusLine
= "Ready to send...";
409 statusWindow
.setStatus( 0, statusLine
);
411 // Get the boolean values for HTML/Office document
412 // params: ( XScriptContext, StatusWindow, html document, office document )
414 boolean html
= false;
415 boolean office
= false;
416 if( officeHtmlButton
.isSelected() ) { html
= true; office
= true; }
417 if( officeButton
.isSelected() ) { office
= true; html
= false; }
418 if( htmlButton
.isSelected() ) { html
= true; office
= false; }
420 OfficeAttachment officeAttach
= new OfficeAttachment( xscriptcontext
, statusWindow
, html
, office
);
422 statusLine
= "Getting user input";
423 statusWindow
.setStatus( 2, statusLine
);
424 // Get replyto, subject, comment from textboxes
425 String replyto
= replyTextField
.getText();
426 String subject
= subjectTextField
.getText();
427 String comment
= commentTextArea
.getText();
429 // Get newsgroup from combo box (corresponding position)
432 int position
= newsgroupComboBox
.getSelectedIndex();
433 if( subscribedNewsgroups
== null || position
== -1 )
435 host
= hostTextField
.getText();
436 group
= newsgroupComboBox
.getSelectedItem().toString();
440 //int position = newsgroupComboBox.getSelectedIndex();
441 host
= subscribedNewsgroups
[ position
].getHostName();
442 group
= subscribedNewsgroups
[ position
].getNewsgroupName();
445 statusLine
= "Creating sender object";
446 statusWindow
.setStatus( 3, statusLine
);
447 Sender sender
= new Sender( statusWindow
, officeAttach
, replyto
, subject
, comment
, host
, group
);
448 if( !sender
.sendMail() )
450 //System.out.println( "Should end here (?)" );
451 statusWindow
.enableCancelButton( true );
452 officeAttach
.cleanUpOnError();
456 statusLine
= "Send is complete";
457 statusWindow
.setStatus( 14, statusLine
);
461 //System.out.println( "Non valid data" );
468 private JPanel
constructButtonPanel()
470 Action postAction
= new AbstractAction() {
471 public void actionPerformed( ActionEvent event
) {
477 Action cancelAction
= new AbstractAction() {
478 public void actionPerformed( ActionEvent event
) {
479 // cancelling actions
484 postButton
= new JButton();
485 postButton
.setAction( postAction
);
486 postButton
.setToolTipText( postText
);
487 postButton
.setText( "Post" );
488 postButton
.setPreferredSize( new Dimension( BUTTONWIDTH
+ 20, BUTTONHEIGHT
) );
490 cancelButton
= new JButton();
491 cancelButton
.setAction( cancelAction
);
492 cancelButton
.setToolTipText( cancelText
);
493 cancelButton
.setText( "Cancel" );
494 cancelButton
.setPreferredSize( new Dimension( BUTTONWIDTH
+ 20, BUTTONHEIGHT
) );
496 JSeparator sep
= new JSeparator( SwingConstants
.HORIZONTAL
);
498 JPanel buttonPanel
= new JPanel();
499 buttonPanel
.setLayout( new GridBagLayout() );
500 GridBagConstraints constraints
= new GridBagConstraints();
501 constraints
.fill
= GridBagConstraints
.BOTH
;
502 constraints
.insets
= new Insets( 5, 5, 5, 5 );
504 JPanel emptyPanel1
= new JPanel();
505 emptyPanel1
.setPreferredSize( new Dimension( BUTTONWIDTH
, BUTTONHEIGHT
) );
507 JPanel emptyPanel2
= new JPanel();
508 emptyPanel2
.setPreferredSize( new Dimension( BUTTONWIDTH
, BUTTONHEIGHT
) );
510 constraints
.gridx
= 0;
511 constraints
.gridy
= 0;
512 constraints
.gridwidth
= 4;
513 constraints
.gridheight
= 1;
514 buttonPanel
.add( sep
, constraints
);
516 constraints
.gridx
= 0;
517 constraints
.gridy
= 1;
518 constraints
.gridwidth
= 1;
519 constraints
.gridheight
= 1;
520 buttonPanel
.add( emptyPanel1
, constraints
);
522 constraints
.gridx
= 1;
523 constraints
.gridy
= 1;
524 constraints
.gridwidth
= 1;
525 constraints
.gridheight
= 1;
526 buttonPanel
.add( emptyPanel2
, constraints
);
528 constraints
.gridx
= 2;
529 constraints
.gridy
= 1;
530 constraints
.gridwidth
= 1;
531 constraints
.gridheight
= 1;
532 buttonPanel
.add( postButton
, constraints
);
534 constraints
.gridx
= 3;
535 constraints
.gridy
= 1;
536 constraints
.gridwidth
= 1;
537 constraints
.gridheight
= 1;
538 constraints
.insets
= new Insets( 5, 5, 5, 0 );
539 buttonPanel
.add( cancelButton
, constraints
);
545 public void enableButtons( boolean enable
)
549 postButton
.setEnabled( true );
550 cancelButton
.setEnabled( true );
554 postButton
.setEnabled( false );
555 cancelButton
.setEnabled( false );
560 private boolean isValidData()
562 // newsgroupComboBox must not be blank (format? dots and whitespace)
563 String newsgroupString
= "";
564 int position
= newsgroupComboBox
.getSelectedIndex();
565 if( subscribedNewsgroups
== null || position
== -1 )
567 newsgroupString
= newsgroupComboBox
.getSelectedItem().toString();
571 //int position = newsgroupComboBox.getSelectedIndex();
572 newsgroupString
= subscribedNewsgroups
[ position
].getNewsgroupName();
574 if( newsgroupString
.length() == 0 )
576 //System.out.println( "Please enter a newsgroup name" );
577 newsgroupComboBox
.requestFocus();
578 JOptionPane
.showMessageDialog( window
, "Please enter a newsgroup name", "Input Error", JOptionPane
.ERROR_MESSAGE
);
583 // hostTextField must not be blank (format?)
584 String hostString
= hostTextField
.getText();
585 if( hostString
.length() == 0 )
587 //System.out.println( "Please enter a hostname" );
588 hostTextField
.requestFocus();
589 JOptionPane
.showMessageDialog( window
, "Please enter a hostname", "Input Error", JOptionPane
.ERROR_MESSAGE
);
594 // replyTextField must have <string>@<string>.<string>
595 // (string at least 2 chars long)
596 // consider <s>.<s>@<s>.<s>.<s> format? (array of dot positons?)
597 String replyString
= replyTextField
.getText();
598 int atPos
= replyString
.indexOf( "@" );
599 int dotPos
= replyString
.lastIndexOf( "." );
600 int length
= replyString
.length();
601 //System.out.println( "length: " + length + "\n atPos: " + atPos + "\n dotPos: " + dotPos );
602 if( length
== 0 || atPos
== -1 || dotPos
== -1 || atPos
< 2 || dotPos
< atPos
|| dotPos
+ 2 == length
|| atPos
+ 2 == dotPos
|| atPos
!= replyString
.lastIndexOf( "@" ) || replyString
.indexOf(" ") != -1 )
604 //System.out.println( "Please enter a valid reply to email address" );
605 replyTextField
.requestFocus();
606 JOptionPane
.showMessageDialog( window
, "Please enter a valid reply to email address", "Input Error", JOptionPane
.ERROR_MESSAGE
);
611 // subjectTextField must not be blank?
612 String subjectString
= subjectTextField
.getText();
613 if( subjectString
.length() == 0 )
615 //System.out.println( "Please enter subject title" );
616 subjectTextField
.requestFocus();
617 JOptionPane
.showMessageDialog( window
, "Please enter subject title", "Input Error", JOptionPane
.ERROR_MESSAGE
);