📄 tspmenu.java.svn-base
字号:
}
}
PrintWriter report = new PrintWriter(fileXML.getAbsolutePath(),"UTF-8");
report.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
report.println();
report.println("<tsp-report>");
report.println(" <results>");
Map<String,String> params=Report.getResultInfo(parent);
Iterator keys=params.keySet().iterator();
while(keys.hasNext()) {
String key=(String)keys.next();
report.println(" <result name=\""+key+"\" value=\""+params.get(key)+"\"/>");
}
report.println(" </results>");
report.println(" <system-info>");
Map<String,String> info=Report.getSystemProperties();
keys=info.keySet().iterator();
while(keys.hasNext()) {
String key=(String)keys.next();
report.println(" <info name=\""+key+"\" value=\""+info.get(key)+"\"/>");
}
report.println(" </system-info>");
//reorder cities so the list starts and ends with the start city
City cities[]=parent.cities;
if(parent.bestChromosome!=null) {
cities=parent.bestChromosome.cities;
}
City cities2[]=new City[cities.length+1];
int i;
int iStart=0;
//start cities from starting city
for(i=0; i<cities.length; i++) {
if(cities[i].startCity) {
iStart=i;
break;
}
}
i=0;
while(i<cities.length) {
cities2[i]=cities[iStart];
iStart++;
i++;
if(iStart>=cities.length)
iStart=0;
}
//add the start city again at the end
cities2[cities.length]=cities2[0];
report.println(" <path>");
for(City city:cities2) {
report.println(" <city name=\""+city.name+"\" x=\""+city.SJTSKX+"\" y=\""+city.SJTSKY+"\"/>");
}
report.println(" </path>");
report.print("</tsp-report>");
report.close();
if(!parent.configuration.console) {
JOptionPane.showMessageDialog(parent.gui,"OK, report created to the file: \n"+fileXML,"Info",JOptionPane.INFORMATION_MESSAGE);
//open the report file
//only at windows workstations
try {
Runtime.getRuntime().exec(new String[]{"cmd.exe","/c",fileXML.getAbsolutePath()});
} catch(Throwable ex2) {
try {
Runtime.getRuntime().exec(new String[]{"cmd.exe","/c","start",fileXML.getAbsolutePath()});
} catch(Throwable ex3) {
// nop
}
}
}
} catch(Throwable ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(parent.gui,"Can not create pdf report.","Error",JOptionPane.WARNING_MESSAGE);
}
}
/**
* Creates PDF Report from previously saved XML report
* @param e
*/
protected void actionXML2PDFReport(@SuppressWarnings("unused") ActionEvent e) {
/**
* load XML report file
*/
JFileChooser fileChooser=new JFileChooser();
if(reportPrevDir==null) {
reportPrevDir=new File(".").getAbsoluteFile();
}
fileChooser.setCurrentDirectory(reportPrevDir);
fileChooser.setFileFilter(new FileFilter(){
@Override
public boolean accept(File f) {
if(f.isDirectory()) return true;
if(f.getAbsolutePath().toLowerCase().endsWith(".xml")) return true;
return false;
}
@Override
public String getDescription() {
return "*.xml";
}
});
File fileXML=null;
Map<String,String> params=new LinkedHashMap<String,String>();
Map<String,String> info=new LinkedHashMap<String,String>();
City path[]=null;
BufferedImage screenImage=null; //screen image is also reconstructed from XML
//read xml report
if(fileChooser.showOpenDialog(parent.gui)==JFileChooser.APPROVE_OPTION) {
try {
reportPrevDir=fileChooser.getCurrentDirectory();
fileXML=fileChooser.getSelectedFile();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document document = factory.newDocumentBuilder().parse(new InputSource(new FileInputStream(fileXML)));
Element root=document.getDocumentElement();
//read result info
{
NodeList resultInfos=root.getElementsByTagName("result");
for(int i=0; i<resultInfos.getLength(); i++) {
Node resultInfo=resultInfos.item(i);
NamedNodeMap attributes = resultInfo.getAttributes();
String name=attributes.getNamedItem("name").getNodeValue();
String value=attributes.getNamedItem("value").getNodeValue();
params.put(name,value);
}
}
//read system info
{
NodeList systemInfos=root.getElementsByTagName("info");
for(int i=0; i<systemInfos.getLength(); i++) {
Node systemInfo=systemInfos.item(i);
NamedNodeMap attributes = systemInfo.getAttributes();
String name=attributes.getNamedItem("name").getNodeValue();
String value=attributes.getNamedItem("value").getNodeValue();
info.put(name,value);
}
}
//read cities
{
NodeList cities=root.getElementsByTagName("city");
path=new City[cities.getLength()];
for(int i=0; i<cities.getLength(); i++) {
Node city=cities.item(i);
NamedNodeMap attributes = city.getAttributes();
String name=attributes.getNamedItem("name").getNodeValue();
String x=attributes.getNamedItem("x").getNodeValue();
String y=attributes.getNamedItem("y").getNodeValue();
path[i]=new City(i,null,name,Integer.parseInt(x),Integer.parseInt(y));
}
}
} catch(Throwable ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(parent.gui,"Can not read XML report file.","Error",JOptionPane.WARNING_MESSAGE);
return;
}
} else {
return; // cancel pressed
}
//save the PDF report
fileChooser=new JFileChooser();
if(reportPrevDir==null) {
reportPrevDir=fileChooser.getCurrentDirectory();
} else {
fileChooser.setCurrentDirectory(reportPrevDir);
}
//change file extension to pdf
String pdfFileName=fileXML.getName();
int iDot=pdfFileName.lastIndexOf('.');
if(iDot!=-1) {
pdfFileName=pdfFileName.substring(0,iDot);
}
if(!pdfFileName.endsWith(".")) {
pdfFileName+=".";
}
pdfFileName+="pdf";
fileChooser.setSelectedFile(new File(fileXML.getParent(),pdfFileName));
fileChooser.setFileFilter(new FileFilter(){
@Override
public boolean accept(File f) {
if(f.isDirectory()) return true;
if(f.getAbsolutePath().toLowerCase().endsWith(".pdf")) return true;
return false;
}
@Override
public String getDescription() {
return "*.pdf";
}
});
if(fileChooser.showSaveDialog(parent.gui)==JFileChooser.APPROVE_OPTION) {
try {
reportPrevDir=fileChooser.getCurrentDirectory();
File filePDF=fileChooser.getSelectedFile();
//set .pdf extension
String fileName=filePDF.getName();
if(!fileName.toLowerCase().endsWith(".pdf")) {
String ext="pdf";
if(!fileName.endsWith(".")) {
ext="."+ext;
}
fileName+=ext;
File parentDir=filePDF.getParentFile();
filePDF=new File(parentDir,fileName);
}
if(filePDF.exists()) {
if(JOptionPane.showConfirmDialog(parent.gui,"Should be existing file overwritten ?","Question",JOptionPane.YES_NO_OPTION)==JOptionPane.NO_OPTION) {
return;
}
}
//create image in new TSP window
{
TSP tsp2=new TSP(false);
tsp2.cities=new City[path.length-1];
tsp2.configuration.setAntialiasing(true);
for(int i=0; i<tsp2.cities.length; i++) {
tsp2.cities[i]=path[i];
tsp2.cities[i].configuration=tsp2.configuration;
}
//initialize the cities - do not initialize the cache (cache is static)
tsp2.loadCities(tsp2.cities,false);
//loadcities recomputes x and y, so reset the path
for(int i=0; i<tsp2.cities.length; i++) {
path[i]=tsp2.cities[i];
}
path[path.length-1]=path[0];
//draw and get the image
tsp2.start();
tsp2.gui.setEnabled(false);
tsp2.bestChromosome=new TSPChromosome(tsp2.cities,false);
tsp2.gui.repaint();
Component c=tsp2.gui.getContentPane();
screenImage = (BufferedImage) c.createImage(c.getWidth(), c.getHeight());
Graphics2D graphics=screenImage.createGraphics();
//switch on antialiasing
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BICUBIC );
tsp2.gui.statusBar.setVisible(false);
c.paint(graphics);
tsp2.gui.dispose();
}
//create report
new Report().saveReport(filePDF,path,screenImage,params,info);
JOptionPane.showMessageDialog(parent.gui,"OK, report created to the file: \n"+filePDF,"Info",JOptionPane.INFORMATION_MESSAGE);
//open the report file
//only at windows workstations
try {
Runtime.getRuntime().exec(new String[]{"cmd.exe","/c",filePDF.getAbsolutePath()});
} catch(Throwable ex2) {
try {
Runtime.getRuntime().exec(new String[]{"cmd.exe","/c","start",filePDF.getAbsolutePath()});
} catch(Throwable ex3) {
// nop
}
}
} catch(Throwable ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(parent.gui,"Can not create pdf report.","Error",JOptionPane.WARNING_MESSAGE);
}
} else {
return; //cancel pressed on PDF file dialog
}
}
/**
* Displays application help
* @param e
*/
protected void actionAbout(ActionEvent e) {
BufferedReader reader=new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/org/saiko/ai/genetics/tsp/etc/about.html")));
try {
StringBuffer message=new StringBuffer();
String line;
while((line=reader.readLine())!=null) {
message.append(line);
message.append(" ");
}
reader.close();
showHtmlDialog("About application", message.toString().replaceAll("__VERSION__",TSP.getAppVersion()));
} catch (Exception ex) {
// nothing
}
}
/**
* Shows dialog with html content
* @param title
* @param htmlMessage
*/
protected void showHtmlDialog( String title, String htmlMessage) {
//create new dialog
final JDialog dlg=new JDialog(parent.gui);
dlg.setSize(600,400);
dlg.setTitle(title);
dlg.setLayout(new BorderLayout());
dlg.setComponentOrientation(parent.gui.getComponentOrientation());
dlg.setLocationRelativeTo(parent.gui);
dlg.setResizable(true);
//create close button
JButton ok=new JButton(" Close ");
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
dlg.dispose();
}});
JPanel south=new JPanel();
dlg.add(south,BorderLayout.SOUTH);
south.add(ok);
dlg.setModal(true);
//create content center scrolling pane
JScrollPane scrollPane=new JScrollPane();
JEditorPane content=new JEditorPane("text/html",htmlMessage);
content.setCaretPosition(0);
content.setEditable(false);
content.setBackground(null);
content.setOpaque(true);
content.addHyperlinkListener(createHyperLinkListener());
scrollPane.getViewport().add(content);
dlg.add(scrollPane,BorderLayout.CENTER);
dlg.setVisible(true);
}
/**
* @return HyperlinkListener which transfers the requests to system
*/
protected static HyperlinkListener createHyperLinkListener() {
return new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
String url=e.getURL().toExternalForm();
try {
Runtime.getRuntime().exec(new String[]{url});
} catch(Throwable ex2) {
try {
Runtime.getRuntime().exec(new String[]{"cmd.exe","/c","start",url});
} catch(Throwable ex3) {
// nop
}
}
}
}
};
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -