/**
* This file is part of Rydia.
*
* Rydia is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rydia is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Rydia. If not, see <http://www.gnu.org/licenses/>.
**/
package org.rydia.server;
import org.jsurveylib.Survey;
import org.jsurveylib.model.Label;
import org.jsurveylib.model.SurveyElement;
import org.jsurveylib.model.Page;
import org.jsurveylib.model.question.*;
import java.util.ArrayList;
import java.util.List;
/**
* <u><b><font color="red">FOR INTERNAL USE ONLY.</font></b></u>
* Copyright (c)2007, Daniel Kaplan
*
* @author Daniel Kaplan
* @since 7.11.6
*/
public class PageTranslator {
private static final String PAGE = "P";
private static final String[] PREVIOUS_BUTTON_ENABLED = new String[]{"B", "P", "T"};
private static final String[] PREVIOUS_BUTTON_DISABLED = new String[]{"B", "P", "F"};
private static final String[] NEXT_BUTTON_ENABLED = new String[]{"B", "N", "T"};
private static final String[] NEXT_BUTTON_DISABLED = new String[]{"B", "N", "F"};
private static final String[] FINISH_BUTTON_ENABLED = new String[]{"B", "F", "T"};
private static final String[] FINISH_BUTTON_DISABLED = new String[]{"B", "F", "F"};
private static final String LABEL = "L";
private static final String QUESTION = "Q";
private static final String FILE_CHOOSER = "FC";
private static final String CHECKBOX = "CB";
private static final String RADIO_BUTTONS = "RB";
private static final String TEXT_AREA = "TA";
private static final String TEXT_FIELD = "TF";
private static final String DROPDOWN = "DD";
private Survey survey;
public PageTranslator(Survey survey) {
this.survey = survey;
}
public String[][] translate() {
List<String[]> pageTranslation = new ArrayList<String[]>();
Page currentPage = survey.getCurrentPage();
pageTranslation.add(addPage(currentPage, survey.getCurrentPageNumber()));
pageTranslation.add(addPreviousButton());
pageTranslation.add(addNextButton());
pageTranslation.add(addFinishButton());
for (SurveyElement se : currentPage.getSurveyElements()) {
if (se instanceof Label) {
pageTranslation.add(addLabel((Label) se));
}
if (se instanceof Question) {
if (se instanceof FileChooserQuestion) {
pageTranslation.add(addFileChooser((FileChooserQuestion) se));
} else if (se instanceof CheckboxQuestion) {
pageTranslation.add(addCheckbox((CheckboxQuestion) se));
} else if (se instanceof RadioButtonsQuestion) {
pageTranslation.add(addRadioButtons((RadioButtonsQuestion) se));
} else if (se instanceof TextFieldQuestion) {
pageTranslation.add(addTextField((TextFieldQuestion) se));
} else if (se instanceof TextAreaQuestion) {
pageTranslation.add(addTextArea((TextAreaQuestion) se));
} else if (se instanceof DropdownQuestion) {
pageTranslation.add(addDropdown((DropdownQuestion) se));
}
}
}
return pageTranslation.toArray(new String[0][]);
}
private String[] addPage(Page page, int pageNum) {
String [] p = new String[3];
p[0] = PAGE;
p[1] = String.valueOf(pageNum);
p[2] = page.getLabel();
return p;
}
private String[] addDropdown(DropdownQuestion dd) {
List<String> t = new ArrayList<String>();
t.add(QUESTION);
t.add(DROPDOWN);
t.add(dd.getId());
t.add(dd.getLabel().getText());
t.add(dd.isMandatory() ? "T" : "F");
t.add(dd.getAnswer());
t.add(dd.isEnabled() ? "T" : "F");
t.add(dd.isVisible() ? "T" : "F");
t.add(dd.isValid() ? "T" : "F");
t.add(dd.getCurrentValidationMessage());
for (Choice c : dd.getChoices()) {
t.add(c.getId());
t.add(c.getLabel());
}
return t.toArray(new String[0]);
}
private String[] addTextField(TextFieldQuestion tt) {
return new String[]{
QUESTION,
TEXT_FIELD,
tt.getId(),
tt.getLabel().getText(),
tt.isMandatory() ? "T" : "F",
tt.getAnswer(),
tt.isEnabled() ? "T" : "F",
tt.isVisible() ? "T" : "F",
tt.isValid() ? "T" : "F",
tt.getCurrentValidationMessage()
};
}
private String[] addTextArea(TextAreaQuestion tt) {
return new String[]{
QUESTION,
TEXT_AREA,
tt.getId(),
tt.getLabel().getText(),
tt.isMandatory() ? "T" : "F",
tt.getAnswer(),
tt.isEnabled() ? "T" : "F",
tt.isVisible() ? "T" : "F",
tt.isValid() ? "T" : "F",
tt.getCurrentValidationMessage(),
String.valueOf(tt.getRows())
};
}
private String[] addCheckbox(CheckboxQuestion cb) {
return new String[]{
QUESTION,
CHECKBOX,
cb.getId(),
cb.getLabel().getText(),
cb.isMandatory() ? "T" : "F",
cb.getAnswer(),
cb.isEnabled() ? "T" : "F",
cb.isVisible() ? "T" : "F",
cb.isValid() ? "T" : "F",
cb.getCurrentValidationMessage()
};
}
private String[] addRadioButtons(RadioButtonsQuestion rb) {
List<String> t = new ArrayList<String>();
t.add(QUESTION);
t.add(RADIO_BUTTONS);
t.add(rb.getId());
t.add(rb.getLabel().getText());
t.add(rb.isMandatory() ? "T" : "F");
t.add(rb.getAnswer());
t.add(rb.isEnabled() ? "T" : "F");
t.add(rb.isVisible() ? "T" : "F");
t.add(rb.isValid() ? "T" : "F");
t.add(rb.getCurrentValidationMessage());
t.add(rb.isVertical() ? "T" : "F");
for (Choice c : rb.getChoices()) {
t.add(c.getId());
t.add(c.getLabel());
}
return t.toArray(new String[0]);
}
private String[] addFileChooser(FileChooserQuestion fc) {
return new String[]{
QUESTION,
FILE_CHOOSER,
fc.getId(),
fc.getLabel().getText(),
fc.isMandatory() ? "T" : "F",
fc.getAnswer(),
fc.isEnabled() ? "T" : "F",
fc.isVisible() ? "T" : "F",
fc.isValid() ? "T" : "F",
fc.getCurrentValidationMessage()
};
}
private String[] addLabel(Label label) {
List<String> l = new ArrayList<String>();
l.add(LABEL);
l.add(label.getText());
return l.toArray(new String[0]);
}
private String[] addFinishButton() {
if (survey.isLastPageAndComplete()) {
return FINISH_BUTTON_ENABLED;
} else {
return FINISH_BUTTON_DISABLED;
}
}
private String[] addNextButton() {
if (survey.isNextPageAvailable()) {
return NEXT_BUTTON_ENABLED;
} else {
return NEXT_BUTTON_DISABLED;
}
}
private String[] addPreviousButton() {
if (survey.isPreviousPageAvailable()) {
return PREVIOUS_BUTTON_ENABLED;
} else {
return PREVIOUS_BUTTON_DISABLED;
}
}
}