001 package com.croftsoft.apps.agoracast.c2p; 002 003 import java.awt.*; 004 import java.awt.event.*; 005 import javax.swing.*; 006 import javax.swing.border.*; 007 008 import com.croftsoft.core.gui.ButtonPanel2; 009 import com.croftsoft.core.gui.LabeledFieldsPanel2; 010 import com.croftsoft.core.lang.NullArgumentException; 011 import com.croftsoft.core.lang.Pair; 012 import com.croftsoft.core.lang.StringLib; 013 import com.croftsoft.core.net.news.UsenetMessage; 014 015 /********************************************************************* 016 * Queries the user for message subject and description. 017 * 018 * <p /> 019 * 020 * @version 021 * 2001-10-12 022 * @since 023 * 2001-07-25 024 * @author 025 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 026 *********************************************************************/ 027 028 public final class AgoracastTextPanel 029 extends JPanel 030 implements ActionListener 031 ////////////////////////////////////////////////////////////////////// 032 ////////////////////////////////////////////////////////////////////// 033 { 034 035 private static final String SUBJECT_FIELD_NAME = "Subject"; 036 037 private final AgoracastMediator agoracastMediator; 038 039 private final AgoracastPostPanel agoracastPostPanel; 040 041 private final LabeledFieldsPanel2 labeledFieldsPanel2; 042 043 private final JTextArea jTextArea; 044 045 private final JButton cancelButton; 046 047 private final JButton postButton; 048 049 // 050 051 private UsenetMessage usenetMessage; 052 053 ////////////////////////////////////////////////////////////////////// 054 ////////////////////////////////////////////////////////////////////// 055 056 public AgoracastTextPanel ( 057 AgoracastMediator agoracastMediator, 058 AgoracastPostPanel agoracastPostPanel ) 059 ////////////////////////////////////////////////////////////////////// 060 { 061 super ( new BorderLayout ( ), true ); // isDoubleBuffered 062 063 NullArgumentException.check ( 064 this.agoracastMediator = agoracastMediator ); 065 066 NullArgumentException.check ( 067 this.agoracastPostPanel = agoracastPostPanel ); 068 069 AgoracastLib.setColor ( this, agoracastMediator ); 070 071 JPanel centerPanel = new JPanel ( new BorderLayout ( ), true ); 072 073 labeledFieldsPanel2 = new LabeledFieldsPanel2 ( 074 new Pair [ ] { new Pair ( SUBJECT_FIELD_NAME, null ) }, 075 agoracastMediator.getPanelBackgroundColor ( ), 076 agoracastMediator.getTextFieldBackgroundColor ( ) ); 077 078 centerPanel.add ( labeledFieldsPanel2, BorderLayout.NORTH ); 079 080 centerPanel.add ( new JScrollPane ( jTextArea = new JTextArea ( ) ), 081 BorderLayout.CENTER ); 082 083 add ( centerPanel, BorderLayout.CENTER ); 084 085 jTextArea.setBorder ( new EmptyBorder ( 4, 4, 4, 4 ) ); 086 087 cancelButton = new JButton ( "Cancel" ); 088 089 cancelButton.addActionListener ( this ); 090 091 postButton = new JButton ( "Post" ); 092 093 postButton.addActionListener ( this ); 094 095 add ( new ButtonPanel2 ( new JButton [ ] { 096 cancelButton, postButton } ), BorderLayout.SOUTH ); 097 098 add ( new JLabel ( AgoracastConstants.DESCRIBE_TEXT ), 099 BorderLayout.NORTH ); 100 } 101 102 ////////////////////////////////////////////////////////////////////// 103 ////////////////////////////////////////////////////////////////////// 104 105 public synchronized void setUsenetMessage ( 106 UsenetMessage usenetMessage ) 107 ////////////////////////////////////////////////////////////////////// 108 { 109 NullArgumentException.check ( this.usenetMessage = usenetMessage ); 110 111 String subject 112 = usenetMessage.getHeader ( UsenetMessage.HEADER_SUBJECT ); 113 114 if ( subject == null ) 115 { 116 subject = ""; 117 } 118 119 labeledFieldsPanel2.setText ( 120 new Pair ( SUBJECT_FIELD_NAME, subject ) ); 121 122 String defaultDescription = StringLib.trimToNull ( 123 agoracastMediator.getDefaultDescription ( ) ); 124 125 if ( defaultDescription != null ) 126 { 127 jTextArea.setText ( defaultDescription ); 128 } 129 else 130 { 131 jTextArea.setText ( "" ); 132 } 133 } 134 135 public synchronized void actionPerformed ( ActionEvent actionEvent ) 136 ////////////////////////////////////////////////////////////////////// 137 { 138 Object source = actionEvent.getSource ( ); 139 140 if ( source == cancelButton ) 141 { 142 agoracastPostPanel.cancel ( ); 143 } 144 else if ( source == postButton ) 145 { 146 String subject 147 = labeledFieldsPanel2.getText ( SUBJECT_FIELD_NAME ); 148 149 if ( ( subject != null ) 150 && !"".equals ( subject.trim ( ) ) ) 151 { 152 subject = AgoracastConstants.SUBJECT_PREFIX + " " + subject; 153 } 154 else 155 { 156 subject = AgoracastConstants.SUBJECT_PREFIX; 157 } 158 159 usenetMessage.setHeader ( UsenetMessage.HEADER_SUBJECT, subject ); 160 161 StringBuffer stringBuffer 162 = new StringBuffer ( usenetMessage.getBody ( ) ); 163 164 String text = StringLib.trimToNull ( jTextArea.getText ( ) ); 165 166 if ( text != null ) 167 { 168 stringBuffer.append ( "\r\n" ); 169 170 stringBuffer.append ( text ); 171 172 stringBuffer.append ( "\r\n" ); 173 } 174 175 stringBuffer.append ( "\r\n" ); 176 177 stringBuffer.append ( AgoracastConstants.FOOTER ); 178 179 usenetMessage.setBody ( stringBuffer.toString ( ) ); 180 181 agoracastPostPanel.confirm ( usenetMessage ); 182 } 183 } 184 185 ////////////////////////////////////////////////////////////////////// 186 ////////////////////////////////////////////////////////////////////// 187 }