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

import java.util.ArrayList;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.postgresforest.tool.action.GscAction;
import org.postgresforest.tool.util.CommonTableContentProvider;
import org.postgresforest.tool.util.EnableSelection;
import org.postgresforest.tool.util.ErrorLog;
import org.postgresforest.tool.util.Messages;
import org.postgresforest.tool.view.ViewBase;
import org.postgresforest.tool.view.action.AddGscAction;
import org.postgresforest.tool.view.action.DelGscAction;

/**
 * グローバルシステムカタログビュークラス
 * 
 * グローバルシステムカタログを表示し、グローバルシステムカタログの作成、削除を行う。
 */
public class GscView extends ViewBase {
	
	public static final String ID = "org.postgresforest.tool.view.GscView";

	private TableViewer viewer = null;


	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
	 */
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		Table table = new Table(parent,
				SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.SINGLE|SWT.BORDER);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn column = new TableColumn(table, SWT.LEFT, 0);
		column.setText(Messages.getString("form.db.name"));
		column.setWidth(100);
		column = new TableColumn(table, SWT.LEFT, 1);
		column.setText(Messages.getString("form.db.hostname"));
		column.setWidth(100);
		column = new TableColumn(table, SWT.LEFT, 2);
		column.setText(Messages.getString("form.db.port"));
		column.setWidth(100);
		column = new TableColumn(table, SWT.LEFT, 3);
		column.setText("");
		column.setWidth(0);
		column.setResizable(false);
		
		viewer = new TableViewer(table);
		
		viewer.setContentProvider(new CommonTableContentProvider());
		
		viewer.setLabelProvider(new GscLabelProvider());
		viewer.addSelectionChangedListener(new ISelectionChangedListener(){
		    public void selectionChanged(SelectionChangedEvent event){
		    
		        ISelection selection = event.getSelection();
		        setEnabledAction(DelGscAction.ID,selection);
		        
		    }
    								
		});		
		//MASUCOMMENT 右クリック対策
		createContextMenu(viewer.getTable());
	}

	
	/**
	 * 選択データベース名取得処理
	 * 
	 * @return データベース名
	 */
	public String getDbName() {
		
		Table tbl = viewer.getTable();
		TableItem[] selectItem = tbl.getSelection();
		if(selectItem.length == 1){
			return selectItem[0].getText(0);
		}
		else{
			return "";
		}
	}
	
	/**
	 * 最後のGSCかの判定処理
	 * 
	 * @return サーバID
	 */
	public boolean isLastGsc() {
		
		Table tbl = viewer.getTable();
		TableItem[] items = tbl.getItems();
		if(items.length == 1){
			return true;
		}
		else{
			return false;
		}
	}
	
	/**
	 * 選択サーバID取得処理
	 * 
	 * @return サーバID
	 */
	public String getSelectId() {
		
		Table tbl = viewer.getTable();
		TableItem[] selectItem = tbl.getSelection();
		if(selectItem.length == 1){
			return selectItem[0].getText(3);
		}
		else{
			return "";
		}
	}
	
	/**
	 * GSC情報取得
	 * 
	 * @return GSC情報
	 */
	private ArrayList getGscList() {
		ArrayList gscList = null;
		
		//GSC情報取得
		GscAction gsc = new GscAction(getGsc());
		try {
			gscList = gsc.getGscList();
		} catch (Exception e) {
			e.printStackTrace();
			ErrorLog.log("", e);
		}
	    
		return gscList;
	}
	
	public void update() {
	    if(!isLogin()){
			viewer.setInput(null);
			setEnabledAction(AddGscAction.ID, EnableSelection.FALSE);
	        return;
	    }

	    //GSC情報を取得
		ArrayList gscList = getGscList();

		//TableViewerにGSC情報を設定
		viewer.setInput(gscList);
		setEnabledAction(AddGscAction.ID, EnableSelection.TRUE);

	}
	
}
