1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package com.update.check.action.view; 17 18 import com.intellij.openapi.project.Project; 19 import com.intellij.openapi.ui.DialogWrapper; 20 import com.update.check.action.DataUpdateNotifier; 21 import com.update.check.log.Logger; 22 import org.jetbrains.annotations.NotNull; 23 24 import javax.swing.JPanel; 25 import javax.swing.JTable; 26 import javax.swing.JComponent; 27 import javax.swing.table.AbstractTableModel; 28 import javax.swing.table.TableColumn; 29 import java.awt.event.ActionEvent; 30 import java.awt.event.MouseAdapter; 31 import java.awt.event.MouseEvent; 32 import java.util.ArrayList; 33 import java.util.LinkedHashMap; 34 import java.util.List; 35 import java.util.Map; 36 37 /** 38 * Popup box for selecting change type within the tool. 39 * 40 * @since 23-07-18 41 */ 42 public class ShowTypeDialog extends DialogWrapper { 43 private static final Logger LOGGER = Logger.createLogger(); 44 45 private static final String LOG_TAG = ShowTypeDialog.class.getName(); 46 47 private JPanel rootPanel; 48 49 private JTable typeTable; 50 51 private LinkedHashMap<String, Boolean> types = new LinkedHashMap<>(); 52 53 private Project project; 54 55 private List<String> result = new ArrayList<>(); 56 57 /** 58 * ShowTypeDialog 59 * 60 * @param project project 61 * @param types change type 62 */ ShowTypeDialog(Project project, LinkedHashMap<String, Boolean> types)63 public ShowTypeDialog(Project project, LinkedHashMap<String, Boolean> types) { 64 super(project, false); 65 LOGGER.info(LOG_TAG, "start filter changeType"); 66 this.types = types; 67 this.project = project; 68 this.setStyle(); 69 this.setListenerToReportPanel(); 70 this.init(); 71 } 72 setStyle()73 private void setStyle() { 74 ChangeType changeType = new ChangeType(types); 75 this.typeTable.setModel(changeType); 76 this.typeTable.setRowSelectionAllowed(true); 77 this.typeTable.setColumnSelectionAllowed(true); 78 TableColumn tc = this.typeTable.getColumnModel().getColumn(0); 79 tc.setCellEditor(this.typeTable.getDefaultEditor(Boolean.class)); 80 tc.setCellRenderer(this.typeTable.getDefaultRenderer(Boolean.class)); 81 tc.setPreferredWidth(50); 82 tc.setMaxWidth(200); 83 tc.setMinWidth(100); 84 } 85 setListenerToReportPanel()86 private void setListenerToReportPanel() { 87 this.typeTable.addMouseListener(new MouseAdapter() { 88 @Override 89 public void mouseClicked(MouseEvent e) { 90 int col = typeTable.columnAtPoint(e.getPoint()); 91 int row = typeTable.getSelectedRow(); 92 processSelectionBox((boolean) typeTable.getValueAt(row, col), row); 93 } 94 }); 95 } 96 processSelectionBox(boolean valueAt, int row)97 private void processSelectionBox(boolean valueAt, int row) { 98 changeTypes(row, valueAt); 99 if (row == 0) { 100 LOGGER.info(LOG_TAG, "select all or deselect all"); 101 for (int i = 1; i < typeTable.getRowCount(); i++) { 102 changeTypes(i, valueAt); 103 } 104 } 105 if (!valueAt && (boolean) typeTable.getValueAt(0, 0)) { 106 changeTypes(0, valueAt); 107 } 108 setStyle(); 109 } 110 changeTypes(int row, boolean isSelected)111 private void changeTypes(int row, boolean isSelected) { 112 this.types.computeIfPresent(String.valueOf(typeTable.getValueAt(row, 1)), 113 (String key, Boolean value) -> isSelected); 114 } 115 116 class ChangeType extends AbstractTableModel { 117 List<Object[]> dataList = new ArrayList<>(); 118 String[] titles = new String[3]; 119 int sum = types.size(); 120 121 /** 122 * ChangeType 123 * 124 * @param types change types 125 */ ChangeType(LinkedHashMap<String, Boolean> types)126 public ChangeType(LinkedHashMap<String, Boolean> types) { 127 for (Map.Entry<String, Boolean> entry : types.entrySet()) { 128 dataList.add(new Object[]{entry.getValue(), entry.getKey()}); 129 } 130 titles[0] = ConstString.get("check.choose.type"); 131 titles[1] = ConstString.get("check.types"); 132 } 133 134 @Override isCellEditable(int row, int col)135 public boolean isCellEditable(int row, int col) { 136 return col == 0; 137 } 138 139 @Override getColumnName(int column)140 public String getColumnName(int column) { 141 return titles[column]; 142 } 143 144 @Override getRowCount()145 public int getRowCount() { 146 return sum; 147 } 148 149 @Override getColumnCount()150 public int getColumnCount() { 151 return 2; 152 } 153 154 @Override getValueAt(int rowIndex, int columnIndex)155 public Object getValueAt(int rowIndex, int columnIndex) { 156 return this.dataList.get(rowIndex)[columnIndex]; 157 } 158 159 @Override setValueAt(Object aValue, int rowIndex, int columnIndex)160 public void setValueAt(Object aValue, int rowIndex, int columnIndex) { 161 this.dataList.get(rowIndex)[columnIndex] = aValue; 162 } 163 } 164 165 /** 166 * Load Popup 167 * 168 * @param project project 169 * @param types change type 170 */ showDialog(@otNull Project project, LinkedHashMap<String, Boolean> types)171 public static void showDialog(@NotNull Project project, LinkedHashMap<String, Boolean> types) { 172 ShowTypeDialog showTypeDialog = new ShowTypeDialog(project, types); 173 showTypeDialog.show(); 174 } 175 176 @Override createCenterPanel()177 protected JComponent createCenterPanel() { 178 return this.rootPanel; 179 } 180 181 @Override doCancelAction()182 public void doCancelAction() { 183 this.onCancel(null); 184 } 185 onCancel(ActionEvent event)186 private void onCancel(ActionEvent event) { 187 super.doCancelAction(); 188 dispose(); 189 } 190 191 @Override doOKAction()192 protected void doOKAction() { 193 LOGGER.info(LOG_TAG, "filter completed"); 194 super.doOKAction(); 195 DataUpdateNotifier 196 .getInstance() 197 .notifyDataChange(types, "chooseType"); 198 } 199 } 200