001 package com.croftsoft.apps.agoracast.c2p; 002 003 import java.awt.Color; 004 import java.awt.Frame; 005 import java.io.*; 006 import javax.swing.*; 007 008 import com.croftsoft.core.lang.NullArgumentException; 009 import com.croftsoft.core.lang.Pair; 010 import com.croftsoft.core.lang.StringLib; 011 import com.croftsoft.core.lang.lifecycle.Commissionable; 012 import com.croftsoft.core.lang.lifecycle.InitializationException; 013 import com.croftsoft.core.util.ArrayLib; 014 import com.croftsoft.core.util.log.Log; 015 import com.croftsoft.core.util.log.PrintStreamLog; 016 017 /********************************************************************* 018 * 019 * <p /> 020 * 021 * @version 022 * 2001-11-09 023 * @since 024 * 2001-08-01 025 * @author 026 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 027 *********************************************************************/ 028 029 public final class AgoracastMediator 030 implements Commissionable, AgoracastModel 031 ////////////////////////////////////////////////////////////////////// 032 ////////////////////////////////////////////////////////////////////// 033 { 034 035 private Log log; 036 037 private AgoracastConfig agoracastConfig; 038 039 private AgoracastNewsrc agoracastNewsrc; 040 041 private AgoracastMemory agoracastMemory; 042 043 private Frame parentFrame; 044 045 private JTabbedPane jTabbedPane; 046 047 ////////////////////////////////////////////////////////////////////// 048 ////////////////////////////////////////////////////////////////////// 049 050 public AgoracastMediator ( ) 051 ////////////////////////////////////////////////////////////////////// 052 { 053 log = PrintStreamLog.SYSTEM_ERR_INSTANCE; 054 } 055 056 ////////////////////////////////////////////////////////////////////// 057 ////////////////////////////////////////////////////////////////////// 058 059 public synchronized void init ( ) 060 ////////////////////////////////////////////////////////////////////// 061 { 062 try 063 { 064 agoracastConfig 065 = AgoracastConfig.load ( AgoracastConstants.CONFIG_FILENAME ); 066 } 067 catch ( FileNotFoundException ex ) 068 { 069 } 070 catch ( Exception ex ) 071 { 072 log.record ( ex ); 073 } 074 075 if ( agoracastConfig == null ) 076 { 077 agoracastConfig = new AgoracastConfig ( ); 078 } 079 080 try 081 { 082 agoracastNewsrc 083 = AgoracastNewsrc.load ( AgoracastConstants.NEWSRC_FILENAME ); 084 } 085 catch ( FileNotFoundException ex ) 086 { 087 } 088 catch ( Exception ex ) 089 { 090 log.record ( ex ); 091 } 092 093 if ( agoracastNewsrc == null ) 094 { 095 agoracastNewsrc = new AgoracastNewsrc ( ); 096 } 097 098 try 099 { 100 agoracastMemory = AgoracastMemory.load ( 101 AgoracastConstants.DATABASE_FILENAME ); 102 } 103 catch ( FileNotFoundException ex ) 104 { 105 } 106 catch ( Exception ex ) 107 { 108 log.record ( ex ); 109 } 110 111 if ( agoracastMemory == null ) 112 { 113 agoracastMemory = new AgoracastMemory ( ); 114 } 115 } 116 117 public synchronized void destroy ( ) 118 ////////////////////////////////////////////////////////////////////// 119 { 120 log = PrintStreamLog.SYSTEM_ERR_INSTANCE; 121 122 try 123 { 124 agoracastConfig.saveIfDirty ( 125 AgoracastConstants.CONFIG_FILENAME ); 126 } 127 catch ( Exception ex ) 128 { 129 log.record ( ex ); 130 } 131 132 try 133 { 134 agoracastNewsrc.saveIfDirty ( 135 AgoracastConstants.NEWSRC_FILENAME ); 136 } 137 catch ( Exception ex ) 138 { 139 log.record ( ex ); 140 } 141 142 try 143 { 144 agoracastMemory.saveIfDirty ( 145 AgoracastConstants.DATABASE_FILENAME ); 146 } 147 catch ( Exception ex ) 148 { 149 log.record ( ex ); 150 } 151 } 152 153 ////////////////////////////////////////////////////////////////////// 154 ////////////////////////////////////////////////////////////////////// 155 156 public Log getLog ( ) 157 ////////////////////////////////////////////////////////////////////// 158 { 159 return log; 160 } 161 162 public AgoracastDatabase getAgoracastDatabase ( ) 163 ////////////////////////////////////////////////////////////////////// 164 { 165 return agoracastMemory; 166 } 167 168 public AgoracastNewsrc getAgoracastNewsrc ( ) 169 ////////////////////////////////////////////////////////////////////// 170 { 171 return agoracastNewsrc; 172 } 173 174 public Frame getParentFrame ( ) 175 ////////////////////////////////////////////////////////////////////// 176 { 177 return parentFrame; 178 } 179 180 ////////////////////////////////////////////////////////////////////// 181 ////////////////////////////////////////////////////////////////////// 182 183 public synchronized void setLog ( Log log ) 184 ////////////////////////////////////////////////////////////////////// 185 { 186 this.log = log; 187 } 188 189 public synchronized void setParentFrame ( Frame parentFrame ) 190 ////////////////////////////////////////////////////////////////////// 191 { 192 this.parentFrame = parentFrame; 193 } 194 195 public synchronized void setJTabbedPane ( JTabbedPane jTabbedPane ) 196 ////////////////////////////////////////////////////////////////////// 197 { 198 NullArgumentException.check ( this.jTabbedPane = jTabbedPane ); 199 200 String server = StringLib.trimToNull ( getServer ( ) ); 201 202 String email = StringLib.trimToNull ( getEmail ( ) ); 203 204 if ( server == null ) 205 { 206 setTabEnabled ( AgoracastConstants.TAB_INDEX_BROWSE , false ); 207 208 setTabEnabled ( AgoracastConstants.TAB_INDEX_DEFAULTS, false ); 209 210 setTabEnabled ( AgoracastConstants.TAB_INDEX_POST , false ); 211 } 212 else if ( email == null ) 213 { 214 setTabEnabled ( AgoracastConstants.TAB_INDEX_DEFAULTS, false ); 215 216 setTabEnabled ( AgoracastConstants.TAB_INDEX_POST , false ); 217 } 218 } 219 220 ////////////////////////////////////////////////////////////////////// 221 ////////////////////////////////////////////////////////////////////// 222 223 public synchronized void setTabEnabled ( 224 int index, 225 boolean enabled ) 226 ////////////////////////////////////////////////////////////////////// 227 { 228 jTabbedPane.setEnabledAt ( index, enabled ); 229 } 230 231 ////////////////////////////////////////////////////////////////////// 232 // interface AgoracastModel accessor methods 233 ////////////////////////////////////////////////////////////////////// 234 235 public String getEmail ( ) 236 ////////////////////////////////////////////////////////////////////// 237 { 238 return agoracastConfig.getEmail ( ); 239 } 240 241 public String getServer ( ) 242 ////////////////////////////////////////////////////////////////////// 243 { 244 return agoracastConfig.getServer ( ); 245 } 246 247 public String getUsername ( ) 248 ////////////////////////////////////////////////////////////////////// 249 { 250 return agoracastConfig.getUsername ( ); 251 } 252 253 public String getNewsgroup ( ) 254 ////////////////////////////////////////////////////////////////////// 255 { 256 String newsgroup 257 = StringLib.trimToNull ( agoracastConfig.getNewsgroup ( ) ); 258 259 return newsgroup != null 260 ? newsgroup : AgoracastConstants.DEFAULT_NEWSGROUP; 261 } 262 263 public String getPassword ( ) 264 ////////////////////////////////////////////////////////////////////// 265 { 266 return agoracastConfig.getPassword ( ); 267 } 268 269 public AgoracastField getAgoracastField ( String name ) 270 ////////////////////////////////////////////////////////////////////// 271 { 272 return agoracastConfig.getAgoracastField ( name ); 273 } 274 275 public AgoracastField [ ] getAgoracastFields ( ) 276 ////////////////////////////////////////////////////////////////////// 277 { 278 return agoracastConfig.getAgoracastFields ( ); 279 } 280 281 public String [ ] getAgoracastFieldNames ( ) 282 ////////////////////////////////////////////////////////////////////// 283 { 284 return agoracastConfig.getAgoracastFieldNames ( ); 285 } 286 287 public String getDefaultDescription ( ) 288 ////////////////////////////////////////////////////////////////////// 289 { 290 return agoracastConfig.getDefaultDescription ( ); 291 } 292 293 public Color getPanelBackgroundColor ( ) 294 ////////////////////////////////////////////////////////////////////// 295 { 296 return agoracastConfig.getPanelBackgroundColor ( ); 297 } 298 299 public Color getTextFieldBackgroundColor ( ) 300 ////////////////////////////////////////////////////////////////////// 301 { 302 return agoracastConfig.getTextFieldBackgroundColor ( ); 303 } 304 305 ////////////////////////////////////////////////////////////////////// 306 // interface AgoracastModel accessor methods 307 ////////////////////////////////////////////////////////////////////// 308 309 public void add ( AgoracastField agoracastField ) 310 ////////////////////////////////////////////////////////////////////// 311 { 312 agoracastConfig.add ( agoracastField ); 313 } 314 315 public void setAgoracastFields ( 316 AgoracastField [ ] agoracastFields ) 317 ////////////////////////////////////////////////////////////////////// 318 { 319 agoracastConfig.setAgoracastFields ( agoracastFields ); 320 } 321 322 public synchronized void setEmail ( String email ) 323 ////////////////////////////////////////////////////////////////////// 324 { 325 email = StringLib.trimToNull ( email ); 326 327 agoracastConfig.setEmail ( email ); 328 329 setTabEnabled ( AgoracastConstants.TAB_INDEX_DEFAULTS, 330 email != null && getServer ( ) != null ); 331 332 setTabEnabled ( AgoracastConstants.TAB_INDEX_POST, 333 email != null && getServer ( ) != null ); 334 } 335 336 public synchronized void setServer ( String server ) 337 ////////////////////////////////////////////////////////////////////// 338 { 339 server = StringLib.trimToNull ( server ); 340 341 agoracastConfig.setServer ( server ); 342 343 setTabEnabled ( AgoracastConstants.TAB_INDEX_BROWSE, 344 server != null ); 345 346 setTabEnabled ( AgoracastConstants.TAB_INDEX_DEFAULTS, 347 server != null && getEmail ( ) != null ); 348 349 setTabEnabled ( AgoracastConstants.TAB_INDEX_POST, 350 server != null && getEmail ( ) != null ); 351 } 352 353 public void setUsername ( String username ) 354 ////////////////////////////////////////////////////////////////////// 355 { 356 agoracastConfig.setUsername ( username ); 357 } 358 359 public void setNewsgroup ( String newsgroup ) 360 ////////////////////////////////////////////////////////////////////// 361 { 362 agoracastConfig.setNewsgroup ( 363 StringLib.trimToNull ( newsgroup ) ); 364 } 365 366 public void setPassword ( String password ) 367 ////////////////////////////////////////////////////////////////////// 368 { 369 agoracastConfig.setPassword ( password ); 370 } 371 372 public AgoracastCategory getAgoracastCategory ( String name ) 373 ////////////////////////////////////////////////////////////////////// 374 { 375 if ( name == null ) 376 { 377 return AgoracastConstants.CATEGORY_ALL; 378 } 379 380 for ( int i = 0; i < AgoracastConstants.CATEGORIES.length; i++ ) 381 { 382 AgoracastCategory agoracastCategory 383 = AgoracastConstants.CATEGORIES [ i ]; 384 385 if ( agoracastCategory.getName ( ).equals ( name ) ) 386 { 387 return agoracastCategory; 388 } 389 } 390 391 return null; 392 } 393 394 ////////////////////////////////////////////////////////////////////// 395 // Miscellaneous 396 ////////////////////////////////////////////////////////////////////// 397 398 // redo this whole mess, isColumn should not be an attribute of AgoracastField 399 400 /* 401 402 public synchronized void setColumnNames ( String [ ] columnNames ) 403 ////////////////////////////////////////////////////////////////////// 404 { 405 AgoracastField [ ] agoracastFields = getAgoracastFields ( ); 406 407 for ( int i = 0; i < agoracastFields.length; i++ ) 408 { 409 String fieldName = agoracastFields [ i ].getName ( ); 410 411 boolean isColumn = ArrayLib.contains ( columnNames, fieldName ); 412 413 setFieldIsColumn ( fieldName, isColumn ); 414 } 415 } 416 417 // redo this whole mess, isColumn should not be an attribute of AgoracastField 418 419 public synchronized void setFieldIsColumn ( 420 String fieldName, 421 boolean isColumn ) 422 ////////////////////////////////////////////////////////////////////// 423 { 424 AgoracastField agoracastField = getAgoracastField ( fieldName ); 425 426 if ( isColumn != agoracastField.isColumn ( ) ) 427 { 428 AgoracastField newAgoracastField = new AgoracastField ( 429 agoracastField.getName ( ), 430 agoracastField.getValue ( ), 431 isColumn, 432 agoracastField.getType ( ), 433 agoracastField.isReverse ( ), 434 agoracastField.getChoices ( ), 435 agoracastField.isPosted ( ), 436 agoracastField.getSemantic ( ) ); 437 438 add ( newAgoracastField ); 439 } 440 } 441 */ 442 443 ////////////////////////////////////////////////////////////////////// 444 ////////////////////////////////////////////////////////////////////// 445 }