JJ Brown 0dca50d851 Removed boilerplate code from example with lambdas
Given the advent of Java 8, anonymous classes for one-line commands are more visual distraction than they are helpful.
I replaced the two used here as action listeners for buttons with equivalent lambdas.
2019-02-23 12:08:10 -06:00

56 lines
2.0 KiB
Java

package myToolWindow;
import com.intellij.openapi.wm.ToolWindow;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
public class MyToolWindow {
private JButton refreshToolWindowButton;
private JButton hideToolWindowButton;
private JLabel currentDate;
private JLabel currentTime;
private JLabel timeZone;
private JPanel myToolWindowContent;
public MyToolWindow(ToolWindow toolWindow) {
hideToolWindowButton.addActionListener(e -> toolWindow.hide(null));
refreshToolWindowButton.addActionListener(e -> currentDateTime());
this.currentDateTime();
}
public void currentDateTime() {
// Get current date and time
Calendar instance = Calendar.getInstance();
currentDate.setText(String.valueOf(instance.get(Calendar.DAY_OF_MONTH)) + "/"
+ String.valueOf(instance.get(Calendar.MONTH) + 1) + "/" +
String.valueOf(instance.get(Calendar.YEAR)));
currentDate.setIcon(new ImageIcon(getClass().getResource("/myToolWindow/Calendar-icon.png")));
int min = instance.get(Calendar.MINUTE);
String strMin;
if (min < 10) {
strMin = "0" + String.valueOf(min);
} else {
strMin = String.valueOf(min);
}
currentTime.setText(instance.get(Calendar.HOUR_OF_DAY) + ":" + strMin);
currentTime.setIcon(new ImageIcon(getClass().getResource("/myToolWindow/Time-icon.png")));
// Get time zone
long gmt_Offset = instance.get(Calendar.ZONE_OFFSET); // offset from GMT in milliseconds
String str_gmt_Offset = String.valueOf(gmt_Offset / 3600000);
str_gmt_Offset = (gmt_Offset > 0) ? "GMT + " + str_gmt_Offset : "GMT - " + str_gmt_Offset;
timeZone.setText(str_gmt_Offset);
timeZone.setIcon(new ImageIcon(getClass().getResource("/myToolWindow/Time-zone-icon.png")));
}
public JPanel getContent() {
return myToolWindowContent;
}
}