/** @author Steve Ebersole */
public class StandardClassLoaderDelegateImpl implements ClassLoaderDelegate {
  /** Singleton access */
  public static final StandardClassLoaderDelegateImpl INSTANCE =
      new StandardClassLoaderDelegateImpl();

  private static final Logger log = LoggerFactory.logger(StandardClassLoaderDelegateImpl.class);

  @Override
  @SuppressWarnings("unchecked")
  public <T> Class<T> classForName(String className) throws ClassLoadingException {
    try {
      ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
      if (contextClassLoader != null) {
        return (Class<T>) Class.forName(className, true, contextClassLoader);
      }
    } catch (Throwable ignore) {
      log.debugf("Unable to locate Class [%s] using TCCL, falling back to HCANN ClassLoader");
    }

    try {
      return (Class<T>) Class.forName(className, true, getClass().getClassLoader());
    } catch (ClassNotFoundException e) {
      throw new ClassLoadingException("Unable to load Class [" + className + "]", e);
    }
  }
}
/**
 * GUI to create or edit a episode
 *
 * @author Bryan Beck
 * @since 27.08.2011
 */
public class CreateEpisodeGUI extends CreateMovieGUI implements ColorInterface {
  /** Logger. */
  private static final Logger LOG = LoggerFactory.logger(CreateEpisodeGUI.class);

  private Episode episode;

  private int seasonNr;
  private int episodeNr;

  /**
   * Create an prefilled GUI to edit a episode
   *
   * @see javax.swing.JDialog#JDialog(java.awt.Frame, boolean)
   * @param outdatedEpisode the episode to edit
   * @param seasonNr the season number to add
   */
  public CreateEpisodeGUI(
      java.awt.Frame parent, boolean modal, final Episode outdatedEpisode, int seasonNr) {
    super(parent, modal, outdatedEpisode, null, false);
    init();
    this.setTitle(bundle.getString("CreateEpisodeGUI.headline.edit"));
    this.episode = outdatedEpisode;
    // GUI mit Werten belegen
    jTextField3.setText(String.valueOf(seasonNr)); // Staffel
    jTextField9.setText(String.valueOf(outdatedEpisode.getEpisodeNumber())); // Episode
  }

  /**
   * Create an GUI to create a new episode
   *
   * @see javax.swing.JDialog#JDialog(java.awt.Frame, boolean)
   */
  public CreateEpisodeGUI(java.awt.Frame parent, boolean modal) {
    super(parent, modal, false);
    init();
  }

  /** Zusammengeführte Initialisierung beider Konstruktoren */
  private void init() {
    initComponents();
    // Episode Tab
    this.getRightPanel().add(jPanel40); // , 4);
  }

  /**
   * This method is called from within the constructor to initialize the form. WARNING: Do NOT
   * modify this code. The content of this method is always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jPanel40 = new javax.swing.JPanel();
    jPanel8 = new javax.swing.JPanel();
    jLabel12 = new javax.swing.JLabel();
    jTextField3 = new javax.swing.JTextField();
    jPanel16 = new javax.swing.JPanel();
    jLabel13 = new javax.swing.JLabel();
    jTextField9 = new javax.swing.JTextField();

    java.util.ResourceBundle bundle =
        java.util.ResourceBundle.getBundle("jMovieManager/swing/resources/MovieManager"); // NOI18N
    jPanel40.setBorder(
        javax.swing.BorderFactory.createTitledBorder(
            javax.swing.BorderFactory.createEtchedBorder(
                null, ColorInterface.list_tree_BorderShadow),
            bundle.getString("CreateEpisodeGUI.jPanel40"))); // NOI18N
    jPanel40.setMaximumSize(new java.awt.Dimension(180, 80));
    jPanel40.setMinimumSize(new java.awt.Dimension(180, 80));
    jPanel40.setPreferredSize(new java.awt.Dimension(180, 80));
    jPanel40.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

    jPanel8.setMaximumSize(new java.awt.Dimension(160, 20));
    jPanel8.setMinimumSize(new java.awt.Dimension(160, 20));
    jPanel8.setLayout(new javax.swing.BoxLayout(jPanel8, javax.swing.BoxLayout.LINE_AXIS));

    jLabel12.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
    jLabel12.setText(bundle.getString("CreateEpisodeGUI.jLabel12")); // NOI18N
    jLabel12.setMaximumSize(new java.awt.Dimension(100, 20));
    jLabel12.setMinimumSize(new java.awt.Dimension(100, 20));
    jLabel12.setPreferredSize(new java.awt.Dimension(100, 20));
    jPanel8.add(jLabel12);

    jTextField3.setDocument(new MaxLengthDocument(2, MaxLengthDocument.INTEGER));
    jTextField3.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    jTextField3.setMinimumSize(new java.awt.Dimension(30, 20));
    jTextField3.setPreferredSize(new java.awt.Dimension(30, 20));
    jPanel8.add(jTextField3);

    jPanel40.add(jPanel8);

    jPanel16.setMaximumSize(new java.awt.Dimension(160, 20));
    jPanel16.setLayout(new javax.swing.BoxLayout(jPanel16, javax.swing.BoxLayout.LINE_AXIS));

    jLabel13.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
    jLabel13.setText(bundle.getString("CreateEpisodeGUI.jLabel13")); // NOI18N
    jLabel13.setMaximumSize(new java.awt.Dimension(100, 20));
    jLabel13.setMinimumSize(new java.awt.Dimension(100, 20));
    jLabel13.setPreferredSize(new java.awt.Dimension(100, 20));
    jPanel16.add(jLabel13);

    jTextField9.setDocument(new MaxLengthDocument(3, MaxLengthDocument.INTEGER));
    jTextField9.setHorizontalAlignment(javax.swing.JTextField.CENTER);
    jTextField9.setMinimumSize(new java.awt.Dimension(30, 20));
    jTextField9.setPreferredSize(new java.awt.Dimension(30, 20));
    jPanel16.add(jTextField9);

    jPanel40.add(jPanel16);

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle(bundle.getString("CreateEpisodeGUI.headline")); // NOI18N

    pack();
  } // </editor-fold>//GEN-END:initComponents

  /**
   * Aktion die ausgeführt werden soll, nachdem der Start Button gedrückt wurde
   *
   * @param evt Das Event, welches ausgelöst wurde
   */
  @Override
  protected void startButtonPressed(java.awt.event.ActionEvent evt)
      throws IllegalArgumentException {
    super.startButtonPressed(evt);
    try {
      seasonNr = Integer.valueOf(jTextField3.getText());
    } catch (NumberFormatException ex) {
      javax.swing.JOptionPane.showMessageDialog(
          this, bundle.getString("CreateEpisodeGUI.error.noSeason"));
      throw new IllegalArgumentException(ex);
    }
    try {
      episodeNr = Integer.valueOf(jTextField9.getText());
    } catch (NumberFormatException ex) {
      javax.swing.JOptionPane.showMessageDialog(
          this, bundle.getString("CreateEpisodeGUI.error.noEpisode"));
      throw new IllegalArgumentException(ex);
    }
  }
  /**
   * Macht die GUI sichtbar und gibt die neu erstellte Episode zurück
   *
   * @return episode Die erstellte bzw. aktualisierte Episode<br>
   *     null, wenn der Dialog abgebrochen wurde
   */
  @Override
  public Episode showGUI() {
    this.setVisible(true);

    if (!dialogCanceled) {
      if (episode == null) {
        episode = new Episode(title, episodeNr);
      }
      updateEpisodeData(episode);
      //            PersistingManager.INSTANCE.setDocumentChanged(true);
      return episode;
    } else {
      return null;
    }
  }

  /**
   * Updates all attributes from the episode
   *
   * @param episode
   */
  protected void updateEpisodeData(Episode episode) {
    super.updateMovieData(episode);
    episode.setEpisodeNumber(episodeNr);
  }

  /** @return the seasonNr */
  public int getSeasonNr() {
    return seasonNr;
  }

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JLabel jLabel12;
  private javax.swing.JLabel jLabel13;
  private javax.swing.JPanel jPanel16;
  private javax.swing.JPanel jPanel40;
  private javax.swing.JPanel jPanel8;
  private javax.swing.JTextField jTextField3;
  private javax.swing.JTextField jTextField9;
  // End of variables declaration//GEN-END:variables
}