Where The Streets Have No Name

LocalStorage 예제 본문

Developement/Java

LocalStorage 예제

highheat 2009. 11. 16. 09:43
MapListModel listModel;
String file = "logManagerOption.xml";
ApplicationContext ctxt;

public DesktopApplication2View(SingleFrameApplication app) {
        super(app);       
        ctxt = app.getContext();
        listModel = new MapListModel();
        initComponents();

         ...

}

@Action
public void addPhoneEntry() {
     String key = txtName.getText().trim();
     String value = txtPhone.getText().trim();
     listModel.put(key, value);
}

@Action
public void saveMap() {
    LinkedHashMap<String, String> map = listModel.getMap();
    try{
         ctxt.getLocalStorage().save(map, file);
    }catch(IOException ioe){
         System.out.println(ioe);
    }
    showFileMessage("savedFile", file);
}

@Action
public void loadMap() throws IOException {
   Object map = ctxt.getLocalStorage().load(file);
   listModel.setMap((LinkedHashMap<String, String>)map);
   showFileMessage("loadedFile", file);
}

@Action
public void clearMap() {
   listModel.clear();
   txtName.setText("");
   txtPhone.setText("");
}

private void showFileMessage(String messageKey, String file) {
   File dir = ctxt.getLocalStorage().getDirectory();
   File path = (dir == null) ? new File(file) : new File(dir, file);
   ResourceMap resourceMap = ctxt.getResourceMap(getClass());
   String message = resourceMap.getString(messageKey, path.toString());
   txtStatus.setText(message);
}
package desktopapplication2;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import javax.swing.AbstractListModel;

class MapListModel extends AbstractListModel {

    private final LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
    private List<String> keys = null;

    private List<String> getKeys() {
        if (keys == null) {
            keys = new ArrayList<String>(map.keySet());
        }
        return keys;
    }

    public void put(String key, String value) {
        int index = -1;
        if (map.containsKey(key)) {
            index = getKeys().indexOf(key);
        } else {
            index = map.size();
            keys = null;
        }
        map.put(key, value);
        fireContentsChanged(this, index, index);
    }

    public void clear() {
        if (map.size() > 0) {
            int lastIndex = map.size() - 1;
            map.clear();
            keys = null;
            fireIntervalRemoved(this, 0, lastIndex);
        }
    }

    public LinkedHashMap<String, String> getMap() {
        return new LinkedHashMap<String, String>(map);
    }

    public void setMap(LinkedHashMap<String, String> newMap) {
        int oldLastIndex = Math.max(map.size() - 1, 0);
        map.clear();
        map.putAll(newMap);
        int newLastIndex = Math.max(map.size() - 1, 0);
        fireContentsChanged(this, 0, Math.max(oldLastIndex, newLastIndex));
    }

    public int getSize() {
        return map.size();
    }

    public Object getElementAt(int index) {
        String key = getKeys().get(index);
        return key + " = " + map.get(key);
    }
}