NetworkSpec.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.furthurnet.xmlparser |
![]() |
![]() |
Furthurnet |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* FURTHUR - A distributed peer-to-peer file sharing system.
* Copyright (C) 2001-2002 Jamie M. Addessi, Furthur Network
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.furthurnet.xmlparser;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import org.furthurnet.datastructures.supporting.Common;
import org.furthurnet.furi.BrowserLaunch;
import org.furthurnet.furi.Res;
import org.furthurnet.furi.ServiceManager;
public class NetworkSpec implements ReplacementManager {
public static final int MAX_SEARCH_PARAMS = 50;
public static final int MAX_ATTRIBUTES = 40;
private String networkType = null;
private Vector paramList = new Vector(); // Vector of Params
private Vector inputGuis = null; // Vector of ParamInputGuis
private Vector queryGuis = null; // Vector of ParamQueryGuis
private Vector webSiteList = new Vector(); // Vector of WebSites
private String fileSetNameConstructor = null;
private Vector replacementList = new Vector(); // Vector of Replacements (defined at the bottom of this class) to be replaced when generating the file set name
private String displayName = null;
private AutoPopulator autoPopulator = null;
public NetworkSpec(XmlObject obj) throws XmlException {
if (! obj.getName().equals(XmlTags.NETWORK_SPEC))
throw new XmlException("Could not read element " + XmlTags.NETWORK_SPEC);
networkType = obj.getAttribute(XmlTags.NETWORK_TYPE).getValue();
generateParamList(obj.getAttribute(XmlTags.PARAMETER_LIST));
generateWebSiteList(obj.getAttribute(XmlTags.WEB_SITE_LIST));
fileSetNameConstructor = obj.getAttribute(XmlTags.FILE_SET_NAME_CONSTRUCTOR).getValue();
try {
displayName = obj.getAttribute(XmlTags.DISPLAY_NAME).getValue();
}
catch (Exception e) {
displayName = networkType;
}
}
private void generateParamList(XmlObject obj) throws XmlException {
if (! obj.getName().equals(XmlTags.PARAMETER_LIST))
throw new XmlException("Could not read element " +
XmlTags.PARAMETER_LIST);
for (int i = 0; i < obj.numAttributes(); i++)
if (obj.getAttribute(i).getName().equals(XmlTags.PARAMETER))
paramList.add(Param.generateParameter(obj.getAttribute(i),
this));
}
private void generateWebSiteList(XmlObject obj) throws XmlException {
if (! obj.getName().equals(XmlTags.WEB_SITE_LIST))
throw new XmlException("Could not read element " +
XmlTags.WEB_SITE_LIST);
for (int i = 0; i < obj.numAttributes(); i++)
if (obj.getAttribute(i).getName().equals(XmlTags.WEB_SITE))
webSiteList.add(new WebSite(obj.getAttribute(i)));
}
public void addReplacement(String name, String id) {
replacementList.add(new Replacement(name, id));
}
public synchronized JPanel createInputPanel(int maxNumRows) {
if (networkType.equals("LIVE MUSIC") ||
networkType.equals("LIVE VIDEO")) {
autoPopulator = new LiveMusicAutoPopulator(this);
}
JPanel inputPanel = new JPanel();
inputPanel.setBorder(BorderFactory.createTitledBorder("Item Attributes"));
inputPanel.setLayout(new GridLayout(maxNumRows, 1));
inputPanel.setPreferredSize(new Dimension(540, paramList.size()*30));
inputGuis = new Vector();
ParamInputGui gui;
int i;
for (i = 0; i < paramList.size(); i++) {
gui = ((Param) paramList.elementAt(i)).createInputGui(autoPopulator);
inputPanel.add(gui, "Center");
inputGuis.add(gui);
}
for (; i < maxNumRows; i++) {
inputPanel.add(new JPanel());
}
return inputPanel;
}
public synchronized JPanel createQueryPanel() {
JPanel queryPanel = new JPanel();
queryPanel.setBorder(BorderFactory.createTitledBorder("Query Parameters"));
queryPanel.setLayout(new GridLayout(paramList.size(), 1));
queryPanel.setPreferredSize(new Dimension(700, paramList.size()*30));
queryGuis = new Vector();
ParamQueryGui gui;
for (int i = 0; i < paramList.size(); i++) {
gui = ((Param) paramList.elementAt(i)).createQueryGui();
queryPanel.add(gui, "Center");
queryGuis.add(gui);
}
return queryPanel;
}
public String encodeInputValues() throws EncodingException {
StringBuffer enc = new StringBuffer();
enc.append("<" + XmlTags.INPUT_SPEC + ">");
for (int i = 0; i < inputGuis.size(); i++)
enc.append(((ParamInputGui)inputGuis.elementAt(i)).encodeInputValues());
enc.append("</" + XmlTags.INPUT_SPEC + ">");
return enc.toString();
}
public String encodeQueryValues(boolean firewall) throws EncodingException {
StringBuffer enc = new StringBuffer();
boolean foundValid = false;
String firewallStr = "NO";
if (firewall)
firewallStr = "YES";
enc.append("<" + XmlTags.QUERY_SPEC + " " + XmlTags.NETWORK_TYPE + "=\"" + networkType + "\"" + " " + XmlTags.FURTHUR_VERSION + "=\"" + Res.getStr("Program.Version") + "\"" + " " + XmlTags.FIREWALL + "=\"" + firewallStr + "\">");
for (int i = 0; i < queryGuis.size(); i++) {
String selection = ((ParamQueryGui)queryGuis.elementAt(i)).encodeQueryValues();
if (selection != null) {
enc.append(selection);
foundValid = true;
}
}
if (! foundValid)
return null;
enc.append("</" + XmlTags.QUERY_SPEC + ">");
return enc.toString();
}
public String validateInputs() {
for (int i = 0; i < inputGuis.size(); i++) {
String validationString = ((ParamInputGui)inputGuis.elementAt(i)).validateInput();
if (validationString != null)
return validationString;
}
return null; // null signifies success
}
public void clearInputSelections() {
for (int i = 0; i < inputGuis.size(); i++)
((ParamInputGui)inputGuis.elementAt(i)).clearInputSelection();
}
public void clearQuerySelections() {
for (int i = 0; i < queryGuis.size(); i++)
((ParamQueryGui)queryGuis.elementAt(i)).clearQuerySelection();
}
public void setAttributeValues(String attr) throws XmlException {
XmlObject obj = (XmlObject)XmlObject.parse(attr).elementAt(0);
if (! obj.getName().equals(XmlTags.INPUT_SPEC))
throw new XmlException("Could not read element " + XmlTags.NETWORK_SPEC);
else
for (int i = 0; i < obj.numAttributes(); i++) {
XmlObject inputParam = obj.getAttribute(i);
if (! inputParam.getName().equals(XmlTags.INPUT_PARAM))
throw new XmlException("Could not read element " +
XmlTags.NETWORK_SPEC);
else {
String name = null;
String value = null;
for (int j = 0; j < inputParam.numAttributes(); j++) {
XmlObject nameOrValue = inputParam.getAttribute(j);
if (nameOrValue.getName().equals(XmlTags.INPUT_NAME))
name = nameOrValue.getValue();
if (nameOrValue.getName().equals(XmlTags.INPUT_VALUE))
value = nameOrValue.getValue();
}
if (name != null && value != null)
for (int k = 0; k < paramList.size(); k++)
if (name.equals(((Param)paramList.elementAt(k)).name))
((ParamInputGui)inputGuis.elementAt(k)).setInputSelection(value);
}
}
}
public int numParams() {
return paramList.size();
}
public Param getParam(int i) {
return (Param) paramList.elementAt(i);
}
public Param getParam(String name) {
try {
for (int i = 0; i < paramList.size(); i++)
if (((Param)paramList.elementAt(i)).getName().equals(name))
return (Param) paramList.elementAt(i);
}
catch (Exception e) {}
return null;
}
private int getParamIndex(String name) {
try {
for (int i = 0; i < paramList.size(); i++)
if (((Param)paramList.elementAt(i)).getName().equals(name))
return i;
}
catch (Exception e) {}
return -1;
}
public synchronized JPanel createWebSitePanel() {
WebSiteTableModel tableModel =
new WebSiteTableModel(new Vector(webSiteList));
JTable tableView = new JTable(tableModel);
tableView.setLayout(new BorderLayout());
tableView.setRowHeight(18);
JScrollPane tableAggregate = new JScrollPane(tableView);
tableView.getTableHeader().setReorderingAllowed(false);
tableView.setShowHorizontalLines(true);
tableView.setShowVerticalLines(false);
tableView.setIntercellSpacing(new Dimension(4, 4));
tableView.setColumnSelectionAllowed(false);
tableView.setRowSelectionAllowed(true);
tableView.setCellSelectionEnabled(false);
tableView.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tableView.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
tableView.addMouseListener(
new MyMouseAdapter(tableView, new Vector(webSiteList)));
tableView.getColumnModel().getColumn(0).setPreferredWidth(80);
tableView.getColumnModel().getColumn(1).setPreferredWidth(380);
JPanel tablePanel = new JPanel();
tablePanel.setLayout(new BorderLayout());
tablePanel.add(tableAggregate, BorderLayout.CENTER);
JPanel webSitePanel = new JPanel();
webSitePanel.setLayout(new BorderLayout());
webSitePanel.add(tableAggregate);
return webSitePanel;
}
class MyMouseAdapter extends MouseAdapter {
private JTable table;
private Vector sites;
private MyMouseAdapter() {}
public MyMouseAdapter(JTable t, Vector v) {
table = t;
sites = v;
}
public void mousePressed(MouseEvent e) {
if (e.getClickCount() == 2) {
Point p = e.getPoint();
int r = table.getSelectedRow();
Rectangle rect = table.getCellRect(r, 0, true);
if (p.y > rect.y && p.y < rect.y + rect.height) {
launchWebSite(r);
}
}
}
private void launchWebSite(int row) {
try {
WebSite ws = (WebSite) sites.elementAt(row);
BrowserLaunch.openURL(ws.getLink());
}
catch (Exception e) {
JOptionPane.showMessageDialog(ServiceManager.getManager().getMainFrame(), "Failed to launch browser " + e.getMessage(), "Can't Launch Browser", JOptionPane.ERROR_MESSAGE);
}
}
}
public void addQueryKeyListener(KeyListener kl) {
for (int i = 0; i < queryGuis.size(); i++)
((ParamQueryGui)queryGuis.elementAt(i)).addQueryKeyListener(kl);
}
public void setReadOnly(boolean readOnly) {
for (int i = 0; i < inputGuis.size(); i++)
((ParamInputGui)inputGuis.elementAt(i)).setReadOnly(readOnly);
}
public String getInputValue(String name) {
try {
for (int i = 0; i < paramList.size(); i++) {
Param param = ((Param)paramList.elementAt(i));
if (param.getName().equals(name))
return ((ParamInputGui)inputGuis.elementAt(i)).getInputSelection();
}
}
catch (Exception e) {}
return null;
}
public String getDisplayValue(String id) {
for (int i = 0; i < replacementList.size(); i++) {
Replacement r = (Replacement)replacementList.elementAt(i);
if (Common.equalStrings(id, r.id))
return r.display;
}
return "";
}
public String getIdValue(String display) {
for (int i = 0; i < replacementList.size(); i++) {
Replacement r = (Replacement)replacementList.elementAt(i);
if (Common.equalStrings(display, r.display))
return r.id;
}
return "";
}
public void updateAttributes(String[] name, String[] value) {
for (int i = 0; i < paramList.size(); i++)
for (int j = 0; j < name.length; j++)
if (value[j] != null) {
Param param = ((Param)paramList.elementAt(i));
if (param.getName().equals(name[j]))
((ParamInputGui)inputGuis.elementAt(i)).setInputSelection(value[j]);
}
}
public String generateFileSetName() throws XmlException {
String template = null;
try {
template = new String(fileSetNameConstructor);
for (int i = 0; i < paramList.size(); i++) {
Param param = ((Param)paramList.elementAt(i));
template = Common.replaceAll(template, "[" + param.getName() + "]", Param.displayValue(param.getType(), ((ParamInputGui)inputGuis.elementAt(i)).getInputSelection()));
}
}
catch (Exception e) {
throw new XmlException("Could not autogenerate file set name.");
}
if (template.length() == 0)
throw new XmlException("Could not autogenerate file set name.");
else
return template.toLowerCase();
}
private class Replacement {
public String display = null;
public String id = null;
public Replacement(String d, String i) {
display = d;
id = i;
}
}
public void tryAutoGenAttributes(String folderName, String[] extensions) throws Exception {
if (networkType.equals("LIVE MUSIC") || networkType.equals("LIVE VIDEO")) {
// only set values if they're previously blank
if (getInputValue("Date").length() > 0)
return;
if (getInputValue("Band Name").length() > 0)
return;
if (getInputValue("Setlist").length() > 0)
return;
String format = "";
String bandname = "";
String date = "";
for (int i = 0; i < extensions.length; i++) {
if (extensions[i].equalsIgnoreCase("flac"))
format = "flac16";
else if (extensions[i].equalsIgnoreCase("mp3"))
format = "mp3";
else if (extensions[i].equalsIgnoreCase("ogg"))
format = "ogg";
else if (extensions[i].equalsIgnoreCase("shn"))
format = "shn";
}
try {
String temp = "";
if (folderName.indexOf(".") >= 0) {
temp = folderName.substring(folderName.lastIndexOf(".")+1);
folderName = folderName.substring(0, folderName.lastIndexOf("."));
}
else if (folderName.indexOf("_") >= 0) {
temp = folderName.substring(folderName.lastIndexOf("_")+1);
folderName = folderName.substring(0, folderName.lastIndexOf("_"));
}
if (temp.length() == 4 && temp.substring(3).equalsIgnoreCase("F"))
temp = temp.substring(0, 3);
if (temp.length() == 3)
format = temp;
date = folderName.substring(folderName.length()-10);
if (! DatePanel.isValidDate(date))
date = "";
bandname = folderName.substring(0, folderName.length()-10);
if (bandname == null)
bandname = "";
}
catch (Exception e)
{}
// set the values
if (getInputValue("Format").length() == 0)
setParamInputGuiValue("Format", format.toUpperCase());
setParamInputGuiValue("Band Name", bandname.toLowerCase());
setParamInputGuiValue("Date", date);
// autopopulate other values via setlist.com interface
autoPopulator.attributeChanged("Date");
}
}
public String getNetworkType() {
return networkType;
}
public String getDisplayName() {
return displayName;
}
public void setParamInputGuiValue(String param, String value) {
((ParamInputGui) inputGuis.elementAt(getParamIndex(param))).setInputSelection(value);
}
}
The table below shows all metrics for NetworkSpec.java.




