001: /*
002: * ToolDefinitionPanel.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.settings;
013:
014: import java.awt.Component;
015: import java.awt.event.ActionListener;
016: import java.awt.event.FocusAdapter;
017: import java.awt.event.FocusEvent;
018: import java.beans.PropertyChangeEvent;
019: import java.beans.PropertyChangeListener;
020: import java.io.File;
021: import javax.swing.JFileChooser;
022: import javax.swing.JPanel;
023: import javax.swing.SwingUtilities;
024: import workbench.gui.WbSwingUtilities;
025: import workbench.gui.components.FlatButton;
026: import workbench.gui.components.StringPropertyEditor;
027: import workbench.gui.components.TextComponentMouseListener;
028: import workbench.interfaces.SimplePropertyEditor;
029: import workbench.log.LogMgr;
030: import workbench.resource.ResourceMgr;
031: import workbench.util.ExceptionUtil;
032: import workbench.util.StringUtil;
033: import workbench.util.ToolDefinition;
034:
035: /**
036: *
037: * @author support@sql-workbench.net
038: */
039: public class ToolDefinitionPanel extends JPanel implements
040: ActionListener {
041: private ToolDefinition currentTool;
042: private PropertyChangeListener changeListener;
043:
044: /** Creates new form BeanForm */
045: public ToolDefinitionPanel() {
046: initComponents();
047: tfName.addFocusListener(new FocusAdapter() {
048: public void focusLost(FocusEvent evt) {
049: nameFieldFocusLost(evt);
050: }
051: });
052:
053: }
054:
055: public void setPropertyListener(PropertyChangeListener l) {
056: this .changeListener = l;
057: }
058:
059: public void nameFieldFocusLost(FocusEvent evt) {
060: if (this .changeListener != null) {
061: PropertyChangeEvent pEvt = new PropertyChangeEvent(
062: this .currentTool, "name", null, tfName.getText());
063: this .changeListener.propertyChange(pEvt);
064: }
065: }
066:
067: /** This method is called from within the constructor to
068: * initialize the form.
069: * WARNING: Do NOT modify this code. The content of this method is
070: * always regenerated by the Form Editor.
071: */
072: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
073: private void initComponents() {
074: java.awt.GridBagConstraints gridBagConstraints;
075:
076: lblName = new javax.swing.JLabel();
077: tfName = new StringPropertyEditor();
078: lblPath = new javax.swing.JLabel();
079: tfPath = new StringPropertyEditor();
080: selectLibButton = new FlatButton();
081: jPanel1 = new javax.swing.JPanel();
082:
083: setLayout(new java.awt.GridBagLayout());
084:
085: lblName.setText(ResourceMgr.getString("LblLnFName"));
086: gridBagConstraints = new java.awt.GridBagConstraints();
087: gridBagConstraints.gridx = 0;
088: gridBagConstraints.gridy = 0;
089: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
090: gridBagConstraints.insets = new java.awt.Insets(4, 10, 0, 7);
091: add(lblName, gridBagConstraints);
092:
093: tfName.setHorizontalAlignment(javax.swing.JTextField.LEFT);
094: tfName.setName("name"); // NOI18N
095: tfName.addMouseListener(new TextComponentMouseListener());
096: gridBagConstraints = new java.awt.GridBagConstraints();
097: gridBagConstraints.gridx = 1;
098: gridBagConstraints.gridy = 0;
099: gridBagConstraints.gridwidth = 2;
100: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
101: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
102: gridBagConstraints.weightx = 1.0;
103: gridBagConstraints.insets = new java.awt.Insets(4, 3, 0, 3);
104: add(tfName, gridBagConstraints);
105:
106: lblPath.setText(ResourceMgr.getString("LblLnFLib"));
107: gridBagConstraints = new java.awt.GridBagConstraints();
108: gridBagConstraints.gridx = 0;
109: gridBagConstraints.gridy = 1;
110: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
111: gridBagConstraints.insets = new java.awt.Insets(2, 10, 0, 7);
112: add(lblPath, gridBagConstraints);
113:
114: tfPath.setColumns(10);
115: tfPath.setHorizontalAlignment(javax.swing.JTextField.LEFT);
116: tfPath.setName("applicationPath"); // NOI18N
117: tfPath.addMouseListener(new TextComponentMouseListener());
118: gridBagConstraints = new java.awt.GridBagConstraints();
119: gridBagConstraints.gridx = 1;
120: gridBagConstraints.gridy = 1;
121: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
122: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
123: gridBagConstraints.weightx = 1.0;
124: gridBagConstraints.insets = new java.awt.Insets(2, 3, 0, 3);
125: add(tfPath, gridBagConstraints);
126:
127: selectLibButton.setText("...");
128: selectLibButton.setMaximumSize(new java.awt.Dimension(20, 20));
129: selectLibButton.setMinimumSize(new java.awt.Dimension(20, 20));
130: selectLibButton
131: .setPreferredSize(new java.awt.Dimension(20, 20));
132: selectLibButton.addActionListener(this );
133: gridBagConstraints = new java.awt.GridBagConstraints();
134: gridBagConstraints.gridx = 2;
135: gridBagConstraints.gridy = 1;
136: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
137: gridBagConstraints.insets = new java.awt.Insets(2, 0, 0, 3);
138: add(selectLibButton, gridBagConstraints);
139:
140: jPanel1.setDoubleBuffered(false);
141: jPanel1.setEnabled(false);
142: jPanel1.setOpaque(false);
143: gridBagConstraints = new java.awt.GridBagConstraints();
144: gridBagConstraints.gridx = 0;
145: gridBagConstraints.gridy = 2;
146: gridBagConstraints.weighty = 1.0;
147: add(jPanel1, gridBagConstraints);
148: }
149:
150: // Code for dispatching events from components to event handlers.
151:
152: public void actionPerformed(java.awt.event.ActionEvent evt) {
153: if (evt.getSource() == selectLibButton) {
154: ToolDefinitionPanel.this .selectTool(evt);
155: }
156: }// </editor-fold>//GEN-END:initComponents
157:
158: private void selectTool(java.awt.event.ActionEvent evt)//GEN-FIRST:event_selectTool
159: {//GEN-HEADEREND:event_selectTool
160: try {
161: File current = new File(this .tfPath.getText());
162: JFileChooser jf = new JFileChooser();
163: if (current.isAbsolute()) {
164: File p = current.getParentFile();
165: if (p != null)
166: jf.setCurrentDirectory(p);
167: }
168: int answer = jf.showOpenDialog(SwingUtilities
169: .getWindowAncestor(this ));
170: if (answer == JFileChooser.APPROVE_OPTION) {
171: File f = jf.getSelectedFile();
172: this .tfPath.setText(f.getAbsolutePath());
173: }
174: } catch (Throwable e) {
175: LogMgr.logError("ToolDefinitionPanel.selectTool()",
176: "Error selecting file", e);
177: WbSwingUtilities.showErrorMessage(ExceptionUtil
178: .getDisplay(e));
179: }
180: }//GEN-LAST:event_selectTool
181:
182: private void initPropertyEditors() {
183: for (int i = 0; i < this .getComponentCount(); i++) {
184: Component c = this .getComponent(i);
185: if (c instanceof SimplePropertyEditor) {
186: SimplePropertyEditor editor = (SimplePropertyEditor) c;
187: String property = c.getName();
188: if (!StringUtil.isEmptyString(property)) {
189: editor.setSourceObject(this .currentTool, property);
190: editor.setImmediateUpdate(true);
191: }
192: }
193: }
194: }
195:
196: public void setDefinition(ToolDefinition tool) {
197: this .currentTool = tool;
198: initPropertyEditors();
199: }
200:
201: public ToolDefinition getDefinition() {
202: return this .currentTool;
203: }
204:
205: // Variables declaration - do not modify//GEN-BEGIN:variables
206: public javax.swing.JPanel jPanel1;
207: public javax.swing.JLabel lblName;
208: public javax.swing.JLabel lblPath;
209: public javax.swing.JButton selectLibButton;
210: public javax.swing.JTextField tfName;
211: public javax.swing.JTextField tfPath;
212: // End of variables declaration//GEN-END:variables
213:
214: }
|