mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-28 01:07:49 +08:00
tool_window code sample: Reimplement to get rid of the form created with GUI form designer
This commit is contained in:
parent
b2614dd513
commit
aa4bfe5497
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.intellij.sdk.toolWindow.MyToolWindow">
|
||||
<grid id="27dc6" binding="myToolWindowContent" layout-manager="GridLayoutManager" row-count="2" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<xy x="20" y="20" width="500" height="400"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="9aa3f" class="javax.swing.JLabel" binding="currentDate">
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Date"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="44be3" class="javax.swing.JLabel" binding="timeZone">
|
||||
<constraints>
|
||||
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="TimeZone"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="f02af" class="javax.swing.JLabel" binding="currentTime">
|
||||
<constraints>
|
||||
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Time"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="5fceb" class="javax.swing.JButton" binding="refreshToolWindowButton">
|
||||
<constraints>
|
||||
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Refresh"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="3edc1" class="javax.swing.JButton" binding="hideToolWindowButton">
|
||||
<constraints>
|
||||
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Hide"/>
|
||||
</properties>
|
||||
</component>
|
||||
<hspacer id="8bf9c">
|
||||
<constraints>
|
||||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</hspacer>
|
||||
<hspacer id="26841">
|
||||
<constraints>
|
||||
<grid row="1" column="4" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
</hspacer>
|
||||
</children>
|
||||
</grid>
|
||||
</form>
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
<!-- 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. -->
|
||||
<!-- Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. -->
|
||||
<!-- Plugin Configuration File. Read more: https://plugins.jetbrains.com/docs/intellij/plugin-configuration-file.html -->
|
||||
|
||||
<idea-plugin>
|
||||
@ -34,8 +34,8 @@
|
||||
<vendor url="https://plugins.jetbrains.com">IntelliJ Platform SDK</vendor>
|
||||
|
||||
<extensions defaultExtensionNs="com.intellij">
|
||||
<toolWindow id="Sample Calendar" secondary="true" icon="AllIcons.General.Modified" anchor="right"
|
||||
factoryClass="org.intellij.sdk.toolWindow.MyToolWindowFactory"/>
|
||||
<toolWindow id="Sample Calendar" secondary="true" icon="AllIcons.Toolwindows.WebToolWindow" anchor="right"
|
||||
factoryClass="org.intellij.sdk.toolWindow.CalendarToolWindowFactory"/>
|
||||
</extensions>
|
||||
|
||||
</idea-plugin>
|
||||
|
Loading…
x
Reference in New Issue
Block a user