import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.*;
import javax.tools.*;
import javax.annotation.processing.*;
import javax.lang.model.*;
import javax.lang.model.element.*;
import javax.lang.model.type.*;
import javax.lang.model.util.*;
import java.net.URI;
public class Compile2 extends JPanel implements ActionListener
{
static private final String newline = "\n";
JButton compileButton;
JTextArea log;
JFileChooser fc;
public Compile2()
{
super(new BorderLayout());
log = new JTextArea(5, 20);
log.setMargin(new Insets(5, 5, 5, 5));
log.setEditable(false);
JScrollPane logScrollPane = new JScrollPane(log);
fc = new JFileChooser();
compileButton = new JButton("Compile");
compileButton.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.add(compileButton);
add(buttonPanel, BorderLayout.PAGE_START);
add(logScrollPane, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == compileButton)
{
int returnVal = fc.showOpenDialog(Compile2.this);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
log.append("Compiling file: " +file.getName()+ "." +newline);
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager =
javac.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> compilationUnits1 =
fileManager.getJavaFileObjects(file);
javac.getTask(null, fileManager, null, null, null,
compilationUnits1).call();
DiagnosticCollector<JavaFileObject> diagnostics = new
DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager2 =
javac.getStandardFileManager(diagnostics, null, null);
javac.getTask(null, fileManager, diagnostics, null, null,
compilationUnits1).call();
for(Diagnostic<? extends JavaFileObject> diagnostic:
diagnostics.getDiagnostics())
System.out.format("Error on line %d in %s%n",
diagnostic.getLineNumber(), diagnostic.getSource().toUri());
class JavaSourceFromString extends SimpleJavaFileObject
{
final String code;
public JavaSourceFromString(String name, String code)
{
super(URI.create("string:///" + name.replace('.','/') +
Kind.SOURCE.extension),
Kind.SOURCE);
this.code = code;
}
@Override
public CharSequence getCharContent(boolean
ignoreEncodingErrors) {
return code;
}
}
log.append("Class file created!" +newline);
}
log.setCaretPosition(log.getDocument().getLength());
}
}
private static void Show()
{
JFrame frame = new JFrame("Compiler");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Compile2());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
UIManager.put("swing.boldMetal", Boolean.FALSE);
Show();
}
}
);
}
}