/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.silabsoft.runescape.bestiary;
import com.google.gson.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Map.Entry;
import java.util.concurrent.ExecutionException;
import javax.swing.SwingWorker;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import org.silabsoft.runescape.bestiary.model.BestiaryName;
/**
*
* @author Silabsoft
*/
public class RunescapeBestiary extends javax.swing.JFrame {
public static final String TITLE = "Silabsoft's Runescape Bestiary Viewer";
private final DefaultMutableTreeNode bestiaryRootNode;
public static final String alphabet[] = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
/**
* Creates new form RunescapeBestiary
*/
public RunescapeBestiary() {
bestiaryRootNode = new DefaultMutableTreeNode("Bestiary List");
for (String s : alphabet) {
bestiaryRootNode.add(new DefaultMutableTreeNode(s));
}
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTree1 = new javax.swing.JTree();
jProgressBar1 = new javax.swing.JProgressBar();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
updateMenuItem = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle(TITLE);
jTree1.setModel(new javax.swing.tree.DefaultTreeModel(bestiaryRootNode));
jScrollPane1.setViewportView(jTree1);
jMenu1.setText("File");
updateMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_U, java.awt.event.InputEvent.CTRL_MASK));
updateMenuItem.setText("Update");
updateMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
updateMenuItemActionPerformed(evt);
}
});
jMenu1.add(updateMenuItem);
jMenuBar1.add(jMenu1);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 529, Short.MAX_VALUE)
.addComponent(jScrollPane1)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
pack();
}//
int index = 0;
private void updateMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
UpdateTask updateTask = new UpdateTask();
updateTask.addPropertyChangeListener(
new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if ("progress".equals(evt.getPropertyName())) {
jProgressBar1.setValue((Integer) evt.getNewValue());
}
}
});
updateTask.execute();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(RunescapeBestiary.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(RunescapeBestiary.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(RunescapeBestiary.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(RunescapeBestiary.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new RunescapeBestiary().setVisible(true);
}
});
}
private static String readUrl(String urlString) throws Exception {
BufferedReader reader = null;
try {
URL url = new URL(urlString);
reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder buffer = new StringBuilder();
int read;
char[] chars = new char[1024];
while ((read = reader.read(chars)) != -1) {
buffer.append(chars, 0, read);
}
return buffer.toString();
} finally {
if (reader != null) {
reader.close();
}
}
}
// Variables declaration - do not modify
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JProgressBar jProgressBar1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTree jTree1;
private javax.swing.JMenuItem updateMenuItem;
// End of variables declaration
class UpdateTask extends SwingWorker<Integer, Object> {
int count;
@Override
public Integer doInBackground() {
updateMenuItem.setEnabled(false);
setTitle(TITLE + " UPDATING LIST");
for (int i = 0; i < 26; i++) {
updateNode((DefaultMutableTreeNode) ((DefaultTreeModel) jTree1.getModel()).getChild(bestiaryRootNode, i), i);
this.setProgress(100 * i / 25);
}
setTitle(TITLE + " UPDATING Bestiary Data");
for (int i = 0; i < 26; i++) {
updateBeastData((DefaultMutableTreeNode) ((DefaultTreeModel) jTree1.getModel()).getChild(bestiaryRootNode, i));
this.setProgress(100 * i / 25);
}
return count;
}
@Override
protected void done() {
try {
setTitle(TITLE + " Mob Count: " + get());
} catch (InterruptedException ex) {
} catch (ExecutionException ex) {
}
updateMenuItem.setEnabled(true);
}
private void updateNode(DefaultMutableTreeNode child, int index) {
Gson gson = new Gson();
JsonParser jp = new JsonParser();
JsonArray ja = null;
try {
ja = jp.parse(readUrl("http://services.runescape.com/m=itemdb_rs/bestiary/bestiaryNames.json?letter=" + alphabet[index])).getAsJsonArray();
} catch (Exception ex) {
return;
}
int size = ja.size();
int idx = 0;
for (int i = 0; i < size; i++) {
JsonObject jo = ja.get(i).getAsJsonObject();
if (jo.has("npcs")) {
JsonArray npcArray = jo.getAsJsonArray("npcs");
DefaultMutableTreeNode groupNode = new DefaultMutableTreeNode(jo.get("dupe").getAsString());
((DefaultTreeModel) jTree1.getModel()).insertNodeInto(groupNode, child, idx++);
for (int x = 0; x < npcArray.size(); x++) {
BestiaryName bn = gson.fromJson(npcArray.get(x), BestiaryName.class);
((DefaultTreeModel) jTree1.getModel()).insertNodeInto(new DefaultMutableTreeNode(bn), groupNode, x);
count++;
}
} else {
BestiaryName bn = gson.fromJson(jo, BestiaryName.class);
((DefaultTreeModel) jTree1.getModel()).insertNodeInto(new DefaultMutableTreeNode(bn), child, idx++);
count++;
}
}
}
private void updateBeastData(DefaultMutableTreeNode child) {
int size = child.getChildCount();
for (int i = 0; i < size; i++) { Object o = child.getChildAt(i); if (((DefaultMutableTreeNode) o).getChildCount() > 0) {
updateBeastData((DefaultMutableTreeNode) o);
} else {
parseBeastData((DefaultMutableTreeNode) o);
}
}
}
private void parseBeastData(DefaultMutableTreeNode node) {
JsonParser jp = new JsonParser();
JsonObject jo = null;
try {
jo = jp.parse(readUrl("http://services.runescape.com/m=itemdb_rs/bestiary/beastData.json?beastid=" + ((BestiaryName) node.getUserObject()).getValue())).getAsJsonObject();
} catch (Exception ex) {
return;
}
int idx = 0;
for (Entry<String, JsonElement> e : jo.entrySet()) {
insertBeastDataNode(node, e.getKey(), e.getValue());
}
}
private void insertBeastDataNode(DefaultMutableTreeNode parent, String key, JsonElement value) {
if (value.isJsonObject()) {
DefaultMutableTreeNode dataCat = new DefaultMutableTreeNode(key);
((DefaultTreeModel) jTree1.getModel()).insertNodeInto(dataCat, parent, parent.getChildCount());
for (Entry<String, JsonElement> e : value.getAsJsonObject().entrySet()) {
insertBeastDataNode(dataCat, e.getKey(), e.getValue());
}
}
else if(value.isJsonArray()){
DefaultMutableTreeNode dataCat = new DefaultMutableTreeNode(key);
((DefaultTreeModel) jTree1.getModel()).insertNodeInto(dataCat, parent, parent.getChildCount());
JsonArray a = value.getAsJsonArray();
int size = a.size();
for(int i = 0; i < size; i++){
((DefaultTreeModel) jTree1.getModel()).insertNodeInto(new DefaultMutableTreeNode(a.get(i)), dataCat,i);
}
}
else {
((DefaultTreeModel) jTree1.getModel()).insertNodeInto(new DefaultMutableTreeNode(key + ":" + value), parent, parent.getChildCount());
}
}
}
}