1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.util;
21
22 import java.beans.PropertyChangeEvent;
23 import java.beans.PropertyChangeListener;
24 import java.lang.reflect.InvocationTargetException;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import javax.swing.JComponent;
29
30 import com.cosylab.introspection.BeanIntrospector;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public final class UserSettingsProtection implements PropertyChangeListener {
50
51 static final Map<JComponent,UserSettingsProtection> protections= new HashMap<JComponent,UserSettingsProtection>(10);
52 private JComponent bean;
53 private Map<String,Boolean> prot= new HashMap<String,Boolean>(5);
54
55
56
57
58 private UserSettingsProtection(JComponent bean) {
59 this.bean=bean;
60 }
61
62
63
64
65
66
67
68 public static void setProtection(JComponent bean, String[] properties, boolean protection) {
69 UserSettingsProtection p= protections.get(bean);
70 if (protection) {
71 if (p==null) {
72 p= new UserSettingsProtection(bean);
73 protections.put(bean,p);
74 }
75 p.setProtection(properties,protection);
76 } else {
77 if (p!=null) {
78 p.setProtection(properties,protection);
79 }
80 }
81 }
82
83 public void clearProtections() {
84 for (String property : prot.keySet()) {
85 bean.removePropertyChangeListener(property,this);
86 }
87 prot.clear();
88 }
89
90 public void setProtection(String[] properties, boolean protection) {
91 for (int i = 0; i < properties.length; i++) {
92 prot.put(properties[i],protection);
93 if (protection) {
94 bean.addPropertyChangeListener(properties[i],this);
95 } else {
96 bean.removePropertyChangeListener(properties[i],this);
97 }
98 }
99 }
100
101
102
103
104
105
106
107
108
109
110
111 public static boolean setUnprotected(JComponent bean, String property, Object value) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
112 if (property==null || value== null) {
113 return false;
114 }
115 UserSettingsProtection p= protections.get(bean);
116 if (p!=null) {
117 return p.setUnprotected(property,value);
118 }
119 BeanIntrospector.setPropertyValue(bean,property,value);
120 return true;
121 }
122
123
124
125
126
127
128
129
130
131
132 public boolean setUnprotected(String property, Object value) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
133 if (property==null || value== null) {
134 return false;
135 }
136 Boolean b= prot.get(property);
137 if (b!=null && b.booleanValue()) {
138 return false;
139 }
140 if (b!=null) {
141 bean.removePropertyChangeListener(property,this);
142 BeanIntrospector.setPropertyValue(bean,property,value);
143 bean.addPropertyChangeListener(property,this);
144 } else {
145 BeanIntrospector.setPropertyValue(bean,property,value);
146 }
147 return true;
148 }
149
150
151
152
153 public void propertyChange(PropertyChangeEvent evt) {
154 String p= evt.getPropertyName();
155 if (prot.containsKey(p)) prot.put(p,true);
156 }
157
158 }