001/*
002 * $Id: TipLoader.java 542 2005-10-10 18:03:15Z rbair $
003 *
004 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005 * Santa Clara, California 95054, U.S.A. All rights reserved.
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 * 
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015 * Lesser General Public License for more details.
016 * 
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020 */
021package org.jdesktop.swingx.tips;
022
023import java.util.ArrayList;
024import java.util.List;
025import java.util.Properties;
026
027/**
028 * Loads tips from Properties.<br>
029 * 
030 * @author <a href="mailto:fred@L2FProd.com">Frederic Lavigne</a>
031 */
032public class TipLoader {
033
034  private TipLoader() { }
035  
036  /**
037   * Initializes a TipOfTheDayModel from properties. Each tip is defined by two
038   * properties, its name and its description:
039   * 
040   * <pre>
041   * <code>
042   * tip.1.name=First Tip
043   * tip.1.description=This is the description
044   *  
045   * tip.2.name=Second Tip
046   * tip.2.description=&lt;html&gt;This is an html description
047   * 
048   * ...
049   * 
050   * tip.10.description=No name for this tip, name is optional
051   * </code>
052   * </pre>
053   * 
054   * @param props
055   * @return a TipOfTheDayModel
056   * @throws IllegalArgumentException
057   *           if a name is found without description
058   */
059  public static TipOfTheDayModel load(Properties props) {
060    List<TipOfTheDayModel.Tip> tips = new ArrayList<TipOfTheDayModel.Tip>();
061
062    int count = 1;
063    while (true) {
064      String nameKey = "tip." + count + ".name";
065      String nameValue = props.getProperty(nameKey);
066
067      String descriptionKey = "tip." + count + ".description";
068      String descriptionValue = props.getProperty(descriptionKey);
069
070      if (nameValue != null && descriptionValue == null) { throw new IllegalArgumentException(
071        "No description for name " + nameValue); }
072
073      if (descriptionValue == null) {
074        break;
075      }
076
077      DefaultTip tip = new DefaultTip(nameValue, descriptionValue);
078      tips.add(tip);
079
080      count++;
081    }
082
083    return new DefaultTipOfTheDayModel(tips);
084  }
085
086}