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 I would like to know the areas where I can improve on. I'm specifically interested in the structure of my code and the log file monitoring method followed.
The 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;
private ToolWindow toolWindow;
@Override
public void createToolWindowContent(Project project, ToolWindow toolWindow) {
this.toolWindow = toolWindow;
//this generates the log viewer GRID within the IDE
ui = new UIOperations(toolWindow);
ui.createTable();
//Create new thread to retrieve data from log file. this is a continuous process, hence the new Thread
new Thread(){
public void run(){
log_func = new LogFileFunctions();
log_func.connectToLog();
while(true){
String[] result = log_func.getData();
if (result!=null) {
//GUI updated on the Event Dispatcher Thread
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
ui.updateTable(result);
}
});
}
else {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}.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 void createTable(){
JComponent panel = new JPanel(new BorderLayout()); //new BorderLayout extends panel completely inside the tool Window
panel.setBackground(JBColor.WHITE);
toolWindow.getComponent().add(panel);
//set column names
table_model = new DefaultTableModel(headers,0);
table = new JBTable(table_model);
JScrollPane scrollPane = new JBScrollPane(table);
panel.add(scrollPane);
//table formatting
table.setShowHorizontalLines(false);
table.setShowVerticalLines(false);
table.setFillsViewportHeight(true);//sets table background colour
System.out.println("Log Viewer Table Generated..");
}
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(){
//System.out.println(System.getProperty("user.dir")); gives the executing directory
String str; //holds string read from file
String[] result=null; //holds tokenized string
try {
str = in.readLine();
if (!(str == null)) {
result = str.split("[|]");
}
}catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
//connect FileReader to log file
public void connectToLog() {
try {
FileReader fileReader = new FileReader(getClass().getResource("log.txt").getPath());
in = new BufferedReader(fileReader);
System.out.println("Log File location: " + getClass().getResource("log.txt").getPath());
} catch (FileNotFoundException e) {
System.out.println("ERROR: log.txt file not found at " + getClass().getResource("log.txt").getPath() );
}
}
}
EDIT: I would appreciate pointers on how to optimize my code