|
| 1 | +package com.qbb.component; |
| 2 | + |
| 3 | +import com.intellij.notification.Notification; |
| 4 | +import com.intellij.notification.NotificationDisplayType; |
| 5 | +import com.intellij.notification.NotificationGroup; |
| 6 | +import com.intellij.notification.Notifications; |
| 7 | +import com.intellij.openapi.application.ApplicationManager; |
| 8 | +import com.intellij.openapi.components.ApplicationComponent; |
| 9 | +import com.intellij.openapi.components.BaseComponent; |
| 10 | +import com.intellij.openapi.options.Configurable; |
| 11 | +import com.intellij.openapi.options.ConfigurationException; |
| 12 | +import com.intellij.openapi.options.SearchableConfigurable; |
| 13 | +import com.intellij.openapi.ui.MessageType; |
| 14 | +import com.intellij.openapi.ui.Messages; |
| 15 | +import com.qbb.dto.ConfigDTO; |
| 16 | +import org.apache.commons.lang.StringUtils; |
| 17 | +import org.jetbrains.annotations.Nls; |
| 18 | +import org.jetbrains.annotations.NotNull; |
| 19 | +import org.jetbrains.annotations.Nullable; |
| 20 | +import org.jetbrains.jps.api.CmdlineRemoteProto; |
| 21 | + |
| 22 | +import javax.swing.*; |
| 23 | +import javax.swing.border.Border; |
| 24 | +import java.awt.*; |
| 25 | +import java.awt.event.ActionListener; |
| 26 | +import java.util.UUID; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author zhangyunfan |
| 30 | + * @version 1.0 |
| 31 | + * @ClassName: ConfigComponent |
| 32 | + * @Description: 配置界面 |
| 33 | + * @date 2020/12/25 |
| 34 | + */ |
| 35 | +public class ConfigComponent implements SearchableConfigurable { |
| 36 | + |
| 37 | + private ConfigPersistence configPersistence = ConfigPersistence.getInstance(); |
| 38 | + |
| 39 | + private static final int TEXT_AREA_ROWS = 1; |
| 40 | + |
| 41 | + private static final int TEXT_AREA_COLUMNS = 5; |
| 42 | + |
| 43 | + private static final int TEXT_AREA_FONTSIZE = 15; |
| 44 | + |
| 45 | + private JPanel panel; |
| 46 | + |
| 47 | + private JLabel yapiJLabel; |
| 48 | + private JTextField yapiTextArea; |
| 49 | + |
| 50 | + private JLabel projectTokenJLabel; |
| 51 | + private JTextField projectTokenTextArea; |
| 52 | + |
| 53 | + private JLabel projectIdJLabel; |
| 54 | + private JTextField projectIdTextArea; |
| 55 | + |
| 56 | + private JLabel projectTypeJLabel; |
| 57 | + private JComboBox projectTypeTextArea; |
| 58 | + |
| 59 | + @NotNull |
| 60 | + @Override |
| 61 | + public String getId() { |
| 62 | + return UUID.randomUUID().toString().replaceAll("-", ""); |
| 63 | + } |
| 64 | + |
| 65 | + @Nls(capitalization = Nls.Capitalization.Title) |
| 66 | + @Override |
| 67 | + public String getDisplayName() { |
| 68 | + return "YapiUpload"; |
| 69 | + } |
| 70 | + |
| 71 | + @Nullable |
| 72 | + @Override |
| 73 | + public JComponent createComponent() { |
| 74 | + panel = new JPanel(); |
| 75 | + GridBagLayout gridBagLayout = new GridBagLayout(); |
| 76 | + panel.setLayout(gridBagLayout); |
| 77 | + //实例化这个对象用来对组件进行管理 |
| 78 | + GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
| 79 | + gridBagConstraints.fill = GridBagConstraints.BOTH; |
| 80 | + gridBagConstraints.insets = new Insets(10, 10, 0, 0); |
| 81 | + Border border = BorderFactory.createLineBorder(Color.BLACK); |
| 82 | + yapiJLabel = new JLabel("yapi地址:"); |
| 83 | + yapiTextArea = new JTextField(configPersistence.getConfigDTO() == null ? "" : configPersistence.getConfigDTO().getYapiUrl()); |
| 84 | + yapiTextArea.setBorder(border); |
| 85 | + yapiTextArea.setFont(new Font(null, Font.PLAIN, TEXT_AREA_FONTSIZE)); |
| 86 | + yapiTextArea.setSize(10, 10); |
| 87 | + yapiJLabel.setAlignmentY(yapiJLabel.LEFT_ALIGNMENT); |
| 88 | + |
| 89 | + projectTokenJLabel = new JLabel("项目Token:"); |
| 90 | + projectTokenTextArea = new JTextField(configPersistence.getConfigDTO() == null ? "" : configPersistence.getConfigDTO().getProjectToken()); |
| 91 | + projectTokenTextArea.setBorder(border); |
| 92 | + projectTokenTextArea.setFont(new Font(null, Font.PLAIN, TEXT_AREA_FONTSIZE)); |
| 93 | + projectTokenJLabel.setAlignmentY(projectTokenJLabel.LEFT_ALIGNMENT); |
| 94 | + |
| 95 | + projectIdJLabel = new JLabel("项目ID:"); |
| 96 | + projectIdTextArea = new JTextField(configPersistence.getConfigDTO() == null ? "" : configPersistence.getConfigDTO().getProjectId()); |
| 97 | + projectIdTextArea.setBorder(border); |
| 98 | + projectIdTextArea.setFont(new Font(null, Font.PLAIN, TEXT_AREA_FONTSIZE)); |
| 99 | + projectIdJLabel.setAlignmentY(projectIdJLabel.LEFT_ALIGNMENT); |
| 100 | + |
| 101 | + projectTypeJLabel = new JLabel("项目类型:"); |
| 102 | + String[] select = {"api", "dubbo"}; |
| 103 | + projectTypeTextArea = new JComboBox(); |
| 104 | + projectTypeTextArea.setModel(new DefaultComboBoxModel(select)); |
| 105 | + projectTypeTextArea.setBounds(15, 15, 100, 25); |
| 106 | + projectTypeJLabel.setAlignmentY(projectTypeJLabel.LEFT_ALIGNMENT); |
| 107 | + |
| 108 | + gridBagConstraints.gridx = 1; |
| 109 | + gridBagConstraints.gridy = 4; |
| 110 | + gridBagConstraints.gridwidth = 1; |
| 111 | + gridBagConstraints.gridheight = 1; |
| 112 | + gridBagLayout.setConstraints(yapiJLabel, gridBagConstraints); |
| 113 | + gridBagConstraints.gridx = 20; |
| 114 | + gridBagConstraints.gridy = 4; |
| 115 | + gridBagConstraints.gridwidth = 1; |
| 116 | + gridBagConstraints.gridheight = 1; |
| 117 | + gridBagLayout.setConstraints(yapiTextArea, gridBagConstraints); |
| 118 | + |
| 119 | + |
| 120 | + gridBagConstraints.gridx = 1; |
| 121 | + gridBagConstraints.gridy = 8; |
| 122 | + gridBagConstraints.gridwidth = 1; |
| 123 | + gridBagConstraints.gridheight = 1; |
| 124 | + gridBagLayout.setConstraints(projectTokenJLabel, gridBagConstraints); |
| 125 | + gridBagConstraints.gridx = 20; |
| 126 | + gridBagConstraints.gridy = 8; |
| 127 | + gridBagConstraints.gridwidth = 1; |
| 128 | + gridBagConstraints.gridheight = 1; |
| 129 | + gridBagLayout.setConstraints(projectTokenTextArea, gridBagConstraints); |
| 130 | + |
| 131 | + |
| 132 | + gridBagConstraints.gridx = 1; |
| 133 | + gridBagConstraints.gridy = 10; |
| 134 | + gridBagConstraints.gridwidth = 1; |
| 135 | + gridBagConstraints.gridheight = 1; |
| 136 | + gridBagLayout.setConstraints(projectIdJLabel, gridBagConstraints); |
| 137 | + gridBagConstraints.gridx = 20; |
| 138 | + gridBagConstraints.gridy = 10; |
| 139 | + gridBagConstraints.gridwidth = 1; |
| 140 | + gridBagConstraints.gridheight = 1; |
| 141 | + gridBagLayout.setConstraints(projectIdTextArea, gridBagConstraints); |
| 142 | + |
| 143 | + gridBagConstraints.gridx = 1; |
| 144 | + gridBagConstraints.gridy = 12; |
| 145 | + gridBagConstraints.gridwidth = 1; |
| 146 | + gridBagConstraints.gridheight = 1; |
| 147 | + gridBagLayout.setConstraints(projectTypeJLabel, gridBagConstraints); |
| 148 | + gridBagConstraints.gridx = 20; |
| 149 | + gridBagConstraints.gridy = 12; |
| 150 | + gridBagConstraints.gridwidth = 1; |
| 151 | + gridBagConstraints.gridheight = 1; |
| 152 | + gridBagLayout.setConstraints(projectTypeTextArea, gridBagConstraints); |
| 153 | + |
| 154 | + panel.add(projectTypeJLabel); |
| 155 | + panel.add(projectTypeTextArea); |
| 156 | + panel.add(projectIdJLabel); |
| 157 | + panel.add(projectIdTextArea); |
| 158 | + panel.add(projectTokenJLabel); |
| 159 | + panel.add(projectTokenTextArea); |
| 160 | + panel.add(yapiJLabel); |
| 161 | + panel.add(yapiTextArea); |
| 162 | + |
| 163 | + return panel; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public boolean isModified() { |
| 168 | + if (configPersistence.getConfigDTO() == null) { |
| 169 | + return true; |
| 170 | + } |
| 171 | + //当用户修改配置参数后,在点击“OK”“Apply”按钮前,框架会自动调用该方法,判断是否有修改,进而控制按钮“OK”“Apply”的是否可用。 |
| 172 | + return !StringUtils.equals(yapiTextArea.getText(), configPersistence.getConfigDTO().getYapiUrl()) |
| 173 | + || !StringUtils.equals(projectTokenTextArea.getText(), configPersistence.getConfigDTO().getProjectToken()) |
| 174 | + || !StringUtils.equals(projectIdTextArea.getText(), configPersistence.getConfigDTO().getProjectId()) |
| 175 | + || !StringUtils.equals((String) projectTypeTextArea.getSelectedItem(), configPersistence.getConfigDTO().getProjectType()); |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public void apply() throws ConfigurationException { |
| 180 | + //用户点击“OK”或“Apply”按钮后会调用该方法,通常用于完成配置信息持久化。 |
| 181 | + ConfigDTO configDTO = new ConfigDTO(); |
| 182 | + configDTO.setYapiUrl(yapiTextArea.getText()); |
| 183 | + configDTO.setProjectToken(projectTokenTextArea.getText()); |
| 184 | + configDTO.setProjectId(projectIdTextArea.getText()); |
| 185 | + configDTO.setProjectType((String) projectTypeTextArea.getSelectedItem()); |
| 186 | + configPersistence.setConfigDTO(configDTO); |
| 187 | + } |
| 188 | +} |
0 commit comments