| 1 |
/* |
| 2 |
* This file is part of the LibreOffice project. |
| 3 |
* |
| 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/. |
| 7 |
* |
| 8 |
* This file incorporates work covered by the following license notice: |
| 9 |
* |
| 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 . |
| 17 |
*/ |
| 18 |
|
| 19 |
package org.libreoffice.example.java_scripts; |
| 20 |
|
| 21 |
import java.awt.*; |
| 22 |
import java.awt.event.*; |
| 23 |
import javax.swing.*; |
| 24 |
import javax.swing.border.*; |
| 25 |
import java.util.Vector; |
| 26 |
import com.sun.star.script.framework.runtime.XScriptContext; |
| 27 |
|
| 28 |
|
| 29 |
public class PostNewsgroup extends JFrame { |
| 30 |
|
| 31 |
// Post to newsgroup objects |
| 32 |
private NewsGroup[] subscribedNewsgroups = null; |
| 33 |
private XScriptContext xscriptcontext = null; |
| 34 |
|
| 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; |
| 42 |
|
| 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; |
| 54 |
|
| 55 |
// JFrame for launch progress dialog |
| 56 |
private StatusWindow statusWindow = null; |
| 57 |
private String statusLine = ""; |
| 58 |
|
| 59 |
// Tool tip text |
| 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"; |
| 71 |
|
| 72 |
|
| 73 |
public void post(XScriptContext xsc) { |
| 74 |
xscriptcontext = xsc; |
| 75 |
window = this; |
| 76 |
|
| 77 |
// create mailcap and mimetypes files (fix for classloader problem) |
| 78 |
MimeConfiguration.createFiles(xscriptcontext); |
| 79 |
|
| 80 |
this.setTitle("Post Document To Newsgroup"); |
| 81 |
this.setLocation(FRAMEX, FRAMEY); |
| 82 |
|
| 83 |
this.addFocusListener(new FocusAdapter() { |
| 84 |
public void focusGained(FocusEvent event) { |
| 85 |
System.out.println("Focus gained"); |
| 86 |
window.update(window.getGraphics()); |
| 87 |
} |
| 88 |
|
| 89 |
public void focusLost(FocusEvent event) { |
| 90 |
System.out.println("Focus lost"); |
| 91 |
} |
| 92 |
}); |
| 93 |
|
| 94 |
Container container = getContentPane(); |
| 95 |
container.setLayout(new GridBagLayout()); |
| 96 |
GridBagConstraints constraints = new GridBagConstraints(); |
| 97 |
constraints.fill = GridBagConstraints.BOTH; |
| 98 |
|
| 99 |
JPanel labelPanel = constructLabelPanel(); |
| 100 |
JPanel textPanel = constructTextPanel(); |
| 101 |
JPanel optionPanel = constructOptionPanel(); |
| 102 |
JPanel buttonPanel = constructButtonPanel(); |
| 103 |
|
| 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); |
| 110 |
|
| 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); |
| 117 |
|
| 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); |
| 124 |
|
| 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); |
| 131 |
|
| 132 |
this.pack(); |
| 133 |
this.setResizable(false); |
| 134 |
this.setVisible(true); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 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:"); |
| 144 |
|
| 145 |
newsgroupLabel.setToolTipText(newsgroupText); |
| 146 |
hostLabel.setToolTipText(hostText); |
| 147 |
replyLabel.setToolTipText(replyText); |
| 148 |
subjectLabel.setToolTipText(subjectText); |
| 149 |
commentLabel.setToolTipText(commentText); |
| 150 |
|
| 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(); |
| 167 |
|
| 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)); |
| 174 |
|
| 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); |
| 180 |
|
| 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); |
| 187 |
|
| 188 |
constraints.gridx = 0; |
| 189 |
constraints.gridy = 1; |
| 190 |
constraints.gridwidth = 1; |
| 191 |
constraints.gridheight = 1; |
| 192 |
panel.add(hostPanel, constraints); |
| 193 |
|
| 194 |
constraints.gridx = 0; |
| 195 |
constraints.gridy = 2; |
| 196 |
constraints.gridwidth = 1; |
| 197 |
constraints.gridheight = 1; |
| 198 |
panel.add(replyPanel, constraints); |
| 199 |
|
| 200 |
constraints.gridx = 0; |
| 201 |
constraints.gridy = 3; |
| 202 |
constraints.gridwidth = 1; |
| 203 |
constraints.gridheight = 1; |
| 204 |
panel.add(subjectPanel, constraints); |
| 205 |
|
| 206 |
constraints.gridx = 0; |
| 207 |
constraints.gridy = 4; |
| 208 |
constraints.gridwidth = 1; |
| 209 |
constraints.gridheight = 1; |
| 210 |
panel.add(commentPanel, constraints); |
| 211 |
|
| 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); |
| 218 |
|
| 219 |
return panel; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
private JPanel constructTextPanel() { |
| 224 |
hostTextField = new JTextField(); |
| 225 |
hostTextField.setPreferredSize(new Dimension(TEXTBOXWIDTH, TEXTBOXHEIGHT)); |
| 226 |
hostTextField.setToolTipText(hostText); |
| 227 |
hostTextField.setBorder(new EtchedBorder()); |
| 228 |
|
| 229 |
//optionPanel.setBorder( new TitledBorder( new EtchedBorder(), "Document Format" ) ); |
| 230 |
newsgroupComboBox = getNewsgroupCombo(); |
| 231 |
|
| 232 |
replyTextField = new JTextField(); |
| 233 |
replyTextField.setPreferredSize(new Dimension(TEXTBOXWIDTH, TEXTBOXHEIGHT)); |
| 234 |
replyTextField.setToolTipText(replyText); |
| 235 |
replyTextField.setBorder(new EtchedBorder()); |
| 236 |
|
| 237 |
subjectTextField = new JTextField(); |
| 238 |
subjectTextField.setPreferredSize(new Dimension(TEXTBOXWIDTH, TEXTBOXHEIGHT)); |
| 239 |
subjectTextField.setToolTipText(subjectText); |
| 240 |
subjectTextField.setBorder(new EtchedBorder()); |
| 241 |
|
| 242 |
commentTextArea = new JTextArea(); |
| 243 |
commentTextArea.setPreferredSize(new Dimension(TEXTBOXWIDTH, TEXTAREAHEIGHT)); |
| 244 |
commentTextArea.setToolTipText(commentText); |
| 245 |
commentTextArea.setBorder(new EtchedBorder()); |
| 246 |
|
| 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); |
| 252 |
|
| 253 |
constraints.gridx = 0; |
| 254 |
constraints.gridy = 0; |
| 255 |
constraints.gridwidth = 1; |
| 256 |
constraints.gridheight = 1; |
| 257 |
panel.add(newsgroupComboBox, constraints); |
| 258 |
|
| 259 |
constraints.gridx = 0; |
| 260 |
constraints.gridy = 1; |
| 261 |
constraints.gridwidth = 1; |
| 262 |
constraints.gridheight = 1; |
| 263 |
panel.add(hostTextField, constraints); |
| 264 |
|
| 265 |
constraints.gridx = 0; |
| 266 |
constraints.gridy = 2; |
| 267 |
constraints.gridwidth = 1; |
| 268 |
constraints.gridheight = 1; |
| 269 |
panel.add(replyTextField, constraints); |
| 270 |
|
| 271 |
constraints.gridx = 0; |
| 272 |
constraints.gridy = 3; |
| 273 |
constraints.gridwidth = 1; |
| 274 |
constraints.gridheight = 1; |
| 275 |
panel.add(subjectTextField, constraints); |
| 276 |
|
| 277 |
constraints.gridx = 0; |
| 278 |
constraints.gridy = 4; |
| 279 |
constraints.gridwidth = 1; |
| 280 |
constraints.gridheight = 2; |
| 281 |
panel.add(commentTextArea, constraints); |
| 282 |
|
| 283 |
return panel; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
private JComboBox getNewsgroupCombo() { |
| 288 |
newsgroupComboBox = new JComboBox(); |
| 289 |
//newsgroupComboBox.setBorder( new EtchedBorder() ); |
| 290 |
|
| 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(); |
| 296 |
|
| 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() + ")"); |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
}); |
| 306 |
|
| 307 |
NewsGroup groupToSend = null; |
| 308 |
SubscribedNewsgroups newsgroups = new SubscribedNewsgroups(); |
| 309 |
subscribedNewsgroups = newsgroups.getNewsGroups(); |
| 310 |
|
| 311 |
// Test for no .mozilla or no subscribed newsgroups |
| 312 |
// subscribedNewsgroups = null; |
| 313 |
|
| 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); |
| 318 |
} else { |
| 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); |
| 322 |
|
| 323 |
for (int i = 0; i < subscribedNewsgroups.length; i++) { |
| 324 |
vector.add(subscribedNewsgroups[i].getNewsgroupName()); |
| 325 |
} |
| 326 |
|
| 327 |
// Compare and alter |
| 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++) { |
| 332 |
if (j != i |
| 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() + ")"); |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
// Copy converted newsgroups from vector to combo box |
| 344 |
for (int i = 0; i < subscribedNewsgroups.length; i++) { |
| 345 |
newsgroupComboBox.addItem(vector.elementAt(i)); |
| 346 |
} |
| 347 |
}// else |
| 348 |
|
| 349 |
newsgroupComboBox.setPreferredSize(new Dimension(TEXTBOXWIDTH, TEXTBOXHEIGHT)); |
| 350 |
newsgroupComboBox.setEditable(true); |
| 351 |
|
| 352 |
return newsgroupComboBox; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
private JPanel constructOptionPanel() { |
| 358 |
officeHtmlButton = new JRadioButton("Office and HTML", true); |
| 359 |
officeHtmlButton.setToolTipText(officeHtmlText); |
| 360 |
|
| 361 |
officeButton = new JRadioButton("Office"); |
| 362 |
officeButton.setToolTipText(officeText); |
| 363 |
|
| 364 |
htmlButton = new JRadioButton("HTML"); |
| 365 |
htmlButton.setToolTipText(htmlText); |
| 366 |
|
| 367 |
JRadioButton[] rbuttons = { officeHtmlButton, officeButton, htmlButton }; |
| 368 |
ButtonGroup radioButtonGroup = new ButtonGroup(); |
| 369 |
|
| 370 |
for (int i = 0; i < rbuttons.length; i++) { |
| 371 |
radioButtonGroup.add(rbuttons[i]); |
| 372 |
} |
| 373 |
|
| 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; |
| 379 |
|
| 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); |
| 386 |
|
| 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); |
| 393 |
|
| 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); |
| 400 |
|
| 401 |
return optionPanel; |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
public boolean sendingActions() { |
| 407 |
// posting actions |
| 408 |
// Validate the data |
| 409 |
if (isValidData()) { |
| 410 |
// Create status window |
| 411 |
StatusWindow statusWindow = new StatusWindow(window, "Posting to Newsgroup", |
| 412 |
FRAMEX, FRAMEY); |
| 413 |
|
| 414 |
statusWindow.setVisible(true); |
| 415 |
statusLine = "Ready to send..."; |
| 416 |
statusWindow.setStatus(0, statusLine); |
| 417 |
|
| 418 |
// Get the boolean values for HTML/Office document |
| 419 |
// params: ( XScriptContext, StatusWindow, html document, office document ) |
| 420 |
|
| 421 |
boolean html = false; |
| 422 |
boolean office = false; |
| 423 |
|
| 424 |
if (officeHtmlButton.isSelected()) { |
| 425 |
html = true; |
| 426 |
office = true; |
| 427 |
} |
| 428 |
|
| 429 |
if (officeButton.isSelected()) { |
| 430 |
office = true; |
| 431 |
html = false; |
| 432 |
} |
| 433 |
|
| 434 |
if (htmlButton.isSelected()) { |
| 435 |
html = true; |
| 436 |
office = false; |
| 437 |
} |
| 438 |
|
| 439 |
OfficeAttachment officeAttach = new OfficeAttachment(xscriptcontext, |
| 440 |
statusWindow, html, office); |
| 441 |
|
| 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(); |
| 448 |
|
| 449 |
// Get newsgroup from combo box (corresponding position) |
| 450 |
String host = ""; |
| 451 |
String group = ""; |
| 452 |
int position = newsgroupComboBox.getSelectedIndex(); |
| 453 |
|
| 454 |
if (subscribedNewsgroups == null || position == -1) { |
| 455 |
host = hostTextField.getText(); |
| 456 |
group = newsgroupComboBox.getSelectedItem().toString(); |
| 457 |
} else { |
| 458 |
host = subscribedNewsgroups[ position ].getHostName(); |
| 459 |
group = subscribedNewsgroups[ position ].getNewsgroupName(); |
| 460 |
} |
| 461 |
|
| 462 |
statusLine = "Creating sender object"; |
| 463 |
statusWindow.setStatus(3, statusLine); |
| 464 |
Sender sender = new Sender(statusWindow, officeAttach, replyto, subject, |
| 465 |
comment, host, group); |
| 466 |
|
| 467 |
if (!sender.sendMail()) { |
| 468 |
statusWindow.enableCancelButton(true); |
| 469 |
officeAttach.cleanUpOnError(); |
| 470 |
return false; |
| 471 |
} |
| 472 |
|
| 473 |
statusLine = "Send is complete"; |
| 474 |
statusWindow.setStatus(14, statusLine); |
| 475 |
} else { |
| 476 |
return false; |
| 477 |
} |
| 478 |
|
| 479 |
return true; |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
private JPanel constructButtonPanel() { |
| 484 |
Action postAction = new AbstractAction() { |
| 485 |
public void actionPerformed(ActionEvent event) { |
| 486 |
// posting actions |
| 487 |
sendingActions(); |
| 488 |
}// actionPerformed |
| 489 |
}; |
| 490 |
|
| 491 |
Action cancelAction = new AbstractAction() { |
| 492 |
public void actionPerformed(ActionEvent event) { |
| 493 |
// cancelling actions |
| 494 |
window.dispose(); |
| 495 |
} |
| 496 |
}; |
| 497 |
|
| 498 |
postButton = new JButton(); |
| 499 |
postButton.setAction(postAction); |
| 500 |
postButton.setToolTipText(postText); |
| 501 |
postButton.setText("Post"); |
| 502 |
postButton.setPreferredSize(new Dimension(BUTTONWIDTH + 20, BUTTONHEIGHT)); |
| 503 |
|
| 504 |
cancelButton = new JButton(); |
| 505 |
cancelButton.setAction(cancelAction); |
| 506 |
cancelButton.setToolTipText(cancelText); |
| 507 |
cancelButton.setText("Cancel"); |
| 508 |
cancelButton.setPreferredSize(new Dimension(BUTTONWIDTH + 20, BUTTONHEIGHT)); |
| 509 |
|
| 510 |
JSeparator sep = new JSeparator(SwingConstants.HORIZONTAL); |
| 511 |
|
| 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); |
| 517 |
|
| 518 |
JPanel emptyPanel1 = new JPanel(); |
| 519 |
emptyPanel1.setPreferredSize(new Dimension(BUTTONWIDTH, BUTTONHEIGHT)); |
| 520 |
|
| 521 |
JPanel emptyPanel2 = new JPanel(); |
| 522 |
emptyPanel2.setPreferredSize(new Dimension(BUTTONWIDTH, BUTTONHEIGHT)); |
| 523 |
|
| 524 |
constraints.gridx = 0; |
| 525 |
constraints.gridy = 0; |
| 526 |
constraints.gridwidth = 4; |
| 527 |
constraints.gridheight = 1; |
| 528 |
buttonPanel.add(sep, constraints); |
| 529 |
|
| 530 |
constraints.gridx = 0; |
| 531 |
constraints.gridy = 1; |
| 532 |
constraints.gridwidth = 1; |
| 533 |
constraints.gridheight = 1; |
| 534 |
buttonPanel.add(emptyPanel1, constraints); |
| 535 |
|
| 536 |
constraints.gridx = 1; |
| 537 |
constraints.gridy = 1; |
| 538 |
constraints.gridwidth = 1; |
| 539 |
constraints.gridheight = 1; |
| 540 |
buttonPanel.add(emptyPanel2, constraints); |
| 541 |
|
| 542 |
constraints.gridx = 2; |
| 543 |
constraints.gridy = 1; |
| 544 |
constraints.gridwidth = 1; |
| 545 |
constraints.gridheight = 1; |
| 546 |
buttonPanel.add(postButton, constraints); |
| 547 |
|
| 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); |
| 554 |
|
| 555 |
return buttonPanel; |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
public void enableButtons(boolean enable) { |
| 560 |
if (enable) { |
| 561 |
postButton.setEnabled(true); |
| 562 |
cancelButton.setEnabled(true); |
| 563 |
} else { |
| 564 |
postButton.setEnabled(false); |
| 565 |
cancelButton.setEnabled(false); |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
private boolean isValidData() { |
| 571 |
// newsgroupComboBox must not be blank (format? dots and whitespace) |
| 572 |
String newsgroupString = ""; |
| 573 |
int position = newsgroupComboBox.getSelectedIndex(); |
| 574 |
|
| 575 |
if (subscribedNewsgroups == null || position == -1) { |
| 576 |
newsgroupString = newsgroupComboBox.getSelectedItem().toString(); |
| 577 |
} else { |
| 578 |
newsgroupString = subscribedNewsgroups[ position ].getNewsgroupName(); |
| 579 |
} |
| 580 |
|
| 581 |
if (newsgroupString.length() == 0) { |
| 582 |
newsgroupComboBox.requestFocus(); |
| 583 |
JOptionPane.showMessageDialog(window, "Please enter a newsgroup name", |
| 584 |
"Input Error", JOptionPane.ERROR_MESSAGE); |
| 585 |
return false; |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
// hostTextField must not be blank (format?) |
| 590 |
String hostString = hostTextField.getText(); |
| 591 |
|
| 592 |
if (hostString.length() == 0) { |
| 593 |
hostTextField.requestFocus(); |
| 594 |
JOptionPane.showMessageDialog(window, "Please enter a hostname", "Input Error", |
| 595 |
JOptionPane.ERROR_MESSAGE); |
| 596 |
return false; |
| 597 |
} |
| 598 |
|
| 599 |
|
| 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(); |
| 607 |
|
| 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); |
| 615 |
return false; |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
// subjectTextField must not be blank? |
| 620 |
String subjectString = subjectTextField.getText(); |
| 621 |
|
| 622 |
if (subjectString.length() == 0) { |
| 623 |
subjectTextField.requestFocus(); |
| 624 |
JOptionPane.showMessageDialog(window, "Please enter subject title", |
| 625 |
"Input Error", JOptionPane.ERROR_MESSAGE); |
| 626 |
return false; |
| 627 |
} |
| 628 |
|
| 629 |
// details are valid |
| 630 |
return true; |
| 631 |
} |
| 632 |
|
| 633 |
} |