Exemplo n.º 1
0
// SetTo
void
PropertyListView::SetTo(PropertyObject* object)
{
	// try to do without rebuilding the list
	// it should in fact be pretty unlikely that this does not
	// work, but we keep being defensive
	if (fPropertyObject && object &&
		fPropertyObject->ContainsSameProperties(*object)) {
		// iterate over view items and update their value views
		bool error = false;
		for (int32 i = 0; PropertyItemView* item = _ItemAt(i); i++) {
			Property* property = object->PropertyAt(i);
			if (!item->AdoptProperty(property)) {
				// the reason for this can be that the property is
				// unkown to the PropertyEditorFactory and therefor
				// there is no editor view at this item
				fprintf(stderr, "PropertyListView::_SetTo() - "
								"property mismatch at %ld\n", i);
				error = true;
				break;
			}
			if (property)
				item->SetEnabled(property->IsEditable());
		}
		// we didn't need to make empty, but transfer ownership
		// of the object
		if (!error) {
			// if the "adopt" process went only halfway,
			// some properties of the original object
			// are still referenced, so we can only
			// delete the original object if the process
			// was successful and leak Properties otherwise,
			// but this case is only theoretical anyways...
			delete fPropertyObject;
		}
		fPropertyObject = object;
	} else {
		// remember scroll pos, selection and focused item
		BPoint scrollOffset = ScrollOffset();
		BList selection(20);
		int32 focused = -1;
		for (int32 i = 0; PropertyItemView* item = _ItemAt(i); i++) {
			if (item->IsSelected())
				selection.AddItem((void*)i);
			if (item->IsFocused())
				focused = i;
		}
		if (Window())
			Window()->BeginViewTransaction();
		fSuspendUpdates = true;

		// rebuild list
		_MakeEmpty();
		fPropertyObject = object;

		if (fPropertyObject) {
			// fill with content
			for (int32 i = 0; Property* property = fPropertyObject->PropertyAt(i); i++) {
				PropertyItemView* item = new PropertyItemView(property);
				item->SetEnabled(property->IsEditable());
				_AddItem(item);
			}
			_LayoutItems();

			// restore scroll pos, selection and focus
			SetScrollOffset(scrollOffset);
			for (int32 i = 0; PropertyItemView* item = _ItemAt(i); i++) {
				if (selection.HasItem((void*)i))
					item->SetSelected(true);
				if (i == focused)
					item->MakeFocus(true);
			}
		}

		if (Window())
			Window()->EndViewTransaction();
		fSuspendUpdates = false;

		SetDataRect(_ItemsRect());
	}

	_UpdateSavedProperties();
	_CheckMenuStatus();
	Invalidate();
}