/*
 * Copyright © 2022 Luis Andrés Lange <http://javacomm.net>
 *
 * The MIT Licence
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package net.javacomm.multilingual;

import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import net.javacomm.multilingual.schema.COMMAND;
import net.javacomm.multilingual.schema.ISO639;
import net.javacomm.multilingual.schema.KEY;
import net.javacomm.multilingual.schema.LANGUAGE;
import net.javacomm.multilingual.schema.LANGUAGES;



@SuppressWarnings("serial")
public class MultilingualTitledBorder extends TitledBorder implements Babelfish {

  private ISO639 language;
  private KEY key;



  /**
   * 
   * 
   * @param text
   *             der Rahmentext
   */
  public MultilingualTitledBorder(String text) {
    super(text);
  }



  /**
   * 
   * 
   * @param key
   *               der Zugriffsschlüssel auf den Rahmentext
   * @param border
   *               der Rahmen
   */
  public MultilingualTitledBorder(KEY key, Border border) {
    super(border);
    setLanguage(this.key, ISO639.DE);
  }



  /**
   * 
   * @param key
   *            der Zugriffsschlüssel
   */
  public MultilingualTitledBorder(KEY key) {
    this(key, ISO639.DE);
  }



  /**
   * Ein Label wird in einer bestimmten Sprache erzeugt.
   *
   * @param key
   *                 der Zugriffsschlüssel
   * @param language
   *                 die angezeigte Sprache
   */
  public MultilingualTitledBorder(KEY key, ISO639 language) {
    this("");
    setLanguage(key, language);
  }



  /**
   *
   * @param key
   *            der Zugriffschlüssel
   *
   */
  public void setKey(KEY key) {
    setLanguage(key, language);
  }



  /**
   * Die Sprache wird gesetzt.
   *
   * @param language
   *                 die Sprache
   */
  @Override
  public void setLanguage(ISO639 language) {
    setLanguage(this.key, language);
  }



  /**
   * Die Sprache darf jederzeit geändert werden.
   *
   * @param key
   *                 der Schlüssel für das ausgewählte Kommando
   * @param language
   *                 der Text wird in dieser Sprache angezeigt
   */
  public void setLanguage(KEY key, ISO639 language) {
    if (key == null) throw new IllegalArgumentException("key ist null");
    if (language == null) throw new IllegalArgumentException("language ist null");
    this.language = language;
    this.key = key;
    COMMAND command = Multilingual.getInstance().getCommand(this.key);
    LANGUAGES languages = command.getLANGUAGES();
    if (languages == null) return;
    for (LANGUAGE tmp : languages.getLANGUAGE()) {
      if (tmp.getISO639() != language) continue;
      if (tmp.getTEXT() != null) {
        setTitle(tmp.getTEXT());
      }
      break;
    }
  }
}
