PropertiesTableModel.java
Dosyayı İndir
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
import java.io.*;
public class PropertiesTableModel extends AbstractTableModel{
public final static int NONE=0;
public final static int VALUE=1;
public final static int BOTH=2;
public int thisEditable=NONE;
private String thePath=null;
private Properties theProperties=new Properties();
private ArrayList dataList=new ArrayList();
private String[] columnname=new String[]{"Key","Value"};
public PropertiesTableModel(String systemid) throws FileNotFoundException,IOException{
setSystemid(systemid);
theProperties.load(new FileInputStream(thePath));
setDatas();
}
public PropertiesTableModel(String path,int editable) throws FileNotFoundException,IOException{
setSystemid(path);
theProperties.load(new FileInputStream(thePath));
setEditable(editable);
setDatas();
}
public Properties getProperties(){
return theProperties;
}
public void setSystemid(String path){
thePath=path;
}
public void setEditable(int editable){
thisEditable=editable;
}
public int getEditable(){
return thisEditable;
}
public int getRowCount(){
return dataList.size()/2;
}
public int getColumnCount(){
return 2;
}
public Object getValueAt(int row, int column){
String[] keys=getKeys();
if(column==0){
return dataList.get(row*2);
}else if(column==1){
return dataList.get(row*2+1);
}else{
return null;
}
}
public void setValueAt(Object a,int row,int column){
if(column==0){
dataList.set(2*row,(String)a);
}
else if(column==1){
dataList.set(2*row+1,(String)a);
}
}
public String getColumnName(int column){
return columnname[column];
}
public boolean isCellEditable(int rowindex,int colindex){
if(thisEditable==2){
return true;
}else if(thisEditable==1){
if(colindex==1){
return true;
}else{
return false;
}
}else{
return false;
}
}
public void updateProperties(){
Properties newProperties=new Properties();
for(int i=0;i<dataList.size()/2;i++){
newProperties.put(dataList.get(i*2),dataList.get(i*2+1));
}
theProperties=newProperties;
}
public void store()
throws IOException,FileNotFoundException
{
store(thePath);
}
public void store(String path)
throws IOException,FileNotFoundException
{
updateProperties();
FileOutputStream fos=new FileOutputStream(path);
theProperties.store(fos,"");
}
public void insertEmptyRow(){
dataList.add("");
dataList.add("");
fireTableDataChanged();
}
public void deleteRow(int index){
dataList.remove(index*2);
dataList.remove(index*2);
fireTableDataChanged();
}
public void deleteRows(int[] indexs){
for(int i=0;i<indexs.length;i++){
deleteRow(indexs[i]);
}
}
private void setDatas(){
String[] keys=getKeys();
for(int i=0;i<keys.length;i++){
dataList.add(2*i,keys[i]);
dataList.add(2*i+1,theProperties.get(keys[i]));
}
}
private String[] getKeys(){
String[] keys=new String[theProperties.size()];
int i=0;
for (Enumeration e=theProperties.keys();e.hasMoreElements();){
keys[i]=(String)e.nextElement();
i=i+1;
}
return keys;
}
}
Dosyayı İndir