diff --git a/tool_window/src/main/java/org/intellij/sdk/toolWindow/CalendarToolWindowFactory.java b/tool_window/src/main/java/org/intellij/sdk/toolWindow/CalendarToolWindowFactory.java new file mode 100644 index 000000000..bbf448bdf --- /dev/null +++ b/tool_window/src/main/java/org/intellij/sdk/toolWindow/CalendarToolWindowFactory.java @@ -0,0 +1,106 @@ +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package org.intellij.sdk.toolWindow; + +import com.intellij.openapi.project.DumbAware; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.wm.ToolWindow; +import com.intellij.openapi.wm.ToolWindowFactory; +import com.intellij.ui.content.Content; +import com.intellij.ui.content.ContentFactory; +import org.apache.commons.lang3.StringUtils; +import org.jetbrains.annotations.NotNull; + +import javax.swing.*; +import java.awt.*; +import java.util.Calendar; +import java.util.Objects; + +public class CalendarToolWindowFactory implements ToolWindowFactory, DumbAware { + + @Override + public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) { + CalendarToolWindowContent toolWindowContent = new CalendarToolWindowContent(toolWindow); + Content content = ContentFactory.getInstance().createContent(toolWindowContent.getContentPanel(), "", false); + toolWindow.getContentManager().addContent(content); + } + + private static class CalendarToolWindowContent { + + private static final String CALENDAR_ICON_PATH = "/toolWindow/Calendar-icon.png"; + private static final String TIME_ZONE_ICON_PATH = "/toolWindow/Time-zone-icon.png"; + private static final String TIME_ICON_PATH = "/toolWindow/Time-icon.png"; + + private final JPanel contentPanel = new JPanel(); + private final JLabel currentDate = new JLabel(); + private final JLabel timeZone = new JLabel(); + private final JLabel currentTime = new JLabel(); + + public CalendarToolWindowContent(ToolWindow toolWindow) { + contentPanel.setLayout(new BorderLayout(0, 20)); + contentPanel.setBorder(BorderFactory.createEmptyBorder(40, 0, 0, 0)); + contentPanel.add(createCalendarPanel(), BorderLayout.PAGE_START); + contentPanel.add(createControlsPanel(toolWindow), BorderLayout.CENTER); + updateCurrentDateTime(); + } + + @NotNull + private JPanel createCalendarPanel() { + JPanel calendarPanel = new JPanel(); + setIconLabel(currentDate, CALENDAR_ICON_PATH); + setIconLabel(timeZone, TIME_ZONE_ICON_PATH); + setIconLabel(currentTime, TIME_ICON_PATH); + calendarPanel.add(currentDate); + calendarPanel.add(timeZone); + calendarPanel.add(currentTime); + return calendarPanel; + } + + private void setIconLabel(JLabel label, String imagePath) { + label.setIcon(new ImageIcon(Objects.requireNonNull(getClass().getResource(imagePath)))); + } + + @NotNull + private JPanel createControlsPanel(ToolWindow toolWindow) { + JPanel controlsPanel = new JPanel(); + JButton refreshDateAndTimeButton = new JButton("Refresh"); + refreshDateAndTimeButton.addActionListener(e -> updateCurrentDateTime()); + controlsPanel.add(refreshDateAndTimeButton); + JButton hideToolWindowButton = new JButton("Hide"); + hideToolWindowButton.addActionListener(e -> toolWindow.hide(null)); + controlsPanel.add(hideToolWindowButton); + return controlsPanel; + } + + private void updateCurrentDateTime() { + Calendar calendar = Calendar.getInstance(); + currentDate.setText(getCurrentDate(calendar)); + timeZone.setText(getTimeZone(calendar)); + currentTime.setText(getCurrentTime(calendar)); + } + + private String getCurrentDate(Calendar calendar) { + return calendar.get(Calendar.DAY_OF_MONTH) + "/" + + (calendar.get(Calendar.MONTH) + 1) + "/" + + calendar.get(Calendar.YEAR); + } + + private String getTimeZone(Calendar calendar) { + long gmtOffset = calendar.get(Calendar.ZONE_OFFSET); // offset from GMT in milliseconds + String gmtOffsetString = String.valueOf(gmtOffset / 3600000); + return (gmtOffset > 0) ? "GMT + " + gmtOffsetString : "GMT - " + gmtOffsetString; + } + + private String getCurrentTime(Calendar calendar) { + return getFormattedValue(calendar, Calendar.HOUR_OF_DAY) + ":" + getFormattedValue(calendar, Calendar.MINUTE); + } + + private String getFormattedValue(Calendar calendar, int calendarField) { + int value = calendar.get(calendarField); + return StringUtils.leftPad(Integer.toString(value), 2, "0"); + } + + public JPanel getContentPanel() { + return contentPanel; + } + } +} diff --git a/tool_window/src/main/java/org/intellij/sdk/toolWindow/MyToolWindow.form b/tool_window/src/main/java/org/intellij/sdk/toolWindow/MyToolWindow.form deleted file mode 100644 index 5c8f35098..000000000 --- a/tool_window/src/main/java/org/intellij/sdk/toolWindow/MyToolWindow.form +++ /dev/null @@ -1,63 +0,0 @@ - -
diff --git a/tool_window/src/main/java/org/intellij/sdk/toolWindow/MyToolWindow.java b/tool_window/src/main/java/org/intellij/sdk/toolWindow/MyToolWindow.java deleted file mode 100644 index 0b2f36743..000000000 --- a/tool_window/src/main/java/org/intellij/sdk/toolWindow/MyToolWindow.java +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2000-2022 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. - -package org.intellij.sdk.toolWindow; - -import com.intellij.openapi.wm.ToolWindow; - -import javax.swing.*; -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( - instance.get(Calendar.DAY_OF_MONTH) + "/" - + (instance.get(Calendar.MONTH) + 1) + "/" - + instance.get(Calendar.YEAR) - ); - currentDate.setIcon(new ImageIcon(getClass().getResource("/toolWindow/Calendar-icon.png"))); - int min = instance.get(Calendar.MINUTE); - String strMin = min < 10 ? "0" + min : String.valueOf(min); - currentTime.setText(instance.get(Calendar.HOUR_OF_DAY) + ":" + strMin); - currentTime.setIcon(new ImageIcon(getClass().getResource("/toolWindow/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("/toolWindow/Time-zone-icon.png"))); - } - - public JPanel getContent() { - return myToolWindowContent; - } - -} diff --git a/tool_window/src/main/java/org/intellij/sdk/toolWindow/MyToolWindowFactory.java b/tool_window/src/main/java/org/intellij/sdk/toolWindow/MyToolWindowFactory.java deleted file mode 100644 index 677e8b939..000000000 --- a/tool_window/src/main/java/org/intellij/sdk/toolWindow/MyToolWindowFactory.java +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. -package org.intellij.sdk.toolWindow; - -import com.intellij.openapi.project.Project; -import com.intellij.openapi.wm.ToolWindow; -import com.intellij.openapi.wm.ToolWindowFactory; -import com.intellij.ui.content.Content; -import com.intellij.ui.content.ContentFactory; -import org.jetbrains.annotations.NotNull; - -public class MyToolWindowFactory implements ToolWindowFactory { - - /** - * Create the tool window content. - * - * @param project current project - * @param toolWindow current tool window - */ - public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) { - MyToolWindow myToolWindow = new MyToolWindow(toolWindow); - Content content = ContentFactory.getInstance().createContent(myToolWindow.getContent(), "", false); - toolWindow.getContentManager().addContent(content); - } - -} diff --git a/tool_window/src/main/resources/META-INF/plugin.xml b/tool_window/src/main/resources/META-INF/plugin.xml index 8ff087aaf..b00945015 100644 --- a/tool_window/src/main/resources/META-INF/plugin.xml +++ b/tool_window/src/main/resources/META-INF/plugin.xml @@ -1,4 +1,4 @@ - +