1 /*
2 Copyright (c) 2003, Laurent Caillette.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modifica-
6 tion, are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10
11 2. Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14
15 3. The end-user documentation included with the redistribution, if any, must
16 include the following acknowledgment: "This product includes software
17 written by Laurent Caillette."
18 Alternately, this acknowledgment may appear in the software itself, if
19 and wherever such third-party acknowledgments normally appear.
20
21 4. The name "Laurent Caillette" must not be used to endorse or
22 promote products derived from this software without
23 prior written permission. For written permission, please contact
24 laurent.caillette@laposte.net
25
26 5. Products derived from this software may not be called
27 "Laurent Caillette", nor may "Laurent Caillette" appear
28 in their name, without prior written permission of the
29 author.
30
31 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
32 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
33 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
34 AUTHOR (LAURENT CAILLETTE) BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
36 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
37 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 */
42 package tweed.swing;
43 import javax.swing.JButton;
44 import javax.swing.JComboBox;
45 import javax.swing.MutableComboBoxModel;
46 import tweed.action.ActionEndEvent;
47 import tweed.action.ActionNipple;
48 import tweed.action.ActionStartEvent;
49 import tweed.binding.ValidationMessage;
50 import tweed.context.Contextualized;
51 import tweed.message.UserMessageEvent;
52 import tweed.message.UserMessageNipple;
53 import java.awt.*;
54 import java.awt.event.*;
55
56 /***
57 * A ready-to-use component for displaying {@link tweed.message.UserMessage}s.
58 * Once constructed with a valid <code>Contextualized</code> object,
59 * it plugs automatically to the Context's event bus and displays information
60 * as needed.
61 *
62 * @author Laurent Caillette
63 * @version $Id$
64 */
65 public class UserMessageBar extends ContextualizedPanel {
66
67 GridBagLayout gridBagLayout1 = new GridBagLayout();
68 JComboBox jComboBox1 = new JComboBox();
69 JButton jButtonClear = new JButton();
70
71 public UserMessageBar() {
72 jbInit();
73 connect() ;
74 }
75
76 public UserMessageBar( Contextualized contextualized ) {
77 super( contextualized ) ;
78 jbInit() ;
79 connect() ;
80 }
81
82 private boolean programmaticChange = false ;
83
84 private void connect() {
85 userMessageNipple = new UserMessageNipple( this ) {
86 public void handleUserMessage( UserMessageEvent event ) {
87 try {
88 programmaticChange = true ;
89 jComboBox1.addItem( event.getUserMessage() ) ;
90 } finally {
91 programmaticChange = false ;
92 }
93 }
94 } ;
95
96 actionNipple = new ActionNipple( this ) {
97 public void handleActionStart( ActionStartEvent event ) {
98 removeValidationMessages( jComboBox1 ) ;
99 }
100 public void handleActionEnd( ActionEndEvent event ) { }
101 } ;
102 }
103
104 private static void removeValidationMessages( JComboBox comboBox ) {
105 MutableComboBoxModel model = ( MutableComboBoxModel ) comboBox.getModel() ;
106 int itemCount = model.getSize() ;
107 int index = 0 ;
108 while( index < itemCount ) {
109 Object item = model.getElementAt( index ) ;
110 if( item instanceof ValidationMessage ) {
111 model.removeElement( item ) ;
112 itemCount = model.getSize() ;
113 } else {
114 index++ ;
115 }
116 }
117
118 }
119
120 private void jbInit() {
121 this.setLayout(gridBagLayout1);
122 jButtonClear.setText("Clear");
123 jButtonClear.addActionListener(new java.awt.event.ActionListener() {
124 public void actionPerformed(ActionEvent e) {
125 jButtonClear_actionPerformed(e);
126 }
127 });
128 jComboBox1.addActionListener(new java.awt.event.ActionListener() {
129 public void actionPerformed(ActionEvent e) {
130 jComboBox1_actionPerformed(e);
131 }
132 });
133 this.add(jComboBox1, new GridBagConstraints(0, 0, 3, 1, 0.0, 0.0
134 ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
135 this.add(jButtonClear, new GridBagConstraints(4, 0, 1, 2, 0.0, 0.0
136 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
137 }
138
139 private UserMessageNipple userMessageNipple ;
140 private ActionNipple actionNipple ;
141
142 private void jButtonClear_actionPerformed(ActionEvent e) {
143 jComboBox1.removeAllItems();
144 }
145
146 private void jComboBox1_actionPerformed( ActionEvent ev ) {
147 if( ! programmaticChange ) {
148 Object item = jComboBox1.getSelectedItem() ;
149 if( item instanceof ValidationMessage ) {
150 Component[] components =
151 ( ( ValidationMessage ) item ).getComponents() ;
152 if( components.length > 0 ) {
153 components[ 0 ].requestFocus() ;
154 }
155 }
156 }
157 }
158
159 }
This page was automatically generated by Maven