[Squirrel-sql-commits] sql12/app/src/net/sourceforge/squirrel_sql/client/session/properties EditWher
A Java SQL client for any JDBC compliant database
Brought to you by:
colbell,
gerdwagner
From: g. g. <gw...@us...> - 2004-04-23 18:34:04
|
Update of /cvsroot/squirrel-sql/sql12/app/src/net/sourceforge/squirrel_sql/client/session/properties In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6633 Modified Files: EditWhereColsPanel.java Log Message: limit cols in edit now functional Index: EditWhereColsPanel.java =================================================================== RCS file: /cvsroot/squirrel-sql/sql12/app/src/net/sourceforge/squirrel_sql/client/session/properties/EditWhereColsPanel.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** EditWhereColsPanel.java 20 Apr 2004 19:50:24 -0000 1.1 --- EditWhereColsPanel.java 23 Apr 2004 18:33:55 -0000 1.2 *************** *** 18,43 **** * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ - import java.awt.Component; import java.awt.Dimension; ! import java.awt.GridBagConstraints; ! import java.awt.GridBagLayout; ! import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; - import java.util.Map; import java.util.SortedSet; - import javax.swing.BorderFactory; - import javax.swing.Box; - import javax.swing.BoxLayout; import javax.swing.JButton; - import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; ! import javax.swing.JTextArea; ! import javax.swing.JTextField; - import net.sourceforge.squirrel_sql.client.session.ISession; /** --- 18,38 ---- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ import java.awt.Dimension; ! import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.SortedSet; + import java.util.TreeSet; + import java.util.HashMap; + import java.util.Iterator; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; ! import javax.swing.JList; ! import javax.swing.ListModel; ! import javax.swing.JOptionPane; /** *************** *** 46,53 **** * of columns and the WHERE clause generated using all the columns exceeds the DBMS limit. */ ! public class EditWhereColsPanel { ! /** The actual GUI panel that allows user to do the maintenance. */ ! private EditWhereColsSubPanel _myPanel; /** --- 41,74 ---- * of columns and the WHERE clause generated using all the columns exceeds the DBMS limit. */ ! public class EditWhereColsPanel extends JPanel { ! /** The name of the database table the Where clause applies to. */ ! private String _tableName; ! ! /** The name of the table including the URL **/ ! private String _unambiguousTableName; ! ! /** The list of all possible columns in the table **/ ! private SortedSet _columnList; ! ! /** The list of "to use" column names as seen by the user **/ ! private JList useColsList; ! ! /** The list of "to NOT use" column names as seen by the user **/ ! private JList notUseColsList; ! ! /** The list of column names to use as calculated when window is created **/ ! private Object[] initalUseColsArray; ! ! /** The list of column names to NOT use as calculated when window is created **/ ! private Object[] initalNotUseColsArray; ! ! /** ! * ?? this should be changed to use the I18N file mechanism. ! */ ! interface EditWhereColsPanelI18N { ! String TITLE = "Limit Columns in Cell Edit"; ! String HINT = "Limit columns used in WHERE clause when editing table "; ! } /** *************** *** 55,100 **** * * @param columnList A list of column names for the database table. - * @param textColumns A collection of column names that are "text" - * columns. * @param tableName The name of the database table that the filter * information will apply to. * * @throws IllegalArgumentException * The exception thrown if invalid arguments are passed. */ ! public EditWhereColsPanel(SortedSet columnList, Map textColumns, ! String tableName) throws IllegalArgumentException { super(); - _myPanel = new EditWhereColsSubPanel(columnList, textColumns, tableName); - } ! /** ! * Initialize the components of the WhereClausePanel. ! * ! * @param sqlFilterClauses An instance of a class containing information ! * about SQL filters already in place for the table. ! * ! * @throws IllegalArgumentException ! * Thrown if an invalid argument is passed. ! */ ! public void initialize(ISession session) ! throws IllegalArgumentException ! { ! //???????????? ! //???? _myPanel.loadData(_sqlFilterClauses); ! } ! /** ! * Returns the panel created by the class. ! * ! * @return Return an instance of a WhereClauseSubPanel. ! */ ! public Component getPanelComponent() ! { ! return _myPanel; } /** * Get the title of the panel. --- 76,128 ---- * * @param columnList A list of column names for the database table. * @param tableName The name of the database table that the filter * information will apply to. + * @param unambiguousTableName The name of the table including the URL + * to the specific DBMS * * @throws IllegalArgumentException * The exception thrown if invalid arguments are passed. */ ! public EditWhereColsPanel(SortedSet columnList, ! String tableName, String unambiguousTableName) throws IllegalArgumentException { super(); ! // save the input for use later ! _columnList = columnList; ! _tableName = tableName; ! _unambiguousTableName = unambiguousTableName; ! ! // look up the table in the EditWhereCols list ! HashMap colsTable = EditWhereCols.get(unambiguousTableName); ! ! if (colsTable == null) { ! // use all of the columns ! initalUseColsArray = _columnList.toArray(); ! initalNotUseColsArray = new Object[0]; ! } ! else { ! // use just the columns listed in the table, and set the not-used cols to the ones ! // that are not mentioned in the table ! SortedSet initialUseColsSet = new TreeSet( ); ! SortedSet initialNotUseColsList = new TreeSet(); ! ! Iterator it = _columnList.iterator(); ! while (it.hasNext()) { ! Object colName = it.next(); ! if (colsTable.get(colName) != null) ! initialUseColsSet.add(colName); ! else initialNotUseColsList.add(colName); ! } ! initalUseColsArray = initialUseColsSet.toArray(); ! initalNotUseColsArray = initialNotUseColsList.toArray(); ! } ! // create all of the gui objects now ! createGUI(); } + /** * Get the title of the panel. *************** *** 104,108 **** public String getTitle() { ! return EditWhereColsSubPanel.EditWhereColsSubPanelI18n.WHERE_CLAUSE; } --- 132,136 ---- public String getTitle() { ! return EditWhereColsPanelI18N.TITLE; } *************** *** 114,441 **** public String getHint() { ! return EditWhereColsSubPanel.EditWhereColsSubPanelI18n.HINT; } /** ! * Update the current session with any changes to the SQL filter ! * information. */ ! public void applyChanges() ! { ! _myPanel.applyChanges(); } ! /** ! * A private class that makes up the bulk of the GUI for the panel. */ ! private static final class EditWhereColsSubPanel extends JPanel ! { ! /** ! * This interface defines locale specific strings. This should be ! * replaced with a property file. ! */ ! interface EditWhereColsSubPanelI18n ! { ! String COLUMNS = "Columns"; ! String OPERATORS = "Operators"; ! String VALUE = "Value"; ! String WHERE_CLAUSE = "Where Clause"; ! String HINT = "Where clause for the selected table"; ! String ADD = "Add"; ! String AND = "AND"; ! String OR = "OR"; ! String LIKE = "LIKE"; ! String IN = "IN"; ! String IS_NULL = "IS NULL"; ! String IS_NOT_NULL = "IS NOT NULL"; ! } ! ! /** ! * A JComboBox component containing a list of the names of the ! * columns for the current table. ! */ ! private JComboBox _columnCombo; ! ! /** A label to identify the column combo box. */ ! private JLabel _columnLabel = new JLabel(EditWhereColsSubPanelI18n.COLUMNS); ! ! /** ! * A JComboBox containing a list of valid operators used in SQL Where clause ! * expressions. ! */ ! private OperatorTypeCombo _operatorCombo = new OperatorTypeCombo(); ! ! /** A label to identify the operator combo box. */ ! private JLabel _operatorLabel = new JLabel(EditWhereColsSubPanelI18n.OPERATORS); ! ! /** A field used to enter the right-hand side of a WhereClause expression. */ ! private JTextField _valueField = new JTextField(10); ! ! /** A label to identify the valueField text area. */ ! private JLabel _valueLabel = new JLabel(EditWhereColsSubPanelI18n.VALUE); ! ! /** A JComboBox used to list Where clause connectors. */ ! private AndOrCombo _andOrCombo = new AndOrCombo(); ! ! /** A label to identify the andor combo box. */ ! private JLabel _andOrLabel = new JLabel(" "); ! ! /** A text area used to contain all of the information for the Where clause. */ ! private JTextArea _whereClauseArea = new JTextArea(10, 40); ! ! /** ! * A button used to add information from the combo boxes and text fields into the ! * Where clause text area. ! */ ! private JButton _addTextButton = new JButton(EditWhereColsSubPanelI18n.ADD); ! ! /** The name of the database table the Where clause applies to. */ ! private String _tableName; ! ! /** A List containing the names of the text columns */ ! private Map _textColumns; ! ! /** ! * A JPanel used for a bulk of the GUI elements of the panel. ! * ! * @param columnList A list of the column names for the table. ! * @param tableName The name of the database table. ! */ ! EditWhereColsSubPanel(SortedSet columnList, Map textColumns, ! String tableName) ! { ! super(); ! _tableName = tableName; ! _columnCombo = new JComboBox(columnList.toArray()); ! _textColumns = textColumns; ! createGUI(); ! } ! ! /** ! * Load existing clause information into the panel. ! * ! * @param sqlFilterClauses An instance of a class containing ! * SQL Filter information for the current table. ! * ! */ ! void loadData() ! { ! //????? _whereClauseArea.setText( ! //????? sqlFilterClauses.get(getClauseIdentifier(), _tableName)); } ! ! /** Update the current SQuirreL session with any changes to the SQL filter ! * information. ! * @param sqlFilterClauses An instance of a class containing SQL Filter information for the current table. ! * ! */ ! void applyChanges() ! { ! //???? sqlFilterClauses.put( ! //???? getClauseIdentifier(), ! //???? _tableName, ! //????? _whereClauseArea.getText()); } ! /** ! * Create the GUI elements for the panel. ! */ ! private void createGUI() ! { ! setLayout(new GridBagLayout()); ! final GridBagConstraints gbc = new GridBagConstraints(); ! gbc.anchor = GridBagConstraints.WEST; ! gbc.fill = GridBagConstraints.HORIZONTAL; ! ! gbc.gridx = 0; ! gbc.gridy = 0; ! add(createGeneralPanel(), gbc); } ! /** ! * Create a JPanel with GUI components. ! * ! * @return Returns a JPanel ! */ ! private JPanel createGeneralPanel() ! { ! final JPanel pnl = new JPanel(new GridBagLayout()); ! ! final GridBagConstraints gbc = new GridBagConstraints(); ! gbc.anchor = GridBagConstraints.WEST; ! gbc.insets = new Insets(4, 4, 4, 4); ! gbc.weightx = 1.0; ! ! gbc.fill = GridBagConstraints.NONE; ! gbc.gridx = 0; ! gbc.gridy = 0; ! gbc.gridwidth = 1; ! JPanel andOrPanel = new JPanel(); ! andOrPanel.setLayout(new BoxLayout(andOrPanel, BoxLayout.Y_AXIS)); ! _andOrLabel.setAlignmentX(Component.LEFT_ALIGNMENT); ! andOrPanel.add(_andOrLabel); ! _andOrCombo.setAlignmentX(Component.LEFT_ALIGNMENT); ! andOrPanel.add(_andOrCombo); ! pnl.add(andOrPanel, gbc); ! ! gbc.gridx++; ! gbc.gridwidth = 5; ! gbc.fill = GridBagConstraints.HORIZONTAL; ! JPanel columnPanel = new JPanel(); ! columnPanel.setLayout(new BoxLayout(columnPanel, BoxLayout.Y_AXIS)); ! _columnLabel.setAlignmentX(Component.LEFT_ALIGNMENT); ! columnPanel.add(_columnLabel); ! _columnCombo.setAlignmentX(Component.LEFT_ALIGNMENT); ! columnPanel.add(_columnCombo); ! pnl.add(columnPanel, gbc); ! ! gbc.gridx += 5; ! gbc.gridwidth = 1; ! gbc.fill = GridBagConstraints.NONE; ! JPanel operatorPanel = new JPanel(); ! operatorPanel.setLayout( ! new BoxLayout(operatorPanel, BoxLayout.Y_AXIS)); ! _operatorLabel.setAlignmentX(Component.LEFT_ALIGNMENT); ! operatorPanel.add(_operatorLabel); ! _operatorCombo.setAlignmentX(Component.LEFT_ALIGNMENT); ! operatorPanel.add(_operatorCombo); ! pnl.add(operatorPanel, gbc); ! ! gbc.gridx++; ! gbc.gridwidth = 1; ! JPanel valuePanel = new JPanel(); ! valuePanel.setLayout(new BoxLayout(valuePanel, BoxLayout.Y_AXIS)); ! _valueLabel.setAlignmentX(Component.LEFT_ALIGNMENT); ! valuePanel.add(_valueLabel); ! valuePanel.add(Box.createRigidArea(new Dimension(5, 5))); ! _valueField.setAlignmentX(Component.LEFT_ALIGNMENT); ! valuePanel.add(_valueField); ! pnl.add(valuePanel, gbc); ! ! gbc.gridx++; ! _addTextButton.addActionListener(new ActionListener() ! { ! public void actionPerformed(ActionEvent evt) ! { ! addTextToClause(); ! } ! }); ! pnl.add(_addTextButton, gbc); ! ! gbc.gridy++; // new line ! gbc.gridx = 0; ! gbc.gridwidth = 9; ! gbc.fill = GridBagConstraints.HORIZONTAL; ! gbc.ipady = 4; ! _whereClauseArea.setBorder(BorderFactory.createEtchedBorder()); ! _whereClauseArea.setLineWrap(true); ! JScrollPane sp = new JScrollPane(_whereClauseArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, ! JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); ! pnl.add(sp, gbc); ! ! return pnl; } ! private static final class OperatorTypeCombo extends JComboBox ! { ! OperatorTypeCombo() ! { ! addItem("="); ! addItem("<>"); ! addItem(">"); ! addItem("<"); ! addItem(">="); ! addItem("<="); ! addItem(EditWhereColsSubPanelI18n.IN); ! addItem(EditWhereColsSubPanelI18n.LIKE); ! addItem(EditWhereColsSubPanelI18n.IS_NULL); ! addItem(EditWhereColsSubPanelI18n.IS_NOT_NULL); ! } ! } ! private static final class AndOrCombo extends JComboBox { ! AndOrCombo() { ! addItem(EditWhereColsSubPanelI18n.AND); ! addItem(EditWhereColsSubPanelI18n.OR); } ! } ! ! /** ! * Combine the information entered in the combo boxes ! * and the text field and add it to the Where clause information. ! */ ! private void addTextToClause() { ! String value = (String)_valueField.getText(); ! String operator = (String)_operatorCombo.getSelectedItem(); ! if (((value != null) && (value.length() > 0)) ! || ((operator.equals(EditWhereColsSubPanelI18n.IS_NULL)) ! || (operator.equals(EditWhereColsSubPanelI18n.IS_NOT_NULL)))) { ! String andOr = (String)_andOrCombo.getSelectedItem(); ! String column = (String)_columnCombo.getSelectedItem(); ! ! // Put the 'AND' or the 'OR' in front of the clause if ! // there are already values in the text area. ! if (_whereClauseArea.getText().length() > 0) ! { ! _whereClauseArea.append("\n" + andOr + " "); ! } ! ! // If the operator is 'IN' and there are no parenthesis ! // around the value, put them there. ! if (operator.equals(EditWhereColsSubPanelI18n.IN) ! && (!value.trim().startsWith("("))) ! { ! value = "(" + value + ")"; ! } ! ! // If the column is a text column, and there aren't single quotes around the value, put them there. ! ! else if ((value != null) && (value.length() > 0)) ! { ! if (_textColumns.containsKey(column) ! && (!value.trim().startsWith("'"))) ! { ! value = "'" + value + "'"; ! } ! } ! _whereClauseArea.append(column + " " + operator); ! ! if ((value != null) && (value.length() > 0)) ! { ! _whereClauseArea.append(" " + value); ! } } ! _valueField.setText(""); ! } ! ! /** ! * Erase all information for the current filter. ! */ ! public void clearFilter() ! { ! //?? currently ignored; this should reset the lists of fields to the original values ! } ! } ! ! /** ! * Erase any information for the appropriate filter. ! */ ! public void clearFilter() ! { ! _myPanel.clearFilter(); ! } ! /** ! * Get a value that uniquely identifies this SQL filter clause. ! * ! * @return Return a String value containing an identifing value. ! */ ! public static String getClauseIdentifier() ! { ! return EditWhereColsSubPanel.EditWhereColsSubPanelI18n.WHERE_CLAUSE; } } \ No newline at end of file --- 142,296 ---- public String getHint() { ! return EditWhereColsPanelI18N.HINT; } /** ! * Reset the panel to the contents at the time we started editing ! * (as set in initialize). ! * */ ! public void reset() { ! useColsList.setListData(initalUseColsArray); ! notUseColsList.setListData(initalNotUseColsArray); } ! /** ! * Put the current data into the EditWhereCols table. */ ! public boolean ok() { ! ! // if all cols are in the "to use" side, delete from EditWhereCols ! if (notUseColsList.getModel().getSize() == 0) { ! EditWhereCols.put(_unambiguousTableName, null); } ! else { ! // some cols are not to be used ! ListModel useColsModel = useColsList.getModel(); ! ! // do not let user remove everything from the list ! if (useColsModel.getSize() == 0) { ! JOptionPane.showMessageDialog(this, ! "You cannot remove all of the fields from the 'use columns' list."); ! return false; ! } ! ! // create the HashMap of names to use and put it in EditWhereCols ! HashMap useColsMap = new HashMap(useColsModel.getSize()); ! ! for (int i=0; i< useColsModel.getSize(); i++) { ! useColsMap.put(useColsModel.getElementAt(i), useColsModel.getElementAt(i)); ! } ! ! EditWhereCols.put(_unambiguousTableName, useColsMap); } + return true; + } + + /** + * Move selected fields from "used" to "not used" + */ + private void moveToNotUsed() { + + // get the values from the "not use" list and convert to sorted set + ListModel notUseColsModel = notUseColsList.getModel(); + SortedSet notUseColsSet = new TreeSet(); + for (int i=0; i<notUseColsModel.getSize(); i++) + notUseColsSet.add(notUseColsModel.getElementAt(i)); + + // get the values from the "use" list + ListModel useColsModel = useColsList.getModel(); + + // create an empty set for the "use" list + SortedSet useColsSet = new TreeSet(); ! // for each element in the "use" set, if selected then add to "not use", ! // otherwise add to new "use" set ! for (int i=0; i<useColsModel.getSize(); i++) { ! Object colName = useColsModel.getElementAt(i); ! if (useColsList.isSelectedIndex(i)) ! notUseColsSet.add(colName); ! else useColsSet.add(colName); } + + useColsList.setListData(useColsSet.toArray()); + notUseColsList.setListData(notUseColsSet.toArray()); + } + + /** + * Move selected fields from "not used" to "used" + */ + private void moveToUsed() { + // get the values from the "use" list and convert to sorted set + ListModel useColsModel = useColsList.getModel(); + SortedSet useColsSet = new TreeSet(); + for (int i=0; i<useColsModel.getSize(); i++) + useColsSet.add(useColsModel.getElementAt(i)); + + // get the values from the "not use" list + ListModel notUseColsModel = notUseColsList.getModel(); + + // create an empty set for the "not use" list + SortedSet notUseColsSet = new TreeSet(); ! // for each element in the "not use" set, if selected then add to "use", ! // otherwise add to new "not use" set ! for (int i=0; i<notUseColsModel.getSize(); i++) { ! Object colName = notUseColsModel.getElementAt(i); ! if (notUseColsList.isSelectedIndex(i)) ! useColsSet.add(colName); ! else notUseColsSet.add(colName); } + + useColsList.setListData(useColsSet.toArray()); + notUseColsList.setListData(notUseColsSet.toArray()); + } + + + /** + * Create the GUI elements for the panel. + */ + private void createGUI() + { ! JPanel useColsPanel = new JPanel(new BorderLayout()); ! useColsPanel.add(new JLabel("Use Columns"), BorderLayout.NORTH); ! useColsList = new JList(initalUseColsArray); ! JScrollPane scrollPane = new JScrollPane(useColsList); ! scrollPane.setPreferredSize(new Dimension(200, 200)); ! useColsPanel.add(scrollPane, BorderLayout.SOUTH); ! add(useColsPanel); ! JPanel moveButtonsPanel = new JPanel(); ! JPanel buttonPanel = new JPanel(new BorderLayout()); ! //????? if desired, get fancy and use icons in buttons instead of text ????????? ! JButton moveToNotUsedButton = new JButton("=>"); ! moveToNotUsedButton.addActionListener(new ActionListener() { ! public void actionPerformed(ActionEvent evt) { ! moveToNotUsed(); } ! }); ! buttonPanel.add(moveToNotUsedButton, BorderLayout.NORTH); ! JButton moveToUsedButton = new JButton("<="); ! moveToUsedButton.addActionListener(new ActionListener() { ! public void actionPerformed(ActionEvent evt) { ! moveToUsed(); } ! }); ! buttonPanel.add(moveToUsedButton, BorderLayout.SOUTH); ! moveButtonsPanel.add(buttonPanel, BorderLayout.CENTER); ! add(moveButtonsPanel); ! ! JPanel notUseColsPanel = new JPanel(new BorderLayout()); ! notUseColsPanel.add(new JLabel("Not Use Columns"), BorderLayout.NORTH); ! notUseColsList = new JList(initalNotUseColsArray); ! JScrollPane notUseScrollPane = new JScrollPane(notUseColsList); ! notUseScrollPane.setPreferredSize(new Dimension(200, 200)); ! notUseColsPanel.add(notUseScrollPane, BorderLayout.SOUTH); ! add(notUseColsPanel); } } \ No newline at end of file |