added tags to main menu, gets and displays all tags automatically

This commit is contained in:
aj 2018-04-05 22:40:15 -07:00
parent 379e3a014c
commit 1f5fd21304
8 changed files with 166 additions and 22 deletions

View File

@ -20,11 +20,12 @@ public class MainMenu extends JFrame {
JButton getArtist = new JButton("Get Artist");
JButton viewLastTrack = new JButton("View Last Track");
JButton viewList = new JButton("View List");
JButton viewTag = new JButton("View Tags");
public MainMenu() {
super("fmframework - " + Reference.getUserName());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(2, 2));
setLayout(new GridLayout(3, 2));
setSize(300, 300);
setResizable(false);
@ -64,8 +65,15 @@ public class MainMenu extends JFrame {
view.setVisible(true);
}
});
viewTag.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
TagMenuView view = new TagMenuView();
view.setVisible(true);
}
});
add(viewLastTrack);
add(viewList);
add(viewTag);
add(getAlbum);
add(getArtist);

View File

@ -91,7 +91,7 @@ public class RefListsView extends JFrame {
JButton viewRockTag = new JButton("Rock Tag");
viewRockTag.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
FMObjList list = Getter.getTag(Reference.getUserName(), "rock");
FMObjList list = Getter.getUserTag(Reference.getUserName(), "rock");
list.view();
}
});
@ -100,7 +100,7 @@ public class RefListsView extends JFrame {
JButton viewRapTag = new JButton("Rap Tag");
viewRapTag.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
FMObjList list = Getter.getTag(Reference.getUserName(), "rap");
FMObjList list = Getter.getUserTag(Reference.getUserName(), "rap");
list.view();
}
});

View File

@ -1,5 +1,42 @@
package sarsoo.fmframework.gui;
public class TagMenuView {
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import sarsoo.fmframework.music.Tag;
import sarsoo.fmframework.util.FMObjList;
import sarsoo.fmframework.util.Getter;
import sarsoo.fmframework.util.Reference;
public class TagMenuView extends JFrame {
public TagMenuView() {
super("View Tags");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new GridLayout(3, 2));
setSize(300, 300);
setResizable(false);
ArrayList<Tag> tags = Getter.getUserTags(Reference.getUserName());
int counter;
for(counter = 0; counter < tags.size(); counter++) {
Tag tag = tags.get(counter);
JButton view = new JButton(tag.getName());
view.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
FMObjList list = Getter.getUserTag(Reference.getUserName(), tag.getName());
list.view();
}
});
add(view);
}
}
}

View File

@ -1,11 +1,45 @@
package sarsoo.fmframework.music;
public class Tag {
protected String name;
protected String url;
protected int reach;
protected int taggings;
protected Boolean streamable;
protected String summary;
protected String content;
private String name;
private String url;
private int count;
private int reach;
private int taggings;
private Boolean streamable;
private String summary;
private String content;
public Tag(String name, String url) {
this.setName(name);
this.setUrl(url);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String toString() {
return name;
}
}

View File

@ -128,6 +128,23 @@ public class URLBuilder {
return null;
}
public static String getUserTopTags(String username) {
String urlString;
try {
urlString = String.format("http://ws.audioscrobbler.com/2.0/?method=user.gettoptags&user=%s&api_key=%s",
URLEncoder.encode(username, "UTF-8"),
URLEncoder.encode(Key.getKey(), "UTF-8"));
return urlString;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println(urlString);
return null;
}
// public static String getUserTopTags() {
//
// }

View File

@ -3,6 +3,7 @@ package sarsoo.fmframework.parser;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
@ -18,6 +19,7 @@ import org.w3c.dom.NodeList;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.Tag;
import sarsoo.fmframework.music.Track;
import sarsoo.fmframework.music.Wiki;
import sarsoo.fmframework.net.URLBuilder;
@ -273,7 +275,39 @@ public class Parser {
}
public static FMObjList parseTagList(Document doc) {
public static ArrayList<Tag> parseUserTags(Document doc) {
if (doc.getDocumentElement().getAttribute("status").equals("ok")) {
NodeList objList = doc.getElementsByTagName("tag");
// Node obj = objList.item(0);
ArrayList<Tag> list = new ArrayList<Tag>();
// String name = obj.getFirstChild().getTextContent();
// System.out.println(name);
//
// Artist artist = Artist.getArtist(name, Reference.getUserName());
//
// System.out.println(artist);
int counter;
for (counter = 0; counter < objList.getLength(); counter++) {
NodeList obj = objList.item(counter).getChildNodes();
String name = obj.item(0).getTextContent();
String url = obj.item(4).getTextContent();
// System.out.println(obj.getTextContent());
Tag tag = new Tag(name, url);
// System.out.println(name + " " + url);
list.add(tag);
}
return list;
}
return null;
}
public static FMObjList parseUserTagList(Document doc) {
if (doc.getDocumentElement().getAttribute("status").equals("ok")) {
NodeList objList = doc.getElementsByTagName("artist");
@ -295,7 +329,7 @@ public class Parser {
// System.out.println(obj.getTextContent());
Artist artist = Artist.getArtist(name, Reference.getUserName());
System.out.println(artist);
// System.out.println(artist);
list.add(artist);
}
return list;

View File

@ -3,12 +3,14 @@ package sarsoo.fmframework.test;
import static org.junit.jupiter.api.Assertions.*;
import java.io.InputStream;
import java.util.ArrayList;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.Tag;
import sarsoo.fmframework.music.Track;
import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.net.TestCall;
@ -52,17 +54,17 @@ class NetworkTest {
@Test
void testTag() {
Reference.setUserName("sarsoo");
String url = URLBuilder.getUserPersonalTags("sarsoo", "rock");
String url = URLBuilder.getUserTopTags("sarsoo");
Document response = Network.getResponse(url);
FMObjList list = Parser.parseTagList(response);
ArrayList<Tag> list = Parser.parseUserTags(response);
list.view();
// FMObjList list = Parser.parseTagList(response);
// int counter;
// for(counter = 0; counter < list.size(); counter++) {
// System.out.println(list.get(counter));
// }
int counter;
for(counter = 0; counter < list.size(); counter++) {
System.out.println(list.get(counter));
}
// System.out.println(url);
}

View File

@ -1,11 +1,14 @@
package sarsoo.fmframework.util;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import org.w3c.dom.Document;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.Tag;
import sarsoo.fmframework.music.Track;
import sarsoo.fmframework.net.Network;
import sarsoo.fmframework.net.TestCall;
@ -61,11 +64,20 @@ public class Getter {
return 0;
}
public static FMObjList getTag(String username, String tag) {
public static FMObjList getUserTag(String username, String tag) {
String url = URLBuilder.getUserPersonalTags(username, tag);
Document doc = Network.getResponse(url);
if (doc != null) {
return Parser.parseTagList(doc);
return Parser.parseUserTagList(doc);
}
return null;
}
public static ArrayList<Tag> getUserTags(String username) {
String url = URLBuilder.getUserTopTags(username);
Document doc = Network.getResponse(url);
if (doc != null) {
return Parser.parseUserTags(doc);
}
return null;
}