source: trunk/autoquest-jfcmonitor/src/main/java/de/ugoe/cs/autoquest/jfcmonitor/JFCTitleChangeListener.java @ 928

Last change on this file since 928 was 928, checked in by fglaser, 12 years ago
  • Title change hierarchy control added
  • JFCNameChangeListener renamed to JFCTitleChangeListener
  • JFCTitleChangeListener now additionaly listens on icon and position changes
  • Property svn:mime-type set to text/plain
File size: 3.2 KB
Line 
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
15package de.ugoe.cs.autoquest.jfcmonitor;
16
17import java.awt.Component;
18import java.beans.PropertyChangeEvent;
19import java.beans.PropertyChangeListener;
20
21import javax.accessibility.AccessibleContext;
22
23import 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 */
33public class JFCTitleChangeListener implements PropertyChangeListener {
34        /**
35         * <p>
36         * Writer for logging events.
37         * </p>
38         */
39        final private JFCMonitorOutputWriter outputWriter;
40
41        final 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 name 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("<componentNameChange hash=\"");
93                                        builder.append(Integer.toHexString(component.hashCode()));
94                                        builder.append("\" newName=\"" + newTitle);
95                                        builder.append("\" source=\"" + propertyName);
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}
Note: See TracBrowser for help on using the repository browser.