001: //The Salmon Open Framework for Internet Applications (SOFIA)
002: //Copyright (C) 1999 - 2002, Salmon LLC
003: //
004: //This program is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU General Public License version 2
006: //as published by the Free Software Foundation;
007: //
008: //This program is distributed in the hope that it will be useful,
009: //but WITHOUT ANY WARRANTY; without even the implied warranty of
010: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: //GNU General Public License for more details.
012: //
013: //You should have received a copy of the GNU General Public License
014: //along with this program; if not, write to the Free Software
015: //Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
016: //
017: //For more information please visit http://www.salmonllc.com
018: package com.salmonllc.examples.example14;
019:
020: import com.salmonllc.jsp.*;
021: import com.salmonllc.html.events.*;
022: import com.salmonllc.sql.*;
023:
024: import java.sql.SQLException;
025:
026: public class ListDataEntryController extends JspController implements
027: SubmitListener, PageListener {
028:
029: //Visual Components
030: public com.salmonllc.html.HtmlCheckBox _delete;
031: public com.salmonllc.html.HtmlSubmitButton _add;
032: public com.salmonllc.html.HtmlSubmitButton _save;
033: public com.salmonllc.html.HtmlTextEdit _firstName;
034: public com.salmonllc.jsp.JspDataTable _datatable1;
035: public com.salmonllc.html.HtmlValidatorText _errorMessage;
036:
037: //DataSources
038: public com.salmonllc.examples.example8.ContactModel _dsContact;
039: private String DELETE_FLAG = "DELETE_FLAG";
040:
041: public void initialize() {
042: addPageListener(this );
043: _add.addSubmitListener(this );
044: _save.addSubmitListener(this );
045: _dsContact.setAutoValidate(true);
046:
047: //add a bucket to the model to track the delete check box
048: _dsContact.addBucket(DELETE_FLAG, DataStore.DATATYPE_INT);
049: _delete.setColumn(_dsContact, DELETE_FLAG);
050: _delete.setFalseValue(null);
051:
052: //we don't need to validate rows that will be deleted so add a skipRow expression to the validator
053: try {
054: _errorMessage.setSkipRowExpression(DELETE_FLAG + " == 1");
055: } catch (Exception e) {
056: }
057: }
058:
059: public boolean submitPerformed(SubmitEvent e) throws Exception {
060: if (e.getComponent() == _save) {
061: //remove deleted items and new rows that weren't modified
062: int count = _dsContact.getRowCount() - 1;
063: for (int i = count; i >= 0; i--) {
064: if (_dsContact.getRowStatus() == DataStore.STATUS_NEW
065: || _dsContact.getInt(i, DELETE_FLAG) == 1)
066: _dsContact.deleteRow(i);
067: }
068:
069: //update the datastore
070: _dsContact.update();
071:
072: //scroll back to the first page in the table
073: _datatable1.setPage(0);
074: } else if (e.getComponent() == _add) {
075: //add a new row, scroll to it and set focus to the first field
076: int row = _dsContact.insertRow();
077: _datatable1.setPage(_datatable1.getPage(row));
078: _firstName.setFocus(row);
079: }
080:
081: return true;
082: }
083:
084: public void pageRequested(PageEvent p) throws SQLException,
085: DataStoreException {
086: if (!isReferredByCurrentPage())
087: _dsContact.retrieve();
088: }
089:
090: public void pageRequestEnd(PageEvent p) {
091: }
092:
093: public void pageSubmitEnd(PageEvent p) {
094: }
095:
096: public void pageSubmitted(PageEvent p) {
097: if (isExpired()) {
098: _errorMessage
099: .addErrorMessage("Error this page is from the browsers cache, you must refresh the page before you can make changes.");
100: p.setContinueProcessing(false);
101: }
102:
103: }
104:
105: }
|