/** Updates the Rating-Lists */
  private void updateLists() {
    RatingComparator comperator = new RatingComparator();

    Vector<Rating> personalVector = new Vector<Rating>(mPlugin.getDatabase().getPersonalRatings());
    Collections.sort(personalVector, comperator);
    int index = mPersonalRatings.getSelectedIndex();
    mPersonalRatings.setListData(personalVector);
    mPersonalRatings.setSelectedIndex(index);

    Vector<Rating> overallVector = new Vector<Rating>(mPlugin.getDatabase().getServerRatings());
    Collections.sort(overallVector, comperator);
    index = mOverallRatings.getSelectedIndex();
    mOverallRatings.setListData(overallVector);
    mOverallRatings.setSelectedIndex(index);
  }
  /**
   * Accept Programs if rating fits
   *
   * @param program check this program
   * @return true, if rating fits
   */
  public boolean accept(Program program) {
    Rating rating = TVRaterPlugin.getInstance().getRating(program);

    if (rating == null) {
      return false;
    }

    if (mBest
        && rating.getOverallRating() >= mAcceptValues[TVRaterFilterAllCategories.OVERALL_INDEX]
        && rating.getActionRating() >= mAcceptValues[TVRaterFilterAllCategories.ACTION_INDEX]
        && rating.getFunRating() >= mAcceptValues[TVRaterFilterAllCategories.FUN_INDEX]
        && rating.getEroticRating() >= mAcceptValues[TVRaterFilterAllCategories.EROTIC_INDEX]
        && rating.getTensionRating() >= mAcceptValues[TVRaterFilterAllCategories.TENSION_INDEX]
        && rating.getEntitlementRating()
            >= mAcceptValues[TVRaterFilterAllCategories.ENTITLEMENT_INDEX]) {
      return true;
    } else if (!mBest
        && rating.getOverallRating() <= mAcceptValues[TVRaterFilterAllCategories.OVERALL_INDEX]
        && rating.getActionRating() <= mAcceptValues[TVRaterFilterAllCategories.ACTION_INDEX]
        && rating.getFunRating() <= mAcceptValues[TVRaterFilterAllCategories.FUN_INDEX]
        && rating.getEroticRating() <= mAcceptValues[TVRaterFilterAllCategories.EROTIC_INDEX]
        && rating.getTensionRating() <= mAcceptValues[TVRaterFilterAllCategories.TENSION_INDEX]
        && rating.getEntitlementRating()
            <= mAcceptValues[TVRaterFilterAllCategories.ENTITLEMENT_INDEX]) {
      return true;
    }
    return false;
  }
  /** Updates the Database from the Server */
  protected void update() {
    mBtnUpdate.setEnabled(false);
    mPlugin.runUpdate(
        true,
        new Runnable() {

          @Override
          public void run() {
            mBtnUpdate.setEnabled(true);
          }
        });
  }
  /** Creates a DialogRating with the selected Rating */
  protected void view() {
    if ((mTabbedPane.getSelectedIndex() == 0) && (mOverallRatings.getSelectedValue() != null)) {

      DialogRating dlg =
          new DialogRating(
              mPlugin.getParentFrameForTVRater(),
              mPlugin,
              ((Rating) mOverallRatings.getSelectedValue()).getTitle());

      UiUtilities.centerAndShow(dlg);

      updateLists();
    } else if ((mTabbedPane.getSelectedIndex() == 1)
        && (mPersonalRatings.getSelectedValue() != null)) {
      DialogRating dlg =
          new DialogRating(
              mPlugin.getParentFrameForTVRater(),
              mPlugin,
              ((Rating) mPersonalRatings.getSelectedValue()).getTitle());
      UiUtilities.centerAndShow(dlg);
      updateLists();
    }
  }
  /** Creates the GUI */
  private void createGUI() {
    UiUtilities.registerForClosing(this);

    JPanel panel = (JPanel) getContentPane();
    panel.setLayout(new BorderLayout());

    RatingComparator comperator = new RatingComparator();

    Vector<Rating> overallData = new Vector<Rating>(mPlugin.getDatabase().getServerRatings());
    Collections.sort(overallData, comperator);

    mTabbedPane = new JTabbedPane();

    mOverallRatings = new JList(overallData);
    mOverallRatings.setCellRenderer(new RatingCellRenderer());
    mOverallRatings.addMouseListener(
        new MouseAdapter() {

          public void mousePressed(MouseEvent evt) {
            if (evt.isPopupTrigger()) {
              showPopUpMenu(evt);
            }
          }

          public void mouseReleased(MouseEvent evt) {
            if (evt.isPopupTrigger()) {
              showPopUpMenu(evt);
            }
          }

          public void mouseClicked(MouseEvent e) {
            if ((e.getButton() == MouseEvent.BUTTON1) && (e.getClickCount() == 2)) {
              view();
            }
            super.mouseClicked(e);
          }
        });

    mTabbedPane.addTab(
        mLocalizer.msg("overall", "Overall Ratings"), new JScrollPane(mOverallRatings));

    Vector<Rating> personalData = new Vector<Rating>(mPlugin.getDatabase().getPersonalRatings());
    Collections.sort(personalData, comperator);

    mPersonalRatings = new JList(personalData);
    mPersonalRatings.setCellRenderer(new RatingCellRenderer());
    mPersonalRatings.addMouseListener(
        new MouseAdapter() {
          public void mousePressed(MouseEvent evt) {
            if (evt.isPopupTrigger()) {
              showPopUpMenu(evt);
            }
          }

          public void mouseReleased(MouseEvent evt) {
            if (evt.isPopupTrigger()) {
              showPopUpMenu(evt);
            }
          }

          public void mouseClicked(MouseEvent e) {
            if ((e.getButton() == MouseEvent.BUTTON1) && (e.getClickCount() == 2)) {
              view();
            }
            super.mouseClicked(e);
          }
        });

    mTabbedPane.addTab(
        mLocalizer.msg("personal", "Your Ratings"), new JScrollPane(mPersonalRatings));

    panel.add(mTabbedPane, BorderLayout.CENTER);

    JPanel buttonpanel = new JPanel(new GridBagLayout());

    GridBagConstraints c4 = new GridBagConstraints();
    c4.gridwidth = GridBagConstraints.REMAINDER;
    c4.fill = GridBagConstraints.HORIZONTAL;
    c4.weightx = 1;
    c4.anchor = GridBagConstraints.CENTER;

    LinkButton linkButton = new LinkButton("http://tvaddicted.de");
    buttonpanel.add(linkButton, c4);

    GridBagConstraints c = new GridBagConstraints();
    c.weightx = 0;
    c.fill = GridBagConstraints.NONE;
    c.insets = new Insets(5, 5, 5, 5);

    mBtnUpdate = new JButton(mLocalizer.msg("update", "Update"));
    buttonpanel.add(mBtnUpdate, c);
    mBtnUpdate.addActionListener(
        new java.awt.event.ActionListener() {

          public void actionPerformed(ActionEvent e) {
            update();
          }
        });

    GridBagConstraints c2 = new GridBagConstraints();
    c2.weightx = 1;
    c2.fill = GridBagConstraints.HORIZONTAL;

    buttonpanel.add(new JPanel(), c2);

    JButton view = new JButton(mLocalizer.msg("view", "View"));
    buttonpanel.add(view, c);
    view.addActionListener(
        new java.awt.event.ActionListener() {

          public void actionPerformed(ActionEvent e) {
            view();
          }
        });

    JButton close = new JButton(Localizer.getLocalization(Localizer.I18N_CLOSE));
    buttonpanel.add(close, c);
    close.addActionListener(
        new java.awt.event.ActionListener() {

          public void actionPerformed(ActionEvent e) {
            setVisible(false);
          }
        });

    getRootPane().setDefaultButton(close);

    panel.add(buttonpanel, BorderLayout.SOUTH);
  }