From d0a169567b95392a8eceda1380f5e25d6797404e Mon Sep 17 00:00:00 2001 From: Peter Schojer Date: Tue, 27 May 2008 13:59:06 +0000 Subject: [PATCH] simplified table serializing --- .../WebWidgets/ExtJS/ArrayTableSerializer.h | 21 +++-- .../Poco/WebWidgets/ExtJS/TableRenderer.h | 4 +- WebWidgets/ExtJS/src/ArrayTableSerializer.cpp | 12 +-- WebWidgets/ExtJS/src/TableRenderer.cpp | 14 +++- WebWidgets/ExtJS/testsuite/src/ExtJSTest.cpp | 11 ++- WebWidgets/WebWidgets_VS80.vcproj | 8 -- WebWidgets/include/Poco/WebWidgets/Table.h | 23 ++++-- .../Poco/WebWidgets/TableModelSerializer.h | 78 ------------------- WebWidgets/src/Table.cpp | 42 ++++++---- WebWidgets/src/TableModelSerializer.cpp | 54 ------------- 10 files changed, 74 insertions(+), 193 deletions(-) delete mode 100644 WebWidgets/include/Poco/WebWidgets/TableModelSerializer.h delete mode 100644 WebWidgets/src/TableModelSerializer.cpp diff --git a/WebWidgets/ExtJS/include/Poco/WebWidgets/ExtJS/ArrayTableSerializer.h b/WebWidgets/ExtJS/include/Poco/WebWidgets/ExtJS/ArrayTableSerializer.h index 4f7c4885c..8af09a836 100644 --- a/WebWidgets/ExtJS/include/Poco/WebWidgets/ExtJS/ArrayTableSerializer.h +++ b/WebWidgets/ExtJS/include/Poco/WebWidgets/ExtJS/ArrayTableSerializer.h @@ -41,31 +41,30 @@ #include "Poco/WebWidgets/ExtJS/ExtJS.h" -#include "Poco/WebWidgets/TableModelSerializer.h" -#include "Poco/Any.h" namespace Poco { namespace WebWidgets { + + class Table; + namespace ExtJS { -class ExtJS_API ArrayTableSerializer: public Poco::WebWidgets::TableModelSerializer +class ExtJS_API ArrayTableSerializer /// ArrayTableSerializer serializes a Table in JSON format { public: - typedef Poco::AutoPtr Ptr; - + static void serialize(std::ostream& ostr, const Poco::WebWidgets::Table* pTable, std::size_t rowBegin = 0, std::size_t rowCnt = 0); + /// Serializes the table starting with row 0. A rowCnt of 0 means serialize all rows + + static const std::string& contentType(); +private: ArrayTableSerializer(); /// Creates the ArrayTableSerializer with the given number of columns. - virtual ~ArrayTableSerializer(); + ~ArrayTableSerializer(); /// Destroys the ArrayTableSerializer. - - void serialize(std::ostream& ostr, const Poco::WebWidgets::Table* pTable, std::size_t rowBegin = 0, std::size_t rowCnt = 0); - /// Serializes the table starting with row 0. A rowCnt of 0 means serialize all rows - - const std::string& contentType() const; }; diff --git a/WebWidgets/ExtJS/include/Poco/WebWidgets/ExtJS/TableRenderer.h b/WebWidgets/ExtJS/include/Poco/WebWidgets/ExtJS/TableRenderer.h index f23117904..502771c79 100644 --- a/WebWidgets/ExtJS/include/Poco/WebWidgets/ExtJS/TableRenderer.h +++ b/WebWidgets/ExtJS/include/Poco/WebWidgets/ExtJS/TableRenderer.h @@ -44,6 +44,7 @@ #include "Poco/WebWidgets/Type.h" #include "Poco/WebWidgets/Renderer.h" #include "Poco/WebWidgets/Cell.h" +#include "Poco/WebWidgets/Table.h" #include @@ -51,7 +52,6 @@ namespace Poco { namespace WebWidgets { - class Table; class TableColumn; class TableModel; @@ -98,6 +98,8 @@ protected: static void renderStore(const Table* pTable, std::ostream& ostr); /// Renders the data store of the table + + static void onBeforeLoad(void* pSender, Table::LoadData& ld); }; diff --git a/WebWidgets/ExtJS/src/ArrayTableSerializer.cpp b/WebWidgets/ExtJS/src/ArrayTableSerializer.cpp index 7e3e43825..c148af50b 100644 --- a/WebWidgets/ExtJS/src/ArrayTableSerializer.cpp +++ b/WebWidgets/ExtJS/src/ArrayTableSerializer.cpp @@ -44,16 +44,6 @@ namespace WebWidgets { namespace ExtJS { -ArrayTableSerializer::ArrayTableSerializer() -{ -} - - -ArrayTableSerializer::~ArrayTableSerializer() -{ -} - - void ArrayTableSerializer::serialize(std::ostream& ostr, const Table* pTable, std::size_t rowBegin, std::size_t rowCntUser) { //[ @@ -119,7 +109,7 @@ void ArrayTableSerializer::serialize(std::ostream& ostr, const Table* pTable, st } -const std::string& ArrayTableSerializer::contentType() const +const std::string& ArrayTableSerializer::contentType() { static const std::string ct("text/javascript"); return ct; diff --git a/WebWidgets/ExtJS/src/TableRenderer.cpp b/WebWidgets/ExtJS/src/TableRenderer.cpp index c9af832c3..0ec5371f9 100644 --- a/WebWidgets/ExtJS/src/TableRenderer.cpp +++ b/WebWidgets/ExtJS/src/TableRenderer.cpp @@ -38,10 +38,12 @@ #include "Poco/WebWidgets/ExtJS/FormRenderer.h" #include "Poco/WebWidgets/ExtJS/Utility.h" #include "Poco/WebWidgets/ExtJS/TableCellHandlerFactory.h" +#include "Poco/WebWidgets/ExtJS/ArrayTableSerializer.h" #include "Poco/WebWidgets/Table.h" #include "Poco/WebWidgets/WebApplication.h" #include "Poco/WebWidgets/RequestHandler.h" #include "Poco/WebWidgets/DateFormatter.h" +#include "Poco/Delegate.h" #include @@ -146,7 +148,9 @@ void TableRenderer::renderProperties(const Table* pTable, const RenderContext& c ostr << ",height:" << pTable->getHeight(); ostr << ",store:"; renderStore(pTable, ostr); - WebApplication::instance().registerAjaxProcessor(Poco::NumberFormatter::format(id), const_cast(pTable)); + Table* pT = const_cast(pTable); + pT->beforeLoad += Poco::delegate(&TableRenderer::onBeforeLoad); + WebApplication::instance().registerAjaxProcessor(Poco::NumberFormatter::format(id), pT); } @@ -258,5 +262,13 @@ void TableRenderer::renderStore(const Table* pTable, std::ostream& ostr) } +void TableRenderer::onBeforeLoad(void* pSender, Table::LoadData& ld) +{ + ld.pResponse->setChunkedTransferEncoding(true); + ld.pResponse->setContentType(ArrayTableSerializer::contentType()); + std::ostream& out = ld.pResponse->send(); + ArrayTableSerializer::serialize(out, ld.pTable, ld.firstRow, ld.rowCnt); +} + } } } // namespace Poco::WebWidgets::ExtJS diff --git a/WebWidgets/ExtJS/testsuite/src/ExtJSTest.cpp b/WebWidgets/ExtJS/testsuite/src/ExtJSTest.cpp index ddd672a13..9e8088112 100644 --- a/WebWidgets/ExtJS/testsuite/src/ExtJSTest.cpp +++ b/WebWidgets/ExtJS/testsuite/src/ExtJSTest.cpp @@ -35,7 +35,6 @@ #include "CppUnit/TestSuite.h" #include "Poco/WebWidgets/ExtJS/Utility.h" #include "Poco/WebWidgets/ExtJS/TableCellHandler.h" -#include "Poco/WebWidgets/ExtJS/ArrayTableSerializer.h" #include "Poco/WebWidgets/Page.h" #include "Poco/WebWidgets/Renderer.h" #include "Poco/WebWidgets/RenderContext.h" @@ -1171,7 +1170,7 @@ void ExtJSTest::testTable() Table::TableColumns tc; tc.push_back(new TableColumn(0, "StaticText")); tc.push_back(new TableColumn(new CheckButtonCell(0, "Const", true), "CheckButton")); - Table::Ptr pTable = new Table(tc, new SimpleTableModel(2), new ArrayTableSerializer()); + Table::Ptr pTable = new Table(tc, new SimpleTableModel(2)); pTable->setValue(std::string("one"), 0,0); pTable->setValue(std::string("two"), 1,0); pTable->setValue(std::string("three"), 2,0); @@ -1202,7 +1201,7 @@ void ExtJSTest::testTableEdit() Table::TableColumns tc; tc.push_back(new TableColumn(new TextFieldCell(0), "DynText")); tc.push_back(new TableColumn(new CheckButtonCell(0, "Const", true), "CheckButton")); - Table::Ptr pTable = new Table(tc, new SimpleTableModel(2), new ArrayTableSerializer()); + Table::Ptr pTable = new Table(tc, new SimpleTableModel(2)); pTable->setValue(std::string("one"), 0,0); pTable->setValue(std::string("two"), 1,0); pTable->setValue(std::string("three"), 2,0); @@ -1237,7 +1236,7 @@ void ExtJSTest::testTableComboBox() pCom->insert(std::string("3")); tc.push_back(new TableColumn(pCom, "DynText")); tc.push_back(new TableColumn(new CheckButtonCell(0, "Const", true), "CheckButton")); - Table::Ptr pTable = new Table(tc, new SimpleTableModel(2), new ArrayTableSerializer()); + Table::Ptr pTable = new Table(tc, new SimpleTableModel(2)); pTable->setValue(std::string("1"), 0,0); pTable->setValue(std::string("2"), 1,0); pTable->setValue(std::string("3"), 2,0); @@ -1268,7 +1267,7 @@ void ExtJSTest::testTableButton() Table::TableColumns tc; tc.push_back(new TableColumn(new ButtonCell(0), "ButtonText")); tc.push_back(new TableColumn(new CheckButtonCell(0, "Const", true), "CheckButton")); - Table::Ptr pTable = new Table(tc, new SimpleTableModel(2), new ArrayTableSerializer()); + Table::Ptr pTable = new Table(tc, new SimpleTableModel(2)); pTable->setValue(std::string("one"), 0,0); pTable->setValue(std::string("two"), 1,0); pTable->setValue(std::string("three"), 2,0); @@ -1299,7 +1298,7 @@ void ExtJSTest::testTableImageButton() Table::TableColumns tc; tc.push_back(new TableColumn(new ImageButtonCell(0,new Image(Poco::URI("dummy.jpg"))))); tc.push_back(new TableColumn(new CheckButtonCell(0, "Const", true), "CheckButton")); - Table::Ptr pTable = new Table(tc, new SimpleTableModel(2), new ArrayTableSerializer()); + Table::Ptr pTable = new Table(tc, new SimpleTableModel(2)); pTable->setValue(std::string("Sunset.jpg"), 0,0); pTable->setValue(std::string("Sunset.jpg"), 1,0); pTable->setValue(std::string("Sunset.jpg"), 2,0); diff --git a/WebWidgets/WebWidgets_VS80.vcproj b/WebWidgets/WebWidgets_VS80.vcproj index 122633713..9ff7860f6 100644 --- a/WebWidgets/WebWidgets_VS80.vcproj +++ b/WebWidgets/WebWidgets_VS80.vcproj @@ -649,10 +649,6 @@ RelativePath=".\include\Poco\WebWidgets\TableModel.h" > - - @@ -717,10 +713,6 @@ RelativePath=".\src\TableModel.cpp" > - - diff --git a/WebWidgets/include/Poco/WebWidgets/Table.h b/WebWidgets/include/Poco/WebWidgets/Table.h index 9e2dc1962..3f378d2ba 100644 --- a/WebWidgets/include/Poco/WebWidgets/Table.h +++ b/WebWidgets/include/Poco/WebWidgets/Table.h @@ -46,7 +46,8 @@ #include "Poco/WebWidgets/TableColumn.h" #include "Poco/WebWidgets/RequestProcessor.h" #include "Poco/WebWidgets/JavaScriptEvent.h" -#include "Poco/WebWidgets/TableModelSerializer.h" +#include "Poco/FIFOEvent.h" +#include "Poco/Net/HTTPServerResponse.h" #include @@ -86,15 +87,26 @@ public: CellValueChange(std::size_t row, std::size_t col, const Poco::Any& oldValue, const Poco::Any& newValue); }; + struct LoadData + { + Poco::Net::HTTPServerResponse* pResponse; + Table* pTable; + int firstRow; + int rowCnt; + LoadData(Poco::Net::HTTPServerResponse* pResponse, Table* pTable, int firstRow, int rowCnt); + }; + JavaScriptEvent cellClicked; JavaScriptEvent cellValueChanged; + FIFOEvent beforeLoad; /// thrown whenever a load is requested, internal event to which the TableRenderer must register - Table(const TableColumns& tc, TableModel::Ptr pModel, TableModelSerializer::Ptr pSer); + + Table(const TableColumns& tc, TableModel::Ptr pModel); /// Creates an anonymous Table. - Table(const std::string& name, const TableColumns& tc, TableModel::Ptr pModel, TableModelSerializer::Ptr pSer); + Table(const std::string& name, const TableColumns& tc, TableModel::Ptr pModel); /// Creates a Table with the given name. std::size_t getColumnCount() const; @@ -140,10 +152,10 @@ private: void handleVal(const std::string& val); void handleCnt(const std::string& val); protected: - Table(const std::string& name, const std::type_info& type, const TableColumns& tc, TableModel::Ptr pModel, TableModelSerializer::Ptr pSer); + Table(const std::string& name, const std::type_info& type, const TableColumns& tc, TableModel::Ptr pModel); /// Creates a Table and assigns it the given name. - Table(const std::type_info& type, const TableColumns& tc, TableModel::Ptr pModel, TableModelSerializer::Ptr pSer); + Table(const std::type_info& type, const TableColumns& tc, TableModel::Ptr pModel); /// Creates a Table. ~Table(); @@ -160,7 +172,6 @@ private: int _cnt; std::string _val; std::string _ev; - TableModelSerializer::Ptr _pSer; }; diff --git a/WebWidgets/include/Poco/WebWidgets/TableModelSerializer.h b/WebWidgets/include/Poco/WebWidgets/TableModelSerializer.h deleted file mode 100644 index 14ed6e870..000000000 --- a/WebWidgets/include/Poco/WebWidgets/TableModelSerializer.h +++ /dev/null @@ -1,78 +0,0 @@ -// -// TableModelSerializer.h -// -// $Id: //poco/Main/WebWidgets/include/Poco/WebWidgets/TableModelSerializer.h#4 $ -// -// Library: WebWidgets -// Package: Views -// Module: TableModelSerializer -// -// Definition of the TableModelSerializer class. -// -// 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. -// - - -#ifndef WebWidgets_TableModelSerializer_INCLUDED -#define WebWidgets_TableModelSerializer_INCLUDED - - -#include "Poco/WebWidgets/WebWidgets.h" -#include "Poco/RefCountedObject.h" -#include "Poco/AutoPtr.h" - - -namespace Poco { -namespace WebWidgets { - - -class Table; - - -class WebWidgets_API TableModelSerializer: public Poco::RefCountedObject - /// TableModelSerializer defines the interface for Table serialization -{ -public: - typedef Poco::AutoPtr Ptr; - - TableModelSerializer(); - /// Creates the TableModelSerializer with the given number of columns. - - virtual ~TableModelSerializer(); - /// Destroys the TableModelSerializer. - - virtual void serialize(std::ostream& ostr, const Table* pTable, std::size_t rowBegin = 0, std::size_t rowCnt = 0) = 0; - /// Serializes the table starting with row 0. A rowCnt <= 0 means serialize all rows - - virtual const std::string& contentType() const = 0; - /// Returns the content type that the answer will be encoded in -}; - - -} } // namespace Poco::WebWidgets - - -#endif // WebWidgets_TableModelSerializer_INCLUDED diff --git a/WebWidgets/src/Table.cpp b/WebWidgets/src/Table.cpp index f72fb7173..efffb5d8a 100644 --- a/WebWidgets/src/Table.cpp +++ b/WebWidgets/src/Table.cpp @@ -53,7 +53,7 @@ const std::string Table::EV_CELLVALUECHANGED("edit"); const std::string Table::EV_LOADDATA("load"); -Table::Table(const TableColumns& tc, TableModel::Ptr pModel, TableModelSerializer::Ptr pSer): +Table::Table(const TableColumns& tc, TableModel::Ptr pModel): View(typeid(Table)), _pModel(pModel), _columns(tc), @@ -61,14 +61,13 @@ Table::Table(const TableColumns& tc, TableModel::Ptr pModel, TableModelSerialize _row(-1), _cnt(-1), _val(), - _ev(), - _pSer(pSer) + _ev() { checkValidConfig(); } -Table::Table(const std::string& name, const TableColumns& tc, TableModel::Ptr pModel, TableModelSerializer::Ptr pSer): +Table::Table(const std::string& name, const TableColumns& tc, TableModel::Ptr pModel): View(name, typeid(Table)), _pModel(pModel), _columns(tc), @@ -76,14 +75,13 @@ Table::Table(const std::string& name, const TableColumns& tc, TableModel::Ptr pM _row(-1), _cnt(-1), _val(), - _ev(), - _pSer(pSer) + _ev() { checkValidConfig(); } -Table::Table(const std::string& name, const std::type_info& type, const TableColumns& tc, TableModel::Ptr pModel, TableModelSerializer::Ptr pSer): +Table::Table(const std::string& name, const std::type_info& type, const TableColumns& tc, TableModel::Ptr pModel): View(name, type), _pModel(pModel), _columns(tc), @@ -91,14 +89,13 @@ Table::Table(const std::string& name, const std::type_info& type, const TableCol _row(-1), _cnt(-1), _val(), - _ev(), - _pSer(pSer) + _ev() { checkValidConfig(); } -Table::Table(const std::type_info& type, const TableColumns& tc, TableModel::Ptr pModel, TableModelSerializer::Ptr pSer): +Table::Table(const std::type_info& type, const TableColumns& tc, TableModel::Ptr pModel): View(type), _pModel(pModel), _columns(tc), @@ -106,8 +103,7 @@ Table::Table(const std::type_info& type, const TableColumns& tc, TableModel::Ptr _row(-1), _cnt(-1), _val(), - _ev(), - _pSer(pSer) + _ev() { checkValidConfig(); } @@ -133,7 +129,6 @@ void Table::checkValidConfig() if ((*it)) adoptChild((*it));; } - poco_check_ptr (_pSer); } @@ -173,14 +168,18 @@ void Table::handleRequestAndResponse(const Poco::Net::HTTPServerRequest& request { /// serialize the Table back /// check for cnt and start if only a segment was requested - response.setChunkedTransferEncoding(true); - response.setContentType(_pSer->contentType()); - std::ostream& out = response.send(); + if (_row < 0) _row = 0; if (_cnt < 0) _cnt = 0; - _pSer->serialize(out, this, _row, _cnt); + LoadData ld(&response, this, _row, _cnt); + _col = -1; + _row = -1; + _cnt = -1; + _val.clear(); + _ev.clear(); + beforeLoad.notify(this, ld); } else { @@ -272,4 +271,13 @@ Table::CellValueChange::CellValueChange(std::size_t r, std::size_t c, const Poco } +Table::LoadData::LoadData(Poco::Net::HTTPServerResponse* pR, Table* pT, int row, int cnt): + pResponse(pR), + pTable(pT), + firstRow(row), + rowCnt(cnt) +{ +} + + } } // namespace Poco::WebWidgets diff --git a/WebWidgets/src/TableModelSerializer.cpp b/WebWidgets/src/TableModelSerializer.cpp deleted file mode 100644 index e16379373..000000000 --- a/WebWidgets/src/TableModelSerializer.cpp +++ /dev/null @@ -1,54 +0,0 @@ -// -// TableModelSerializer.cpp -// -// $Id: //poco/Main/WebWidgets/src/TableModelSerializer.cpp#3 $ -// -// Library: WebWidgets -// Package: Views -// Module: TableModelSerializer -// -// 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/TableModelSerializer.h" - - -namespace Poco { -namespace WebWidgets { - - -TableModelSerializer::TableModelSerializer() -{ -} - - -TableModelSerializer::~TableModelSerializer() -{ -} - - -} } // namespace Poco::WebWidgets