view src/treecms/gui/AttributeEditorTable.java @ 8:f96193babac0

changed byte[] to ByteBuffer added TreeEditor.updateTree(Node,NodeData,Node[]) for node path is known. added GUIEditor
author shoshi
date Thu, 31 Mar 2011 02:08:44 +0900
parents
children
line wrap: on
line source

package treecms.gui;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

class AttributeEditorTable extends JTable
{
	private static final long serialVersionUID = -3520371233895896649L;
	private static final String ADD_ROW_STR = "add new attribute...";
	private static final Object ADD_ROW = new Object(){
		@Override
		public String toString()
		{
			return ADD_ROW_STR;
		}
	};
	
	private static final String[] COLUMNS = new String[]{"Key","Value"};
	private static final String[] DEFAULT_VALUES = new String[]{"new key","new value"};
	
	public AttributeEditorTable()
	{
		super(new AETableModel());
		
		addMouseListener(new MouseAdapter(){
			@Override
			public void mouseClicked(MouseEvent _e)
			{
				if(_e.getClickCount() == 2){
					int row = getSelectedRow();
					Object val = getValueAt(row,0);
					if(val == ADD_ROW){
						DefaultTableModel model = (DefaultTableModel)AttributeEditorTable.this.getModel();
						model.insertRow(row,DEFAULT_VALUES);
					}
				}
			}
		});
	}
	
	public void setAttributeMap(Map<ByteBuffer,ByteBuffer> _map)
	{
		AETableModel model = (AETableModel)getModel();
		model.clear();
		for(ByteBuffer _key : _map.keySet()){
			model.insertRow(0,new String[]{new String(_key.array()),new String(_map.get(_key).array())});
		}
	}
	
	public Map<ByteBuffer,ByteBuffer> getAttributeMap()
	{
		Map<ByteBuffer,ByteBuffer> map = new HashMap<ByteBuffer,ByteBuffer>();
		for(int i = 0;true;i ++){
			Object key = getValueAt(i,0);
			Object value = getValueAt(i,1);
			
			if(key == ADD_ROW){
				break;
			}
			
			ByteBuffer bufKey = ByteBuffer.wrap(key.toString().getBytes());
			ByteBuffer bufValue = ByteBuffer.wrap(value.toString().getBytes());
			map.put(bufKey,bufValue);
		}
		
		return map;
	}
	
	private static class AETableModel extends DefaultTableModel
	{
		private static final long serialVersionUID = -5641721228388548196L;

		public AETableModel()
		{
			super();
			setColumnIdentifiers(COLUMNS);
			addRow(new Object[]{ADD_ROW,""});
		}
		
		public void clear()
		{
			setRowCount(0);
			addRow(new Object[]{ADD_ROW,""});
		}
		
		@Override
		public boolean isCellEditable(int _row,int _col)
		{
			Object val = this.getValueAt(_row,_col);
			return (val == ADD_ROW) ? false : true;
		}
	}
}