📄 question.java
字号:
return this.choices;
}
/**
* Gets the choice indicated by the index.
*
* @param index The index of the choice to retrieve.
* @return The text of the choice located at the specified index.
*/
public String getChoice(int index) {
if(false == this.isLoaded) {
loadQuestion();
}
return (String)this.choices.get(index);
}
/**
* Sets the text in the choice at a certain index in the <code>ArrayList</code>
* to the text passed to this method. If the choice contains extra HTML, this
* function will make a best effort to strip the unneeded HTML before setting it
* to the choice.
*
* @param index The index in the <code>ArrayList</code> to set a value to.
* @param choice The new value to be stored at the index in the
* <code>ArrayList</code>
*/
public void setChoice(int index, String choice) {
TagPosition t = getTextBetweenTags("body", "/body", choice);
String choiceFragment = choice.substring(t.startPos, t.endPos);
if(false == choiceFragment.equals("")) {
this.choices.set(index, choiceFragment);
} else {
this.choices.set(index, choice);
}
}
/**
* Adds a new choice to the question.
*
* @param choiceText The text to be contained in the choice.
*/
private void addChoice(String choiceText) {
this.choices.add(choiceText);
}
/**
* Sets which choice is correct.
*
* @param index The index of the choice that is correct.
*/
public void setCorrectChoice(int index) {
this.correctChoice = index;
}
/**
* Gets which choice is correct.
*
* @return The index of the choice that is the correct one for this question.
*/
public int getCorrectChoice() {
return this.correctChoice;
}
/**
* Sets the extraText (the text after the question/answers).
*
* @param value The new value to be assigned as the extraText.
*/
public void setExtra(String value) {
this.extraText = value;
}
/**
* Gets the extraText for this question.
*
* @return The String representing the extra text.
*/
public String getExtra() {
return this.extraText;
}
/**
* Gets the multiple feedbacks to the choices for this question.
*
* @return Returns an <code>ArrayList</code> of the feedback for this
* question.
*/
public ArrayList getFeedbacks() {
if(false == this.isLoaded) {
loadQuestion();
}
return this.feedbacks;
}
/**
* Gets the feedback indicated by the index.
*
* @param index The index of the feedback to retrieve.
* @return The string representing the feedback located at the specified index.
*/
public String getFeedback(int index) {
if(false == this.isLoaded) {
loadQuestion();
}
return (String)this.feedbacks.get(index);
}
/**
* Sets the text for the feedback to a choice at a certain index in the <code>ArrayList</code>
* to the text passed to this method. If the choice contains extra HTML, this
* function will make a best effort to strip the unneeded HTML before setting it
* to the choice.
*
* @param index The index in the <code>ArrayList</code> to set a value to.
* @param feedback The new value to be stored at the index in the
* <code>ArrayList</code>
*/
public void setFeedback(int index, String feedback) {
if(false == this.isLoaded) {
loadQuestion();
}
TagPosition t = getTextBetweenTags("body", "/body", feedback);
String feedbackFragment = feedback.substring(t.startPos, t.endPos);
if(false == feedbackFragment.equals("")) {
this.feedbacks.set(index, feedbackFragment);
} else {
this.feedbacks.set(index, feedback);
}
}
/**
* Adds a new feedback for a choice to the question.
*
* @param feedbackText The text contained in the feedback.
*/
public void addFeedback(String feedbackText) {
this.feedbacks.add(feedbackText);
}
/**
* Sets whether or not this file is considered to be 'loaded' or not.
* Be careful with this one, as being 'loaded' is something that is only
* used internally. This likely gets called in save routines and in
* the {@link cdt.projects.Project#createNew createNew} method.
*
* @param loaded If the Item is loaded or not.
*/
public void setIsLoaded(boolean loaded) {
isLoaded = loaded;
}
/**
* Sets up the question with default values based on what the question
* number is.
*
* @param questionNumber The number of the question that this represents.
*/
public void setupDefault(int questionNumber) {
this.choices = new ArrayList();
this.correctChoice = -1;
this.questionText = "<h3>Question #" +questionNumber+ "</h3>\n\n<p>Type question text here</p>";
String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(int i = 0; i < 5; ++i) {
this.choices.add(" " +alpha.charAt(i)+ ". Type Answer " +(i+1)+ "'s text here<br>\n");
this.feedbacks.add("Type feedback to this answer here.");
}
this.isLoaded = true;
}
/**
* Cannot add nodes into this one, so this is left blank.
*
* @param into Not used.
*/
public void addNodeInto(Node into) { }
/**
* Makes a copy of this node. Needed for saving.
*
* @return copy of this node.
*/
public Node copy() {
Question n = new Question(this.getName());
n.setFile(this.getFile());
n.setQuestion(this.getQuestion());
n.setExtra(this.getExtra());
Iterator iter = this.getChoices().iterator();
while(iter.hasNext()) {
String choice = (String)iter.next();
n.addChoice(choice);
}
iter = this.getFeedbacks().iterator();
while(iter.hasNext()) {
String feedback = (String)iter.next();
n.addFeedback(feedback);
}
n.setCorrectChoice(this.getCorrectChoice());
n.setIsLoaded(true);
// n.setData(this.getData());
return n;
}
/**
* Is this node visible or not??
*
* @return true.
*/
public boolean isVisible() {
return true;
}
/**
* Writes the project data to an OutputStream.
*
* @param out OutputStream to send project data to.
* @param dir Indicates the directory of the file this {@link Node Node} represents.
*/
public void writeProject(OutputStream out, String dir) {
try {
String tag = "<question name=\"" +getName()+ "\" file=\"" +getFile()+ "\" correctChoice=\""
+getCorrectChoice()+ "\">\n";
out.write(tag.getBytes());
out.write((new String("</question>\n")).getBytes());
} catch(Exception e) {
cdt.ErrorD.ErrorDlg.ErrorMsg("Error writing project -> " +dir+getFile());
}
}
/**
* Questions cannot have children, as that makes them appear as folders
* defaultly in the tree.
*
* @return false.
*/
public boolean getAllowsChildren() {
return false;
}
/**
* a node has been dropped into this one so add it above this one.
*
* @param n node dropped.
*/
public void dropInto(Node n) {
addNodeAbove(n);
}
/**
* Class for parsing HTML. All it does is contain two indices, one for a
* starting position, and one for an ending position.
*/
private static class TagPosition {
/** Where the text in between the tags starts. */
public final int startPos;
/** Where the text in between the tags ends. */
public final int endPos;
public TagPosition(int start, int end) {
this.startPos = start;
this.endPos = end;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -