~ubuntu-branches/ubuntu/trusty/libswingx-java/trusty

« back to all changes in this revision

Viewing changes to src/java/org/jdesktop/swingx/JXFindBar.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2008-03-08 16:18:24 UTC
  • Revision ID: james.westby@ubuntu.com-20080308161824-wsahvl9pwzjcea3g
Tags: upstream-0.9.2
ImportĀ upstreamĀ versionĀ 0.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: JXFindBar.java,v 1.11 2007/09/14 08:12:38 kleopatra Exp $
 
3
 *
 
4
 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
 
5
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Lesser General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2.1 of the License, or (at your option) any later version.
 
11
 * 
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Lesser General Public License for more details.
 
16
 * 
 
17
 * You should have received a copy of the GNU Lesser General Public
 
18
 * License along with this library; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
20
 */
 
21
package org.jdesktop.swingx;
 
22
 
 
23
import java.awt.Color;
 
24
import java.awt.FlowLayout;
 
25
 
 
26
import javax.swing.JButton;
 
27
import javax.swing.JLabel;
 
28
import javax.swing.KeyStroke;
 
29
import javax.swing.SwingConstants;
 
30
 
 
31
/**
 
32
 * A simple low-intrusion default widget for incremental search.
 
33
 * 
 
34
 * Actions registered (in addition to super):
 
35
 * <ul>
 
36
 * <li> {@link JXDialog#CLOSE_ACTION_COMMAND} - an action bound to this
 
37
 * component's cancel method. The method itself is an empty implementation:
 
38
 * Subclassing clients can override the method, all clients can register a
 
39
 * custom action.
 
40
 * </ul>
 
41
 * 
 
42
 * Key bindings:
 
43
 * <ul>
 
44
 * <li> ESCAPE - calls action registered for
 
45
 * {@link JXDialog#CLOSE_ACTION_COMMAND}
 
46
 * </ul>
 
47
 * 
 
48
 * This implementation uses textfield coloring as not-found visualization.
 
49
 * 
 
50
 * <p>
 
51
 * PENDING: the coloring needs to be read from the UIManager instead of
 
52
 * hardcoding.
 
53
 * 
 
54
 * <p>
 
55
 * PENDING: the state transition of found/non-found coloring needs clean-up -
 
56
 * there are spurious problems when re-using the same instance (as SearchFactory
 
57
 * does).
 
58
 * 
 
59
 * @author Jeanette Winzenburg
 
60
 * 
 
61
 */
 
62
public class JXFindBar extends JXFindPanel {
 
63
 
 
64
    protected Color previousBackgroundColor;
 
65
 
 
66
    protected Color previousForegroundColor;
 
67
 
 
68
    // PENDING: need to read from UIManager
 
69
    protected Color notFoundBackgroundColor = Color.decode("#FF6666");
 
70
 
 
71
    protected Color notFoundForegroundColor = Color.white;
 
72
 
 
73
    protected JButton findNext;
 
74
 
 
75
    protected JButton findPrevious;
 
76
 
 
77
    public JXFindBar() {
 
78
        this(null);
 
79
    }
 
80
 
 
81
    public JXFindBar(Searchable searchable) {
 
82
        super(searchable);
 
83
        getPatternModel().setIncremental(true);
 
84
        getPatternModel().setWrapping(true);
 
85
    }
 
86
 
 
87
    @Override
 
88
    public void setSearchable(Searchable searchable) {
 
89
        super.setSearchable(searchable);
 
90
        match();
 
91
    }
 
92
 
 
93
    /**
 
94
     * here: set textfield colors to not-found colors.
 
95
     */
 
96
    @Override
 
97
    protected void showNotFoundMessage() {
 
98
        //JW: quick hack around #487-swingx - NPE in setSearchable
 
99
        if (searchField ==  null) return;
 
100
        searchField.setForeground(notFoundForegroundColor);
 
101
        searchField.setBackground(notFoundBackgroundColor);
 
102
    }
 
103
 
 
104
    /**
 
105
     * here: set textfield colors to normal.
 
106
     */
 
107
    @Override
 
108
    protected void showFoundMessage() {
 
109
        //JW: quick hack around #487-swingx - NPE in setSearchable
 
110
        if (searchField ==  null) return;
 
111
        searchField.setBackground(previousBackgroundColor);
 
112
        searchField.setForeground(previousForegroundColor);
 
113
    }
 
114
 
 
115
    @Override
 
116
    public void addNotify() {
 
117
        super.addNotify();
 
118
        if (previousBackgroundColor == null) {
 
119
            previousBackgroundColor = searchField.getBackground();
 
120
            previousForegroundColor = searchField.getForeground();
 
121
        } else {
 
122
            searchField.setBackground(previousBackgroundColor);
 
123
            searchField.setForeground(previousForegroundColor);
 
124
        }
 
125
    }
 
126
 
 
127
    // --------------------------- action call back
 
128
    /**
 
129
     * Action callback method for bound action JXDialog.CLOSE_ACTION_COMMAND.
 
130
     * 
 
131
     * Here: does nothing. Subclasses can override to define custom "closing"
 
132
     * behaviour. Alternatively, any client can register a custom action with
 
133
     * the actionMap.
 
134
     * 
 
135
     * 
 
136
     */
 
137
    public void cancel() {
 
138
    }
 
139
 
 
140
    // -------------------- init
 
141
 
 
142
    @Override
 
143
    protected void initExecutables() {
 
144
        getActionMap().put(JXDialog.CLOSE_ACTION_COMMAND,
 
145
                createBoundAction(JXDialog.CLOSE_ACTION_COMMAND, "cancel"));
 
146
        super.initExecutables();
 
147
    }
 
148
 
 
149
    @Override
 
150
    protected void bind() {
 
151
        super.bind();
 
152
        searchField
 
153
                .addActionListener(getAction(JXDialog.EXECUTE_ACTION_COMMAND));
 
154
        findNext.setAction(getAction(FIND_NEXT_ACTION_COMMAND));
 
155
        findPrevious.setAction(getAction(FIND_PREVIOUS_ACTION_COMMAND));
 
156
        KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
 
157
        getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(stroke,
 
158
                JXDialog.CLOSE_ACTION_COMMAND);
 
159
    }
 
160
 
 
161
    @Override
 
162
    protected void build() {
 
163
        setLayout(new FlowLayout(SwingConstants.LEADING));
 
164
        add(searchLabel);
 
165
        add(new JLabel(":"));
 
166
        add(new JLabel("  "));
 
167
        add(searchField);
 
168
        add(findNext);
 
169
        add(findPrevious);
 
170
    }
 
171
 
 
172
    @Override
 
173
    protected void initComponents() {
 
174
        super.initComponents();
 
175
        findNext = new JButton();
 
176
        findPrevious = new JButton();
 
177
    }
 
178
 
 
179
}