📄 pajekconverter.java
字号:
//Skye Bender-deMoll draft translation code 2/13/01
//skyebend@bennington.edu
package PajekConverter;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.io.*;
import java.util.*;
import java.awt.color.*;
// THIS BUILDS THE GUI, THE LISTENERS RUN THE PROGRAM
public class PajekConverter extends Frame implements ActionListener, WindowListener
{
//create window objects for later use
//data window
public TextArea InputDisplay;
//status window
public TextArea StatusDisplay;
//button parse text to DataEntry object
public Button ParseButton;
//button to clear text window
public Button ResetButton;
//button to ouput text to file
public Button ExportButton;
//button to output partition
public Button PartButton;
//button to ouput vector
public Button VectorButton;
//text fields and lables
//public TextField FilterField;
//public Label FilterLabel;
public TextField IdColField;
public Label IdColLabel;
public TextField LinkIdColField;
public Label LinkIdColLabel;
public TextField LabelColField;
public Label LabelColLabel;
public TextField ShapeColField;
public Label ShapeColLabel;
public TextField SizeColField;
public Label SizeColLabel;
public TextField NodeColorColField;
public Label NodeColorColLabel;
public TextField XColField;
public Label XColLabel;
public TextField YColField;
public Label YColLabel;
public TextField ArcColorColField;
public Label ArcColorColLabel;
public TextField ArcWidthColField;
public Label ArcWidthColLabel;
public TextField ArcWeightColField;
public Label ArcWeightColLabel;
public Label ColAssignLabel;
public Checkbox IgnoreRowOne;
//--------GLOBAL PARAMS ----------------------------------
//global vars
private String outputFileName; //name of output file
private String outputPathName; //location to write file to
String defaultName = "convertedData"; //default filename for export
String suffix = ".net"; //file suffix for export
private Vector nodes = new Vector(); //holdes DataEntry objects
private int nextNodeId = 1; //holds id of next node between parses
private boolean parsed = false; //says whether current text has been parsed
//private String filterName = "none";
//spreadsheet column mappings
private int idCol = 0;
private int linkIdCol = 0;
private int labelCol = 0;
private int shapeCol = 0;
private int sizeCol = 0;
private int nodeColorCol = 0;
private int xCol = 0;
private int yCol = 0;
private int arcColorCol = 0;
private int arcWidthCol = 0;
private int arcWeightCol =0;
private int numCols = 0; //number of cols containing valid data
private int numAbsAttributes = 0; //number of node attributes with absolute rather than col adress
String[] absAttributes = new String[9]; //holds absolute attribute values
// ------ INTERFACE CONSTRUCTION AND INSTANTIATION -----------------------------
public PajekConverter(){
//LAYOUT
GridBagLayout layout = new GridBagLayout();
setLayout(layout);
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5,5,5,5);
//INSTANTIATE WINDOW COMPONENTS
//column mapping fields and labels
//FilterField = new TextField(filterName,10);
//FilterLabel = new Label("FILTER",Label.RIGHT);
IdColField = new TextField(idCol+"",4);
IdColLabel = new Label("ID",Label.RIGHT);
LinkIdColField = new TextField(linkIdCol+"",4);
LinkIdColLabel = new Label("LinkID",Label.RIGHT);
LabelColField = new TextField(labelCol+"",4);
LabelColLabel = new Label("NodeLabel",Label.RIGHT);
ShapeColField = new TextField(shapeCol+"",4);
ShapeColLabel = new Label("NodeShape",Label.RIGHT);
SizeColField = new TextField(sizeCol+"",4);
SizeColLabel = new Label("NodeSize",Label.RIGHT);
NodeColorColField = new TextField(nodeColorCol+"",4);
NodeColorColLabel = new Label("NodeColor",Label.RIGHT);
XColField = new TextField(xCol+"",4);
XColLabel = new Label("Xcoord",Label.RIGHT);
YColField = new TextField(yCol+"",4);
YColLabel = new Label("Ycoord",Label.RIGHT);
ArcColorColField = new TextField(arcColorCol+"",4);
ArcColorColLabel = new Label("ArcColor",Label.RIGHT);
ArcWidthColField = new TextField(arcWidthCol+"",4);
ArcWidthColLabel = new Label("ArcWidth",Label.RIGHT);
ArcWeightColField = new TextField(arcWeightCol+"",4);
ArcWeightColLabel = new Label("ArcWeight",Label.RIGHT);
ColAssignLabel = new Label("Column Assignments:",Label.RIGHT);
//text areas and buttons
InputDisplay = new TextArea(25,75);
StatusDisplay = new TextArea(10,75);
ParseButton = new Button("Parse Text");
ResetButton = new Button("Reset/Clear network");
ExportButton = new Button("Export network...");
PartButton = new Button("Export partition...");
VectorButton = new Button("Export vector...");
IgnoreRowOne = new Checkbox("Ignore first row (column headings)",true);
// add components to the layout GBlayout using constraints
//text areas
c.gridx=0;c.gridy=0;c.gridwidth=1;c.gridheight=12;c.weightx=0.5;c.weighty=0.0;
add(InputDisplay,c);
c.gridx=0;c.gridy=12;c.gridwidth=1;c.gridheight=1;c.weightx=0.0;c.weighty=0.0;
add(IgnoreRowOne,c);
c.gridx=0;c.gridy=13;c.gridwidth=1;c.gridheight=5;c.weightx=0.5;c.weighty=0.0;
add(StatusDisplay,c);
//buttons
c.gridx=1;c.gridy=13;c.gridwidth=2;c.gridheight=1;c.weightx=0.5;c.weighty=0.0;
add(ParseButton,c);
c.gridx=1;c.gridy=14;c.gridwidth=2;c.gridheight=1;c.weightx=0.5;c.weighty=0.0;
add(ExportButton,c);
c.gridx=1;c.gridy=15;c.gridwidth=2;c.gridheight=1;c.weightx=0.5;c.weighty=0.0;
add(PartButton,c);
c.gridx=1;c.gridy=16;c.gridwidth=2;c.gridheight=1;c.weightx=0.5;c.weighty=0.0;
add(VectorButton,c);
c.gridx=1;c.gridy=17;c.gridwidth=2;c.gridheight=1;c.weightx=0.5;c.weighty=0.0;
add(ResetButton,c);
//fields and lables
//c.gridx=1;c.gridy=0;c.gridwidth=1;c.gridheight=1;c.weightx=0.0;c.weighty=0.0;
//add(FilterLabel,c);
//c.gridx=2;c.gridy=0;
//add(FilterField,c);
c.gridx=1;c.gridy=0;c.weighty=0.0;c.gridwidth=2;
add(ColAssignLabel,c);c.gridwidth=1;
c.gridx=1;c.gridy=1;
add(IdColLabel,c);
c.gridx=2;c.gridy=1;
add(IdColField,c);
c.gridx=1;c.gridy=2;
add(LinkIdColLabel,c);
c.gridx=2;c.gridy=2;
add(LinkIdColField,c);
c.gridx=1;c.gridy=3;
add(LabelColLabel,c);
c.gridx=2;c.gridy=3;
add(LabelColField,c);
c.gridx=1;c.gridy=4;
add(ShapeColLabel,c);
c.gridx=2;c.gridy=4;
add(ShapeColField,c);
c.gridx=1;c.gridy=5;
add(SizeColLabel,c);
c.gridx=2;c.gridy=5;
add(SizeColField,c);
c.gridx=1;c.gridy=6;
add(NodeColorColLabel,c);
c.gridx=2;c.gridy=6;
add(NodeColorColField,c);
c.gridx=1;c.gridy=7;
add(XColLabel,c);
c.gridx=2;c.gridy=7;
add(XColField,c);
c.gridx=1;c.gridy=8;
add(YColLabel,c);
c.gridx=2;c.gridy=8;
add(YColField,c);
c.gridx=1;c.gridy=9;
add(ArcColorColLabel,c);
c.gridx=2;c.gridy=9;
add(ArcColorColField,c);
c.gridx=1;c.gridy=10;
add(ArcWidthColLabel,c);
c.gridx=2;c.gridy=10;
add(ArcWidthColField,c);
c.gridx=1;c.gridy=11;
add(ArcWeightColLabel,c);
c.gridx=2;c.gridy=11;
add(ArcWeightColField,c);
//add action listeners for button clicks
ParseButton.addActionListener(this);
ExportButton.addActionListener(this);
PartButton.addActionListener(this);
VectorButton.addActionListener(this);
ResetButton.addActionListener(this);
//add text lister for text changes
InputDisplay.addTextListener(new TextChangeListener());
//add window listener to handle close box
addWindowListener(this);
// put initial values in fields to aid user
InputDisplay.setText("PajekConverter v1.0\n\nBasic Instructions:\n\n- Paste tab-deliniated data to parse in this window.\n\n- Enter column assignments in the fields on the right.\n\t(to enter a specific value rather than a reletive adress,\n\tprefix entry with \"$\". ex. \"$ellipse\" or \"$Red\" or \"$0.5\".)\n\n- Click \"Parse Text\"\n\n- Click one of the export buttons.\n\nFor more information, see file PajekConvertReadMe.txt\n\n\nWritten by Skye Bender-deMoll for John Padgett, University of Chicago.\n Questions/Bugs to skyebend@santafe.edu\n\nUSE AT YOUR OWN RISK ;-)\n\nJuly 4, 2001 ");
setBackground(Color.lightGray);
}
//MAIN puts it all on screen
public static void main (String[] args){
Frame frm = new PajekConverter();
frm.setSize (725,700);
frm.setTitle ("PajekConverter v1.0");
frm.setVisible (true);
}
//-------- PARSING OPERATIONS METHODS ------------------------------------
//puts null chars inbetween tabs so that tokenizer will work properly
public String preformat(String workString)
{
char tab = ("\t").charAt(0);
char seperator = ("_").charAt(0);
//String workString = InputDisplay.getText();
char[] origArray = workString.toCharArray();
char[] newArray = new char[workString.length()*2];
int newIndex = 0;
//replace double tabs with tab _ tab
for (int index = 0; index <(workString.length()-1); index++)
{
if ((origArray[index] == tab) && (origArray[index+1] == tab))
{
newArray[newIndex] = origArray[index];
newIndex++;
newArray[newIndex] = seperator;
newIndex++;
}
else
{
newArray[newIndex] = origArray[index];
newIndex++;
}
}
newArray[newIndex] = workString.charAt(workString.length()-1);
String returnString = new String(newArray);
return returnString;
}
public void parse()
{
//reset globals so there will not be conflicts
nodes.clear();
nextNodeId = 1;
numCols=0;
numAbsAttributes = 0;
absAttributes = new String[9];
parsed = false;
String statusText = "Parsing text...\n"; //holds error msgs and updates
StatusDisplay.append(statusText);
//read in values from col fields
//if col field contains a string starting with "$", parseField will make that the attribute
//otherwise it returns the col assigment
//filterName = FilterField.getText();
idCol = parseField(IdColField.getText());
linkIdCol = parseField(LinkIdColField.getText());
labelCol = parseField(LabelColField.getText());
shapeCol = parseField(ShapeColField.getText());
sizeCol = parseField(SizeColField.getText());
nodeColorCol = parseField(NodeColorColField.getText());
xCol = parseField(XColField.getText());
yCol = parseField(YColField.getText());
arcColorCol = parseField(ArcColorColField.getText());
arcWidthCol = parseField(ArcWidthColField.getText());
arcWeightCol = parseField(ArcWeightColField.getText());
//get text from textarea and set up tokenizer
String textToParse = InputDisplay.getText();
StringTokenizer rowTokenizer = new StringTokenizer(textToParse, "\n");
//if "ignore first row" is checked, dump the first row
if (IgnoreRowOne.getState())
{
if (rowTokenizer.hasMoreTokens()) //make sure there is one to dump
{
String dump = (String)rowTokenizer.nextToken();
}
}
Vector rowCache = new Vector(); //holds rows to be parsed
//make text to explain parsing cols
//InputDisplay.setText("Line#\tId\tLabel\tLinkId\tShape\tSize\tColor\tX\tY\tOutput?\tError?\tErrors\n");
//parse rows into the row cache
while (rowTokenizer.hasMoreTokens())
{
String line = (String)rowTokenizer.nextToken();
if(line != null){
line = preformat(line);
rowCache.add(line);
}
}
//check which filter is set and instantiate appropriate dataChecker
DefaultChecker dataChecker = new DefaultChecker(rowCache.size());
//parse each line
for (int i = 0; i<rowCache.size(); i++)
{
StringTokenizer tokenizer = new StringTokenizer((String)rowCache.get(i),"\t");
//check that there are the correct number of cols
if (tokenizer.countTokens() < numCols)
{
StatusDisplay.append("ERROR: Line "+i+" is missing columns.\n");
}
else
{
//holds items parsed from each i for easy access and speed
String[] stringCache = new String[1+9+numCols];
//(extra entry in [] will return "default" will have all the unused variable redirected
//to it as is a kludgy way of hadling them
stringCache[0] = "default";
//stick absolue attribute value assigments on the beginning the cache so they can be adressed
for (int a=0; a<numAbsAttributes; a++)
{
stringCache[1+a] = absAttributes[a];
}
//copy string tokens from the tokenizer to the cache so that they will have
//numeric adresses.
for (int j=0; j<numCols; j++)
{
stringCache[1+9+j] = (String)tokenizer.nextToken();
}
//send line to data checker using i mappings, send parse errors to statusdisplay
statusText += dataChecker.parseRow(i, stringCache[idCol], stringCache[linkIdCol], stringCache[labelCol],
stringCache[shapeCol], stringCache[sizeCol], stringCache[nodeColorCol],
stringCache[xCol], stringCache[yCol], stringCache[arcColorCol], stringCache[arcWidthCol],
stringCache[arcWeightCol]);
//send parsed info to input display for debugging
StatusDisplay.setText(statusText +"Rows parsed:"+i+"\n");
}
}
//create networkAssembler object with data from dataChecker, and id to start with
DefaultNetAssembler assembler = new DefaultNetAssembler((DefaultDataEntry)dataChecker.getData(),nextNodeId);
//set up shapes, send errors to satus display
StatusDisplay.append(assembler.setupShape());
//set up size ,send errors to status display
StatusDisplay.append(assembler.setupSize());
//set up color,send errors to status display
StatusDisplay.append(assembler.setupColor());
//set up arc colrs and send errors to status display
StatusDisplay.append(assembler.setupArcColor());
//set up x and y coords,send errors to status display
StatusDisplay.append(assembler.setupCoords());
//EDGES
//ask assembler to create the nodes with lables, set otther vals, send errors to status display
StatusDisplay.append(assembler.makeNodes());
//create network structure send errors to status dispaly
StatusDisplay.append(assembler.makeArcs());
//copy all nodes to ArrayList
nodes.addAll(assembler.getNodes());
StatusDisplay.append("Parsing Complete, (click Export to write file)\n");
parsed = true;
}
public void reset()
{
InputDisplay.setText("");
StatusDisplay.setText("");
nodes.clear();
nextNodeId = 1;
numCols=0;
numAbsAttributes = 0;
absAttributes = new String[9];
parsed = false;
}
public void export()
{
//check if data objects represent current text window
if (parsed == false)
{
StatusDisplay.append("Text must be parsed before output. \n");
}
else
{
//CONSTRUCT TEXT FORMMATTED FOR PAJEK -----------
String vertHeader = "*Vertices "+nodes.size()+"\r";
String arcHeader = "*Arcs\r";
StringBuffer vertBody = new StringBuffer(75*nodes.size()); //start with approx capcity to
StringBuffer arcBody = new StringBuffer(25*nodes.size()); //try and cut memory hanleing costs
//display
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -