/** @param results */
  private void sortAndDisplayTokens(ArrayList<Result> results, TokenStudentPanel panel, int limit) {
    Collections.sort(
        results,
        new Comparator<Result>() {

          public int compare(Result r1, Result r2) {
            String creator1 = r1.getCreator().toLowerCase();
            String creator2 = r2.getCreator().toLowerCase();
            return Collator.getInstance().compare(creator1, creator2);
          }
        });
    panel.updateTokens(results, limit, true);
  }
  public void initialize(ISocialManager manager, Sheet sheet, Role role) {
    this.manager = manager;
    this.sheet = sheet;
    int maxLessonTokens = ((TokenSheet) sheet.getDetails()).getMaxLessonTokens();
    int maxStudents = ((TokenSheet) sheet.getDetails()).getMaxStudents();
    this.maxLessonLimit = maxLessonTokens * maxStudents;
    this.maxUnitLimit = ((TokenSheet) sheet.getDetails()).getMaxUnitTokens();

    String currentLesson = null;
    try {
      currentLesson = manager.getCurrentInstance().getLesson().getId();
    } catch (IOException ex) {
      Logger.getLogger(StudentUnitTokenView.class.getName()).log(Level.SEVERE, null, ex);
    }
    manager.addResultListener(sheet.getId(), this);
    try {

      // Add the current lesson results in the view.
      GridLayout gl = (GridLayout) getLayout();
      currentLessonPanel = new TokenStudentPanel(manager, sheet);
      String lessonName = manager.getCurrentInstance().getLesson().getName();
      currentLabel = new JLabel(currentLessonPanel.getImageIcon());
      currentLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 18));
      currentLabel.setHorizontalTextPosition(JLabel.CENTER);
      currentLabel.setText(lessonName);

      int lessonNameWidth =
          currentLabel.getFontMetrics(currentLabel.getFont()).stringWidth(lessonName) + 15;
      if (role != Role.STUDENT) {
        rows++;
        gl.setRows(rows);
        this.add(currentLabel);
        this.setPreferredSize(
            new Dimension(
                currentLessonPanel.getImageIcon().getIconWidth(),
                currentLessonPanel.getImageIcon().getIconHeight() * rows));
        //                List<Sheet> sheets = manager.getCurrentInstance().getSheets();
        //                for (Sheet sheet1 : sheets) {
        //                    if (sheet1.getDetails() instanceof TokenSheet) {
        //                        Collection<Result> results = manager.getResults(sheet1.getId());
        //                        Result myResult = null;
        //                        for (Result result : results) {
        //                            if (result.getCreator().equals(manager.getUsername())) {
        //                                myResult = result;
        //                                currentLessonPanel.updateStudentTokens((TokenResult)
        // result.getDetails());
        //                            }
        //                        }
        //                        if (myResult != null) {
        //                            results.remove(myResult);
        //                        }
        //
        //                    }
        //                }
        ArrayList<Result> results = (ArrayList<Result>) manager.getResults(sheet.getId());
        sortAndDisplayTokens(results, currentLessonPanel, maxLessonLimit);
      }

      // Add the unit tokens view.

      unitPanel = new TokenStudentPanel(manager, sheet);
      rows++;
      gl.setRows(rows);
      String unitName = manager.getCurrentInstance().getUnit().getName();
      unitLabel = new JLabel(unitPanel.getImageIcon());
      unitLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 18));
      unitLabel.setHorizontalTextPosition(JLabel.CENTER);
      unitLabel.setText(unitName);

      int unitNameWidth = unitLabel.getFontMetrics(unitLabel.getFont()).stringWidth(unitName) + 15;
      this.add(unitLabel);
      this.setPreferredSize(
          new Dimension(
              unitPanel.getImageIcon().getIconWidth(),
              unitPanel.getImageIcon().getIconHeight() * rows));
      ArrayList<Result> unitTokenResults =
          (ArrayList<Result>) manager.getCurrentUnitResults(sheet.getDetails());
      sortAndDisplayTokens(unitTokenResults, unitPanel, maxUnitLimit);
    } catch (IOException ex) {
      Logger.getLogger(StudentUnitTokenView.class.getName()).log(Level.SEVERE, null, ex);
    }
  }