EDIT: I would appreciate pointers on how to optimize my code
EDIT: I would appreciate pointers on how to optimize my code
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 //codeviewer here..GRID createwithin UIOperationsthe instanceIDE
and call method toui create= table
new UIOperations(toolWindow);
ui.createTable();
//Create new thread to retrieve data from log file. This thread continuouslythis monitorsis thea logcontinuous file
process, hence the new Thread
new Thread(){
public void run(){
log_func = new LogFileFunctions();
//code here..create instance of LogFileFunctions and initialize BufferedReader
log_func.connectToLog();
while(true){
String[] result = log_func.getData();
//code here..get tokenized String from log file usingif getData(result!=null) function from LogFileFunctions, do a null test{
//GUI updated on the Event Dispatcher Thread
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
ui.updateTable(result);
//update GUI using String[] retrieved from getData(). here using updateTable method in UIOperations
}
});
}
}); else {
}try {
else { Thread.sleep(1000);
} catch (InterruptedException e) //thread{
sleep for a specified delay e.printStackTrace();
}
}
}
}.start();
}.start();}
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(){
publicJComponent UIOperationspanel = new JPanel(new BorderLayout(){); //new BorderLayout extends panel completely inside the tool Window
panel.setBackground(JBColor.WHITE);
}toolWindow.getComponent().add(panel);
//set column names
publictable_model void= createTablenew DefaultTableModel(headers,0){
;
table = new JBTable(table_model);
//code to initialize componentsJScrollPane andscrollPane create= new JBScrollPane(table);
here 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);
}
}
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; //codeholds heretokenized string
try {
str = in.readLine();
if (!(str == null)) {
result = str.split("[|]");
}
}catch (FileNotFoundException e) {
e.readprintStackTrace();
line from text file,} tokenizecatch and(IOException returne) result{
e.printStackTrace();
}
return result;
}
//connect FileReader to log file
public void connectToLog() {
try {
FileReader fileReader //initialize= bufferedReadernew hereFileReader(getClass().getResource("log.txt").getPath());
in = new BufferedReader(newFileReaderfileReader);
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() );
}
}
}
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();
}}
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);
}
}
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()));
}
}
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();}
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);
}
}
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() );
}
}
}
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()));
}
}
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. Thank you
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 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 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()));
}
}
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;
@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()));
}
}
- 261
- 4
- 10
- 261
- 4
- 10