일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Google Map
- 전자정부프레임워크
- WebLogic
- 선택적조인
- JDOM
- dock
- Eclipse
- MySQL
- iBATIS
- GPS
- jQuery
- PLSQL
- Android
- Ajax
- phonegap
- node.js
- PHP
- rowspan
- swingx
- Spring
- ibsheet
- oracle
- sencha touch
- appspresso
- 가우스
- MFC
- JSON
- jsr 296
- tomcat
- Struts
Archives
- Today
- Total
Where The Streets Have No Name
LocalStorage 예제 본문
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);
}
}