/*
* Created on 2004/09/10
* Author aki@www.xucker.jpn.org
* License Apache2.0 or Common Public License
*/
package org.jpn.xucker.imagemarker;
import java.io.File;
import java.util.Hashtable;
import org.jpn.xucker.commons.swt.ui.FileDirChoicer;
import org.jpn.xucker.commons.util.FileProperties;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;
/**
*
*
*/
public class GUIProperties {
Hashtable hash=new Hashtable();
private FileProperties fileProperties;
public void add(String key,Object ui){
hash.put(key,ui);
}
public Object get(String key){
return hash.get(key);
}
public void load(File file){
fileProperties=new FileProperties(file);
String keys[]=fileProperties.getKeys();
for(int i=0;i<keys.length;i++){
Object ui=hash.get(keys[i]);
parse(ui,fileProperties.get(keys[i]));
}
}
public String[] getKeys(){
return (String[])hash.keySet().toArray(new String[hash.size()]);
}
/**
* @param ui
*/
private void parse(Object ui,String value) {
if(ui==null){
return;
}
if(ui instanceof String){
//do nothing.
}else if(ui instanceof Text){
parseText((Text)ui,value);
}else if(ui instanceof Button){
Button bt=(Button)ui;
if((bt.getStyle() & SWT.CHECK)>0){
parseCheckButton((Button)ui,value);
}
}else if(ui instanceof FileDirChoicer){
parseFileDirChoicer((FileDirChoicer)ui,value);
}else if(ui instanceof Button[]){
parseButtonArray((Button[])ui,value);
}
else{
throw new RuntimeException("unsupported type "+ui.getClass().getName());
}
}
/**
* @param text
* @param value
*/
private void parseText(Text text, String value) {
if(value!=null){
text.setText(value);
}
}
private void parseCheckButton(Button button, String value) {
if(value!=null && value.equals("true")){
button.setSelection(true);
}else{
button.setSelection(false);
}
}
private void parseFileDirChoicer(FileDirChoicer choicer, String value) {
if(value!=null && !value.equals("")){
choicer.setFile(new File(value));
}
}
private void parseButtonArray(Button[] buttonArray,String value){
if(value!=null){
for (int i = 0; i < buttonArray.length; i++) {
if(buttonArray[i].getText().equals(value)){
buttonArray[i].setSelection(true);
}else{
buttonArray[i].setSelection(false);
}
}
}
}
public void save(){
String keys[]=getKeys();
System.out.println(keys.length);
for(int i=0;i<keys.length;i++){
Object ui=hash.get(keys[i]);
toData(keys[i],ui);
}
fileProperties.save();
}
/**
* @param ui
* @param string
*/
private void toData(String key,Object ui) {
if(ui==null){
return;
}
if(ui instanceof String){
fileProperties.set(key,(String)ui);
}else if(ui instanceof Text){
setText(key,(Text)ui);
}else if(ui instanceof Button){
Button bt=(Button)ui;
if((bt.getStyle() & SWT.CHECK)>0){
setCheckButton(key,(Button)ui);
}
}else if(ui instanceof FileDirChoicer){
setFileDirChoicer(key,(FileDirChoicer)ui);
}else if(ui instanceof Button[]){
setButtonArray(key,(Button[])ui);
}
else{
throw new RuntimeException("unsupported type "+ui.getClass().getName());
}
}
/**
* @param key
* @param choicer
*/
private void setFileDirChoicer(String key, FileDirChoicer choicer) {
File file=choicer.getFile();
String path="";
if(file!=null){
path=file.getAbsolutePath();
}
fileProperties.set(key,path);
}
/**
* @param key
* @param button
*/
private void setCheckButton(String key, Button button) {
fileProperties.set(key,""+button.getSelection());
}
/**
* @param key
* @param text
*/
private void setText(String key, Text text) {
// TODO Auto-generated method stub
fileProperties.set(key,text.getText());
}
/**
* @param buttons
* @param value
*/
private void setButtonArray(String key,Button[] buttons) {
for(int i=0;i<buttons.length;i++){
if(buttons[i].getSelection()){
fileProperties.set(key,buttons[i].getText());
}
}
}
}
|