001         package com.croftsoft.apps.agoracast.c2p;
002    
003         import java.awt.Color;
004         import java.io.*;
005         import java.util.*;
006    
007         import com.croftsoft.core.gui.IdentifierDialog;
008         import com.croftsoft.core.lang.NullArgumentException;
009         import com.croftsoft.core.lang.StringLib;
010         import com.croftsoft.core.lang.Pair;
011         import com.croftsoft.core.text.sml.*;
012         import com.croftsoft.core.util.ArrayLib;
013    
014         /*********************************************************************
015         *
016         * <p />
017         *
018         * @version
019         *   2001-11-09
020         * @since
021         *   2001-07-25
022         * @author
023         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
024         *********************************************************************/
025    
026         public final class  AgoracastConfig
027           implements AgoracastConstants, AgoracastModel
028         //////////////////////////////////////////////////////////////////////
029         //////////////////////////////////////////////////////////////////////
030         {
031    
032         private static final String  SML_NODE_NAME = "agoracast-config";
033    
034         //
035    
036         private String     email;
037    
038         private String     server;
039    
040         private String     username;
041    
042         private String     newsgroup;
043    
044         private SortedMap  nameToFieldMap;
045    
046         //
047    
048         private String   password;
049    
050         private boolean  isDirty;
051    
052         //
053    
054         private IdentifierDialog   identifierDialog;
055    
056         private Color              panelBackgroundColor;
057    
058         private Color              textFieldBackgroundColor;
059    
060         //////////////////////////////////////////////////////////////////////
061         //////////////////////////////////////////////////////////////////////
062    
063         public static AgoracastConfig  load ( String  filename )
064           throws IOException
065         //////////////////////////////////////////////////////////////////////
066         {
067           return fromSmlNode ( SmlNodeLib.load ( filename, false ) );
068         }
069    
070         public static AgoracastConfig  fromSmlNode ( SmlNode  smlNode )
071         //////////////////////////////////////////////////////////////////////
072         {
073           if ( !SML_NODE_NAME.equals ( smlNode.getName ( ) ) )
074           {
075             throw new IllegalArgumentException ( smlNode.getName ( ) );
076           }
077    
078           String  email     = smlNode.getString ( "email"    );
079    
080           String  server    = smlNode.getString ( "server"   );
081    
082           String  username  = smlNode.getString ( "username" );
083    
084           String  newsgroup = smlNode.getString ( "newsgroup" );
085    
086           List  defaultPairList = new ArrayList ( );
087    
088           AgoracastField [ ]  agoracastFields = null;
089    
090           SmlNode [ ]  fieldNodes
091             = smlNode.getChildNodes ( AgoracastField.SML_NODE_NAME );
092    
093           if ( fieldNodes != null
094             && fieldNodes.length > 0 )
095           {
096             List  fieldList = new ArrayList ( fieldNodes.length );
097             
098             for ( int  i = 0; i < fieldNodes.length; i++ )
099             {
100               SmlNode  fieldNode = fieldNodes [ i ];
101    
102               fieldList.add ( AgoracastField.fromSmlNode ( fieldNodes [ i ] ) );
103             }
104    
105             agoracastFields = ( AgoracastField [ ] )
106               fieldList.toArray ( new AgoracastField [ ] { } );
107           }
108    
109           return new AgoracastConfig (
110             email, server, username, newsgroup, agoracastFields, null, null );
111         }
112    
113         //////////////////////////////////////////////////////////////////////
114         //////////////////////////////////////////////////////////////////////
115    
116         public  AgoracastConfig (
117           String              email,
118           String              server,
119           String              username,
120           String              newsgroup,
121           AgoracastField [ ]  agoracastFields,
122           Color               panelBackgroundColor,
123           Color               textFieldBackgroundColor )
124         //////////////////////////////////////////////////////////////////////
125         {
126           this.email     = email;
127    
128           this.server    = server;
129    
130           this.username  = username;
131    
132           this.newsgroup = newsgroup;
133    
134           setAgoracastFields ( agoracastFields );
135    
136           this.panelBackgroundColor     = panelBackgroundColor;
137    
138           this.textFieldBackgroundColor = textFieldBackgroundColor;
139    
140           if ( panelBackgroundColor == null )
141           {
142             this.panelBackgroundColor
143               = AgoracastConstants.DEFAULT_PANEL_BACKGROUND_COLOR;
144           }
145    
146           if ( textFieldBackgroundColor == null )
147           {
148             this.textFieldBackgroundColor
149               = AgoracastConstants.DEFAULT_TEXTFIELD_BACKGROUND_COLOR;
150           }
151    
152           isDirty = false;
153         }
154    
155         public  AgoracastConfig ( )
156         //////////////////////////////////////////////////////////////////////
157         {
158           this ( null, null, null, null, null, null, null );
159         }
160    
161         //////////////////////////////////////////////////////////////////////
162         //////////////////////////////////////////////////////////////////////
163    
164         public String  getEmail     ( ) { return email;     }
165    
166         public String  getServer    ( ) { return server;    }
167    
168         public String  getUsername  ( ) { return username;  }
169    
170         public String  getNewsgroup ( ) { return newsgroup; }
171    
172         public String  getPassword  ( ) { return password;  }
173    
174         public AgoracastField  getAgoracastField ( String  name )
175         //////////////////////////////////////////////////////////////////////
176         {
177           return ( AgoracastField ) nameToFieldMap.get ( name );
178         }
179    
180         public AgoracastField [ ]  getAgoracastFields ( )
181         //////////////////////////////////////////////////////////////////////
182         {
183           return ( AgoracastField [ ] )
184             nameToFieldMap.values ( ).toArray ( new AgoracastField [ ] { } );
185         }
186    
187         public String [ ]  getAgoracastFieldNames ( )
188         //////////////////////////////////////////////////////////////////////
189         {
190           return ( String [ ] )
191             nameToFieldMap.keySet ( ).toArray ( new String [ ] { } );
192         }
193    
194         public String  getDefaultDescription ( )
195         //////////////////////////////////////////////////////////////////////
196         {
197           return null;
198         }
199    
200         public Color  getPanelBackgroundColor ( )
201         //////////////////////////////////////////////////////////////////////
202         {
203           return panelBackgroundColor;
204         }
205    
206         public Color  getTextFieldBackgroundColor ( )
207         //////////////////////////////////////////////////////////////////////
208         {
209           return textFieldBackgroundColor;
210         }
211    
212         //////////////////////////////////////////////////////////////////////
213         //////////////////////////////////////////////////////////////////////
214    
215         public synchronized void  add ( AgoracastField  agoracastField )
216         //////////////////////////////////////////////////////////////////////
217         {
218           isDirty = true;
219    
220           nameToFieldMap.put ( agoracastField.getName ( ), agoracastField );
221         }
222    
223         public synchronized void  setAgoracastFields (
224           AgoracastField [ ]  agoracastFields )
225         //////////////////////////////////////////////////////////////////////
226         {
227           isDirty = true;
228    
229           nameToFieldMap = new TreeMap ( );
230    
231           for ( int  i = 0;
232             i < AgoracastConstants.DEFAULT_FIELDS.length; i++ )
233           {
234             AgoracastField  field = AgoracastConstants.DEFAULT_FIELDS [ i ];
235    
236             nameToFieldMap.put ( field.getName ( ), field );
237           }
238    
239           if ( agoracastFields != null )
240           {
241             for ( int  i = 0; i < agoracastFields.length; i++ )
242             {
243               AgoracastField  agoracastField = agoracastFields [ i ];
244    
245               String  name = agoracastField.getName ( );
246    
247               AgoracastField  defaultAgoracastField
248                 = ( AgoracastField ) nameToFieldMap.get ( name );
249    
250               if ( defaultAgoracastField != null )
251               {
252                 String [ ]  choices = ( String [ ] ) ArrayLib.union (
253                   agoracastField.getChoices ( ),
254                   defaultAgoracastField.getChoices ( ) );
255    
256                 agoracastField = new AgoracastField (
257                   agoracastField.getName     ( ),
258                   agoracastField.getValue    ( ),
259                   agoracastField.getType     ( ),
260                   agoracastField.isReverse   ( ),
261                   choices,
262                   agoracastField.getSemantic ( ) );
263               }
264    
265               nameToFieldMap.put ( name, agoracastField );
266             }
267           }
268         }
269    
270         public synchronized void  setEmail ( String  email )
271         //////////////////////////////////////////////////////////////////////
272         {
273           isDirty = true;
274    
275           this.email = StringLib.trimToNull ( email );
276         }
277    
278         public synchronized void  setServer ( String  server   )
279         //////////////////////////////////////////////////////////////////////
280         {
281           isDirty = true;
282    
283           this.server = StringLib.trimToNull ( server );
284         }
285    
286         public synchronized void  setUsername ( String  username )
287         //////////////////////////////////////////////////////////////////////
288         {
289           isDirty = true;
290    
291           this.username = StringLib.trimToNull ( username );
292         }
293    
294         public synchronized void  setNewsgroup ( String  newsgroup )
295         //////////////////////////////////////////////////////////////////////
296         {
297           isDirty = true;
298    
299           this.newsgroup = StringLib.trimToNull ( newsgroup );
300         }
301    
302         public synchronized void  setPassword ( String  password )
303         //////////////////////////////////////////////////////////////////////
304         {
305           isDirty = true;
306    
307           this.password = StringLib.trimToNull ( password );
308         }
309    
310         //////////////////////////////////////////////////////////////////////
311         //////////////////////////////////////////////////////////////////////
312    
313         public synchronized boolean  saveIfDirty ( String  filename )
314           throws IOException
315         //////////////////////////////////////////////////////////////////////
316         {
317           if ( isDirty )
318           {
319             SmlNodeLib.save ( filename, toSmlNode ( ) );
320    
321             isDirty = false;
322    
323             return true;
324           }
325    
326           return false;
327         }
328    
329         public synchronized SmlNode  toSmlNode ( )
330         //////////////////////////////////////////////////////////////////////
331         {
332           SmlNode  rootSmlNode = new SmlNode ( SML_NODE_NAME );
333    
334           if ( email != null )
335           {
336             rootSmlNode.add ( new SmlNode ( "email", email ) );
337           }
338    
339           if ( server != null )
340           {
341             rootSmlNode.add ( new SmlNode ( "server", server ) );
342           }
343    
344           if ( username != null )
345           {
346             rootSmlNode.add ( new SmlNode ( "username", username ) );
347           }
348    
349           if ( newsgroup != null )
350           {
351             rootSmlNode.add ( new SmlNode ( "newsgroup", newsgroup ) );
352           }
353    
354           AgoracastField [ ]  agoracastFields = getAgoracastFields ( );
355    
356           if ( agoracastFields != null )
357           {
358             for ( int  i = 0; i < agoracastFields.length; i++ )
359             {
360               rootSmlNode.add ( agoracastFields [ i ].toSmlNode ( ) );
361             }
362           }
363    
364           return rootSmlNode;
365         }
366    
367         //////////////////////////////////////////////////////////////////////
368         //////////////////////////////////////////////////////////////////////
369         }