/*
 * 作成日: 2006/04/08
 */
package net.sourceforge.swingx.jspread;

class JSpreadControllerHeader {
	JSpread spread;
	JSpreadControllerHeader(JSpread spread) {
		this.spread = spread;
	}
	/**
	 * 表示位置1から表示位置2までの行or列を非表示にします.
	 * 表示位置1 > 表示位置2 で指定しても構いません。 
	 * @param	rowOrColumn	行か列か(JSpread.ROW or JSpread.COLUMN)
	 * @param	viewpos1	表示位置1(0..)(viewIndex)
	 * @param	viewpos2	表示位置2(0..)(viewIndex)
	 */
	void hideHeader(int rowOrColumn, int viewpos1, int viewpos2) {
		JSpreadSheet sheet = spread.sheet;
		assert rowOrColumn == JSpread.ROW || rowOrColumn == JSpread.COLUMN: rowOrColumn;
		if (rowOrColumn == JSpread.ROW) {
			int viewCount = sheet.rowHeader.getViewCount();
			assert 0 <= viewpos1 && viewpos1 < viewCount;
			assert 0 <= viewpos2 && viewpos2 < viewCount;
			int maxpos = Math.max(viewpos1, viewpos2);
			int minpos = Math.min(viewpos1, viewpos2);
			for (int i=minpos; i<=maxpos; i++) {
				sheet.rowHeader.getInfo(sheet.rowHeader.getDataIndex(i)).viewPosition = -1;
			}
			for (int i=0; i<sheet.rowHeader.getCount(); i++) {
				if (sheet.rowHeader.getInfo(i).viewPosition > maxpos) {
					sheet.rowHeader.getInfo(i).viewPosition -= (maxpos-minpos+1);
				}
			}
			sheet.rowHeader.makeDataIndexList();
			sheet.cellSelection = new boolean[sheet.rowHeader.getViewCount()][sheet.colHeader.getViewCount()];
			sheet.rowSelection = new boolean[sheet.rowHeader.getViewCount()];
//			spread.getRowScrollBar(0).setMaximum(sheet.rowHeader.getViewCount()); 
//			spread.getRowScrollBar(1).setMaximum(sheet.rowHeader.getViewCount()); 
		}
		else {
			int viewCount = sheet.colHeader.getViewCount();
			assert 0 <= viewpos1 && viewpos1 < viewCount;
			assert 0 <= viewpos2 && viewpos2 < viewCount;
			int maxpos = Math.max(viewpos1, viewpos2);
			int minpos = Math.min(viewpos1, viewpos2);
			for (int i=minpos; i<=maxpos; i++) {
				sheet.colHeader.getInfo(sheet.colHeader.getDataIndex(i)).viewPosition = -1;
			}
			for (int i=0; i<sheet.colHeader.getCount(); i++) {
				if (sheet.colHeader.getInfo(i).viewPosition > maxpos) {
					sheet.colHeader.getInfo(i).viewPosition -= (maxpos-minpos+1);
				}
			}
			sheet.colHeader.makeDataIndexList();
			sheet.cellSelection = new boolean[sheet.rowHeader.getViewCount()][sheet.colHeader.getViewCount()];
			sheet.colSelection = new boolean[sheet.colHeader.getViewCount()];
//			spread.getColumnScrollBar(0).setMaximum(sheet.colHeader.getViewCount()); 
//			spread.getColumnScrollBar(1).setMaximum(sheet.colHeader.getViewCount()); 
		}
	}
	/**
	 * すべての行or列を表示し、順番を元に戻します. 
	 * @param	rowOrColumn	行か列か(JSpread.ROW or JSpread.COLUMN or JSpread.BOTH)
	 */
	void showHeaderAll(int rowOrColumn) {
		JSpreadSheet sheet = spread.sheet;
		assert rowOrColumn == JSpread.ROW || rowOrColumn == JSpread.COLUMN ||
				rowOrColumn == JSpread.BOTH: rowOrColumn;
		if ((rowOrColumn & JSpread.ROW) != 0) {
			for (int i=0; i<sheet.rowHeader.getCount(); i++) {
				sheet.rowHeader.getInfo(i).viewPosition = i;
			}
			sheet.rowHeader.makeDataIndexList();
			sheet.cellSelection = new boolean[sheet.rowHeader.getViewCount()][sheet.colHeader.getViewCount()];
			sheet.rowSelection = new boolean[sheet.rowHeader.getViewCount()];
//			spread.getRowScrollBar(0).setMaximum(sheet.rowHeader.getViewCount()); 
//			spread.getRowScrollBar(1).setMaximum(sheet.rowHeader.getViewCount()); 
		}
		if ((rowOrColumn & JSpread.COLUMN) != 0) {
			for (int i=0; i<sheet.colHeader.getCount(); i++) {
				sheet.colHeader.getInfo(i).viewPosition = i;
			}
			sheet.colHeader.makeDataIndexList();
			sheet.cellSelection = new boolean[sheet.rowHeader.getViewCount()][sheet.colHeader.getViewCount()];
			sheet.colSelection = new boolean[sheet.colHeader.getViewCount()];
//			spread.getColumnScrollBar(0).setMaximum(sheet.colHeader.getViewCount()); 
//			spread.getColumnScrollBar(1).setMaximum(sheet.colHeader.getViewCount()); 
		}
	}
	/**
	 * ヘッダの最適な幅を取得します.
	 * @param	header		ヘッダの種類. ROW/COLUMN
	 * @param	dataIndex	データ番号(dataIndex)(0..*)
	 * @return	値(>0)
	 */
	int getPreferredWidth(int rowOrColumn, int dataIndex) {
		assert rowOrColumn == JSpread.ROW || rowOrColumn == JSpread.COLUMN: rowOrColumn;
		JSpreadHeader header = rowOrColumn == JSpread.ROW ? spread.sheet.rowHeader : spread.sheet.colHeader;
		assert 0 <= dataIndex && dataIndex < header.getCount(): dataIndex; 
		return header.getInfo(dataIndex).preferredWidth;
	}

}
