If you think the Android project saostar listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package net.azyobuzi.azyotter.saostar.configuration;
/*www.java2s.com*/import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import net.azyobuzi.azyotter.saostar.ContextAccess;
import net.azyobuzi.azyotter.saostar.linq.Enumerable;
import net.azyobuzi.azyotter.saostar.system.Action1;
import net.azyobuzi.azyotter.saostar.system.Action2;
import net.azyobuzi.azyotter.saostar.system.Action3;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.content.Context;
publicclass Tabs {
privatestaticfinal String fileName = "tabs.xml";
privatestatic ArrayList<Tab> list = null;
privatestaticvoid loadTabs() {
list = new ArrayList<Tab>();
try {
Element root = DocumentBuilderFactory.newInstance()
.newDocumentBuilder()
.parse(ContextAccess.openFileInput(fileName))
.getDocumentElement();
NodeList tabs = root.getElementsByTagName("tab");
for (int i = 0; i < tabs.getLength(); i++) {
Element elm = (Element)tabs.item(i);
Tab re = new Tab(
elm.getElementsByTagName("name").item(0).getTextContent(),
elm.getElementsByTagName("filter").item(0).getTextContent()
);
list.add(re);
}
} catch (Exception ex) {
ex.printStackTrace();
//????
Tab homeTab = new Tab();
homeTab.setName("Home");
try {
homeTab.setFilter("prop:isHomeTweet");
} catch (Exception e) {
e.printStackTrace();
}
add(homeTab);
}
}
publicstatic Enumerable<Tab> getAllTabs() {
if (list == null) loadTabs();
return Enumerable.from(list.toArray()).cast();
}
publicstaticint getTabsCount() {
if (list == null) loadTabs();
return list.size();
}
publicstatic Tab get(int index) {
if (list == null) loadTabs();
return list.get(index);
}
publicstaticint indexOf(Tab tab) {
if (list == null) loadTabs();
return list.indexOf(tab);
}
publicstaticvoid add(Tab tab) {
if (list == null) loadTabs();
list.add(tab);
for (Action1<Tab> handler : addedHandler) {
handler.invoke(tab);
}
save();
}
publicstaticfinal ArrayList<Action1<Tab>> addedHandler = new ArrayList<Action1<Tab>>();
publicstaticvoid remove(Tab tab) {
if (list == null) loadTabs();
list.remove(tab);
for (Action1<Tab> handler : removedHandler) {
handler.invoke(tab);
}
save();
}
publicstaticfinal ArrayList<Action1<Tab>> removedHandler = new ArrayList<Action1<Tab>>();
publicstaticvoid move(int from, int to) {
if (list == null) loadTabs();
Tab tab = list.remove(from);
list.add(to, tab);
for (Action3<Tab, Integer, Integer> handler : movedHandler) {
handler.invoke(tab, from, to);
}
save();
}
publicstaticfinal ArrayList<Action3<Tab, Integer, Integer>> movedHandler = new ArrayList<Action3<Tab, Integer, Integer>>();
publicstaticvoid save() {
if (list == null) return;
try {
final Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder()
.newDocument();
final Element root = doc.createElement("tabs");
getAllTabs().forEach(new Action2<Tab, Integer>() {
@Override
publicvoid invoke(Tab arg0, Integer arg1) {
Element elm = doc.createElement("tab");
Element child = doc.createElement("name");
child.appendChild(doc.createTextNode(arg0.getName()));
elm.appendChild(child);
child = doc.createElement("filter");
child.appendChild(doc.createTextNode(arg0.getFilter()));
elm.appendChild(child);
root.appendChild(elm);
}
});
doc.appendChild(root);
TransformerFactory.newInstance()
.newTransformer()
.transform(new DOMSource(doc), new StreamResult(ContextAccess.openFileOutput(fileName, Context.MODE_PRIVATE)));
} catch (Exception ex) {
ex.printStackTrace(); //??????????????????????
}
}
}