Example #1
0
 public static boolean toCsv(String fullyNamedPath, Poll poll) {
   StringBuilder sb = new StringBuilder();
   sb.append(String.format("Questions;Answer;Votes;WeightedVotes;%n"));
   for (Question question : poll.getQuestions()) {
     for (Answer answer : question.getAnswers()) {
       int votesCount = answer.getVotes().size();
       int weightedVotesCount = answer.getVotes().size() * answer.getValue();
       String questionTitle = question.getTitle().replace("\"", "\"\"");
       String answerText = answer.getText().replace("\"", "\"\"");
       sb.append(
           String.format(
               "\"%s\";\"%s\";%s;%s;%n",
               questionTitle, answerText, votesCount, weightedVotesCount));
     }
   }
   return write(fullyNamedPath, sb.toString());
 }
  /**
   * Calling this method will execute the interaction with the given id.
   *
   * @param interactionID The id that specifies the interaction
   * @throws UnknownInteractionException Thrown if interactionID is an unknown id.
   */
  public void interaction(String interactionID) throws UnknownInteractionException {
    // boolean displayAnswer;
    boolean guiBuilded;
    String groupID;
    GroupInfo group;
    Dimension theSize;
    int i;

    if (DEBUG)
      System.out.println("\ninteraction was called\n\tinteractionID=\"" + interactionID + "\"");

    if (!allInteractions.containsKey(interactionID))
      throw (new UnknownInteractionException(
          "Interaction \"" + interactionID + "\" does not exist"));

    if (allInteractions.get(interactionID) instanceof Question) {
      Question aQuestion = (Question) allInteractions.get(interactionID);

      aQuestion.addSubmitListener(this);

      // get group id
      groupID = aQuestion.getGroupID();
      // lookup group infos
      group = (GroupInfo) groupInfos.get(groupID);
      if ((groupID != "") && (group != null)) {
        // if amount of correct questions of group
        // already reached we dont have to
        // continue with this interaction
        if ((group.processed >= group.repeats) && (group.processed != 0) && (group.repeats != 0)) {
          if (DEBUG)
            System.out.println("\t\tNecessary amount of questions of group " + "answered correct.");

          return;
        }
      }

      guiBuilded = aQuestion.getGuiBuilded();
      if (guiBuilded) aQuestion.rebuildQuestion();
      else aQuestion.makeGUI();

      aQuestion.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

      JDialog jdialog = getJDialog(aQuestion.getTitle());
      // theFrame = new JFrame(aQuestion.getTitle());

      aQuestion.setJDialog(jdialog);

      for (i = 0; i < windowListeners.size(); i++)
        jdialog.addWindowListener((WindowListener) (windowListeners.elementAt(i)));

      jdialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
      jdialog.setBackground(new Color(192, 192, 192));
      jdialog.getContentPane().add(aQuestion, BorderLayout.CENTER);
      jdialog.pack();
      theSize = jdialog.getSize();
      jdialog.setSize(350, theSize.height);
      // jdialog.pack();
      jdialog.setLocationRelativeTo(null);
      jdialog.setVisible(true);
      jdialog.requestFocus();
      // theFrame.show();

      // Processing of the interaction will be handled by
      // the event loop.
    } else if (allInteractions.get(interactionID) instanceof Documentation) {
      Documentation doc = (Documentation) allInteractions.get(interactionID);
      doc.makeGUI();
      doc.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

      JFrame theFrame = new JFrame("Documentation");

      theFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

      theFrame.getContentPane().add(doc, BorderLayout.CENTER);

      for (i = 0; i < windowListeners.size(); i++)
        theFrame.addWindowListener((WindowListener) (windowListeners.elementAt(i)));

      theFrame.setSize(400, 500);
      // theFrame.pack();
      theFrame.setLocationRelativeTo(null);
      theFrame.setVisible(true);
      theFrame.requestFocus();
      // theFrame.show();

      // no need to handle anything by the event loop.
    }
    return;
  }