From d374ad22ce74fa9a516b3913501edaa45b54345c Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Sun, 4 Nov 2018 22:16:24 +0100 Subject: [PATCH] Bind ToolWindow to per project object, not global ToolWindowFactory --- .../src/myToolWindow/MyToolWindow.form | 2 +- .../src/myToolWindow/MyToolWindow.java | 63 +++++++++++++++++++ .../src/myToolWindow/MyToolWindowFactory.java | 60 +----------------- 3 files changed, 66 insertions(+), 59 deletions(-) create mode 100644 tool_window/src/myToolWindow/MyToolWindow.java diff --git a/tool_window/src/myToolWindow/MyToolWindow.form b/tool_window/src/myToolWindow/MyToolWindow.form index 515e60337..9dc700e13 100644 --- a/tool_window/src/myToolWindow/MyToolWindow.form +++ b/tool_window/src/myToolWindow/MyToolWindow.form @@ -1,5 +1,5 @@ -
+ diff --git a/tool_window/src/myToolWindow/MyToolWindow.java b/tool_window/src/myToolWindow/MyToolWindow.java new file mode 100644 index 000000000..c04df7a30 --- /dev/null +++ b/tool_window/src/myToolWindow/MyToolWindow.java @@ -0,0 +1,63 @@ +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(new ActionListener() { + public void actionPerformed(ActionEvent e) { + toolWindow.hide(null); + } + }); + refreshToolWindowButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent 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; + } +} diff --git a/tool_window/src/myToolWindow/MyToolWindowFactory.java b/tool_window/src/myToolWindow/MyToolWindowFactory.java index 349e4f108..02c8e57cd 100644 --- a/tool_window/src/myToolWindow/MyToolWindowFactory.java +++ b/tool_window/src/myToolWindow/MyToolWindowFactory.java @@ -4,10 +4,6 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.wm.*; import com.intellij.ui.content.*; -import javax.swing.*; -import java.awt.event.*; -import java.util.Calendar; - /** * Created by IntelliJ IDEA. * User: Alexey.Chursin @@ -15,63 +11,11 @@ import java.util.Calendar; * Time: 2:09:00 PM */ public class MyToolWindowFactory implements ToolWindowFactory { - - private JButton refreshToolWindowButton; - private JButton hideToolWindowButton; - private JLabel currentDate; - private JLabel currentTime; - private JLabel timeZone; - private JPanel myToolWindowContent; - private ToolWindow myToolWindow; - - - public MyToolWindowFactory() { - hideToolWindowButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - myToolWindow.hide(null); - } - }); - refreshToolWindowButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - MyToolWindowFactory.this.currentDateTime(); - } - }); - } - // Create the tool window content. public void createToolWindowContent(Project project, ToolWindow toolWindow) { - myToolWindow = toolWindow; - this.currentDateTime(); + MyToolWindow myToolWindow = new MyToolWindow(toolWindow); ContentFactory contentFactory = ContentFactory.SERVICE.getInstance(); - Content content = contentFactory.createContent(myToolWindowContent, "", false); + Content content = contentFactory.createContent(myToolWindow.getContent(), "", false); toolWindow.getContentManager().addContent(content); - } - - 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"))); - - - } - }