/*
 * Copyright (C) 2005 NTT DATA Corporation
 * 
 */
package org.postgresforest.tool.view.serverstatus;

import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.TableTreeViewer;
import org.eclipse.swt.widgets.Item;
import org.postgresforest.tool.bean.InstanceNode;
import org.postgresforest.tool.bean.ServerInfo;

/**
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ServerStatusCellModifier implements ICellModifier {
	//private TableViewer viewer;
	//TODOMASU TableTreeViewerに変更
	private TableTreeViewer viewer;
	
	//public ServerStatusCellModifier(TableViewer viewer) {
	public ServerStatusCellModifier(TableTreeViewer viewer) {
		this.viewer = viewer;
	}
	
	public boolean canModify(Object element, String property) {
		if(element instanceof InstanceNode){
			return true;
		}else{
			return false;
		}
	}
	
	public Object getValue(Object element, String property) {
		if(element instanceof InstanceNode){
			ServerInfo item = ((InstanceNode) element).getServerInfo();
		
	
			if (property == "change"){
				int index;
				if(item.getChangeStatus() == -1){
					index = 1;
				}
				else{
					index = 0;
				}
				return new Integer(index);
			}
		}
	
		return null;
	}
	
	public void modify(Object element, String property, Object value) {
		if (element instanceof Item) {
			element = ((Item) element).getData();
		}
		
		if(element instanceof InstanceNode){
			ServerInfo item = ((InstanceNode) element).getServerInfo();
	
			if (property == "change") {
				if(((Integer) value).intValue() == 0){
					item.setChangeStatus(1);
				}
				else{
					item.setChangeStatus(-1);
				}
			}
		}
		
		// テーブル・ビューワを更新
		viewer.update(element, null);
	}

}