1 /*
2 * Copyright 2006-2025 The JGUIraffe Team.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License")
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.jguiraffe.gui.platform.swing.builder.event;
17
18 import java.awt.event.FocusEvent;
19 import java.awt.event.FocusListener;
20
21 import net.sf.jguiraffe.gui.builder.event.FormEventManager;
22 import net.sf.jguiraffe.gui.builder.event.FormFocusEvent;
23 import net.sf.jguiraffe.gui.builder.event.FormFocusListener;
24 import net.sf.jguiraffe.gui.builder.event.FormListenerType;
25 import net.sf.jguiraffe.gui.forms.ComponentHandler;
26
27 /**
28 * <p>
29 * A specific event adapter for Swing focus events.
30 * </p>
31 *
32 * @author Oliver Heger
33 * @version $Id: FocusEventAdapter.java 205 2012-01-29 18:29:57Z oheger $
34 */
35 class FocusEventAdapter extends SwingEventAdapter implements FocusListener
36 {
37 /**
38 * Creates a new instance of {@code FocusEventAdapter} that passes received
39 * events to a {@code FormEventManager}.
40 *
41 * @param eventManager the {@code FormEventManager} (must not be
42 * <b>null</b>)
43 * @param handler the component handler
44 * @param name the component's name
45 * @throws IllegalArgumentException if the {@code FormEventManager} is
46 * <b>null</b>
47 */
48 public FocusEventAdapter(FormEventManager eventManager,
49 ComponentHandler<?> handler, String name)
50 {
51 super(eventManager, handler, name);
52 }
53
54 /**
55 * Creates a new instance of {@code FocusEventAdapter} that passes received
56 * events to a {@code FormFocusListener}.
57 *
58 * @param listener the {@code FormFocusListener} (must not be <b>null</b>)
59 * @param handler the component handler
60 * @param name the component's name
61 * @throws IllegalArgumentException if the {@code FormFocusListener} is
62 * <b>null</b>
63 */
64 public FocusEventAdapter(FormFocusListener listener,
65 ComponentHandler<?> handler, String name)
66 {
67 super(listener, handler, name);
68 }
69
70 /**
71 * Call back for focus gained events.
72 *
73 * @param event the event
74 */
75 public void focusGained(FocusEvent event)
76 {
77 fireFocusEvent(event, FormFocusEvent.Type.FOCUS_GAINED);
78 }
79
80 /**
81 * Call back for focus lost events.
82 *
83 * @param event the event
84 */
85 public void focusLost(FocusEvent event)
86 {
87 fireFocusEvent(event, FormFocusEvent.Type.FOCUS_LOST);
88 }
89
90 /**
91 * Sends a focus event to the event manager. The form event is created from
92 * the passed in focus event.
93 *
94 * @param event the Swing focus event
95 * @param type the event type
96 */
97 protected void fireFocusEvent(FocusEvent event, FormFocusEvent.Type type)
98 {
99 fireEvent(createFocusEvent(event, type));
100 }
101
102 /**
103 * Creates a general form focus event from the passed in Swing focus event.
104 *
105 * @param event the original event
106 * @param type the event type
107 * @return the form event
108 */
109 protected FormFocusEvent createFocusEvent(FocusEvent event,
110 FormFocusEvent.Type type)
111 {
112 return new FormFocusEvent(event, getHandler(), getName(), type);
113 }
114
115 /**
116 * Returns the form listener type for this adapter.
117 *
118 * @return the listener type
119 */
120 @Override
121 protected FormListenerType getListenerType()
122 {
123 return FormListenerType.FOCUS;
124 }
125 }