1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
2 | // |
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | // you may not use this file except in compliance with the License. |
---|
5 | // You may obtain a copy of the License at |
---|
6 | // |
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | // |
---|
9 | // Unless required by applicable law or agreed to in writing, software |
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | // See the License for the specific language governing permissions and |
---|
13 | // limitations under the License. |
---|
14 | |
---|
15 | package de.ugoe.cs.autoquest.jfcmonitor; |
---|
16 | |
---|
17 | import java.awt.Component; |
---|
18 | import java.beans.PropertyChangeEvent; |
---|
19 | import java.beans.PropertyChangeListener; |
---|
20 | |
---|
21 | import javax.accessibility.AccessibleContext; |
---|
22 | |
---|
23 | import de.ugoe.cs.util.StringTools; |
---|
24 | |
---|
25 | /** |
---|
26 | * <p> |
---|
27 | * Listener that listens on the name change of a component. |
---|
28 | * </p> |
---|
29 | * |
---|
30 | * @author Fabian Glaser |
---|
31 | * @version 1.0 |
---|
32 | */ |
---|
33 | public class JFCTitleChangeListener implements PropertyChangeListener { |
---|
34 | /** |
---|
35 | * <p> |
---|
36 | * Writer for logging events. |
---|
37 | * </p> |
---|
38 | */ |
---|
39 | final private JFCMonitorOutputWriter outputWriter; |
---|
40 | |
---|
41 | final static private boolean CHECK_HIERARCHY = true; |
---|
42 | |
---|
43 | /** |
---|
44 | * <p> |
---|
45 | * Constructor. Creates a new JFCNameChangeListener with a given |
---|
46 | * {@link JFCMonitorOutputWriter}. |
---|
47 | * </p> |
---|
48 | * |
---|
49 | * @param outputWriter |
---|
50 | */ |
---|
51 | public JFCTitleChangeListener(JFCMonitorOutputWriter outputWriter) { |
---|
52 | this.outputWriter = outputWriter; |
---|
53 | } |
---|
54 | |
---|
55 | @Override |
---|
56 | public synchronized void propertyChange(PropertyChangeEvent evt) { |
---|
57 | String propertyName = evt.getPropertyName(); |
---|
58 | Component component = null; |
---|
59 | int titleSource = JFCComponentTitleHierachy.SOURCE_NOT_KNOWN; |
---|
60 | |
---|
61 | // Set source component and title source |
---|
62 | if (propertyName.equals("AccessibleName")) { |
---|
63 | AccessibleContext context = (AccessibleContext) evt.getSource(); |
---|
64 | component = (Component) context.getAccessibleParent(); |
---|
65 | titleSource = JFCComponentTitleHierachy.ACCESSIBLE_NAME; |
---|
66 | } |
---|
67 | |
---|
68 | if (propertyName.equals("name")) { |
---|
69 | component = (Component) evt.getSource(); |
---|
70 | titleSource = JFCComponentTitleHierachy.NAME; |
---|
71 | } |
---|
72 | |
---|
73 | if (propertyName.equals("icon")) { |
---|
74 | component = (Component) evt.getSource(); |
---|
75 | titleSource = JFCComponentTitleHierachy.ICON; |
---|
76 | } |
---|
77 | |
---|
78 | if (propertyName.equals("x") || propertyName.equals("y")) { |
---|
79 | component = (Component) evt.getSource(); |
---|
80 | titleSource = JFCComponentTitleHierachy.POS; |
---|
81 | } |
---|
82 | |
---|
83 | if (component != null) { |
---|
84 | JFCComponent jfcComponent = JFCComponent.find(component); |
---|
85 | if (jfcComponent != null) { |
---|
86 | int oldTitleSource = jfcComponent.getTitleSource(); |
---|
87 | Object newTitle = evt.getNewValue(); |
---|
88 | // We only print the title change message if the new title is as |
---|
89 | // least as informative as the initial title |
---|
90 | if ((!CHECK_HIERARCHY || oldTitleSource <= titleSource)) { |
---|
91 | StringBuilder builder = new StringBuilder(); |
---|
92 | builder.append("<componentTitleChange hash=\""); |
---|
93 | builder.append(Integer.toHexString(component.hashCode())); |
---|
94 | builder.append("\" newTitle=\"" + newTitle); |
---|
95 | builder.append("\" titleSource=\"" + titleSource); |
---|
96 | builder.append("\" />" + StringTools.ENDLINE); |
---|
97 | outputWriter.write(builder.toString()); |
---|
98 | } |
---|
99 | } else { |
---|
100 | throw new AssertionError("Referenced component is not known."); |
---|
101 | } |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | } |
---|