SGPropertyNode::set_bool (bool val)
{
if (_tied) {
- return _value.bool_val->setValue(val);
+ if (_value.bool_val->setValue(val)) {
+ firePropertyChange();
+ return true;
+ } else {
+ return false;
+ }
} else {
_local_val.bool_val = val;
+ firePropertyChange();
return true;
}
}
SGPropertyNode::set_int (int val)
{
if (_tied) {
- return _value.int_val->setValue(val);
+ if (_value.int_val->setValue(val)) {
+ firePropertyChange();
+ return true;
+ } else {
+ return false;
+ }
} else {
_local_val.int_val = val;
+ firePropertyChange();
return true;
}
}
SGPropertyNode::set_long (long val)
{
if (_tied) {
- return _value.long_val->setValue(val);
+ if (_value.long_val->setValue(val)) {
+ firePropertyChange();
+ return true;
+ } else {
+ return false;
+ }
} else {
_local_val.long_val = val;
+ firePropertyChange();
return true;
}
}
SGPropertyNode::set_float (float val)
{
if (_tied) {
- return _value.float_val->setValue(val);
+ if (_value.float_val->setValue(val)) {
+ firePropertyChange();
+ return true;
+ } else {
+ return false;
+ }
} else {
_local_val.float_val = val;
+ firePropertyChange();
return true;
}
}
SGPropertyNode::set_double (double val)
{
if (_tied) {
- return _value.double_val->setValue(val);
+ if (_value.double_val->setValue(val)) {
+ firePropertyChange();
+ return true;
+ } else {
+ return false;
+ }
} else {
_local_val.double_val = val;
+ firePropertyChange();
return true;
}
}
SGPropertyNode::set_string (const char * val)
{
if (_tied) {
- return _value.string_val->setValue(val);
+ if (_value.string_val->setValue(val)) {
+ firePropertyChange();
+ return true;
+ } else {
+ return false;
+ }
} else {
delete [] _local_val.string_val;
_local_val.string_val = copy_string(val);
+ firePropertyChange();
return true;
}
}
_type(NONE),
_tied(false),
_attr(READ|WRITE),
- _count(0)
+ _count(0),
+ _listeners(0)
{
_local_val.string_val = 0;
}
_type(node._type),
_tied(node._tied),
_attr(node._attr),
- _count(0)
+ _count(0),
+ _listeners(0) // CHECK!!
{
_name = copy_string(node._name);
_local_val.string_val = 0;
_type(NONE),
_tied(false),
_attr(READ|WRITE),
- _count(0)
+ _count(0),
+ _listeners(0)
{
_name = copy_string(name);
_local_val.string_val = 0;
delete [] _name;
delete _path_cache;
clear_value();
+ delete _listeners;
}
node = new SGPropertyNode(name, index, this);
}
_children.push_back(node);
+ if (_parent != 0)
+ _parent->firePropertyChange();
return _children[_children.size()-1];
} else {
return 0;
node->setAttribute(REMOVED, true);
ret = node;
}
+ if (_parent != 0)
+ _parent->firePropertyChange();
return ret;
}
return (node == 0 ? false : node->untie());
}
+void
+SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener)
+{
+ if (_listeners == 0)
+ _listeners = new vector<SGPropertyChangeListener *>;
+ _listeners->push_back(listener);
+}
+
+void
+SGPropertyNode::firePropertyChange (SGPropertyNode * node)
+{
+ if (_listeners != 0) {
+ for (int i = 0; i < _listeners->size(); i++) {
+ (*_listeners)[i]->propertyChanged(node);
+ }
+ }
+ if (_parent != 0)
+ _parent->firePropertyChange(node);
+}
+
\f
////////////////////////////////////////////////////////////////////////
*/
operator const SGPropertyNode *() const;
+ /**
+ * Return the pointer.
+ */
+ SGPropertyNode * ptr () { return _ptr; }
+
/**
* Validity test
*/
SGPropertyNode *_ptr;
};
+
+\f
+/**
+ * The property change listener interface.
+ *
+ * <p>Any class that needs to listen for property changes must implement
+ * this interface.</p>
+ */
+class SGPropertyChangeListener
+{
+public:
+
+ virtual void propertyChanged (SGPropertyNode * node) = 0;
+
+};
+
+
\f
/**
* A node in a property tree.
bool untie (const char * relative_path);
+ /**
+ * Add a change listener to the property.
+ */
+ void addChangeListener (SGPropertyChangeListener * listener);
+
+
+ /**
+ * Fire a property change to all listeners.
+ */
+ void firePropertyChange ()
+ {
+ firePropertyChange(this);
+ }
+
+
protected:
+ /**
+ * Fire a property change with an explicit target node.
+ */
+ void firePropertyChange (SGPropertyNode * node);
+
+
/**
* Protected constructor for making new nodes on demand.
*/
char * string_val;
} _local_val;
+ vector <SGPropertyChangeListener *> * _listeners;
+
\f
/**