I am developing a plugin for intellij that basically monitors a log file and displays the data in a tool window within the IDE in real-time. I have managed to get it working but iI would like to know the areas where iI can improve on. I'm specifically interested in the structure of my code and the log file monitoring method followed. Thank you
createToolWindowContentThe createToolWindowContent
method is called when the log panel is opened for the first time.
public class LogToolWindowLoader implements ToolWindowFactory {
private UIOperations ui;
private LogFileFunctions log_func;
@Override
public void createToolWindowContent(Project project, ToolWindow toolWindow) {
//code here.. create UIOperations instance and call method to create table
//Create new thread to retrieve data from log file. This thread continuously monitors the log file
new Thread(){
public void run(){
//code here..create instance of LogFileFunctions and initialize BufferedReader
while(true) {
//code here..get tokenized String from log file using getData() function from LogFileFunctions, do a null test
//GUI updated on the Event Dispatcher Thread
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
//update GUI using String[] retrieved from getData(). here using updateTable method in UIOperations
}
});
}
else {
//thread sleep for a specified delay
}
}
}
}.start();
}
}
}
UI operations are handled in this class:
public class UIOperations {
private ToolWindow toolWindow;
private DefaultTableModel table_model;
private JBTable table;
private final String headers[]= {"header1","header2","header3","header4","header5","header6"};
public UIOperations(ToolWindow toolWindow){
this.toolWindow = toolWindow;
}
public UIOperations(){
}
public void createTable(){
//code to initialize components and create table here..
}
public void updateTable(String[] str){
table_model.addRow(str);
}
}
Log file processing is handled in this class:
public class LogFileFunctions {
private BufferedReader in;
//reads the log file to retrieve data.
public String[] getData(){
//code here...read line from text file, tokenize and return result
}
//connect FileReader to log file
public void connectToLog() {
//initialize bufferedReader here..
in = new BufferedReader(newFileReader(getClass().getResource("log.txt").getPath()));
}
}