QuestionCellEditorModel.java from GridBlocks at Krugle
Show QuestionCellEditorModel.java syntax highlighted
/**
* Copyright (c) 2004
* Helsinki Institute of Physics
* see LICENSE file for details
*
* QuestionCellEditorModel.java
* Created on Nov 20, 2003
*/
package fi.hip.gb.client.ui.treetable;
import java.util.Hashtable;
import javax.swing.BorderFactory;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;
/**
* Cell editor model for {@link JTreeTable}. There is question column and
* answer columns. Question column is filled with default questions, and they
* are shown to user inside combo box. Depending of the selected question,
* different answer choises are available.
*
* @author Juho Karppinen
* @version $Id: QuestionCellEditorModel.java,v 1.2 2003/11/21 11:35:19 jkarppin
* Exp $
*/
public class QuestionCellEditorModel {
/** Model of <code>JTreeTable</code> */
private TreeTableModelAdapter treeModel;
/** combo box containing all questions */
private JComboBox questions;
/**
* Available answers for questions. Questions are keys and their values are
* <code>TableCellEditor</code> objects containing all answers.
*/
private Hashtable answers;
/** answers are only valid for this class naem */
private String nodeClass;
/** questions are on this column */
private int questionCol;
/** answers are on this column */
private int answerCol;
/**
* Construct question editor for table
*
* @param treeTableModel
* model for <code>JTreeTable</code>
* @param nodeClassType
* questions and answers are only shown for this node class
* @param questionColumn
* column number where questions are located
* @param answerColumn
* column number where answers are located
*/
public QuestionCellEditorModel(TreeTableModelAdapter treeTableModel, Class nodeClassType,
int questionColumn, int answerColumn) {
this.treeModel = treeTableModel;
this.nodeClass = nodeClassType.getName();
this.questionCol = questionColumn;
this.answerCol = answerColumn;
this.answers = new Hashtable();
this.questions = new JComboBox();
clear();
}
/**
* Sets question combobox editable or non editable
*
* @param editable
* the editable status
*/
public void setQuestionEditable(boolean editable) {
this.questions.setEditable(editable);
}
/**
* Adds a new scheduling question and its anwers
* to the top of the combobox.
*
* @param question
* name of scheduling question
* @param answers
* available answers for question. If null, no values can be
* selected or written. If some value inside array is empty
* string, the combobox is made editable. Null element inside
* array is considered as empty element.
* @param description
* description for question
*/
public void addSchedulingOption(String question, String[] answers, String description) {
question = question.toUpperCase();
this.questions.insertItemAt(question, 1);
addAnswer(question, answers, description);
}
/**
* Adds a new question and its anwers to the bottom of the combobox.
*
* @param question
* name of question
* @param answers
* available answers for question. If null, no values can be
* selected or written. If some value inside array is empty
* string, the combobox is made editable. Null element inside
* array is considered as empty element.
* @param description
* description for question
*/
public void addQuestion(String question, String[] answers, String description) {
question = question.toUpperCase();
this.questions.addItem(question);
addAnswer(question, answers, description);
}
/**
* Adds new question and its anwers
*
* @param question
* name of question
* @param answers
* available answers for question. If null, no values can be
* selected or written. If some value inside array is empty
* string, the combobox is made editable. Null element inside
* array is considered as empty element.
* @param description
* description for question
*/
private void addAnswer(String question, String[] answers, String description) {
JComboBox values = new JComboBox();
values.setEditable(false);
values.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
values.setAlignmentX(0.f);
values.setAlignmentY(0.f);
if (answers == null) {
values.setEditable(false);
answers = new String[0];
}
for (int k = 0; k < answers.length; k++) {
if (answers[k] == null) {
answers[k] = "";
values.setEditable(false);
} else if (answers[k] == "") {
values.setEditable(true);
}
values.addItem(answers[k]);
}
DefaultCellEditor editor = (answers.length > 0)
? new DefaultCellEditor(values)
: new DefaultCellEditor(new JTextField());
this.answers.put(question, editor);
}
/**
* Removes question from table
*
* @param question
* question name
*/
public void removeQuestion(String question) {
this.answers.remove(question);
this.questions.removeItem(question);
}
/**
* Clear all registered questions and their answers
*/
public void clear() {
this.answers.clear();
this.questions.removeAllItems();
this.questions.addItem("-- scheduling options --");
this.questions.addItem("-- agent's options --");
}
/**
* Get editor component for cell.
* <p>
* Returns list of questions if column is question column and row contains
* component of correct class. In case of answer column, the available
* answers for selected question are returned inside combo box.
*
* @param row
* current row
* @param col
* current column
* @return return editor for table cell, or null if default cell editor
* should be used
*/
public TableCellEditor getEditor(int row, int col) {
String classname = this.treeModel.nodeForRow(row).getClass().getName();
//System.out.println(row + " " + col + " " + classname + " " + this.nodeClass);
if (classname.equals(this.nodeClass)) {
if (col == this.questionCol || col == this.answerCol) {
String question = (String) this.treeModel.getValueAt(row, this.questionCol);
if (question != null) {
// we have already some question on the cell
question = question.toUpperCase();
if (col == this.questionCol) {
// show available questions
if (this.questions.getItemCount() > 0) {
// set current cell value to returned combobox
this.questions.setSelectedItem(question);
return new DefaultCellEditor(this.questions);
}
} else {
// show available answers for selected question
return (TableCellEditor) this.answers.get(question);
}
} else if (this.questions.getItemCount() > 0) {
// only show the available questions
return new DefaultCellEditor(this.questions);
}
}
}
return null;
}
}
See more files for this project here