1*cd519653SAndrew Rist /**************************************************************
2*cd519653SAndrew Rist  *
3*cd519653SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*cd519653SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*cd519653SAndrew Rist  * distributed with this work for additional information
6*cd519653SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*cd519653SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*cd519653SAndrew Rist  * "License"); you may not use this file except in compliance
9*cd519653SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*cd519653SAndrew Rist  *
11*cd519653SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*cd519653SAndrew Rist  *
13*cd519653SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*cd519653SAndrew Rist  * software distributed under the License is distributed on an
15*cd519653SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*cd519653SAndrew Rist  * KIND, either express or implied.  See the License for the
17*cd519653SAndrew Rist  * specific language governing permissions and limitations
18*cd519653SAndrew Rist  * under the License.
19*cd519653SAndrew Rist  *
20*cd519653SAndrew Rist  *************************************************************/
21*cd519653SAndrew Rist 
22cdf0e10cSrcweir import java.awt.*;
23cdf0e10cSrcweir import java.awt.event.*;
24cdf0e10cSrcweir import javax.swing.*;
25cdf0e10cSrcweir import javax.swing.border.*;
26cdf0e10cSrcweir import java.util.Vector;
27cdf0e10cSrcweir import com.sun.star.script.framework.runtime.XScriptContext;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir 
30cdf0e10cSrcweir public class PostNewsgroup extends JFrame
31cdf0e10cSrcweir {
32cdf0e10cSrcweir 
33cdf0e10cSrcweir 	// Post to newsgroup objects
34cdf0e10cSrcweir 	private NewsGroup[] subscribedNewsgroups = null;
35cdf0e10cSrcweir 	private XScriptContext xscriptcontext = null;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir 	private final int FRAMEX = 300;
38cdf0e10cSrcweir 	private final int FRAMEY = 300;
39cdf0e10cSrcweir 	private final int TEXTBOXWIDTH = 300;
40cdf0e10cSrcweir 	private final int TEXTBOXHEIGHT = 24;
41cdf0e10cSrcweir 	private final int TEXTAREAHEIGHT = 70;
42cdf0e10cSrcweir 	private final int BUTTONWIDTH = 80;
43cdf0e10cSrcweir 	private final int BUTTONHEIGHT = 30;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir 	private PostNewsgroup window = null;
46cdf0e10cSrcweir 	private JComboBox newsgroupComboBox = null;
47cdf0e10cSrcweir 	private JTextField hostTextField = null;
48cdf0e10cSrcweir 	private JTextField replyTextField = null;
49cdf0e10cSrcweir 	private JTextField subjectTextField = null;
50cdf0e10cSrcweir 	private JTextArea commentTextArea = null;
51cdf0e10cSrcweir 	private JRadioButton officeHtmlButton = null;
52cdf0e10cSrcweir 	private JRadioButton officeButton = null;
53cdf0e10cSrcweir 	private JRadioButton htmlButton = null;
54cdf0e10cSrcweir 	private JButton postButton = null;
55cdf0e10cSrcweir 	private JButton cancelButton = null;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir 	// JFrame for launch progress dialog
58cdf0e10cSrcweir 	private StatusWindow statusWindow = null;
59cdf0e10cSrcweir 	private String statusLine = "";
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 	// Tool tip text
62cdf0e10cSrcweir 	private final String newsgroupText = "Newsgroup name";
63cdf0e10cSrcweir 	private final String hostText = "Newsgroup host/server name";
64cdf0e10cSrcweir 	private final String replyText = "Email address to reply to";
65cdf0e10cSrcweir 	private final String subjectText = "Subject title for the mail";
66cdf0e10cSrcweir 	private final String commentText = "Additional comment on mail";
67cdf0e10cSrcweir 	private final String officeHtmlText = "Post as both Office and HTML attachments";
68cdf0e10cSrcweir 	private final String officeText = "Post as Office attachment only";
69cdf0e10cSrcweir 	private final String htmlText = "Post as HTML attachment only";
70cdf0e10cSrcweir 	private final String postText = "Post to newsgroup";
71cdf0e10cSrcweir 	private final String cancelText = "Cancel post to newsgroup";
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 
post( XScriptContext xsc )74cdf0e10cSrcweir 	public void post( XScriptContext xsc )
75cdf0e10cSrcweir 	{
76cdf0e10cSrcweir 		xscriptcontext = xsc;
77cdf0e10cSrcweir 		window = this;
78cdf0e10cSrcweir 
79cdf0e10cSrcweir 		// create mailcap and mimetypes files (fix for classloader problem)
80cdf0e10cSrcweir 		MimeConfiguration.createFiles( xscriptcontext );
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 		this.setTitle( "Post Document To Newsgroup" );
83cdf0e10cSrcweir 		this.setLocation( FRAMEX, FRAMEY );
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 		this.addFocusListener( new FocusAdapter()
86cdf0e10cSrcweir 		{
87cdf0e10cSrcweir 			public void focusGained( FocusEvent event )
88cdf0e10cSrcweir 			{
89cdf0e10cSrcweir 				System.out.println( "Focus gained" );
90cdf0e10cSrcweir 				window.update( window.getGraphics() );
91cdf0e10cSrcweir 			}
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 			public void focusLost( FocusEvent event )
94cdf0e10cSrcweir                         {
95cdf0e10cSrcweir 				System.out.println( "Focus lost" );
96cdf0e10cSrcweir                         }
97cdf0e10cSrcweir 		});
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 		Container container = getContentPane();
100cdf0e10cSrcweir 		container.setLayout( new GridBagLayout() );;
101cdf0e10cSrcweir 		GridBagConstraints constraints = new GridBagConstraints();
102cdf0e10cSrcweir 		constraints.fill = GridBagConstraints.BOTH;
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 		JPanel labelPanel = constructLabelPanel();
105cdf0e10cSrcweir 		JPanel textPanel = constructTextPanel();
106cdf0e10cSrcweir 		JPanel optionPanel = constructOptionPanel();
107cdf0e10cSrcweir 		JPanel buttonPanel = constructButtonPanel();
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 		constraints.gridx = 0;
110cdf0e10cSrcweir 		constraints.gridy = 0;
111cdf0e10cSrcweir 		constraints.gridwidth = 1;
112cdf0e10cSrcweir 		constraints.gridheight = 3;
113cdf0e10cSrcweir 		constraints.insets = new Insets( 15, 15, 5, 5 );
114cdf0e10cSrcweir 		container.add( labelPanel, constraints );
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 		constraints.gridx = 1;
117cdf0e10cSrcweir 		constraints.gridy = 0;
118cdf0e10cSrcweir 		constraints.gridwidth = 4;
119cdf0e10cSrcweir 		constraints.gridheight = 3;
120cdf0e10cSrcweir 		constraints.insets = new Insets( 15, 5, 5, 15 );
121cdf0e10cSrcweir 		container.add( textPanel, constraints );
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 		constraints.gridx = 0;
124cdf0e10cSrcweir 		constraints.gridy = 3;
125cdf0e10cSrcweir 		constraints.gridwidth = 5;
126cdf0e10cSrcweir 		constraints.gridheight = 1;
127cdf0e10cSrcweir 		constraints.insets = new Insets( 5, 15, 5, 15 );
128cdf0e10cSrcweir 		container.add( optionPanel, constraints );
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 		constraints.gridx = 0;
131cdf0e10cSrcweir 		constraints.gridy = 4;
132cdf0e10cSrcweir 		constraints.gridwidth = 5;
133cdf0e10cSrcweir 		constraints.gridheight = 1;
134cdf0e10cSrcweir 		constraints.insets = new Insets( 5, 5, 5, 5 );
135cdf0e10cSrcweir 		container.add( buttonPanel, constraints );
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 		this.pack();
138cdf0e10cSrcweir 		this.setResizable( false );
139cdf0e10cSrcweir 		this.setVisible( true );
140cdf0e10cSrcweir 	}
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 
constructLabelPanel()143cdf0e10cSrcweir 	private JPanel constructLabelPanel()
144cdf0e10cSrcweir 	{
145cdf0e10cSrcweir 		JLabel newsgroupLabel = new JLabel( "Newsgroup:" );
146cdf0e10cSrcweir 		JLabel hostLabel = new JLabel( "Host:" );
147cdf0e10cSrcweir 		JLabel replyLabel = new JLabel( "Reply:" );
148cdf0e10cSrcweir 		JLabel subjectLabel = new JLabel( "Subject:" );
149cdf0e10cSrcweir 		JLabel commentLabel = new JLabel( "Comment:" );
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 		newsgroupLabel.setToolTipText( newsgroupText );
152cdf0e10cSrcweir 		hostLabel.setToolTipText( hostText );
153cdf0e10cSrcweir 		replyLabel.setToolTipText( replyText );
154cdf0e10cSrcweir 		subjectLabel.setToolTipText( subjectText );
155cdf0e10cSrcweir 		commentLabel.setToolTipText( commentText );
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 		JPanel newsgroupPanel = new JPanel();
158cdf0e10cSrcweir 		newsgroupPanel.setLayout( new BorderLayout() );
159cdf0e10cSrcweir 		newsgroupPanel.add( newsgroupLabel, "West" );
160cdf0e10cSrcweir 		JPanel hostPanel = new JPanel();
161cdf0e10cSrcweir 		hostPanel.setLayout( new BorderLayout() );
162cdf0e10cSrcweir 		hostPanel.add( hostLabel, "West" );
163cdf0e10cSrcweir 		JPanel replyPanel = new JPanel();
164cdf0e10cSrcweir 		replyPanel.setLayout( new BorderLayout() );
165cdf0e10cSrcweir 		replyPanel.add( replyLabel, "West" );
166cdf0e10cSrcweir 		JPanel subjectPanel = new JPanel();
167cdf0e10cSrcweir 		subjectPanel.setLayout( new BorderLayout() );
168cdf0e10cSrcweir 		subjectPanel.add( subjectLabel, "West" );
169cdf0e10cSrcweir 		JPanel commentPanel = new JPanel();
170cdf0e10cSrcweir 		commentPanel.setLayout( new BorderLayout() );
171cdf0e10cSrcweir 		commentPanel.add( commentLabel, "West" );
172cdf0e10cSrcweir 		JPanel emptyPanel = new JPanel();
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 		final int labelWidth = 80;
175cdf0e10cSrcweir 		newsgroupPanel.setPreferredSize( new Dimension( labelWidth, TEXTBOXHEIGHT ) );
176cdf0e10cSrcweir 		hostPanel.setPreferredSize( new Dimension( labelWidth, TEXTBOXHEIGHT ) );
177cdf0e10cSrcweir 		replyPanel.setPreferredSize( new Dimension( labelWidth, TEXTBOXHEIGHT ) );
178cdf0e10cSrcweir 		subjectPanel.setPreferredSize( new Dimension( labelWidth, TEXTBOXHEIGHT ) );
179cdf0e10cSrcweir 		commentPanel.setPreferredSize( new Dimension( labelWidth, TEXTBOXHEIGHT ) );
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 		JPanel panel = new JPanel();
182cdf0e10cSrcweir 		panel.setLayout( new GridBagLayout() );
183cdf0e10cSrcweir 		GridBagConstraints constraints = new GridBagConstraints();
184cdf0e10cSrcweir 		constraints.fill = GridBagConstraints.BOTH;
185cdf0e10cSrcweir 		constraints.insets = new Insets( 5, 5, 5, 5 );
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 		constraints.gridx = 0;
188cdf0e10cSrcweir 		constraints.gridy = 0;
189cdf0e10cSrcweir 		constraints.gridwidth = 1;
190cdf0e10cSrcweir 		constraints.gridheight = 1;
191cdf0e10cSrcweir 		constraints.weightx = constraints.weighty = 0.0;
192cdf0e10cSrcweir 		panel.add( newsgroupPanel, constraints );
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 		constraints.gridx = 0;
195cdf0e10cSrcweir 		constraints.gridy = 1;
196cdf0e10cSrcweir 		constraints.gridwidth = 1;
197cdf0e10cSrcweir 		constraints.gridheight = 1;
198cdf0e10cSrcweir 		panel.add( hostPanel, constraints );
199cdf0e10cSrcweir 
200cdf0e10cSrcweir 		constraints.gridx = 0;
201cdf0e10cSrcweir 		constraints.gridy = 2;
202cdf0e10cSrcweir 		constraints.gridwidth = 1;
203cdf0e10cSrcweir 		constraints.gridheight = 1;
204cdf0e10cSrcweir 		panel.add( replyPanel, constraints );
205cdf0e10cSrcweir 
206cdf0e10cSrcweir 		constraints.gridx = 0;
207cdf0e10cSrcweir 		constraints.gridy = 3;
208cdf0e10cSrcweir 		constraints.gridwidth = 1;
209cdf0e10cSrcweir 		constraints.gridheight = 1;
210cdf0e10cSrcweir 		panel.add( subjectPanel, constraints );
211cdf0e10cSrcweir 
212cdf0e10cSrcweir 		constraints.gridx = 0;
213cdf0e10cSrcweir 		constraints.gridy = 4;
214cdf0e10cSrcweir 		constraints.gridwidth = 1;
215cdf0e10cSrcweir 		constraints.gridheight = 1;
216cdf0e10cSrcweir 		panel.add( commentPanel, constraints );
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 		constraints.gridx = 0;
219cdf0e10cSrcweir 		constraints.gridy = 5;
220cdf0e10cSrcweir 		constraints.gridwidth = 1;
221cdf0e10cSrcweir 		constraints.gridheight = 1;
222cdf0e10cSrcweir 		constraints.weightx = constraints.weighty = 1.0;
223cdf0e10cSrcweir 		panel.add( emptyPanel, constraints );
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 		return panel;
226cdf0e10cSrcweir 	}
227cdf0e10cSrcweir 
228cdf0e10cSrcweir 
constructTextPanel()229cdf0e10cSrcweir 	private JPanel constructTextPanel()
230cdf0e10cSrcweir 	{
231cdf0e10cSrcweir 		hostTextField = new JTextField();
232cdf0e10cSrcweir                 hostTextField.setPreferredSize( new Dimension( TEXTBOXWIDTH, TEXTBOXHEIGHT ) );
233cdf0e10cSrcweir                 hostTextField.setToolTipText( hostText );
234cdf0e10cSrcweir 		hostTextField.setBorder( new EtchedBorder() );
235cdf0e10cSrcweir 
236cdf0e10cSrcweir 		//optionPanel.setBorder( new TitledBorder( new EtchedBorder(), "Document Format" ) );
237cdf0e10cSrcweir 		newsgroupComboBox = getNewsgroupCombo();
238cdf0e10cSrcweir 
239cdf0e10cSrcweir 		replyTextField = new JTextField();
240cdf0e10cSrcweir 		replyTextField.setPreferredSize( new Dimension( TEXTBOXWIDTH, TEXTBOXHEIGHT ) );
241cdf0e10cSrcweir 		replyTextField.setToolTipText( replyText );
242cdf0e10cSrcweir 		replyTextField.setBorder( new EtchedBorder() );
243cdf0e10cSrcweir 
244cdf0e10cSrcweir 		subjectTextField = new JTextField();
245cdf0e10cSrcweir 		subjectTextField.setPreferredSize( new Dimension( TEXTBOXWIDTH, TEXTBOXHEIGHT ) );
246cdf0e10cSrcweir 		subjectTextField.setToolTipText( subjectText );
247cdf0e10cSrcweir 		subjectTextField.setBorder( new EtchedBorder() );
248cdf0e10cSrcweir 
249cdf0e10cSrcweir 		commentTextArea = new JTextArea();
250cdf0e10cSrcweir 		commentTextArea.setPreferredSize( new Dimension( TEXTBOXWIDTH, TEXTAREAHEIGHT ) );
251cdf0e10cSrcweir 		commentTextArea.setToolTipText( commentText );
252cdf0e10cSrcweir 		commentTextArea.setBorder( new EtchedBorder() );
253cdf0e10cSrcweir 
254cdf0e10cSrcweir 		JPanel panel = new JPanel();
255cdf0e10cSrcweir 		panel.setLayout( new GridBagLayout() );
256cdf0e10cSrcweir 		GridBagConstraints constraints = new GridBagConstraints();
257cdf0e10cSrcweir 		constraints.fill = GridBagConstraints.BOTH;
258cdf0e10cSrcweir 		constraints.insets = new Insets( 5, 5, 5, 5 );
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 		constraints.gridx = 0;
261cdf0e10cSrcweir 		constraints.gridy = 0;
262cdf0e10cSrcweir 		constraints.gridwidth = 1;
263cdf0e10cSrcweir 		constraints.gridheight = 1;
264cdf0e10cSrcweir 		panel.add( newsgroupComboBox, constraints );
265cdf0e10cSrcweir 
266cdf0e10cSrcweir 		constraints.gridx = 0;
267cdf0e10cSrcweir 		constraints.gridy = 1;
268cdf0e10cSrcweir 		constraints.gridwidth = 1;
269cdf0e10cSrcweir 		constraints.gridheight = 1;
270cdf0e10cSrcweir 		panel.add( hostTextField, constraints );
271cdf0e10cSrcweir 
272cdf0e10cSrcweir 		constraints.gridx = 0;
273cdf0e10cSrcweir 		constraints.gridy = 2;
274cdf0e10cSrcweir 		constraints.gridwidth = 1;
275cdf0e10cSrcweir 		constraints.gridheight = 1;
276cdf0e10cSrcweir 		panel.add( replyTextField, constraints );
277cdf0e10cSrcweir 
278cdf0e10cSrcweir 		constraints.gridx = 0;
279cdf0e10cSrcweir 		constraints.gridy = 3;
280cdf0e10cSrcweir 		constraints.gridwidth = 1;
281cdf0e10cSrcweir 		constraints.gridheight = 1;
282cdf0e10cSrcweir 		panel.add( subjectTextField, constraints );
283cdf0e10cSrcweir 
284cdf0e10cSrcweir 		constraints.gridx = 0;
285cdf0e10cSrcweir 		constraints.gridy = 4;
286cdf0e10cSrcweir 		constraints.gridwidth = 1;
287cdf0e10cSrcweir 		constraints.gridheight = 2;
288cdf0e10cSrcweir 		panel.add( commentTextArea, constraints );
289cdf0e10cSrcweir 
290cdf0e10cSrcweir 		return panel;
291cdf0e10cSrcweir 	}
292cdf0e10cSrcweir 
293cdf0e10cSrcweir 
getNewsgroupCombo()294cdf0e10cSrcweir 	private JComboBox getNewsgroupCombo()
295cdf0e10cSrcweir 	{
296cdf0e10cSrcweir 		newsgroupComboBox = new JComboBox();
297cdf0e10cSrcweir 		//newsgroupComboBox.setBorder( new EtchedBorder() );
298cdf0e10cSrcweir 
299cdf0e10cSrcweir    		newsgroupComboBox.addActionListener(new ActionListener()
300cdf0e10cSrcweir    		{
301cdf0e10cSrcweir     			public void actionPerformed(ActionEvent e)
302cdf0e10cSrcweir     			{
303cdf0e10cSrcweir 				// when newsgroup is selected
304cdf0e10cSrcweir 				if( subscribedNewsgroups != null )
305cdf0e10cSrcweir 				{
306cdf0e10cSrcweir 					int position = newsgroupComboBox.getSelectedIndex();
307cdf0e10cSrcweir 					if( position != -1 )
308cdf0e10cSrcweir 					{
309cdf0e10cSrcweir 						hostTextField.setText( subscribedNewsgroups[ position ].getHostName() );
310cdf0e10cSrcweir 						newsgroupComboBox.setToolTipText( "Newsgroup name: " + subscribedNewsgroups[ position ].getNewsgroupName() + "  (Host name: " + subscribedNewsgroups[ position ].getHostName() + ")" );
311cdf0e10cSrcweir 					}
312cdf0e10cSrcweir 				}
313cdf0e10cSrcweir 			}
314cdf0e10cSrcweir 		});
315cdf0e10cSrcweir 
316cdf0e10cSrcweir 		NewsGroup groupToSend = null;
317cdf0e10cSrcweir 		SubscribedNewsgroups newsgroups = new SubscribedNewsgroups();
318cdf0e10cSrcweir 		subscribedNewsgroups = newsgroups.getNewsGroups();
319cdf0e10cSrcweir 
320cdf0e10cSrcweir 		// Test for no .mozilla or no subscribed newsgroups
321cdf0e10cSrcweir 		// subscribedNewsgroups = null;
322cdf0e10cSrcweir 
323cdf0e10cSrcweir 		if( subscribedNewsgroups == null )
324cdf0e10cSrcweir 		{
325cdf0e10cSrcweir 			//System.out.println( "Couldn't find any subscibed newsgroups in .mozilla" );
326cdf0e10cSrcweir 			JOptionPane.showMessageDialog( window, "No subscribed newsgroups found in mozilla/netscape profile \nPlease enter newsgroup and host name",
327cdf0e10cSrcweir 							"Newsgroups Information", JOptionPane.INFORMATION_MESSAGE );
328cdf0e10cSrcweir 		}
329cdf0e10cSrcweir 		else
330cdf0e10cSrcweir 		{
331cdf0e10cSrcweir 			// Copy all newsgroups into a vector for comparison
332cdf0e10cSrcweir 			// Alter entries (to include host name) if duplication is found
333cdf0e10cSrcweir 			Vector vector = new Vector( subscribedNewsgroups.length );
334cdf0e10cSrcweir 			for(int i=0; i < subscribedNewsgroups.length; i++ )
335cdf0e10cSrcweir 			{
336cdf0e10cSrcweir 				vector.add( subscribedNewsgroups[i].getNewsgroupName() );
337cdf0e10cSrcweir 			}
338cdf0e10cSrcweir 			// Compare and alter
339cdf0e10cSrcweir 			for(int i=0; i < subscribedNewsgroups.length; i++ )
340cdf0e10cSrcweir 			{
341cdf0e10cSrcweir 				// check if combo box already has a newsgroup with same name
342cdf0e10cSrcweir 				// then add host name to differentiate
343cdf0e10cSrcweir 				for(int j=0; j < subscribedNewsgroups.length; j++ )
344cdf0e10cSrcweir 				{
345cdf0e10cSrcweir 					if( j != i && subscribedNewsgroups[j].getNewsgroupName().equalsIgnoreCase( subscribedNewsgroups[i].getNewsgroupName() ) )
346cdf0e10cSrcweir 					{
347cdf0e10cSrcweir 						vector.set( j, subscribedNewsgroups[j].getNewsgroupName() + "  (" + subscribedNewsgroups[j].getHostName() + ")" );
348cdf0e10cSrcweir 						vector.set( i, subscribedNewsgroups[i].getNewsgroupName() + "  (" + subscribedNewsgroups[i].getHostName() + ")" );
349cdf0e10cSrcweir 					}
350cdf0e10cSrcweir 				}
351cdf0e10cSrcweir 			}
352cdf0e10cSrcweir 			// Copy converted newsgroups from vector to combo box
353cdf0e10cSrcweir 			for(int i=0; i < subscribedNewsgroups.length; i++ )
354cdf0e10cSrcweir 			{
355cdf0e10cSrcweir 				newsgroupComboBox.addItem( vector.elementAt(i) );
356cdf0e10cSrcweir 			}
357cdf0e10cSrcweir 		}// else
358cdf0e10cSrcweir 
359cdf0e10cSrcweir 		newsgroupComboBox.setPreferredSize( new Dimension( TEXTBOXWIDTH, TEXTBOXHEIGHT ) );
360cdf0e10cSrcweir 		newsgroupComboBox.setEditable( true );
361cdf0e10cSrcweir 
362cdf0e10cSrcweir 		return newsgroupComboBox;
363cdf0e10cSrcweir 	}
364cdf0e10cSrcweir 
365cdf0e10cSrcweir 
366cdf0e10cSrcweir 
constructOptionPanel()367cdf0e10cSrcweir 	private JPanel constructOptionPanel()
368cdf0e10cSrcweir 	{
369cdf0e10cSrcweir 		officeHtmlButton = new JRadioButton( "Office and HTML", true );
370cdf0e10cSrcweir 		officeHtmlButton.setToolTipText( officeHtmlText );
371cdf0e10cSrcweir 
372cdf0e10cSrcweir 		officeButton = new JRadioButton( "Office" );
373cdf0e10cSrcweir 		officeButton.setToolTipText( officeText );
374cdf0e10cSrcweir 
375cdf0e10cSrcweir 		htmlButton = new JRadioButton( "HTML" );
376cdf0e10cSrcweir 		htmlButton.setToolTipText( htmlText );
377cdf0e10cSrcweir 
378cdf0e10cSrcweir 		JRadioButton[] rbuttons = { officeHtmlButton, officeButton, htmlButton };
379cdf0e10cSrcweir 		ButtonGroup radioButtonGroup = new ButtonGroup();
380cdf0e10cSrcweir 		for( int i=0; i < rbuttons.length; i++ )
381cdf0e10cSrcweir 		{
382cdf0e10cSrcweir 			radioButtonGroup.add( rbuttons[i] );
383cdf0e10cSrcweir 		}
384cdf0e10cSrcweir 
385cdf0e10cSrcweir 		JPanel optionPanel = new JPanel();
386cdf0e10cSrcweir 		//optionPanel.setLayout( new GridLayout( 1, 3, 20, 0 ) );
387cdf0e10cSrcweir 		optionPanel.setBorder( new TitledBorder( new EtchedBorder(), "Document Format" ) );
388cdf0e10cSrcweir 		optionPanel.setLayout( new GridBagLayout() );
389cdf0e10cSrcweir 		GridBagConstraints constraints = new GridBagConstraints();
390cdf0e10cSrcweir 		constraints.fill = GridBagConstraints.BOTH;
391cdf0e10cSrcweir 
392cdf0e10cSrcweir 		constraints.gridx = 0;
393cdf0e10cSrcweir                 constraints.gridy = 0;
394cdf0e10cSrcweir                 constraints.gridwidth = 1;
395cdf0e10cSrcweir                 constraints.gridheight = 1;
396cdf0e10cSrcweir 		constraints.insets = new Insets( 5, 5, 5, 30 );
397cdf0e10cSrcweir                 optionPanel.add( officeHtmlButton, constraints );
398cdf0e10cSrcweir 
399cdf0e10cSrcweir 		constraints.gridx = 1;
400cdf0e10cSrcweir                 constraints.gridy = 0;
401cdf0e10cSrcweir                 constraints.gridwidth = 1;
402cdf0e10cSrcweir                 constraints.gridheight = 1;
403cdf0e10cSrcweir 		constraints.insets = new Insets( 5, 20, 5, 30 );
404cdf0e10cSrcweir                 optionPanel.add( officeButton, constraints );
405cdf0e10cSrcweir 
406cdf0e10cSrcweir 		constraints.gridx = 2;
407cdf0e10cSrcweir                 constraints.gridy = 0;
408cdf0e10cSrcweir                 constraints.gridwidth = 1;
409cdf0e10cSrcweir                 constraints.gridheight = 1;
410cdf0e10cSrcweir 		constraints.insets = new Insets( 5, 20, 5, 5 );
411cdf0e10cSrcweir                 optionPanel.add( htmlButton, constraints );
412cdf0e10cSrcweir 
413cdf0e10cSrcweir                 return optionPanel;
414cdf0e10cSrcweir 	}
415cdf0e10cSrcweir 
416cdf0e10cSrcweir 
417cdf0e10cSrcweir 
sendingActions()418cdf0e10cSrcweir 	public boolean sendingActions()
419cdf0e10cSrcweir 	{
420cdf0e10cSrcweir 			// posting actions
421cdf0e10cSrcweir 			// Validate the data
422cdf0e10cSrcweir 			if( isValidData() )
423cdf0e10cSrcweir 			{
424cdf0e10cSrcweir 				// Create status window
425cdf0e10cSrcweir 				StatusWindow statusWindow = new StatusWindow( window,  "Posting to Newsgroup", FRAMEX, FRAMEY );
426cdf0e10cSrcweir 
427cdf0e10cSrcweir 				statusWindow.setVisible( true );
428cdf0e10cSrcweir 				//statusWindow.requestFocusInWindow();
429cdf0e10cSrcweir 				statusLine = "Ready to send...";
430cdf0e10cSrcweir 				statusWindow.setStatus( 0, statusLine );
431cdf0e10cSrcweir 
432cdf0e10cSrcweir 				// Get the boolean values for HTML/Office document
433cdf0e10cSrcweir 				// params: ( XScriptContext, StatusWindow, html document, office document )
434cdf0e10cSrcweir 
435cdf0e10cSrcweir 				boolean html = false;
436cdf0e10cSrcweir 				boolean office = false;
437cdf0e10cSrcweir 				if( officeHtmlButton.isSelected() ) { html = true; office = true; }
438cdf0e10cSrcweir 				if( officeButton.isSelected() ) { office = true; html = false; }
439cdf0e10cSrcweir 				if( htmlButton.isSelected() ) { html = true; office = false; }
440cdf0e10cSrcweir 
441cdf0e10cSrcweir 				OfficeAttachment officeAttach = new OfficeAttachment( xscriptcontext, statusWindow, html, office );
442cdf0e10cSrcweir 
443cdf0e10cSrcweir 				statusLine = "Getting user input";
444cdf0e10cSrcweir 				statusWindow.setStatus( 2, statusLine );
445cdf0e10cSrcweir 				// Get replyto, subject, comment from textboxes
446cdf0e10cSrcweir 				String replyto = replyTextField.getText();
447cdf0e10cSrcweir 				String subject = subjectTextField.getText();
448cdf0e10cSrcweir 				String comment = commentTextArea.getText();
449cdf0e10cSrcweir 
450cdf0e10cSrcweir 				// Get newsgroup from combo box (corresponding position)
451cdf0e10cSrcweir 				String host = "";
452cdf0e10cSrcweir 				String group = "";
453cdf0e10cSrcweir 				int position = newsgroupComboBox.getSelectedIndex();
454cdf0e10cSrcweir 				if( subscribedNewsgroups == null || position == -1 )
455cdf0e10cSrcweir 				{
456cdf0e10cSrcweir 					host = hostTextField.getText();
457cdf0e10cSrcweir 					group = newsgroupComboBox.getSelectedItem().toString();
458cdf0e10cSrcweir 				}
459cdf0e10cSrcweir 				else
460cdf0e10cSrcweir 				{
461cdf0e10cSrcweir 					//int position = newsgroupComboBox.getSelectedIndex();
462cdf0e10cSrcweir 					host = subscribedNewsgroups[ position ].getHostName();
463cdf0e10cSrcweir 					group = subscribedNewsgroups[ position ].getNewsgroupName();
464cdf0e10cSrcweir 				}
465cdf0e10cSrcweir 
466cdf0e10cSrcweir 				statusLine = "Creating sender object";
467cdf0e10cSrcweir 				statusWindow.setStatus( 3, statusLine );
468cdf0e10cSrcweir 				Sender sender = new Sender( statusWindow, officeAttach, replyto, subject, comment, host, group );
469cdf0e10cSrcweir 				if( !sender.sendMail() )
470cdf0e10cSrcweir 				{
471cdf0e10cSrcweir 					//System.out.println( "Should end here (?)" );
472cdf0e10cSrcweir 					statusWindow.enableCancelButton( true );
473cdf0e10cSrcweir 					officeAttach.cleanUpOnError();
474cdf0e10cSrcweir 					return false;
475cdf0e10cSrcweir 				}
476cdf0e10cSrcweir 
477cdf0e10cSrcweir 				statusLine = "Send is complete";
478cdf0e10cSrcweir 				statusWindow.setStatus( 14, statusLine );
479cdf0e10cSrcweir 			}
480cdf0e10cSrcweir 			else
481cdf0e10cSrcweir 			{
482cdf0e10cSrcweir 				//System.out.println( "Non valid data" );
483cdf0e10cSrcweir 				return false;
484cdf0e10cSrcweir 			}
485cdf0e10cSrcweir 			return true;
486cdf0e10cSrcweir 	}
487cdf0e10cSrcweir 
488cdf0e10cSrcweir 
constructButtonPanel()489cdf0e10cSrcweir 	private JPanel constructButtonPanel()
490cdf0e10cSrcweir 	{
491cdf0e10cSrcweir 		Action postAction = new AbstractAction() {
492cdf0e10cSrcweir 			public void actionPerformed( ActionEvent event ) {
493cdf0e10cSrcweir 				// posting actions
494cdf0e10cSrcweir 				sendingActions();
495cdf0e10cSrcweir 			}// actionPerformed
496cdf0e10cSrcweir 		};
497cdf0e10cSrcweir 
498cdf0e10cSrcweir 		Action cancelAction = new AbstractAction() {
499cdf0e10cSrcweir                         public void actionPerformed( ActionEvent event ) {
500cdf0e10cSrcweir                                 // cancelling actions
501cdf0e10cSrcweir 				window.dispose();
502cdf0e10cSrcweir                         }
503cdf0e10cSrcweir                 };
504cdf0e10cSrcweir 
505cdf0e10cSrcweir 		postButton = new JButton();
506cdf0e10cSrcweir 		postButton.setAction( postAction );
507cdf0e10cSrcweir 		postButton.setToolTipText( postText );
508cdf0e10cSrcweir 		postButton.setText( "Post" );
509cdf0e10cSrcweir 		postButton.setPreferredSize( new Dimension( BUTTONWIDTH + 20, BUTTONHEIGHT ) );
510cdf0e10cSrcweir 
511cdf0e10cSrcweir 		cancelButton = new JButton();
512cdf0e10cSrcweir 		cancelButton.setAction( cancelAction );
513cdf0e10cSrcweir 		cancelButton.setToolTipText( cancelText );
514cdf0e10cSrcweir 		cancelButton.setText( "Cancel" );
515cdf0e10cSrcweir 		cancelButton.setPreferredSize( new Dimension( BUTTONWIDTH + 20, BUTTONHEIGHT ) );
516cdf0e10cSrcweir 
517cdf0e10cSrcweir 		JSeparator sep = new JSeparator( SwingConstants.HORIZONTAL );
518cdf0e10cSrcweir 
519cdf0e10cSrcweir 		JPanel buttonPanel = new JPanel();
520cdf0e10cSrcweir 		buttonPanel.setLayout( new GridBagLayout() );
521cdf0e10cSrcweir 		GridBagConstraints constraints = new GridBagConstraints();
522cdf0e10cSrcweir 		constraints.fill = GridBagConstraints.BOTH;
523cdf0e10cSrcweir 		constraints.insets = new Insets( 5, 5, 5, 5 );
524cdf0e10cSrcweir 
525cdf0e10cSrcweir 		JPanel emptyPanel1 = new JPanel();
526cdf0e10cSrcweir 		emptyPanel1.setPreferredSize( new Dimension( BUTTONWIDTH, BUTTONHEIGHT ) );
527cdf0e10cSrcweir 
528cdf0e10cSrcweir 		JPanel emptyPanel2 = new JPanel();
529cdf0e10cSrcweir 		emptyPanel2.setPreferredSize( new Dimension( BUTTONWIDTH, BUTTONHEIGHT ) );
530cdf0e10cSrcweir 
531cdf0e10cSrcweir 		constraints.gridx = 0;
532cdf0e10cSrcweir                 constraints.gridy = 0;
533cdf0e10cSrcweir                 constraints.gridwidth = 4;
534cdf0e10cSrcweir                 constraints.gridheight = 1;
535cdf0e10cSrcweir                 buttonPanel.add( sep, constraints );
536cdf0e10cSrcweir 
537cdf0e10cSrcweir 		constraints.gridx = 0;
538cdf0e10cSrcweir                 constraints.gridy = 1;
539cdf0e10cSrcweir                 constraints.gridwidth = 1;
540cdf0e10cSrcweir                 constraints.gridheight = 1;
541cdf0e10cSrcweir                 buttonPanel.add( emptyPanel1, constraints );
542cdf0e10cSrcweir 
543cdf0e10cSrcweir                 constraints.gridx = 1;
544cdf0e10cSrcweir                 constraints.gridy = 1;
545cdf0e10cSrcweir                 constraints.gridwidth = 1;
546cdf0e10cSrcweir                 constraints.gridheight = 1;
547cdf0e10cSrcweir                 buttonPanel.add( emptyPanel2, constraints );
548cdf0e10cSrcweir 
549cdf0e10cSrcweir 		constraints.gridx = 2;
550cdf0e10cSrcweir 		constraints.gridy = 1;
551cdf0e10cSrcweir 		constraints.gridwidth = 1;
552cdf0e10cSrcweir 		constraints.gridheight = 1;
553cdf0e10cSrcweir 		buttonPanel.add( postButton, constraints );
554cdf0e10cSrcweir 
555cdf0e10cSrcweir 		constraints.gridx = 3;
556cdf0e10cSrcweir 		constraints.gridy = 1;
557cdf0e10cSrcweir 		constraints.gridwidth = 1;
558cdf0e10cSrcweir 		constraints.gridheight = 1;
559cdf0e10cSrcweir 		constraints.insets = new Insets( 5, 5, 5, 0 );
560cdf0e10cSrcweir 		buttonPanel.add( cancelButton, constraints );
561cdf0e10cSrcweir 
562cdf0e10cSrcweir                 return buttonPanel;
563cdf0e10cSrcweir 	}
564cdf0e10cSrcweir 
565cdf0e10cSrcweir 
enableButtons( boolean enable )566cdf0e10cSrcweir 	public void enableButtons( boolean enable )
567cdf0e10cSrcweir 	{
568cdf0e10cSrcweir 		if( enable )
569cdf0e10cSrcweir 		{
570cdf0e10cSrcweir 			postButton.setEnabled( true );
571cdf0e10cSrcweir 			cancelButton.setEnabled( true );
572cdf0e10cSrcweir 		}
573cdf0e10cSrcweir 		else
574cdf0e10cSrcweir 		{
575cdf0e10cSrcweir 			postButton.setEnabled( false );
576cdf0e10cSrcweir 			cancelButton.setEnabled( false );
577cdf0e10cSrcweir 		}
578cdf0e10cSrcweir 	}
579cdf0e10cSrcweir 
580cdf0e10cSrcweir 
isValidData()581cdf0e10cSrcweir 	private boolean isValidData()
582cdf0e10cSrcweir 	{
583cdf0e10cSrcweir 		// newsgroupComboBox must not be blank (format? dots and whitespace)
584cdf0e10cSrcweir 		String newsgroupString = "";
585cdf0e10cSrcweir 		int position = newsgroupComboBox.getSelectedIndex();
586cdf0e10cSrcweir 		if( subscribedNewsgroups == null || position == -1 )
587cdf0e10cSrcweir 		{
588cdf0e10cSrcweir 			newsgroupString = newsgroupComboBox.getSelectedItem().toString();
589cdf0e10cSrcweir 		}
590cdf0e10cSrcweir 		else
591cdf0e10cSrcweir 		{
592cdf0e10cSrcweir 			//int position = newsgroupComboBox.getSelectedIndex();
593cdf0e10cSrcweir 			newsgroupString = subscribedNewsgroups[ position ].getNewsgroupName();
594cdf0e10cSrcweir 		}
595cdf0e10cSrcweir 		if( newsgroupString.length() == 0 )
596cdf0e10cSrcweir 		{
597cdf0e10cSrcweir 			//System.out.println( "Please enter a newsgroup name" );
598cdf0e10cSrcweir 			newsgroupComboBox.requestFocus();
599cdf0e10cSrcweir 			JOptionPane.showMessageDialog( window, "Please enter a newsgroup name", "Input Error", JOptionPane.ERROR_MESSAGE );
600cdf0e10cSrcweir 			return false;
601cdf0e10cSrcweir 		}
602cdf0e10cSrcweir 
603cdf0e10cSrcweir 
604cdf0e10cSrcweir 		// hostTextField must not be blank (format?)
605cdf0e10cSrcweir 		String  hostString = hostTextField.getText();
606cdf0e10cSrcweir                 if( hostString.length() == 0 )
607cdf0e10cSrcweir                 {
608cdf0e10cSrcweir 			//System.out.println( "Please enter a hostname" );
609cdf0e10cSrcweir 			hostTextField.requestFocus();
610cdf0e10cSrcweir 			JOptionPane.showMessageDialog( window, "Please enter a hostname", "Input Error", JOptionPane.ERROR_MESSAGE );
611cdf0e10cSrcweir                         return false;
612cdf0e10cSrcweir                 }
613cdf0e10cSrcweir 
614cdf0e10cSrcweir 
615cdf0e10cSrcweir 		// replyTextField must have <string>@<string>.<string>
616cdf0e10cSrcweir 		// (string at least 2 chars long)
617cdf0e10cSrcweir 		// consider <s>.<s>@<s>.<s>.<s> format? (array of dot positons?)
618cdf0e10cSrcweir 		String replyString = replyTextField.getText();
619cdf0e10cSrcweir 		int atPos = replyString.indexOf( "@" );
620cdf0e10cSrcweir 		int dotPos = replyString.lastIndexOf( "." );
621cdf0e10cSrcweir 		int length = replyString.length();
622cdf0e10cSrcweir 		//System.out.println( "length: " + length + "\n atPos: " + atPos + "\n dotPos: " + dotPos );
623cdf0e10cSrcweir 		if( length == 0 || atPos == -1 || dotPos == -1 || atPos < 2 || dotPos < atPos || dotPos + 2 == length || atPos + 2 == dotPos || atPos != replyString.lastIndexOf( "@" ) || replyString.indexOf(" ") != -1 )
624cdf0e10cSrcweir 		{
625cdf0e10cSrcweir 			//System.out.println( "Please enter a valid reply to email address" );
626cdf0e10cSrcweir 			replyTextField.requestFocus();
627cdf0e10cSrcweir 			JOptionPane.showMessageDialog( window, "Please enter a valid reply to email address", "Input Error", JOptionPane.ERROR_MESSAGE );
628cdf0e10cSrcweir 			return false;
629cdf0e10cSrcweir 		}
630cdf0e10cSrcweir 
631cdf0e10cSrcweir 
632cdf0e10cSrcweir 		// subjectTextField must not be blank?
633cdf0e10cSrcweir 		String subjectString = subjectTextField.getText();
634cdf0e10cSrcweir 		if( subjectString.length() == 0 )
635cdf0e10cSrcweir 		{
636cdf0e10cSrcweir 			//System.out.println( "Please enter subject title" );
637cdf0e10cSrcweir 			subjectTextField.requestFocus();
638cdf0e10cSrcweir 			JOptionPane.showMessageDialog( window, "Please enter subject title", "Input Error", JOptionPane.ERROR_MESSAGE );
639cdf0e10cSrcweir 			return false;
640cdf0e10cSrcweir 		}
641cdf0e10cSrcweir 
642cdf0e10cSrcweir 		// details are valid
643cdf0e10cSrcweir 		return true;
644cdf0e10cSrcweir 	}
645cdf0e10cSrcweir 
646cdf0e10cSrcweir }
647