editgridbean.java

来自「OperaMasks是一种基于J2EE的Web开发技术」· Java 代码 · 共 107 行

JAVA
107
字号
/*
 * $Id 
 *
 * Copyright (C) 2006 Operamasks Community.
 * Copyright (C) 2000-2006 Apusic Systems, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses.
 */
package demo.grid;

import java.util.UUID;

import javax.faces.FacesException;

import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.ManagedProperty;
import org.operamasks.faces.component.grid.CellSelectionModel;
import org.operamasks.faces.component.grid.GridSelectionModel;
import org.operamasks.faces.component.grid.impl.UIEditDataGrid;

import demo.grid.GridDataModel.TrainRecord;

@ManagedBean(scope = ManagedBeanScope.REQUEST)
public class EditGridBean {
	@SuppressWarnings("unused")
	@Bind(id = "trainRecord")
	private UIEditDataGrid trainRecord;

	@ManagedProperty("#{GridDataModel}")
	private GridDataModel model;

	@Bind(id = "trainRecord", attribute = "addedData")
	private Object addedData;
	@Bind(id = "trainRecord", attribute = "modifiedData")
	private Object modifiedData;
	@Bind(id = "trainRecord", attribute = "removedData")
	private Object removedData;
	@Bind(id = "trainRecord", attribute = "bindBean")
	private String bindBean = "demo.grid.GridDataModel$TrainRecord";

	@SuppressWarnings("unused")
	@Bind(id = "trainRecord", attribute = "selectionModel")
	private GridSelectionModel selectionType = new CellSelectionModel();

	@Action(id = "add")
	public void insert() {
		trainRecord.insertRow(0);
	}

	@Action(id = "remove")
	public void remove() {
		trainRecord.remove();
	}

	@Action(id = "save")
	public void save() {
		try {
			if (addedData != null) {
				add((TrainRecord[]) addedData);
			}
			if (modifiedData != null) {
				update((TrainRecord[]) modifiedData);
			}
			if (removedData != null) {
				remove((TrainRecord[]) removedData);
			}
			trainRecord.commit();
		} catch (Exception e) {
			throw new FacesException(e);
		}
	}

	private void remove(GridDataModel.TrainRecord[] data) {
		for (GridDataModel.TrainRecord record : data) {
			model.trainRecord.remove(model.getDataById(record.getId()));
		}
	}

	private void update(GridDataModel.TrainRecord[] data) {
		for (GridDataModel.TrainRecord record : data) {
			model.updateValue(record);
		}
	}

	private void add(GridDataModel.TrainRecord[] data) {
		for (GridDataModel.TrainRecord record : data) {
			record.setId(UUID.randomUUID().toString());
			model.trainRecord.add(record);
		}
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?