001: /*
002: * VariablesEditor.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.sql;
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:
027: import workbench.WbManager;
028: import workbench.gui.WbSwingUtilities;
029: import workbench.gui.components.DataStoreTableModel;
030: import workbench.gui.components.ValidatingDialog;
031: import workbench.gui.components.WbTable;
032: import workbench.gui.components.WbTextCellEditor;
033: import workbench.interfaces.ValidatingComponent;
034: import workbench.log.LogMgr;
035: import workbench.resource.ResourceMgr;
036: import workbench.sql.VariablePool;
037: import workbench.storage.DataStore;
038:
039: /**
040: * A panel to enter the value for Workbench variables inside SQL statements
041: * @see VariablePrompter
042: * @see workbench.sql.VariablePool
043: *
044: * @author support@sql-workbench.net
045: */
046: public class VariablesEditor extends JPanel implements
047: ValidatingComponent {
048: private DataStore varData;
049: private WbTable variablesTable;
050:
051: public VariablesEditor(DataStore data) {
052:
053: this .variablesTable = new WbTable();
054: this .variablesTable.setRowSelectionAllowed(false);
055: this .variablesTable.setColumnSelectionAllowed(false);
056: this .varData = data;
057: DataStoreTableModel model = new DataStoreTableModel(data);
058: model.setLockedColumn(0);
059: this .variablesTable.setModel(model);
060: this .variablesTable
061: .setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
062:
063: JLabel l = new JLabel(ResourceMgr
064: .getString("TxtVariableInputText"));
065: Border b = BorderFactory.createEmptyBorder(5, 2, 5, 2);
066: l.setBorder(b);
067: l.setBackground(Color.WHITE);
068: l.setOpaque(true);
069: l.setHorizontalAlignment(SwingConstants.CENTER);
070:
071: this .setLayout(new BorderLayout());
072:
073: JScrollPane scroll = new JScrollPane(this .variablesTable);
074: b = BorderFactory.createEmptyBorder(5, 0, 0, 0);
075: Border b2 = BorderFactory.createCompoundBorder(b, scroll
076: .getBorder());
077: scroll.setBorder(b2);
078:
079: this .add(l, BorderLayout.NORTH);
080: this .add(scroll, BorderLayout.CENTER);
081: }
082:
083: public void componentDisplayed() {
084: this .variablesTable.setColumnSelectionInterval(1, 1);
085: this .variablesTable.editCellAt(0, 1);
086: TableCellEditor editor = this .variablesTable.getCellEditor();
087: if (editor instanceof WbTextCellEditor) {
088: WbTextCellEditor wbedit = (WbTextCellEditor) editor;
089: wbedit.selectAll();
090: wbedit.requestFocus();
091: }
092:
093: }
094:
095: public boolean validateInput() {
096: this .variablesTable.stopEditing();
097: int rows = this .varData.getRowCount();
098: for (int i = 0; i < rows; i++) {
099: String varName = this .varData.getValueAsString(i, 0);
100: if (!VariablePool.getInstance()
101: .isValidVariableName(varName)) {
102: String msg = ResourceMgr
103: .getString("ErrIllegalVariableName");
104: msg = msg.replaceAll("%varname%", varName);
105: msg = msg + "\n"
106: + ResourceMgr.getString("ErrVarDefWrongName");
107: WbSwingUtilities.showErrorMessage(this , msg);
108: return false;
109: }
110: }
111: return true;
112: }
113:
114: public static boolean showVariablesDialog(DataStore vardata) {
115: VariablesEditor editor = new VariablesEditor(vardata);
116: Dimension d = new Dimension(300, 250);
117: editor.setMinimumSize(d);
118: editor.setPreferredSize(d);
119:
120: boolean result = false;
121: boolean ok = ValidatingDialog.showConfirmDialog(WbManager
122: .getInstance().getCurrentWindow(), editor, ResourceMgr
123: .getString("TxtEditVariablesWindowTitle"));
124: if (ok) {
125: try {
126: vardata.updateDb(null, null);
127: result = true;
128: } catch (Exception e) {
129: LogMgr.logError(
130: "VariablesEditor.showVariablesDialog()",
131: "Error when saving values", e);
132: result = false;
133: }
134: }
135: return result;
136: }
137:
138: }
|