01: /*
02: * FocusIndicator.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.gui.components;
13:
14: import java.awt.Color;
15: import java.awt.event.FocusEvent;
16: import java.awt.event.FocusListener;
17: import javax.swing.JComponent;
18: import javax.swing.border.Border;
19: import javax.swing.border.CompoundBorder;
20: import javax.swing.border.EmptyBorder;
21: import javax.swing.border.LineBorder;
22: import workbench.resource.Settings;
23:
24: /**
25: *
26: * @author support@sql-workbench.net
27: */
28: public class FocusIndicator implements FocusListener {
29: private Border focusBorder = null;
30: private Border noFocusBorder = null;
31: private Border originalBorder = null;
32: private JComponent focusClient;
33: private JComponent borderClient;
34: private Color borderColor = null;
35:
36: public FocusIndicator(JComponent focusToCheck, JComponent border) {
37: focusClient = focusToCheck;
38: focusClient.addFocusListener(this );
39: borderClient = border;
40: borderColor = Settings.getInstance().getColor(
41: "workbench.gui.focusindicator.bordercolor",
42: Color.YELLOW.brighter());
43: initBorder();
44: }
45:
46: private void initBorder() {
47: if (noFocusBorder == null && focusBorder == null
48: && originalBorder == null) {
49: originalBorder = borderClient.getBorder();
50: noFocusBorder = new CompoundBorder(new EmptyBorder(1, 1, 1,
51: 1), originalBorder);
52: borderClient.setBorder(noFocusBorder);
53: focusBorder = new CompoundBorder(new LineBorder(
54: borderColor, 1), originalBorder);
55: }
56: }
57:
58: public void dispose() {
59: if (this .focusClient != null) {
60: focusClient.removeFocusListener(this );
61: }
62:
63: if (this .borderClient != null && originalBorder != null) {
64: this .borderClient.setBorder(originalBorder);
65: }
66: }
67:
68: public void focusGained(FocusEvent e) {
69: if (this .borderClient != null) {
70: initBorder();
71: this .borderClient.setBorder(focusBorder);
72: }
73: }
74:
75: public void focusLost(FocusEvent e) {
76: if (this.borderClient != null) {
77: initBorder();
78: this.borderClient.setBorder(noFocusBorder);
79: }
80: }
81:
82: }
|