// // ListBoxCell.cpp // // $Id: //poco/Main/WebWidgets/src/ListBoxCell.cpp#2 $ // // Library: WebWidgets // Package: Controls // Module: ListBoxCell // // Copyright (c) 2007, Applied Informatics Software Engineering GmbH. // and Contributors. // // Permission is hereby granted, free of charge, to any person or organization // obtaining a copy of the software and accompanying documentation covered by // this license (the "Software") to use, reproduce, display, distribute, // execute, and transmit the Software, and to prepare derivative works of the // Software, and to permit third-parties to whom the Software is furnished to // do so, all subject to the following: // // The copyright notices in the Software and this entire statement, including // the above license grant, this restriction and the following disclaimer, // must be included in all copies of the Software, in whole or in part, and // all derivative works of the Software, unless such copies or derivative // works are solely in the form of machine-executable object code generated by // a source language processor. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. // #include "Poco/WebWidgets/ListBoxCell.h" #include "Poco/DateTime.h" namespace Poco { namespace WebWidgets { ListBoxCell::ListBoxCell(View* pOwner): Cell(pOwner, typeid(ListBoxCell)), _data(), _fmtCache(), _height(-1), _width(-1) { } ListBoxCell::ListBoxCell(View* pOwner, const std::type_info& type): Cell(pOwner, type), _data(), _fmtCache(), _height(-1), _width(-1) { } ListBoxCell::~ListBoxCell() { } void ListBoxCell::handleForm(const std::string& field, const std::string& value) { throw Poco::NotImplementedException(); } ListBoxCell::Data::iterator ListBoxCell::find(const Any& elem) { Data::iterator it = begin(); Formatter::Ptr ptr=getFormatter(); std::string elemStr(ptr->format(elem)); std::size_t pos(0); while (it != end()) { if (elemStr == _fmtCache[pos]) { return it; } ++it; ++pos; } return it; } ListBoxCell::Data::const_iterator ListBoxCell::find(const Any& elem) const { Data::const_iterator it = begin(); Formatter::Ptr ptr=getFormatter(); std::string elemStr(ptr->format(elem)); std::size_t pos(0); while (it != end()) { if (elemStr == _fmtCache[pos]) { return it; } ++it; ++pos; } return it; } void ListBoxCell::setElements(const Data& elems) { _data = elems; // update _fmtCache _fmtCache.clear(); Formatter::Ptr ptr = getFormatter(); Data::iterator it = _data.begin(); for (; it != _data.end(); ++it) _fmtCache.push_back(ptr->format(it->first)); } void ListBoxCell::erase(const Any& elem) { Data::iterator it = find(elem); if (it != _data.end()) _data.erase(it); } void ListBoxCell::select(const Any& elem) { Data::iterator it = find(elem); if (it != _data.end()) it->second = true; } void ListBoxCell::deselect(const Any& elem) { Data::iterator it = find(elem); if (it != _data.end()) it->second = false; } bool ListBoxCell::hasSelected() const { Data::const_iterator it = _data.begin(); for (; it != _data.end(); ++it) if (it->second) return true; return false; } const Any& ListBoxCell::getSelected() const { Data::const_iterator it = _data.begin(); for (; it != _data.end(); ++it) if (it->second) return it->first; throw Poco::NotFoundException("No element selected"); } bool ListBoxCell::serializeJSON(std::ostream& out, const std::string& name) { out << name << ":"; if (hasSelected()) { const Poco::Any& sel = getSelected(); if (sel.type() == typeid(std::string) || sel.type() == typeid(Poco::DateTime)) out << ":'" << getFormatter()->format(sel) << "'"; else out << ":" << getFormatter()->format(sel); } else out << "''"; return true; } } } // namespace Poco::WebWidgets