/* * 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.gui; import com.itemanalysis.jmetrik.model.VariableListFilter; import com.itemanalysis.jmetrik.model.VariableListModel; import com.itemanalysis.jmetrik.sql.DataTableName; import com.itemanalysis.jmetrik.sql.DatabaseName; import com.itemanalysis.jmetrik.workspace.*; import com.itemanalysis.psychometrics.data.DataType; import com.itemanalysis.psychometrics.data.ItemType; import com.itemanalysis.psychometrics.data.VariableAttributes; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.util.ArrayList; public class SubsetCasesDialog extends JDialog implements VariableChangeListener{ // Variables declaration - do not modify private JButton andButton; private JButton cancelButton; private JButton clearButton; private JButton equalButton; private JButton greaterThanButton; private JButton greaterThanOrEqualButton; private JButton lessThanButton; private JButton lessThanOrEqualButton; private JScrollPane listScrollPane; private JButton notEqualButton; private JButton okButton; private JButton orButton; private JButton selectButton; private JTextArea sqlTextArea; private JLabel tableLabel; private JTextField tableTextField; private JScrollPane textAreaScrollPane; private JList unselectedVariableList; private boolean canRun = false; private boolean selectVariables = true; // private VariableListFilter unselectedVariableFilter; private VariableListModel unselectedListModel; private SubsetCasesCommand command; private DatabaseName dbName; private DataTableName tableName; // End of variables declaration /** Creates new form SubsetCasesDialog */ public SubsetCasesDialog(JFrame parent, DatabaseName dbName, DataTableName tableName, ArrayList<VariableAttributes> variables) { super(parent, "Subset Cases", true); this.dbName = dbName; this.tableName = tableName; // ArrayList<VariableType> variableFilter = new ArrayList<VariableType>(); // variableFilter.add(new VariableType(VariableType.NO_FILTER, VariableType.NO_FILTER)); // //create list filter and list model // unselectedVariableFilter = new VariableListFilter(); // for(VariableType t : variableFilter){ // unselectedVariableFilter.addFilteredType(t); // } VariableListFilter variableListfilter = new VariableListFilter(DataType.NO_DATATYPE_FILTER, ItemType.NO_ITEMTYPE_FILTER); unselectedListModel = new VariableListModel(variableListfilter); //add variables to unselected list for(VariableAttributes v : variables){ unselectedListModel.addElement(v); } initComponents(); setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE); setResizable(false); 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() { listScrollPane = new JScrollPane(); unselectedVariableList = new JList(); unselectedVariableList.setName("unselectedVariableList"); unselectedVariableList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); unselectedVariableList.addFocusListener(new ListFocusListener()); selectButton = new JButton(); textAreaScrollPane = new JScrollPane(); sqlTextArea = new JTextArea(); sqlTextArea.setName("sqlTextArea"); sqlTextArea.addFocusListener(new ListFocusListener()); lessThanButton = new JButton(); greaterThanButton = new JButton(); lessThanOrEqualButton = new JButton(); greaterThanOrEqualButton = new JButton(); equalButton = new JButton(); notEqualButton = new JButton(); andButton = new JButton(); orButton = new JButton(); tableLabel = new JLabel(); tableTextField = new JTextField(); okButton = new JButton(); cancelButton = new JButton(); clearButton = new JButton(); listScrollPane.setMinimumSize(new Dimension(125, 325)); listScrollPane.setPreferredSize(new Dimension(125, 325)); unselectedVariableList.setModel(unselectedListModel); listScrollPane.setViewportView(unselectedVariableList); selectButton.setText(">"); selectButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(selectVariables){ //add selected variable name to text area at the cursor position int selected = unselectedVariableList.getSelectedIndex(); String varName = unselectedListModel.getElementAt(selected).getName().toString()+" "; int position = sqlTextArea.getCaretPosition(); sqlTextArea.insert(varName, position); sqlTextArea.setCaretPosition(position+varName.length()); sqlTextArea.requestFocusInWindow(); }else{ //delete highlighted text String selectedText = sqlTextArea.getSelectedText(); if(selectedText!=null){ String temp = sqlTextArea.getText(); int start = sqlTextArea.getSelectionStart(); int end = sqlTextArea.getSelectionEnd(); String newText = temp.substring(0, start); newText += temp.substring(end, temp.length()); sqlTextArea.setText(newText); sqlTextArea.setCaretPosition(start); sqlTextArea.requestFocusInWindow(); }else{ sqlTextArea.setCaretPosition(sqlTextArea.getCaretPosition()); sqlTextArea.requestFocusInWindow(); } } } }); selectButton.setMaximumSize(new Dimension(49, 28)); selectButton.setMinimumSize(new Dimension(49, 28)); selectButton.setPreferredSize(new Dimension(49, 28)); sqlTextArea.setColumns(20); sqlTextArea.setRows(5); textAreaScrollPane.setViewportView(sqlTextArea); lessThanButton.setText("<"); lessThanButton.setToolTipText("Less than"); lessThanButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int position = sqlTextArea.getCaretPosition(); sqlTextArea.insert("< ", position); sqlTextArea.setCaretPosition(position + 2); sqlTextArea.requestFocusInWindow(); } }); lessThanButton.setMaximumSize(new Dimension(49, 28)); lessThanButton.setMinimumSize(new Dimension(49, 28)); lessThanButton.setPreferredSize(new Dimension(49, 28)); greaterThanButton.setText(">"); greaterThanButton.setToolTipText("Greater than"); greaterThanButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int position = sqlTextArea.getCaretPosition(); sqlTextArea.insert("> ", position); sqlTextArea.setCaretPosition(position+2); sqlTextArea.requestFocusInWindow(); } }); greaterThanButton.setMaximumSize(new Dimension(49, 28)); greaterThanButton.setMinimumSize(new Dimension(49, 28)); greaterThanButton.setPreferredSize(new Dimension(49, 28)); lessThanOrEqualButton.setText("<="); lessThanOrEqualButton.setToolTipText("Less than or equal to"); lessThanOrEqualButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int position = sqlTextArea.getCaretPosition(); sqlTextArea.insert("<= ", position); sqlTextArea.setCaretPosition(position+3); sqlTextArea.requestFocusInWindow(); } }); lessThanOrEqualButton.setMaximumSize(new Dimension(49, 28)); lessThanOrEqualButton.setMinimumSize(new Dimension(49, 28)); lessThanOrEqualButton.setPreferredSize(new Dimension(49, 28)); greaterThanOrEqualButton.setText(">="); greaterThanOrEqualButton.setToolTipText("Greater than or equal to"); greaterThanOrEqualButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int position = sqlTextArea.getCaretPosition(); sqlTextArea.insert(">= ", position); sqlTextArea.setCaretPosition(position+3); sqlTextArea.requestFocusInWindow(); } }); greaterThanOrEqualButton.setMaximumSize(new Dimension(49, 28)); greaterThanOrEqualButton.setMinimumSize(new Dimension(49, 28)); greaterThanOrEqualButton.setPreferredSize(new Dimension(49, 28)); equalButton.setText("="); equalButton.setToolTipText("Equal to"); equalButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int position = sqlTextArea.getCaretPosition(); sqlTextArea.insert("= ", position); sqlTextArea.setCaretPosition(position+2); sqlTextArea.requestFocusInWindow(); } }); equalButton.setMaximumSize(new Dimension(49, 28)); equalButton.setMinimumSize(new Dimension(49, 28)); equalButton.setPreferredSize(new Dimension(49, 28)); notEqualButton.setText("<>"); notEqualButton.setToolTipText("Not equal to"); notEqualButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int position = sqlTextArea.getCaretPosition(); sqlTextArea.insert("<> ", position); sqlTextArea.setCaretPosition(position+3); sqlTextArea.requestFocusInWindow(); } }); notEqualButton.setMaximumSize(new Dimension(49, 28)); notEqualButton.setMinimumSize(new Dimension(49, 28)); notEqualButton.setPreferredSize(new Dimension(49, 28)); andButton.setText("AND"); andButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int position = sqlTextArea.getCaretPosition(); sqlTextArea.insert("AND ", position); sqlTextArea.setCaretPosition(position+4); sqlTextArea.requestFocusInWindow(); } }); andButton.setMaximumSize(new Dimension(55, 28)); andButton.setMinimumSize(new Dimension(55, 28)); andButton.setPreferredSize(new Dimension(55, 28)); orButton.setText("OR"); orButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int position = sqlTextArea.getCaretPosition(); sqlTextArea.insert("OR ", position); sqlTextArea.setCaretPosition(position+3); sqlTextArea.requestFocusInWindow(); } }); orButton.setMaximumSize(new Dimension(55, 28)); orButton.setMinimumSize(new Dimension(55, 28)); orButton.setPreferredSize(new Dimension(55, 28)); tableLabel.setText("New Table Name:"); okButton.setText("OK"); okButton.addActionListener(new OkActionListener()); okButton.setMaximumSize(new Dimension(69, 28)); okButton.setMinimumSize(new Dimension(69, 28)); okButton.setPreferredSize(new Dimension(69, 28)); cancelButton.setText("Cancel"); cancelButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { setVisible(false); } }); cancelButton.setMaximumSize(new Dimension(69, 28)); cancelButton.setMinimumSize(new Dimension(69, 28)); cancelButton.setPreferredSize(new Dimension(69, 28)); clearButton.setText("Clear"); clearButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { sqlTextArea.setText(""); sqlTextArea.setCaretPosition(0); sqlTextArea.requestFocusInWindow(); canRun = false; } }); clearButton.setMaximumSize(new Dimension(69, 28)); clearButton.setMinimumSize(new Dimension(69, 28)); clearButton.setPreferredSize(new Dimension(69, 28)); GroupLayout layout = new GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(listScrollPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(selectButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(textAreaScrollPane, GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(lessThanButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(greaterThanButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(andButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(equalButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(notEqualButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addGap(260, 260, 260)) .addGroup(layout.createSequentialGroup() .addComponent(lessThanOrEqualButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(greaterThanOrEqualButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(orButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(okButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(cancelButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(clearButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addComponent(tableTextField, GroupLayout.PREFERRED_SIZE, 259, GroupLayout.PREFERRED_SIZE) .addComponent(tableLabel)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(24, 24, 24) .addComponent(selectButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup() .addComponent(textAreaScrollPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addGap(11, 11, 11) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addGroup(layout.createSequentialGroup() .addComponent(lessThanButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(lessThanOrEqualButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(greaterThanOrEqualButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(equalButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(notEqualButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))) .addComponent(greaterThanButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup() .addComponent(andButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(orButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))) .addGap(30, 30, 30) .addComponent(tableLabel) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addComponent(tableTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(okButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(cancelButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(clearButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))) .addComponent(listScrollPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))) .addContainerGap(22, Short.MAX_VALUE)) ); pack(); }// </editor-fold> public void setVariables(ArrayList<VariableAttributes> variables){ reset(); unselectedListModel.clear(); for(VariableAttributes v : variables){ unselectedListModel.addElement(v); } } public void reset(){ sqlTextArea.setText(""); } public boolean canRun(){ return canRun; } public SubsetCasesCommand getCommand(){ return command; } class OkActionListener implements ActionListener{ public void actionPerformed(ActionEvent e) { command = new SubsetCasesCommand(); command.getPairedOptionList("data").addValue("db", dbName.getName()); command.getPairedOptionList("data").addValue("table", tableName.getTableName()); boolean ready = true; if("".equals(sqlTextArea.getText().trim())){ JOptionPane.showMessageDialog(SubsetCasesDialog.this, "You must provide at least one variable, operator, value statement.", "No Statement Provided", JOptionPane.ERROR_MESSAGE); ready = false; } if("".equals(tableTextField.getText().trim())){ JOptionPane.showMessageDialog(SubsetCasesDialog.this, "You must provide a name for the new table.", "No Table Name Provided", JOptionPane.ERROR_MESSAGE); ready = false; } if(ready){ command.getFreeOption("where").add(sqlTextArea.getText().trim()); command.getFreeOption("newtable").add(tableTextField.getText().trim()); command.getSelectAllOption("options").setSelected("display", true); canRun = true; setVisible(false); } } } public class ListFocusListener implements FocusListener { public void focusGained(FocusEvent e){ String compName = e.getComponent().getName(); if(compName!=null){ if("unselectedVariableList".equals(compName)){ selectButton.setText(">"); selectButton.setToolTipText("Select variable"); selectVariables = true; } if("sqlTextArea".equals(compName)){ selectButton.setText("<"); selectButton.setToolTipText("Remove selection"); selectVariables = false; } } } public void focusLost(FocusEvent e){ //do nothing } } public void variableChanged(VariableChangeEvent e){ VariableAttributes varAttr = e.getVariable(); if(e.getChangeType()== VariableChangeType.VARIABLE_DELETED){ unselectedListModel.removeElement(varAttr); //find variable name in text area and remove it String name = varAttr.getName().toString(); String temp = sqlTextArea.getText().trim(); String[] text = temp.split("\\s+"); temp = ""; for(String s : text){ String t = s.replaceAll("\\(\\)", "").trim(); if(!name.equals(t)){ temp += s + " "; } } sqlTextArea.setText(temp.trim()); }else if(e.getChangeType()==VariableChangeType.VARIABLE_ADDED){ unselectedListModel.addElement(varAttr); }else if(e.getChangeType()==VariableChangeType.VARIABLE_MODIFIED){ //do not use selectedListModel.replaceElement(v) because of need to filter variable if(unselectedListModel.contains(varAttr)){ //do not use variableListModel.replaceElement(v) because of need to filter variable unselectedListModel.removeElement(varAttr); unselectedListModel.addElement(varAttr); //will force filtering of modified variable } } } }