Project

General

Profile

Bug #6681 ยป listener-patch.txt

Daniel Crawl, 03/02/2015 02:01 PM

 
1
Index: actor/gui/PtolemyQuery.java
2
===================================================================
3
--- actor/gui/PtolemyQuery.java	(revision 71666)
4
+++ actor/gui/PtolemyQuery.java	(working copy)
5
@@ -535,6 +535,12 @@
6
 
7
         // Attach the entry to the attribute by registering a listener.
8
         attribute.addValueListener(this);
9
+        
10
+        // if the attribute is a Variable, set a weak dependency to avoid
11
+        // warnings if the attribute changes containers.
12
+        if(attribute instanceof Variable) {
13
+            ((Variable)attribute).setValueListenerAsWeakDependency(this);
14
+        }
15
 
16
         // Put the attribute in a Map from attribute -> (list of entry names
17
         // attached to attribute), but only if entryName is not already
18
Index: data/expr/Variable.java
19
===================================================================
20
--- data/expr/Variable.java	(revision 71666)
21
+++ data/expr/Variable.java	(working copy)
22
@@ -816,6 +816,9 @@
23
         if (_valueListeners != null) {
24
             _valueListeners.remove(listener);
25
         }
26
+        if (_weakValueListeners != null) {
27
+            _weakValueListeners.remove(listener);
28
+        }
29
     }
30
 
31
     /** Reset the variable to its initial value. If the variable was
32
@@ -882,16 +885,26 @@
33
             NameDuplicationException {
34
         Nameable previousContainer = getContainer();
35
 
36
+        
37
         // Warn if there are variables that depend on this one.
38
-        if (container != previousContainer && previousContainer != null
39
-                && _valueListeners != null && _valueListeners.size() > 0) {
40
-            if (!MessageHandler
41
-                    .yesNoQuestion("WARNING: There are variables depending on "
42
-                            + getName() + ". Continue?")) {
43
-                // Cancel.
44
-                throw new IllegalActionException(this,
45
-                        "Cancelled change of container.");
46
+        if (container != previousContainer && previousContainer != null) {
47
+            Set<ValueListener> listeners = new HashSet<ValueListener>();
48
+            if(_valueListeners != null && !_valueListeners.isEmpty()) {
49
+                listeners.addAll(_valueListeners);
50
             }
51
+            if(_weakValueListeners != null && !_weakValueListeners.isEmpty()) {
52
+                listeners.removeAll(_weakValueListeners);
53
+            }
54
+            
55
+            if(!listeners.isEmpty()) {
56
+                if (!MessageHandler
57
+                        .yesNoQuestion("WARNING: There are variables depending on "
58
+                                + getName() + ". Continue?")) {
59
+                    // Cancel.
60
+                    throw new IllegalActionException(this,
61
+                            "Cancelled change of container.");
62
+                }
63
+            }
64
         }
65
 
66
         super.setContainer(container);
67
@@ -1382,6 +1395,17 @@
68
         _isTokenUnknown = value;
69
     }
70
 
71
+    /** Set a value listener as a weak dependency. When this Variable changes
72
+     *  containers, the value listener is not considered a dependency.
73
+     *  @see #setContainer()
74
+     */
75
+    public void setValueListenerAsWeakDependency(ValueListener listener) {
76
+        if (_weakValueListeners == null) {
77
+            _weakValueListeners = new HashSet<ValueListener>();
78
+        }           
79
+        _weakValueListeners.add(listener);
80
+    }
81
+    
82
     /** Set the visibility of this variable.  The argument should be one
83
      *  of the public static instances in Settable.
84
      *  @param visibility The visibility of this variable.
85
@@ -2536,7 +2560,11 @@
86
 
87
     // The visibility of this variable.
88
     private Settable.Visibility _visibility = Settable.EXPERT;
89
+    
90
+    /** Value listeners that should not be treated as true dependencies. */
91
+    private Set<ValueListener> _weakValueListeners;
92
 
93
+
94
     ///////////////////////////////////////////////////////////////////
95
     ////                         inner classes                     ////
96
 
    (1-1/1)