1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 import javax.swing.border.*; 5 import java.util.Vector; 6 import com.sun.star.script.framework.runtime.XScriptContext; 7 8 9 public class PostNewsgroup extends JFrame 10 { 11 12 // Post to newsgroup objects 13 private NewsGroup[] subscribedNewsgroups = null; 14 private XScriptContext xscriptcontext = null; 15 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; 23 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; 35 36 // JFrame for launch progress dialog 37 private StatusWindow statusWindow = null; 38 private String statusLine = ""; 39 40 // Tool tip text 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"; 51 52 53 public void post( XScriptContext xsc ) 54 { 55 xscriptcontext = xsc; 56 window = this; 57 58 // create mailcap and mimetypes files (fix for classloader problem) 59 MimeConfiguration.createFiles( xscriptcontext ); 60 61 this.setTitle( "Post Document To Newsgroup" ); 62 this.setLocation( FRAMEX, FRAMEY ); 63 64 this.addFocusListener( new FocusAdapter() 65 { 66 public void focusGained( FocusEvent event ) 67 { 68 System.out.println( "Focus gained" ); 69 window.update( window.getGraphics() ); 70 } 71 72 public void focusLost( FocusEvent event ) 73 { 74 System.out.println( "Focus lost" ); 75 } 76 }); 77 78 Container container = getContentPane(); 79 container.setLayout( new GridBagLayout() );; 80 GridBagConstraints constraints = new GridBagConstraints(); 81 constraints.fill = GridBagConstraints.BOTH; 82 83 JPanel labelPanel = constructLabelPanel(); 84 JPanel textPanel = constructTextPanel(); 85 JPanel optionPanel = constructOptionPanel(); 86 JPanel buttonPanel = constructButtonPanel(); 87 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 ); 94 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 ); 101 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 ); 108 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 ); 115 116 this.pack(); 117 this.setResizable( false ); 118 this.setVisible( true ); 119 } 120 121 122 private JPanel constructLabelPanel() 123 { 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:" ); 129 130 newsgroupLabel.setToolTipText( newsgroupText ); 131 hostLabel.setToolTipText( hostText ); 132 replyLabel.setToolTipText( replyText ); 133 subjectLabel.setToolTipText( subjectText ); 134 commentLabel.setToolTipText( commentText ); 135 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(); 152 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 ) ); 159 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 ); 165 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 ); 172 173 constraints.gridx = 0; 174 constraints.gridy = 1; 175 constraints.gridwidth = 1; 176 constraints.gridheight = 1; 177 panel.add( hostPanel, constraints ); 178 179 constraints.gridx = 0; 180 constraints.gridy = 2; 181 constraints.gridwidth = 1; 182 constraints.gridheight = 1; 183 panel.add( replyPanel, constraints ); 184 185 constraints.gridx = 0; 186 constraints.gridy = 3; 187 constraints.gridwidth = 1; 188 constraints.gridheight = 1; 189 panel.add( subjectPanel, constraints ); 190 191 constraints.gridx = 0; 192 constraints.gridy = 4; 193 constraints.gridwidth = 1; 194 constraints.gridheight = 1; 195 panel.add( commentPanel, constraints ); 196 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 ); 203 204 return panel; 205 } 206 207 208 private JPanel constructTextPanel() 209 { 210 hostTextField = new JTextField(); 211 hostTextField.setPreferredSize( new Dimension( TEXTBOXWIDTH, TEXTBOXHEIGHT ) ); 212 hostTextField.setToolTipText( hostText ); 213 hostTextField.setBorder( new EtchedBorder() ); 214 215 //optionPanel.setBorder( new TitledBorder( new EtchedBorder(), "Document Format" ) ); 216 newsgroupComboBox = getNewsgroupCombo(); 217 218 replyTextField = new JTextField(); 219 replyTextField.setPreferredSize( new Dimension( TEXTBOXWIDTH, TEXTBOXHEIGHT ) ); 220 replyTextField.setToolTipText( replyText ); 221 replyTextField.setBorder( new EtchedBorder() ); 222 223 subjectTextField = new JTextField(); 224 subjectTextField.setPreferredSize( new Dimension( TEXTBOXWIDTH, TEXTBOXHEIGHT ) ); 225 subjectTextField.setToolTipText( subjectText ); 226 subjectTextField.setBorder( new EtchedBorder() ); 227 228 commentTextArea = new JTextArea(); 229 commentTextArea.setPreferredSize( new Dimension( TEXTBOXWIDTH, TEXTAREAHEIGHT ) ); 230 commentTextArea.setToolTipText( commentText ); 231 commentTextArea.setBorder( new EtchedBorder() ); 232 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 ); 238 239 constraints.gridx = 0; 240 constraints.gridy = 0; 241 constraints.gridwidth = 1; 242 constraints.gridheight = 1; 243 panel.add( newsgroupComboBox, constraints ); 244 245 constraints.gridx = 0; 246 constraints.gridy = 1; 247 constraints.gridwidth = 1; 248 constraints.gridheight = 1; 249 panel.add( hostTextField, constraints ); 250 251 constraints.gridx = 0; 252 constraints.gridy = 2; 253 constraints.gridwidth = 1; 254 constraints.gridheight = 1; 255 panel.add( replyTextField, constraints ); 256 257 constraints.gridx = 0; 258 constraints.gridy = 3; 259 constraints.gridwidth = 1; 260 constraints.gridheight = 1; 261 panel.add( subjectTextField, constraints ); 262 263 constraints.gridx = 0; 264 constraints.gridy = 4; 265 constraints.gridwidth = 1; 266 constraints.gridheight = 2; 267 panel.add( commentTextArea, constraints ); 268 269 return panel; 270 } 271 272 273 private JComboBox getNewsgroupCombo() 274 { 275 newsgroupComboBox = new JComboBox(); 276 //newsgroupComboBox.setBorder( new EtchedBorder() ); 277 278 newsgroupComboBox.addActionListener(new ActionListener() 279 { 280 public void actionPerformed(ActionEvent e) 281 { 282 // when newsgroup is selected 283 if( subscribedNewsgroups != null ) 284 { 285 int position = newsgroupComboBox.getSelectedIndex(); 286 if( position != -1 ) 287 { 288 hostTextField.setText( subscribedNewsgroups[ position ].getHostName() ); 289 newsgroupComboBox.setToolTipText( "Newsgroup name: " + subscribedNewsgroups[ position ].getNewsgroupName() + " (Host name: " + subscribedNewsgroups[ position ].getHostName() + ")" ); 290 } 291 } 292 } 293 }); 294 295 NewsGroup groupToSend = null; 296 SubscribedNewsgroups newsgroups = new SubscribedNewsgroups(); 297 subscribedNewsgroups = newsgroups.getNewsGroups(); 298 299 // Test for no .mozilla or no subscribed newsgroups 300 // subscribedNewsgroups = null; 301 302 if( subscribedNewsgroups == null ) 303 { 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 ); 307 } 308 else 309 { 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++ ) 314 { 315 vector.add( subscribedNewsgroups[i].getNewsgroupName() ); 316 } 317 // Compare and alter 318 for(int i=0; i < subscribedNewsgroups.length; i++ ) 319 { 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++ ) 323 { 324 if( j != i && subscribedNewsgroups[j].getNewsgroupName().equalsIgnoreCase( subscribedNewsgroups[i].getNewsgroupName() ) ) 325 { 326 vector.set( j, subscribedNewsgroups[j].getNewsgroupName() + " (" + subscribedNewsgroups[j].getHostName() + ")" ); 327 vector.set( i, subscribedNewsgroups[i].getNewsgroupName() + " (" + subscribedNewsgroups[i].getHostName() + ")" ); 328 } 329 } 330 } 331 // Copy converted newsgroups from vector to combo box 332 for(int i=0; i < subscribedNewsgroups.length; i++ ) 333 { 334 newsgroupComboBox.addItem( vector.elementAt(i) ); 335 } 336 }// else 337 338 newsgroupComboBox.setPreferredSize( new Dimension( TEXTBOXWIDTH, TEXTBOXHEIGHT ) ); 339 newsgroupComboBox.setEditable( true ); 340 341 return newsgroupComboBox; 342 } 343 344 345 346 private JPanel constructOptionPanel() 347 { 348 officeHtmlButton = new JRadioButton( "Office and HTML", true ); 349 officeHtmlButton.setToolTipText( officeHtmlText ); 350 351 officeButton = new JRadioButton( "Office" ); 352 officeButton.setToolTipText( officeText ); 353 354 htmlButton = new JRadioButton( "HTML" ); 355 htmlButton.setToolTipText( htmlText ); 356 357 JRadioButton[] rbuttons = { officeHtmlButton, officeButton, htmlButton }; 358 ButtonGroup radioButtonGroup = new ButtonGroup(); 359 for( int i=0; i < rbuttons.length; i++ ) 360 { 361 radioButtonGroup.add( rbuttons[i] ); 362 } 363 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; 370 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 ); 377 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 ); 384 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 ); 391 392 return optionPanel; 393 } 394 395 396 397 public boolean sendingActions() 398 { 399 // posting actions 400 // Validate the data 401 if( isValidData() ) 402 { 403 // Create status window 404 StatusWindow statusWindow = new StatusWindow( window, "Posting to Newsgroup", FRAMEX, FRAMEY ); 405 406 statusWindow.setVisible( true ); 407 //statusWindow.requestFocusInWindow(); 408 statusLine = "Ready to send..."; 409 statusWindow.setStatus( 0, statusLine ); 410 411 // Get the boolean values for HTML/Office document 412 // params: ( XScriptContext, StatusWindow, html document, office document ) 413 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; } 419 420 OfficeAttachment officeAttach = new OfficeAttachment( xscriptcontext, statusWindow, html, office ); 421 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(); 428 429 // Get newsgroup from combo box (corresponding position) 430 String host = ""; 431 String group = ""; 432 int position = newsgroupComboBox.getSelectedIndex(); 433 if( subscribedNewsgroups == null || position == -1 ) 434 { 435 host = hostTextField.getText(); 436 group = newsgroupComboBox.getSelectedItem().toString(); 437 } 438 else 439 { 440 //int position = newsgroupComboBox.getSelectedIndex(); 441 host = subscribedNewsgroups[ position ].getHostName(); 442 group = subscribedNewsgroups[ position ].getNewsgroupName(); 443 } 444 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() ) 449 { 450 //System.out.println( "Should end here (?)" ); 451 statusWindow.enableCancelButton( true ); 452 officeAttach.cleanUpOnError(); 453 return false; 454 } 455 456 statusLine = "Send is complete"; 457 statusWindow.setStatus( 14, statusLine ); 458 } 459 else 460 { 461 //System.out.println( "Non valid data" ); 462 return false; 463 } 464 return true; 465 } 466 467 468 private JPanel constructButtonPanel() 469 { 470 Action postAction = new AbstractAction() { 471 public void actionPerformed( ActionEvent event ) { 472 // posting actions 473 sendingActions(); 474 }// actionPerformed 475 }; 476 477 Action cancelAction = new AbstractAction() { 478 public void actionPerformed( ActionEvent event ) { 479 // cancelling actions 480 window.dispose(); 481 } 482 }; 483 484 postButton = new JButton(); 485 postButton.setAction( postAction ); 486 postButton.setToolTipText( postText ); 487 postButton.setText( "Post" ); 488 postButton.setPreferredSize( new Dimension( BUTTONWIDTH + 20, BUTTONHEIGHT ) ); 489 490 cancelButton = new JButton(); 491 cancelButton.setAction( cancelAction ); 492 cancelButton.setToolTipText( cancelText ); 493 cancelButton.setText( "Cancel" ); 494 cancelButton.setPreferredSize( new Dimension( BUTTONWIDTH + 20, BUTTONHEIGHT ) ); 495 496 JSeparator sep = new JSeparator( SwingConstants.HORIZONTAL ); 497 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 ); 503 504 JPanel emptyPanel1 = new JPanel(); 505 emptyPanel1.setPreferredSize( new Dimension( BUTTONWIDTH, BUTTONHEIGHT ) ); 506 507 JPanel emptyPanel2 = new JPanel(); 508 emptyPanel2.setPreferredSize( new Dimension( BUTTONWIDTH, BUTTONHEIGHT ) ); 509 510 constraints.gridx = 0; 511 constraints.gridy = 0; 512 constraints.gridwidth = 4; 513 constraints.gridheight = 1; 514 buttonPanel.add( sep, constraints ); 515 516 constraints.gridx = 0; 517 constraints.gridy = 1; 518 constraints.gridwidth = 1; 519 constraints.gridheight = 1; 520 buttonPanel.add( emptyPanel1, constraints ); 521 522 constraints.gridx = 1; 523 constraints.gridy = 1; 524 constraints.gridwidth = 1; 525 constraints.gridheight = 1; 526 buttonPanel.add( emptyPanel2, constraints ); 527 528 constraints.gridx = 2; 529 constraints.gridy = 1; 530 constraints.gridwidth = 1; 531 constraints.gridheight = 1; 532 buttonPanel.add( postButton, constraints ); 533 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 ); 540 541 return buttonPanel; 542 } 543 544 545 public void enableButtons( boolean enable ) 546 { 547 if( enable ) 548 { 549 postButton.setEnabled( true ); 550 cancelButton.setEnabled( true ); 551 } 552 else 553 { 554 postButton.setEnabled( false ); 555 cancelButton.setEnabled( false ); 556 } 557 } 558 559 560 private boolean isValidData() 561 { 562 // newsgroupComboBox must not be blank (format? dots and whitespace) 563 String newsgroupString = ""; 564 int position = newsgroupComboBox.getSelectedIndex(); 565 if( subscribedNewsgroups == null || position == -1 ) 566 { 567 newsgroupString = newsgroupComboBox.getSelectedItem().toString(); 568 } 569 else 570 { 571 //int position = newsgroupComboBox.getSelectedIndex(); 572 newsgroupString = subscribedNewsgroups[ position ].getNewsgroupName(); 573 } 574 if( newsgroupString.length() == 0 ) 575 { 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 ); 579 return false; 580 } 581 582 583 // hostTextField must not be blank (format?) 584 String hostString = hostTextField.getText(); 585 if( hostString.length() == 0 ) 586 { 587 //System.out.println( "Please enter a hostname" ); 588 hostTextField.requestFocus(); 589 JOptionPane.showMessageDialog( window, "Please enter a hostname", "Input Error", JOptionPane.ERROR_MESSAGE ); 590 return false; 591 } 592 593 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 ) 603 { 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 ); 607 return false; 608 } 609 610 611 // subjectTextField must not be blank? 612 String subjectString = subjectTextField.getText(); 613 if( subjectString.length() == 0 ) 614 { 615 //System.out.println( "Please enter subject title" ); 616 subjectTextField.requestFocus(); 617 JOptionPane.showMessageDialog( window, "Please enter subject title", "Input Error", JOptionPane.ERROR_MESSAGE ); 618 return false; 619 } 620 621 // details are valid 622 return true; 623 } 624 625 } 626