001: /*
002: * LnFDefinitionPanel.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.Color;
015: import java.awt.Font;
016: import java.awt.event.ActionListener;
017: import java.awt.event.FocusAdapter;
018: import java.awt.event.FocusEvent;
019: import java.beans.PropertyChangeEvent;
020: import java.beans.PropertyChangeListener;
021: import javax.swing.JLabel;
022: import javax.swing.JPanel;
023: import javax.swing.SwingUtilities;
024: import javax.swing.UIManager;
025: import workbench.gui.WbSwingUtilities;
026: import workbench.gui.components.ExtensionFileFilter;
027: import workbench.gui.components.StringPropertyEditor;
028: import workbench.gui.components.TextComponentMouseListener;
029: import workbench.gui.components.WbButton;
030: import workbench.gui.lnf.LnFDefinition;
031: import workbench.gui.lnf.LnFLoader;
032: import workbench.resource.ResourceMgr;
033: import workbench.resource.Settings;
034: import workbench.util.StringUtil;
035:
036: /**
037: *
038: * @author support@sql-workbench.net
039: */
040: public class LnFDefinitionPanel extends JPanel implements
041: ActionListener {
042: private LnFDefinition currentLnF;
043: private PropertyChangeListener changeListener;
044:
045: public LnFDefinitionPanel() {
046: initComponents();
047: String text = ResourceMgr.getDescription("LblLnFLib");
048: text = text.replaceAll("%path_sep%", StringUtil
049: .getPathSeparator());
050: lblLibrary.setToolTipText(text);
051: libraryPath.setTextFieldPropertyName("library");
052: libraryPath.setTextfieldTooltip(text);
053: text = ResourceMgr.getDescription("SelectLnfLib");
054: libraryPath.setButtonTooltip(text);
055: libraryPath.setFileFilter(ExtensionFileFilter
056: .getJarFileFilter());
057: libraryPath.setAllowMultiple(true);
058: libraryPath.setLastDirProperty("workbench.lnf.lastdir");
059: tfName.addFocusListener(new FocusAdapter() {
060: public void focusLost(FocusEvent evt) {
061: nameFieldFocusLost(evt);
062: }
063: });
064:
065: Font f = UIManager.getDefaults().getFont("Label.font");
066: f = f.deriveFont((float) (f.getSize() * 1.1));
067: infoText.setFont(f);
068: String button = changeLnfButton.getText();
069: String info = ResourceMgr.getString("TxtChangeLnFInfo")
070: .replaceAll("%button%", button);
071: infoText.setText(info);
072: infoText.setWrapStyleWord(true);
073: infoText.setLineWrap(true);
074: infoText.setOpaque(true);
075: infoText.setBackground(this .getBackground());
076: }
077:
078: public void setPropertyListener(PropertyChangeListener l) {
079: this .changeListener = l;
080: }
081:
082: public void nameFieldFocusLost(FocusEvent evt) {
083: if (this .changeListener != null) {
084: PropertyChangeEvent pEvt = new PropertyChangeEvent(
085: this .currentLnF, "name", null, tfName.getText());
086: this .changeListener.propertyChange(pEvt);
087: }
088: }
089:
090: public void setEnabled(boolean flag) {
091: this .tfClassName.setEnabled(flag);
092: this .libraryPath.setEnabled(flag);
093: this .tfName.setEnabled(flag);
094: }
095:
096: private boolean testLnF(LnFDefinition lnf) {
097: try {
098: LnFLoader loader = new LnFLoader(lnf);
099: return loader.isAvailable();
100: } catch (Exception e) {
101: return false;
102: }
103: }
104:
105: /** This method is called from within the constructor to
106: * initialize the form.
107: * WARNING: Do NOT modify this code. The content of this method is
108: * always regenerated by the Form Editor.
109: */
110: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
111: private void initComponents() {
112: java.awt.GridBagConstraints gridBagConstraints;
113:
114: lblName = new javax.swing.JLabel();
115: tfName = new StringPropertyEditor();
116: lblClassName = new javax.swing.JLabel();
117: tfClassName = new StringPropertyEditor();
118: lblLibrary = new javax.swing.JLabel();
119: infoText = new javax.swing.JTextArea();
120: jSeparator1 = new javax.swing.JSeparator();
121: changeLnfButton = new WbButton();
122: currentLabel = new HtmlLabel();
123: libraryPath = new workbench.gui.components.WbFilePicker();
124:
125: setLayout(new java.awt.GridBagLayout());
126:
127: lblName.setText(ResourceMgr.getString("LblLnFName"));
128: gridBagConstraints = new java.awt.GridBagConstraints();
129: gridBagConstraints.gridx = 0;
130: gridBagConstraints.gridy = 0;
131: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
132: gridBagConstraints.insets = new java.awt.Insets(4, 10, 1, 7);
133: add(lblName, gridBagConstraints);
134:
135: tfName.setHorizontalAlignment(javax.swing.JTextField.LEFT);
136: tfName.setMinimumSize(new java.awt.Dimension(50, 20));
137: tfName.setName("name"); // NOI18N
138: tfName.setPreferredSize(new java.awt.Dimension(100, 20));
139: tfName.addMouseListener(new TextComponentMouseListener());
140: gridBagConstraints = new java.awt.GridBagConstraints();
141: gridBagConstraints.gridx = 1;
142: gridBagConstraints.gridy = 0;
143: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
144: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
145: gridBagConstraints.weightx = 1.0;
146: gridBagConstraints.insets = new java.awt.Insets(4, 3, 1, 3);
147: add(tfName, gridBagConstraints);
148:
149: lblClassName.setText(ResourceMgr.getString("LblLnFClass"));
150: gridBagConstraints = new java.awt.GridBagConstraints();
151: gridBagConstraints.gridx = 0;
152: gridBagConstraints.gridy = 1;
153: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
154: gridBagConstraints.insets = new java.awt.Insets(2, 10, 0, 7);
155: add(lblClassName, gridBagConstraints);
156:
157: tfClassName.setColumns(10);
158: tfClassName.setHorizontalAlignment(javax.swing.JTextField.LEFT);
159: tfClassName.setName("className"); // NOI18N
160: tfClassName.addMouseListener(new TextComponentMouseListener());
161: gridBagConstraints = new java.awt.GridBagConstraints();
162: gridBagConstraints.gridx = 1;
163: gridBagConstraints.gridy = 1;
164: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
165: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
166: gridBagConstraints.insets = new java.awt.Insets(2, 3, 0, 3);
167: add(tfClassName, gridBagConstraints);
168:
169: lblLibrary.setText(ResourceMgr.getString("LblLnFLib"));
170: gridBagConstraints = new java.awt.GridBagConstraints();
171: gridBagConstraints.gridx = 0;
172: gridBagConstraints.gridy = 2;
173: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
174: gridBagConstraints.insets = new java.awt.Insets(2, 10, 0, 7);
175: add(lblLibrary, gridBagConstraints);
176:
177: infoText.setEditable(false);
178: infoText.setLineWrap(true);
179: infoText
180: .setText("Please click on the \"Make current\" button to switch the current Look and Feel");
181: infoText.setWrapStyleWord(true);
182: infoText.setDisabledTextColor(new java.awt.Color(0, 0, 0));
183: gridBagConstraints = new java.awt.GridBagConstraints();
184: gridBagConstraints.gridx = 0;
185: gridBagConstraints.gridy = 4;
186: gridBagConstraints.gridwidth = 2;
187: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
188: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
189: gridBagConstraints.insets = new java.awt.Insets(11, 10, 0, 10);
190: add(infoText, gridBagConstraints);
191: gridBagConstraints = new java.awt.GridBagConstraints();
192: gridBagConstraints.gridx = 0;
193: gridBagConstraints.gridy = 3;
194: gridBagConstraints.gridwidth = 2;
195: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
196: gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
197: add(jSeparator1, gridBagConstraints);
198:
199: changeLnfButton
200: .setText(ResourceMgr.getString("LblActivateLnf"));
201: ((WbButton) changeLnfButton).setResourceKey("LblSwitchLnF");
202: changeLnfButton.addActionListener(this );
203: gridBagConstraints = new java.awt.GridBagConstraints();
204: gridBagConstraints.gridx = 0;
205: gridBagConstraints.gridy = 5;
206: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
207: gridBagConstraints.insets = new java.awt.Insets(10, 8, 0, 0);
208: add(changeLnfButton, gridBagConstraints);
209:
210: currentLabel.setBackground(new java.awt.Color(255, 255, 255));
211: currentLabel.setBorder(javax.swing.BorderFactory
212: .createEmptyBorder(3, 2, 3, 2));
213: currentLabel.setOpaque(true);
214: gridBagConstraints = new java.awt.GridBagConstraints();
215: gridBagConstraints.gridx = 0;
216: gridBagConstraints.gridy = 6;
217: gridBagConstraints.gridwidth = 2;
218: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
219: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
220: gridBagConstraints.weighty = 1.0;
221: gridBagConstraints.insets = new java.awt.Insets(13, 8, 0, 8);
222: add(currentLabel, gridBagConstraints);
223: gridBagConstraints = new java.awt.GridBagConstraints();
224: gridBagConstraints.gridx = 1;
225: gridBagConstraints.gridy = 2;
226: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
227: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
228: gridBagConstraints.insets = new java.awt.Insets(3, 3, 0, 3);
229: add(libraryPath, gridBagConstraints);
230: }
231:
232: // Code for dispatching events from components to event handlers.
233:
234: public void actionPerformed(java.awt.event.ActionEvent evt) {
235: if (evt.getSource() == changeLnfButton) {
236: LnFDefinitionPanel.this .changeLnfButtonActionPerformed(evt);
237: }
238: }// </editor-fold>//GEN-END:initComponents
239:
240: private void changeLnfButtonActionPerformed(
241: java.awt.event.ActionEvent evt)//GEN-FIRST:event_changeLnfButtonActionPerformed
242: {//GEN-HEADEREND:event_changeLnfButtonActionPerformed
243: LnFDefinition lnf = getDefinition();
244: if (testLnF(lnf)) {
245: String className = lnf.getClassName();
246: this .currentLabel.setText(lnf.getName());
247: Settings.getInstance().setLookAndFeelClass(className);
248: WbSwingUtilities.showMessage(SwingUtilities
249: .getWindowAncestor(this ), ResourceMgr
250: .getString("MsgLnFChanged"));
251: } else {
252: WbSwingUtilities.showErrorMessageKey(this ,
253: "MsgLnFNotLoaded");
254: }
255: }//GEN-LAST:event_changeLnfButtonActionPerformed
256:
257: public void setCurrentLookAndFeeld(LnFDefinition lnf) {
258: if (lnf != null)
259: currentLabel.setText(lnf.getName());
260: }
261:
262: public void setDefinition(LnFDefinition lnf) {
263: this .currentLnF = lnf;
264: WbSwingUtilities.initPropertyEditors(this .currentLnF, this );
265: libraryPath.setFilename(lnf.getLibrary());
266: this .setEnabled(!lnf.isBuiltInLnF());
267: }
268:
269: public LnFDefinition getDefinition() {
270: return this .currentLnF;
271: }
272:
273: // Variables declaration - do not modify//GEN-BEGIN:variables
274: public javax.swing.JButton changeLnfButton;
275: public javax.swing.JLabel currentLabel;
276: public javax.swing.JTextArea infoText;
277: public javax.swing.JSeparator jSeparator1;
278: public javax.swing.JLabel lblClassName;
279: public javax.swing.JLabel lblLibrary;
280: public javax.swing.JLabel lblName;
281: public workbench.gui.components.WbFilePicker libraryPath;
282: public javax.swing.JTextField tfClassName;
283: public javax.swing.JTextField tfName;
284:
285: // End of variables declaration//GEN-END:variables
286:
287: static class HtmlLabel extends JLabel {
288: public void setText(String name) {
289: setBackground(Color.WHITE);
290: setForeground(Color.BLACK);
291: super .setText("<html>"
292: + ResourceMgr.getString("LblCurrLnf") + " <b>"
293: + name + "</b></html>");
294: }
295: }
296:
297: }
|