/* * Copyright (c) 2012 Patrick Meyer * * This program 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. * * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */ package com.itemanalysis.jmetrik.scoring; import com.itemanalysis.jmetrik.selector.MultipleSelectionPanel; import com.itemanalysis.jmetrik.sql.DataTableName; import com.itemanalysis.jmetrik.sql.DatabaseName; import com.itemanalysis.jmetrik.swing.DataTable; import com.itemanalysis.psychometrics.data.VariableAttributes; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.TreeSet; public class ScoringToolDialog extends JDialog{ private JButton okButton; private JButton cancelButton; private JScrollPane scoringTableScrollPane; private DataTable scoringTable; private JTextArea syntaxTextArea; private JScrollPane textScrollPane; private OptionScoreTableModel optionScoreTableModel = null; private ArrayList<OptionScoreKey> keys = null; private ScoringCommand command = null; private boolean canRun = false; private DatabaseName dbName = null; private DataTableName tableName = null; private ArrayList<VariableAttributes> variables = null; private TreeSet<VariableAttributes> scoredVariable = null; private MultipleSelectionPanel selectionPanel = null; /** * If true, variables moved from unselectedVariableList to selectedList. * If false, variables moved from selectedList to unselectedVariableList. */ private boolean selectVariables = true; /** Creates new form ScoringToolDialog */ public ScoringToolDialog(JFrame parent, DatabaseName dbName, DataTableName tableName, ArrayList<VariableAttributes> variables) { super(parent, "Advanced Item Scoring", true); this.dbName = dbName; this.tableName = tableName; this.variables = variables; scoredVariable = new TreeSet<VariableAttributes>(); keys = new ArrayList<OptionScoreKey>(); initComponents(); setLocationRelativeTo(parent); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { scoringTableScrollPane = new JScrollPane(); scoringTable = new DataTable(); okButton = new JButton("OK"); okButton.addActionListener(new OkActionListener()); cancelButton = new JButton("Cancel"); cancelButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { setVisible(false); } }); textScrollPane = new JScrollPane(); syntaxTextArea = new JTextArea(); optionScoreTableModel = new OptionScoreTableModel(); selectionPanel = new MultipleSelectionPanel(); // VariableType filterType = new VariableType(VariableType.NO_FILTER, VariableType.NO_FILTER); // selectionPanel.addUnselectedFilterType(filterType); // selectionPanel.addSelectedFilterType(filterType); selectionPanel.setVariables(variables); selectionPanel.setUnselectedListCellRenderer(new ScoredItemListCellRenderer()); selectionPanel.getButton1().setText("Submit"); selectionPanel.getButton1().addActionListener(new SubmitButtonActionListener()); selectionPanel.getButton2().setText("Reset"); selectionPanel.getButton2().addActionListener(new ClearActionListener()); selectionPanel.getButton3().setVisible(false); selectionPanel.getButton4().setVisible(false); setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE); scoringTable.setModel(optionScoreTableModel); scoringTableScrollPane.setViewportView(scoringTable); textScrollPane.setBorder(BorderFactory.createTitledBorder("Scoring Syntax")); syntaxTextArea.setColumns(20); syntaxTextArea.setEditable(false); syntaxTextArea.setRows(5); textScrollPane.setViewportView(syntaxTextArea); GroupLayout layout = new GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(254, 254, 254) .addComponent(okButton, GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(cancelButton, GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE) .addGap(251, 251, 251)) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(scoringTableScrollPane, GroupLayout.PREFERRED_SIZE, 229, GroupLayout.PREFERRED_SIZE) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(selectionPanel, GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE) .addContainerGap()) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(textScrollPane, GroupLayout.DEFAULT_SIZE, 603, Short.MAX_VALUE) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(selectionPanel, GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE) .addComponent(scoringTableScrollPane, 0, 0, Short.MAX_VALUE)) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(textScrollPane, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(cancelButton) .addComponent(okButton)) .addContainerGap()) ); pack(); }// </editor-fold> public boolean canRun(){ return canRun; } public ScoringCommand getCommand(){ return command; } private void resetLists(){ selectionPanel.reset(); selectionPanel.repaintLists(); } class OkActionListener implements ActionListener{ public void actionPerformed(ActionEvent e) { command = new ScoringCommand(); command.getPairedOptionList("data").addValue("db", dbName.getName()); command.getPairedOptionList("data").addValue("table", tableName.getTableName()); int size = keys.size(); if(size==0){ JOptionPane.showMessageDialog(ScoringToolDialog.this, "You must provide scoring for one or more variables.", "No Scoring Provided", JOptionPane.ERROR_MESSAGE); }else{ command.getFreeOption("keys").add(keys.size()); for(OptionScoreKey k : keys){ command.getPairedOptionList(k.getKeyName()).addValue("options", k.getOptions()); command.getPairedOptionList(k.getKeyName()).addValue("scores", k.getScores()); command.getPairedOptionList(k.getKeyName()).addValue("variables", k.getVariableNames()); if(!k.getOmitCode().trim().equals("")){ command.getPairedOptionList(k.getKeyName()).addValue("omit", k.getOmitCode()); } if(!k.getNotReachedCode().trim().equals("")){ command.getPairedOptionList(k.getKeyName()).addValue("nr", k.getNotReachedCode()); } } canRun = true; setVisible(false); } } } class SubmitButtonActionListener implements ActionListener{ public void actionPerformed(ActionEvent e) { VariableAttributes[] varAttr = selectionPanel.getSelectedVariables(); int size = varAttr.length; if(size==0){ JOptionPane.showMessageDialog(ScoringToolDialog.this, "You must select one or more variables. \n " + "Select variables from the list.", "No Variables Selected", JOptionPane.ERROR_MESSAGE); }else{ String varString = "variables = ("; ArrayList<String> varNames = new ArrayList<String>(); for(int i=0;i<size;i++){ scoredVariable.add(varAttr[i]); varNames.add(varAttr[i].getName().toString()); varString += varAttr[i].getName().toString() + ", "; } varString = varString.trim(); varString = varString.substring(0, varString.length()-1); // OptionScoreKey scoreKey = new OptionScoreKey("key" + (keys.size()+1), // optionScoreTableModel.getOptionString(), // optionScoreTableModel.getScoreString(), // varNames); OptionScoreKey scoreKey = new OptionScoreKey("key" + (keys.size()+1), optionScoreTableModel, varNames); keys.add(scoreKey); String omit = scoreKey.getOmitCode().trim(); String notReached = scoreKey.getNotReachedCode().trim(); String scoringText = "options = " + scoreKey.getOptions(); scoringText += ", scores = " + scoreKey.getScores(); if(!omit.equals("")) scoringText += ", omit = " + omit; if(!notReached.equals("")) scoringText += ", nr = " + notReached; // scoringText += ", scores = " + scoreKey.getScores(); scoringText += ", " + varString + ");"; String syntax = syntaxTextArea.getText(); syntaxTextArea.setText(syntax + scoringText + "\n"); optionScoreTableModel.clearAll(); resetLists(); } } } class ClearActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { syntaxTextArea.setText(""); keys.clear(); optionScoreTableModel.clearAll(); scoredVariable.clear(); resetLists(); } } class ScoredItemListCellRenderer extends DefaultListCellRenderer{ public ScoredItemListCellRenderer(){ } public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus){ Font labelFont = UIManager.getFont("Label.font"); JLabel label = (JLabel)super.getListCellRendererComponent(list,value,index,isSelected,cellHasFocus); VariableAttributes v = (VariableAttributes)value; if(scoredVariable.contains(v)){ label.setFont(labelFont.deriveFont(Font.BOLD)) ; }else{ label.setFont(labelFont.deriveFont(Font.PLAIN)); } return this; } } }