11package com .qbb .component ;
22
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 ;
113import com .intellij .openapi .options .ConfigurationException ;
124import com .intellij .openapi .options .SearchableConfigurable ;
13- import com .intellij .openapi .ui .MessageType ;
5+ import com .intellij .openapi .project .Project ;
6+ import com .intellij .openapi .project .ProjectUtil ;
147import com .intellij .openapi .ui .Messages ;
8+ import com .intellij .ui .ToolbarDecorator ;
9+ import com .intellij .ui .components .JBList ;
1510import com .qbb .dto .ConfigDTO ;
16- import org .apache .commons .lang .StringUtils ;
1711import org .jetbrains .annotations .Nls ;
1812import org .jetbrains .annotations .NotNull ;
1913import org .jetbrains .annotations .Nullable ;
20- import org .jetbrains .jps .api .CmdlineRemoteProto ;
2114
2215import javax .swing .*;
23- import javax .swing .border .Border ;
2416import java .awt .*;
25- import java .awt .event .ActionListener ;
17+ import java .util .ArrayList ;
18+ import java .util .Enumeration ;
19+ import java .util .List ;
2620import java .util .UUID ;
2721
2822/**
@@ -36,26 +30,6 @@ public class ConfigComponent implements SearchableConfigurable {
3630
3731 private ConfigPersistence configPersistence = ConfigPersistence .getInstance ();
3832
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-
5933 @ NotNull
6034 @ Override
6135 public String getId () {
@@ -68,121 +42,101 @@ public String getDisplayName() {
6842 return "YapiUpload" ;
6943 }
7044
45+ private JBList <ConfigDTO > list ;
46+
47+ private DefaultListModel <ConfigDTO > defaultListModel ;
48+
7149 @ Nullable
7250 @ Override
7351 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 );
52+ final List <ConfigDTO > configDTOS = configPersistence .getConfigs ();
53+ defaultListModel = new DefaultListModel <>();
54+ for (int i = 0 , len = configDTOS == null ? 0 : configDTOS .size (); i < len ; i ++) {
55+ defaultListModel .addElement (configDTOS .get (i ));
56+ }
57+ list = new JBList <>(defaultListModel );
58+ list .setLayout (new BorderLayout ());
59+ list .setCellRenderer (new ItemComponent ());
60+
61+ // 工具栏
62+ ToolbarDecorator decorator = ToolbarDecorator .createDecorator (list );
63+ decorator .setPreferredSize (new Dimension (0 , 300 ));
64+ // 新增
65+ decorator .setAddAction (actionButton -> addAction ());
66+ // 编辑
67+ decorator .setEditAction (anActionButton -> editAction ());
68+
69+ return decorator .createPanel ();
70+ }
15371
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 );
72+ private void editAction () {
73+ int index = list .getSelectedIndex ();
74+ final Project project = ProjectUtil .guessCurrentProject (list );
75+ ItemAddEditDialog itemAddEditDialog = new ItemAddEditDialog (defaultListModel .get (index ), project );
76+ if (itemAddEditDialog .showAndGet ()) {
77+ final ConfigDTO config = itemAddEditDialog .getConfigDTO ();
78+ for (int i = 0 ; i < defaultListModel .getSize (); i ++) {
79+ if (i == index ) {
80+ continue ;
81+ }
82+ final ConfigDTO dto = defaultListModel .get (i );
83+ if (dto .getProjectName ().equals (config .getProjectName ()) && dto .getModuleName ().equals (config .getModuleName ())) {
84+ Messages .showErrorDialog ("编辑出错了,已添加该模块配置!" , "Error" );
85+ return ;
86+ }
87+ }
88+ defaultListModel .set (index , config );
89+ try {
90+ apply ();
91+ } catch (ConfigurationException e ) {
92+ e .printStackTrace ();
93+ }
94+ } else {
95+ Messages .showInfoMessage ("关闭" , "Info" );
96+ }
97+ }
16298
163- return panel ;
99+ private void addAction () {
100+ final Project project = ProjectUtil .guessCurrentProject (list );
101+ ItemAddEditDialog itemAddEditDialog = new ItemAddEditDialog (null , project );
102+ if (itemAddEditDialog .showAndGet ()) {
103+ final ConfigDTO config = itemAddEditDialog .getConfigDTO ();
104+ final Enumeration <ConfigDTO > elements = defaultListModel .elements ();
105+ while (elements .hasMoreElements ()) {
106+ final ConfigDTO dto = elements .nextElement ();
107+ if (dto .getProjectName ().equals (config .getProjectName ()) && dto .getModuleName ().equals (config .getModuleName ())) {
108+ Messages .showErrorDialog ("添加出错了,已添加该模块配置!" , "Error" );
109+ return ;
110+ }
111+ }
112+ defaultListModel .addElement (config );
113+ try {
114+ apply ();
115+ } catch (ConfigurationException e ) {
116+ e .printStackTrace ();
117+ }
118+ } else {
119+ Messages .showInfoMessage ("关闭" , "Info" );
120+ }
164121 }
165122
166123 @ Override
167124 public boolean isModified () {
168- if (configPersistence .getConfigDTO () == null ) {
125+ if (configPersistence .getConfigs () == null ) {
169126 return true ;
170127 }
171128 //当用户修改配置参数后,在点击“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 ());
129+ return defaultListModel .size () == configPersistence .getConfigs ().size ();
176130 }
177131
178132 @ Override
179133 public void apply () throws ConfigurationException {
180134 //用户点击“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 );
135+ final Enumeration < ConfigDTO > elements = defaultListModel . elements ();
136+ List < ConfigDTO > list = new ArrayList <>( );
137+ while ( elements . hasMoreElements ()) {
138+ list . add ( elements . nextElement ());
139+ }
140+ configPersistence .setConfigs ( list );
187141 }
188142}
0 commit comments