001 package com.croftsoft.apps.agoracast.c2p; 002 003 import java.awt.*; 004 import java.awt.event.*; 005 import java.util.*; 006 import javax.swing.*; 007 import javax.swing.event.*; 008 009 import com.croftsoft.core.gui.ButtonPanel2; 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 * 017 * <p /> 018 * 019 * @version 020 * 2002-02-03 021 * @since 022 * 2001-07-25 023 * @author 024 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 025 *********************************************************************/ 026 027 public final class AgoracastPrepPanel 028 extends JPanel 029 implements ActionListener, ChangeListener 030 ////////////////////////////////////////////////////////////////////// 031 ////////////////////////////////////////////////////////////////////// 032 { 033 034 private final AgoracastMediator agoracastMediator; 035 036 private final AgoracastPostPanel agoracastPostPanel; 037 038 // 039 040 private final JComboBox jComboBox; 041 042 private final JLabel categoryDescriptionLabel; 043 044 private final JButton defaultsButton; 045 046 private final JButton fieldsButton; 047 048 private final JButton postButton; 049 050 // 051 052 private String categoryName; 053 054 private AgoracastInputPanel agoracastInputPanel; 055 056 private JScrollPane inputScrollPane; 057 058 ////////////////////////////////////////////////////////////////////// 059 ////////////////////////////////////////////////////////////////////// 060 061 public AgoracastPrepPanel ( 062 AgoracastMediator agoracastMediator, 063 AgoracastPostPanel agoracastPostPanel ) 064 ////////////////////////////////////////////////////////////////////// 065 { 066 super ( new BorderLayout ( ), true ); // isDoubleBuffered 067 068 NullArgumentException.check ( 069 this.agoracastMediator = agoracastMediator ); 070 071 NullArgumentException.check ( 072 this.agoracastPostPanel = agoracastPostPanel ); 073 074 AgoracastLib.setColor ( this, agoracastMediator ); 075 076 JPanel northPanel = new JPanel ( new GridBagLayout ( ), true ); 077 078 GridBagConstraints gridBagConstraints = new GridBagConstraints ( ); 079 080 gridBagConstraints.weightx = 0.0; 081 082 northPanel.add ( new JLabel ( "Category: " ), gridBagConstraints ); 083 084 gridBagConstraints.insets = new Insets ( 0, 8, 0, 0 ); 085 086 gridBagConstraints.gridx = 1; 087 088 gridBagConstraints.weightx = 0.0; 089 090 northPanel.add ( jComboBox 091 = new JComboBox ( AgoracastConstants.CHOICES_CATEGORY ), 092 gridBagConstraints ); 093 094 gridBagConstraints.gridx = 2; 095 096 gridBagConstraints.weightx = 1.0; 097 098 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; 099 100 northPanel.add ( 101 categoryDescriptionLabel = new JLabel ( ), 102 gridBagConstraints ); 103 104 categoryDescriptionLabel.setHorizontalAlignment ( SwingConstants.LEFT ); 105 106 add ( northPanel, BorderLayout.NORTH ); 107 108 jComboBox.addActionListener ( this ); 109 110 defaultsButton = new JButton ( "Defaults" ); 111 112 defaultsButton.addActionListener ( this ); 113 114 fieldsButton = new JButton ( "Add Fields" ); 115 116 fieldsButton.addActionListener ( this ); 117 118 postButton = new JButton ( "Post" ); 119 120 postButton.setEnabled ( false ); 121 122 postButton.addActionListener ( this ); 123 124 add ( new ButtonPanel2 ( new JButton [ ] { 125 defaultsButton, fieldsButton, postButton } ), 126 BorderLayout.SOUTH ); 127 128 String categoryName = ( String ) jComboBox.getSelectedItem ( ); 129 130 setAgoracastCategory ( 131 agoracastMediator.getAgoracastCategory ( categoryName ) ); 132 } 133 134 ////////////////////////////////////////////////////////////////////// 135 ////////////////////////////////////////////////////////////////////// 136 137 public synchronized void setSelectedFieldNames ( 138 String [ ] selectedFieldNames ) 139 ////////////////////////////////////////////////////////////////////// 140 { 141 if ( inputScrollPane != null ) 142 { 143 remove ( inputScrollPane ); 144 } 145 146 java.util.List pairList = null; 147 148 if ( agoracastInputPanel != null ) 149 { 150 pairList = new ArrayList ( ); 151 152 for ( int i = 0; i < selectedFieldNames.length; i++ ) 153 { 154 String name = selectedFieldNames [ i ]; 155 156 String value = agoracastInputPanel.getValue ( name ); 157 158 if ( value != null ) 159 { 160 pairList.add ( new Pair ( name, value ) ); 161 } 162 } 163 } 164 165 agoracastInputPanel = new AgoracastInputPanel ( 166 agoracastMediator, selectedFieldNames, 167 ( ChangeListener ) this ); 168 169 if ( pairList != null ) 170 { 171 Iterator iterator = pairList.iterator ( ); 172 173 while ( iterator.hasNext ( ) ) 174 { 175 Pair pair = ( Pair ) iterator.next ( ); 176 177 agoracastInputPanel.setValue ( pair.name, pair.value ); 178 } 179 } 180 181 inputScrollPane = new JScrollPane ( agoracastInputPanel ); 182 183 add ( inputScrollPane, BorderLayout.CENTER ); 184 185 agoracastInputPanel.setSelectedAll ( true ); 186 187 postButton.setEnabled ( agoracastInputPanel.hasSelectedField ( ) ); 188 189 validate ( ); 190 } 191 192 ////////////////////////////////////////////////////////////////////// 193 ////////////////////////////////////////////////////////////////////// 194 195 public synchronized void stateChanged ( ChangeEvent changeEvent ) 196 ////////////////////////////////////////////////////////////////////// 197 { 198 postButton.setEnabled ( 199 agoracastInputPanel.hasSelectedField ( ) ); 200 } 201 202 public synchronized void actionPerformed ( ActionEvent actionEvent ) 203 ////////////////////////////////////////////////////////////////////// 204 { 205 Object source = actionEvent.getSource ( ); 206 207 if ( source == jComboBox ) 208 { 209 String categoryName = ( String ) jComboBox.getSelectedItem ( ); 210 211 final AgoracastCategory agoracastCategory 212 = agoracastMediator.getAgoracastCategory ( categoryName ); 213 214 setAgoracastCategory ( agoracastCategory ); 215 } 216 else if ( source == defaultsButton ) 217 { 218 agoracastInputPanel.useDefaults ( ); 219 220 postButton.setEnabled ( 221 agoracastInputPanel.hasSelectedField ( ) ); 222 } 223 else if ( source == fieldsButton ) 224 { 225 agoracastPostPanel.addFields ( 226 agoracastInputPanel.getSelectedNames ( ) ); 227 } 228 else if ( source == postButton ) 229 { 230 post ( ); 231 } 232 } 233 234 ////////////////////////////////////////////////////////////////////// 235 ////////////////////////////////////////////////////////////////////// 236 237 private synchronized void setAgoracastCategory ( 238 AgoracastCategory agoracastCategory ) 239 ////////////////////////////////////////////////////////////////////// 240 { 241 categoryName = agoracastCategory.getName ( ); 242 243 String description = agoracastCategory.getDescription ( ); 244 245 categoryDescriptionLabel.setText ( 246 description != null ? description : "" ); 247 248 setSelectedFieldNames ( agoracastCategory.getFieldNames ( ) ); 249 } 250 251 private void post ( ) 252 ////////////////////////////////////////////////////////////////////// 253 { 254 int maxLength = AgoracastConstants.FIELD_NAME_CATEGORY.length ( ); 255 256 String [ ] names = agoracastInputPanel.getSelectedNames ( ); 257 258 Arrays.sort ( names ); 259 260 for ( int i = 0; i < names.length; i++ ) 261 { 262 String name = names [ i ]; 263 264 if ( name.length ( ) > maxLength ) 265 { 266 maxLength = name.length ( ); 267 } 268 } 269 270 StringBuffer stringBuffer = new StringBuffer ( ); 271 272 writeFieldPair ( stringBuffer, maxLength, 273 AgoracastConstants.FIELD_NAME_CATEGORY, categoryName ); 274 275 for ( int i = 0; i < names.length; i++ ) 276 { 277 String name = names [ i ]; 278 279 String value = agoracastInputPanel.getValue ( name ); 280 281 writeFieldPair ( stringBuffer, maxLength, name, value ); 282 } 283 284 // Need to make sure no null values are being passed 285 286 UsenetMessage usenetMessage = new UsenetMessage ( 287 agoracastMediator.getEmail ( ), 288 agoracastMediator.getNewsgroup ( ), 289 categoryName, // subject 290 stringBuffer.toString ( ) ); 291 292 usenetMessage.setHeader ( 293 UsenetMessage.HEADER_FOLLOWUP_TO, "poster" ); 294 295 agoracastPostPanel.describe ( usenetMessage ); 296 } 297 298 private static void writeFieldPair ( 299 StringBuffer stringBuffer, 300 int maxLength, 301 String name, 302 String value ) 303 ////////////////////////////////////////////////////////////////////// 304 { 305 if ( value != null ) 306 { 307 stringBuffer.append ( StringLib.padRight ( 308 name + " ", '.', maxLength + 4 ) ); 309 310 stringBuffer.append ( ": " ); 311 312 stringBuffer.append ( value ); 313 314 stringBuffer.append ( "\r\n" ); 315 } 316 } 317 318 ////////////////////////////////////////////////////////////////////// 319 ////////////////////////////////////////////////////////////////////// 320 }