📄 pajekconverter.java
字号:
String statusText = StatusDisplay.getText()+"Nodes Exported:";
for (int i=0; i<nodes.size(); i++)
{
PajekNode workNode = (PajekNode)nodes.get(i);
vertBody.append(workNode.toPajekNodeString());
arcBody.append(workNode.toPajekArcString());
//show id of node as it is exported
StatusDisplay.setText(statusText+(i+1)+"\n");
}
String outString = vertHeader+vertBody+arcHeader+arcBody;
// GET FILE NAME FOR OUTPUT -----------------------
//bring up save dialog to get names for file output, but do not open file yet
FileDialog locateOutput = new FileDialog(this, "Enter file name and choose directory for data output", FileDialog.SAVE);
//throw up open dialog
locateOutput.setSize(450,300);
locateOutput.setFile(defaultName+suffix);
locateOutput.setVisible(true);
outputFileName = locateOutput.getFile();
outputPathName = locateOutput.getDirectory();
//WRITE TEXT TO FILE -----------------------------
//check if user canceled save dialog don't output data to flie (but still do to screen)
if ((outputFileName != null) && (outputPathName != null))
{
//report file status to status area
StatusDisplay.append("Exporting parsed text to file: "+ outputPathName + outputFileName+"\n");
//create new file (default family name?)
try
{
//give fileobject name and path from dialog globals
File outfile = new File(outputPathName, outputFileName);
//make new outputstream
FileOutputStream outStream = new FileOutputStream(outfile);
//make new printwrinter
PrintWriter outWriter = new PrintWriter(outStream,true);
//ask printwriter to output string to file
outWriter.println(outString);
//close connection to file
outStream.close();
StatusDisplay.append("Export Complete\n");
}catch (IOException error)
{
StatusDisplay.append("Error saving output file " + error.toString()+"\n");
}
}
}
//reset vals
/*
nodes.clear();
nextNodeId = 1;
numCols=0;
numAbsAttributes = 0;
absAttributes = new String[9];
StatusDisplay.append("Data cleared, ready for new network.\n");
*/
}
//EXPORT PARTITION DIALOG -----------------------------------
public void exportPartDialog()
{
//throw up dialog asking which node var to export
Dialog whichVar = new Dialog(this, "Choose node attribute to export as Partition",true);
whichVar.setSize(500,75);
whichVar.setLocation(225,550);
//make new layout
whichVar.setLayout(new GridBagLayout());
//make objects for layout and add listeners
Button CancelButton = new Button("Cancel");
CancelButton.addActionListener(this);
Button OkButton = new Button("Export Partition");
OkButton.addActionListener(this);
CheckboxGroup cbg = new CheckboxGroup();
//put components in window
whichVar.add(new Checkbox("Node Label", cbg, true));
whichVar.add(new Checkbox("Node Shape", cbg, false));
whichVar.add(new Checkbox("Node Color", cbg, false));
whichVar.add(OkButton);
whichVar.add(CancelButton);
//put it on screen
whichVar.show();
}
//EXPORT PARTITION ------------------------------------------------
public void exportPart(String varToExport)
{
if (parsed == false)
{
StatusDisplay.append("Text must be parsed before output. \n");
}
else
{
StatusDisplay.append("Assembling partition...\n");
//assmeble data
String vertHeader = "*Vertices "+nodes.size()+"\r";
StringBuffer vertBody = new StringBuffer(5*nodes.size()); //start with approx capcity to
String[] textArray = new String[nodes.size()]; //hold strings comming from nodes
int nextClass = 1;
Hashtable textToClass = new Hashtable(); //holds text to class associations
//LABEL
if (varToExport.equals("Node Label"))
{
for (int i=0; i<nodes.size(); i++)
{
//copy from nodes into textArray
textArray[i] = ((PajekNode)nodes.elementAt(i)).getLabel();
}
}
//SHAPE
if (varToExport.equals("Node Shape"))
{
for (int i=0; i<nodes.size(); i++)
{
//copy from nodes into textArray
textArray[i] = ((PajekNode)nodes.elementAt(i)).getShape();
}
}
//COLOR
if (varToExport.equals("Node Color"))
{
for (int i=0; i<nodes.size(); i++)
{
//copy from nodes into textArray
textArray[i] = ((PajekNode)nodes.elementAt(i)).getColor();
}
}
//convert text to class int
for (int i=0; i<nodes.size(); i++)
{
//if the label is already used, copy class to classArray
if (textToClass.containsKey(textArray[i]))
{
vertBody.append(" "+ (Integer)textToClass.get(textArray[i])+"\r");
}
else
{
textToClass.put(textArray[i],(new Integer(nextClass)));
nextClass++;
vertBody.append(" "+ (Integer)textToClass.get(textArray[i])+"\r");
}
}
String outString = vertHeader+vertBody;
//debug
//InputDisplay.setText(outString);
// GET FILE NAME FOR OUTPUT -----------------------
//bring up save dialog to get names for file output, but do not open file yet
FileDialog locateOutput = new FileDialog(this, "Enter file name and choose directory for data output", FileDialog.SAVE);
//throw up open dialog
locateOutput.setSize(450,300);
locateOutput.setFile("nodePartition.clu");
locateOutput.setVisible(true);
outputFileName = locateOutput.getFile();
outputPathName = locateOutput.getDirectory();
//WRITE TEXT TO FILE -----------------------------
//check if user canceled save dialog don't output data to flie (but still do to screen)
if ((outputFileName != null) && (outputPathName != null))
{
//report file status to status area
StatusDisplay.append("Exporting partition to file: "+ outputPathName + outputFileName+"\n");
//create new file (default family name?)
try
{
//give fileobject name and path from dialog globals
File outfile = new File(outputPathName, outputFileName);
//make new outputstream
FileOutputStream outStream = new FileOutputStream(outfile);
//make new printwrinter
PrintWriter outWriter = new PrintWriter(outStream,true);
//ask printwriter to output string to file
outWriter.println(outString);
//close connection to file
outStream.close();
StatusDisplay.append("Partition saved.\n");
}catch (IOException error)
{
StatusDisplay.append("Error saving partition file " + error.toString()+"\n");
}
}
}
}
//EXPORT VECTOR DIALOG -------------------------------------------------
public void exportVectorDialog()
{
//throw up dialog asking which node var to export
Dialog whichVar = new Dialog(this, "Choose node attribute to export as Vector",true);
whichVar.setSize(500,75);
whichVar.setLocation(225,575);
//make new layout
whichVar.setLayout(new GridBagLayout());
//make objects for layout and add listeners
Button CancelButton = new Button("Cancel");
CancelButton.addActionListener(this);
Button OkButton = new Button("Export Vector");
OkButton.addActionListener(this);
CheckboxGroup cbg = new CheckboxGroup();
//put components in window
whichVar.add(new Checkbox("Node Size", cbg, true));
whichVar.add(new Checkbox("X coordinate", cbg, false));
whichVar.add(new Checkbox("Y coordinate", cbg, false));
whichVar.add(OkButton);
whichVar.add(CancelButton);
//put it on screen
whichVar.show();
}
//EXPORT VECTOR - ------------------------------------------------------------------
public void exportVector(String varToExport)
{
if (parsed == false)
{
StatusDisplay.append("Text must be parsed before output. \n");
}
else
{
StatusDisplay.append("Assembling vector...\n");
//assmeble data
String vertHeader = "*Vertices "+nodes.size()+"\r";
StringBuffer vertBody = new StringBuffer(5*nodes.size()); //start with approx capcity to
//SIZE
if (varToExport.equals("Node Size"))
{
for (int i=0; i<nodes.size(); i++)
{
//copy size from nodes
vertBody.append(" "+((PajekNode)nodes.elementAt(i)).getSize()+"\r");
}
}
//X
if (varToExport.equals("X coordinate"))
{
for (int i=0; i<nodes.size(); i++)
{
//copy x coord from nodes
vertBody.append(" "+((PajekNode)nodes.elementAt(i)).getXCoord()+"\r");
}
}
//Y
if (varToExport.equals("Y coordinate"))
{
for (int i=0; i<nodes.size(); i++)
{
//copy y coord from nodes
vertBody.append(" "+((PajekNode)nodes.elementAt(i)).getYCoord()+"\r");
}
}
String outString = vertHeader+vertBody;
//debug
//InputDisplay.setText(outString);
// GET FILE NAME FOR OUTPUT -----------------------
//bring up save dialog to get names for file output, but do not open file yet
FileDialog locateOutput = new FileDialog(this, "Enter file name and choose directory for data output", FileDialog.SAVE);
//throw up open dialog
locateOutput.setSize(450,300);
locateOutput.setFile("nodeVector.vec");
locateOutput.setVisible(true);
outputFileName = locateOutput.getFile();
outputPathName = locateOutput.getDirectory();
//WRITE TEXT TO FILE -----------------------------
//check if user canceled save dialog don't output data to flie (but still do to screen)
if ((outputFileName != null) && (outputPathName != null))
{
//report file status to status area
StatusDisplay.append("Exporting vector to file: "+ outputPathName + outputFileName+"\n");
//create new file (default family name?)
try
{
//give fileobject name and path from dialog globals
File outfile = new File(outputPathName, outputFileName);
//make new outputstream
FileOutputStream outStream = new FileOutputStream(outfile);
//make new printwrinter
PrintWriter outWriter = new PrintWriter(outStream,true);
//ask printwriter to output string to file
outWriter.println(outString);
//close connection to file
outStream.close();
StatusDisplay.append("vector saved.\n");
}catch (IOException error)
{
StatusDisplay.append("Error saving vector file " + error.toString()+"\n");
}
}
}
}
//------------ UTILITY METHODS --------------------------------------
//parse field takes text and returns a column assigment. If text starts with
//"$", it makes the text the attribute. returns 0 if unable to parse
public int parseField(String str)
{
int returnInt = 0;
str.trim();
if(str.startsWith("$"))
{
// trim off $
str = str.substring(1,str.length());
//put string in abs attributes array
absAttributes[numAbsAttributes] = str;
//give adress for "dummy" col
returnInt = numAbsAttributes+1;
//increment
numAbsAttributes++;
}
else
{
try
{
returnInt = new DecimalFormat().parse(str).intValue();
}
catch(ParseException e)
{
}
//numCols equal the largest value parsed
if (returnInt > numCols)
{
numCols = returnInt;
}
//if it was a genuine col assiment, add the offset of 10
if (returnInt != 0)
{
returnInt += 9;
}
{
}
}
return returnInt;
}
//ACTION PERFORMED gets the name of the button clicked and calls the aproprate method
public void actionPerformed(ActionEvent evt)
{
Button btn = (Button)evt.getSource();
String whichBtn = btn.getLabel();
if(whichBtn.equals("Parse Text"))
{
parse();
}
if(whichBtn.equals("Export network..."))
{
export();
}
if(whichBtn.equals("Export partition..."))
{
exportPartDialog();
}
if(whichBtn.equals("Export vector..."))
{
exportVectorDialog();
}
if(whichBtn.equals("Reset/Clear network"))
{
reset();
}
if(whichBtn.equals("Cancel"))
{
((Dialog)btn.getParent()).dispose();
}
if(whichBtn.equals("Export Partition"))
{
//figure out which option was checked
String checked = ((((Checkbox)((Dialog)btn.getParent()).getComponent(1)).getCheckboxGroup()).getSelectedCheckbox()).getLabel();
exportPart(checked);
((Dialog)btn.getParent()).dispose();
}
if(whichBtn.equals("Export Vector"))
{
//figure out which option was checked
String checked = ((((Checkbox)((Dialog)btn.getParent()).getComponent(1)).getCheckboxGroup()).getSelectedCheckbox()).getLabel();
exportVector(checked);
((Dialog)btn.getParent()).dispose();
}
}
//TEXT AREA LISTENER indicates that object representation of the text may no longer be valid
class TextChangeListener implements TextListener {
public void textValueChanged(TextEvent e){
if((e.getID() == TextEvent.TEXT_VALUE_CHANGED))
{
//parsed = false;
}
}
}
//WINDOW LISTENERS windowClosing closes window when close box is clicked
//the rest just have to be there I Guess...
public void windowClosing (WindowEvent evt){
System.exit(0);
}
public void windowActivated(WindowEvent evt){}
public void windowClosed(WindowEvent evt){}
public void windowDeactivated(WindowEvent evt){}
public void windowDeiconified(WindowEvent evt){}
public void windowIconified(WindowEvent evt){}
public void windowOpened(WindowEvent evt){}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -