001: /*
002: * ParameterEditor.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.preparedstatement;
013:
014: import java.awt.BorderLayout;
015: import java.awt.Color;
016: import java.awt.Dimension;
017:
018: import javax.swing.BorderFactory;
019: import javax.swing.JLabel;
020: import javax.swing.JPanel;
021: import javax.swing.JScrollPane;
022: import javax.swing.JTable;
023: import javax.swing.SwingConstants;
024: import javax.swing.border.Border;
025: import javax.swing.table.TableCellEditor;
026: import javax.swing.table.TableColumn;
027:
028: import workbench.WbManager;
029: import workbench.gui.MainWindow;
030: import workbench.gui.WbSwingUtilities;
031: import workbench.gui.components.ValidatingDialog;
032: import workbench.gui.components.WbTable;
033: import workbench.gui.components.WbTextCellEditor;
034: import workbench.interfaces.ValidatingComponent;
035: import workbench.log.LogMgr;
036: import workbench.resource.ResourceMgr;
037: import workbench.sql.preparedstatement.StatementParameters;
038: import workbench.util.SqlUtil;
039:
040: /**
041: *
042: * @author support@sql-workbench.net
043: *
044: * An component to enter parameters for prepared statements
045: */
046: public class ParameterEditor extends JPanel implements
047: ValidatingComponent {
048: private WbTable parameterTable;
049: private StatementParameters parameters;
050: private StatementParameterTableModel model;
051:
052: public ParameterEditor(StatementParameters parms) {
053: this .parameters = parms;
054: this .model = new StatementParameterTableModel(this .parameters);
055: this .parameterTable = new WbTable();
056: this .parameterTable.setRowSelectionAllowed(false);
057: this .parameterTable.setColumnSelectionAllowed(false);
058: this .parameterTable.setModel(this .model);
059: this .parameterTable
060: .setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
061: TableColumn col = this .parameterTable.getColumnModel()
062: .getColumn(2);
063: col.setCellEditor(WbTextCellEditor.createInstance());
064:
065: JLabel l = new JLabel(ResourceMgr
066: .getString("TxtPSParameterInputText"));
067: Border b = BorderFactory.createEmptyBorder(5, 2, 5, 2);
068: l.setBorder(b);
069: l.setBackground(Color.WHITE);
070: l.setOpaque(true);
071: l.setHorizontalAlignment(SwingConstants.LEFT);
072:
073: this .setLayout(new BorderLayout());
074:
075: JScrollPane scroll = new JScrollPane(this .parameterTable);
076: b = BorderFactory.createEmptyBorder(5, 0, 0, 0);
077: Border b2 = BorderFactory.createCompoundBorder(b, scroll
078: .getBorder());
079: scroll.setBorder(b2);
080:
081: this .add(l, BorderLayout.NORTH);
082: this .add(scroll, BorderLayout.CENTER);
083: }
084:
085: public void applyValues() {
086: this .parameterTable.stopEditing();
087: int count = this .parameters.getParameterCount();
088: for (int i = 0; i < count; i++) {
089: String v = this .model.getParameterValue(i);
090: this .parameters.setParameterValue(i, v);
091: }
092: }
093:
094: public void componentDisplayed() {
095: this .parameterTable.setColumnSelectionInterval(2, 2);
096: this .parameterTable.editCellAt(0, 2);
097: TableCellEditor editor = this .parameterTable.getCellEditor();
098: if (editor instanceof WbTextCellEditor) {
099: WbTextCellEditor wbedit = (WbTextCellEditor) editor;
100: wbedit.selectAll();
101: wbedit.requestFocus();
102: }
103: }
104:
105: public boolean validateInput() {
106: this .parameterTable.stopEditing();
107: int count = this .parameters.getParameterCount();
108: for (int i = 0; i < count; i++) {
109: String v = this .model.getParameterValue(i);
110: if (!this .parameters.isValueValid(i, v)) {
111: String error = ResourceMgr.getString(
112: "ErrInvalidPSParameterValue").replaceAll(
113: "%value%", v);
114: error = error.replaceAll("%type%", SqlUtil
115: .getTypeName(this .parameters
116: .getParameterType(i)));
117: WbSwingUtilities.showErrorMessage(this , error);
118: return false;
119: }
120: }
121: return true;
122: }
123:
124: public static boolean showParameterDialog(StatementParameters parms) {
125: ParameterEditor editor = new ParameterEditor(parms);
126: Dimension d = new Dimension(300, 250);
127: editor.setMinimumSize(d);
128: editor.setPreferredSize(d);
129:
130: boolean result = false;
131: //boolean ok = ValidatingDialog.showConfirmDialog(WbManager.getInstance().getCurrentWindow(), editor, ResourceMgr.getString("TxtEditPSParameterWindowTitle"));
132: MainWindow parent = WbManager.getInstance().getCurrentWindow();
133: boolean ok = ValidatingDialog.showConfirmDialog(parent, editor,
134: ResourceMgr.getString("TxtEditPSParameterWindowTitle"));
135: if (ok) {
136: try {
137: editor.applyValues();
138: result = true;
139: } catch (Exception e) {
140: LogMgr.logError(
141: "VariablesEditor.showVariablesDialog()",
142: "Error when saving values", e);
143: result = false;
144: }
145: }
146: return result;
147: }
148:
149: }
|