[927] | 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 | |
---|
[922] | 15 | package de.ugoe.cs.autoquest.jfcmonitor; |
---|
[850] | 16 | |
---|
| 17 | import java.awt.Component; |
---|
| 18 | import java.beans.PropertyChangeEvent; |
---|
| 19 | import java.beans.PropertyChangeListener; |
---|
| 20 | |
---|
[851] | 21 | import javax.accessibility.AccessibleContext; |
---|
| 22 | |
---|
[850] | 23 | import de.ugoe.cs.util.StringTools; |
---|
| 24 | |
---|
| 25 | /** |
---|
[853] | 26 | * <p> |
---|
[928] | 27 | * Listener that listens on the name change of a component. |
---|
[853] | 28 | * </p> |
---|
[928] | 29 | * |
---|
[850] | 30 | * @author Fabian Glaser |
---|
[853] | 31 | * @version 1.0 |
---|
[850] | 32 | */ |
---|
[928] | 33 | public class JFCTitleChangeListener implements PropertyChangeListener { |
---|
| 34 | /** |
---|
| 35 | * <p> |
---|
| 36 | * Writer for logging events. |
---|
| 37 | * </p> |
---|
| 38 | */ |
---|
| 39 | final private JFCMonitorOutputWriter outputWriter; |
---|
| 40 | |
---|
[1269] | 41 | final static private boolean CHECK_HIERARCHY = true; |
---|
[928] | 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 | |
---|
[850] | 55 | @Override |
---|
[928] | 56 | public synchronized void propertyChange(PropertyChangeEvent evt) { |
---|
[854] | 57 | String propertyName = evt.getPropertyName(); |
---|
| 58 | Component component = null; |
---|
[928] | 59 | int titleSource = JFCComponentTitleHierachy.SOURCE_NOT_KNOWN; |
---|
| 60 | |
---|
| 61 | // Set source component and title source |
---|
| 62 | if (propertyName.equals("AccessibleName")) { |
---|
[851] | 63 | AccessibleContext context = (AccessibleContext) evt.getSource(); |
---|
[854] | 64 | component = (Component) context.getAccessibleParent(); |
---|
[928] | 65 | titleSource = JFCComponentTitleHierachy.ACCESSIBLE_NAME; |
---|
[854] | 66 | } |
---|
[928] | 67 | |
---|
| 68 | if (propertyName.equals("name")) { |
---|
[854] | 69 | component = (Component) evt.getSource(); |
---|
[928] | 70 | titleSource = JFCComponentTitleHierachy.NAME; |
---|
[854] | 71 | } |
---|
[928] | 72 | |
---|
| 73 | if (propertyName.equals("icon")) { |
---|
| 74 | component = (Component) evt.getSource(); |
---|
| 75 | titleSource = JFCComponentTitleHierachy.ICON; |
---|
[854] | 76 | } |
---|
[928] | 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(); |
---|
[934] | 88 | // We only print the title change message if the new title is as |
---|
[928] | 89 | // least as informative as the initial title |
---|
| 90 | if ((!CHECK_HIERARCHY || oldTitleSource <= titleSource)) { |
---|
| 91 | StringBuilder builder = new StringBuilder(); |
---|
[934] | 92 | builder.append("<componentTitleChange hash=\""); |
---|
[928] | 93 | builder.append(Integer.toHexString(component.hashCode())); |
---|
[934] | 94 | builder.append("\" newTitle=\"" + newTitle); |
---|
| 95 | builder.append("\" titleSource=\"" + titleSource); |
---|
[928] | 96 | builder.append("\" />" + StringTools.ENDLINE); |
---|
| 97 | outputWriter.write(builder.toString()); |
---|
| 98 | } |
---|
| 99 | } else { |
---|
[854] | 100 | throw new AssertionError("Referenced component is not known."); |
---|
| 101 | } |
---|
| 102 | } |
---|
[850] | 103 | } |
---|
[928] | 104 | |
---|
[850] | 105 | } |
---|