1 /* 2 * Copyright (c) 2021 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 ohos.devtools.views.layout.chartview.utils; 17 18 import com.intellij.icons.AllIcons; 19 import com.intellij.openapi.ui.JBPopupMenu; 20 import com.intellij.ui.JBColor; 21 import com.intellij.ui.components.JBCheckBox; 22 import com.intellij.ui.components.JBLabel; 23 import com.intellij.ui.components.JBPanel; 24 import com.intellij.ui.components.JBScrollPane; 25 import net.miginfocom.swing.MigLayout; 26 import org.apache.logging.log4j.LogManager; 27 import org.apache.logging.log4j.Logger; 28 29 import javax.swing.JButton; 30 import javax.swing.JComponent; 31 import javax.swing.JScrollPane; 32 import javax.swing.JTextField; 33 import java.awt.Dimension; 34 import java.awt.event.ActionEvent; 35 import java.awt.event.ActionListener; 36 import java.util.ArrayList; 37 import java.util.List; 38 39 /** 40 * MultiComboBox 41 * 42 * @param <T> 43 * @since 2021/11/16 9:31 44 */ 45 public class MultiComboBox<T> extends JComponent implements ActionListener { 46 private static final Logger LOGGER = LogManager.getLogger(MultiComboBox.class); 47 48 /** 49 * scroll pane width 50 */ 51 private static final int SCROLL_PANE_WIDTH = 500; 52 53 /** 54 * scroll_pane_height 55 */ 56 private static final int SCROLL_PANE_HEIGHT = 100; 57 58 /** 59 * arrow button width 60 */ 61 private static final int ARROW_BUTTON_WIDTH = 25; 62 63 /** 64 * text field height 65 */ 66 private static final int TEXT_FIELD_HEIGHT = 20; 67 68 /** 69 * list values 70 */ 71 private List<T> values; 72 73 /** 74 * multiDropDown popup 75 */ 76 private MultiDropDownPopup popup; 77 78 /** 79 * multiDropDown textField 80 */ 81 private JTextField textField; 82 83 /** 84 * multiDropDown arrowButton 85 */ 86 private JButton arrowButton; 87 88 /** 89 * MultiComboBox 90 * 91 * @param value value 92 */ MultiComboBox(List<T> value)93 public MultiComboBox(List<T> value) { 94 values = value; 95 initComponent(); 96 } 97 initComponent()98 private void initComponent() { 99 this.setLayout(new MigLayout("insets 0", "[grow]", "[grow]")); 100 popup = new MultiDropDownPopup(values); 101 textField = new JTextField(); 102 textField.setBackground(JBColor.background().brighter()); 103 textField.setPreferredSize(new Dimension(SCROLL_PANE_WIDTH, TEXT_FIELD_HEIGHT)); 104 textField.addActionListener(this); 105 createArrowButton(); 106 arrowButton.setPreferredSize(new Dimension(ARROW_BUTTON_WIDTH, TEXT_FIELD_HEIGHT)); 107 arrowButton.addActionListener(this); 108 add(textField, "split 2,growx"); 109 add(arrowButton, "span, flowx, wrap"); 110 } 111 112 /** 113 * Get selected data 114 * 115 * @return List <T> 116 */ getSelectedValues()117 public List<T> getSelectedValues() { 118 return popup.selectedList; 119 } 120 121 /** 122 * Set the value to be selected 123 * 124 * @param selectValues selectValues 125 */ setSelectValues(List<T> selectValues)126 public void setSelectValues(List<T> selectValues) { 127 popup.setSelectValues(selectValues); 128 setText(selectValues); 129 } 130 setText(List<T> values)131 private void setText(List<T> values) { 132 if (values != null && values.size() > 0) { 133 textField.setText(values.toString().replace("[", "").replace("]", "").strip()); 134 } else { 135 textField.setText(""); 136 } 137 } 138 139 /** 140 * action Performed 141 * 142 * @param arg0 arg0 143 */ 144 @Override actionPerformed(ActionEvent arg0)145 public void actionPerformed(ActionEvent arg0) { 146 if (!popup.isVisible()) { 147 popup.show(this, 0, getHeight()); 148 } 149 } 150 createArrowButton()151 private void createArrowButton() { 152 arrowButton = new JButton(); 153 arrowButton.setContentAreaFilled(false); 154 arrowButton.setFocusPainted(false); 155 arrowButton.setOpaque(true); 156 arrowButton.setIcon(AllIcons.Actions.FindAndShowNextMatchesSmall); 157 arrowButton.setPreferredSize(new Dimension(ARROW_BUTTON_WIDTH, TEXT_FIELD_HEIGHT)); 158 } 159 160 /** 161 * Multi DropDown Popup 162 */ 163 private class MultiDropDownPopup extends JBPopupMenu implements ActionListener { 164 private List<T> values; 165 private List<JBCheckBox> checkBoxList = new ArrayList<>(); 166 private JButton commitButton; 167 private JButton cancelButton; 168 private List<T> selectedList = new ArrayList<>(); 169 170 /** 171 * MultiDropDownPopup 172 * 173 * @param value value 174 */ MultiDropDownPopup(List<T> value)175 public MultiDropDownPopup(List<T> value) { 176 super(); 177 values = value; 178 initComponent(); 179 } 180 initComponent()181 private void initComponent() { 182 JBPanel checkboxPane = new JBPanel(); 183 this.setLayout(new MigLayout("insets 0", "20[grow]5[grow]", "[grow][grow]")); 184 for (Object item : values) { 185 JBCheckBox checkBox = new JBCheckBox(item.toString()); 186 checkBoxList.add(checkBox); 187 } 188 if (checkBoxList.get(0).getText().equals("Select All")) { 189 checkBoxList.get(0).addItemListener(event -> { 190 if (checkBoxList.get(0).isSelected()) { 191 for (int index = 1; index < checkBoxList.size(); index++) { 192 if (!checkBoxList.get(index).isSelected()) { 193 checkBoxList.get(index).setSelected(true); 194 } 195 } 196 } else { 197 for (int index = 1; index < checkBoxList.size(); index++) { 198 if (checkBoxList.get(index).isSelected()) { 199 checkBoxList.get(index).setSelected(false); 200 } 201 } 202 } 203 }); 204 205 checkboxPane.setLayout(new MigLayout("insets 0", "[]")); 206 for (JBCheckBox checkBox : checkBoxList) { 207 checkboxPane.add(checkBox, "span"); 208 } 209 JBPanel buttonPanel = new JBPanel(new MigLayout("insets 0", "[grow][][]")); 210 commitButton = new JButton("Ok"); 211 commitButton.addActionListener(this); 212 cancelButton = new JButton("Cancel"); 213 cancelButton.addActionListener(this); 214 buttonPanel.add(new JBLabel(), "growx,growy"); 215 buttonPanel.add(cancelButton); 216 buttonPanel.add(commitButton, "gapright 5"); 217 JBScrollPane scrollPane = new JBScrollPane(checkboxPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 218 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 219 scrollPane.setPreferredSize(new Dimension(SCROLL_PANE_WIDTH, SCROLL_PANE_HEIGHT)); 220 this.add(scrollPane, "span"); 221 this.add(buttonPanel, "span"); 222 223 } 224 } 225 226 /** 227 * setSelectValues 228 * 229 * @param values values 230 */ setSelectValues(List<T> values)231 private void setSelectValues(List<T> values) { 232 if (values != null && values.size() > 0) { 233 for (T value : values) { 234 for (JBCheckBox jbCheckBox : checkBoxList) { 235 if (value.equals(jbCheckBox.getText())) { 236 jbCheckBox.setSelected(true); 237 } 238 } 239 } 240 setText(getSelectedValues()); 241 } 242 } 243 244 /** 245 * getSelectedValues 246 * 247 * @return List <T> 248 */ getSelectedValues()249 private List<T> getSelectedValues() { 250 List<T> selectedValues = new ArrayList<>(); 251 if (checkBoxList.get(0).getText().equals("Select All")) { 252 if (checkBoxList.get(0).isSelected()) { 253 for (int index = 1; index < checkBoxList.size(); index++) { 254 selectedValues.add(values.get(index)); 255 } 256 } else { 257 for (int index = 1; index < checkBoxList.size(); index++) { 258 if (checkBoxList.get(index).isSelected()) { 259 selectedValues.add(values.get(index)); 260 } 261 } 262 } 263 } else { 264 for (int index = 0; index < checkBoxList.size(); index++) { 265 if (checkBoxList.get(index).isSelected()) { 266 selectedValues.add(values.get(index)); 267 } 268 } 269 } 270 selectedList = selectedValues; 271 return selectedValues; 272 } 273 274 /** 275 * Auto-generated method stub 276 * 277 * @param event event 278 */ 279 @Override actionPerformed(ActionEvent event)280 public void actionPerformed(ActionEvent event) { 281 Object source = event.getSource(); 282 if (source instanceof JButton) { 283 JButton button = (JButton) source; 284 if (button.equals(commitButton)) { 285 selectedList = getSelectedValues(); 286 setText(selectedList); 287 popup.setVisible(false); 288 } else if (button.equals(cancelButton)) { 289 popup.setVisible(false); 290 } else { 291 LOGGER.info("not button is close"); 292 } 293 } 294 } 295 } 296 getTextField()297 public JTextField getTextField() { 298 return textField; 299 } 300 setTextField(JTextField textField)301 public void setTextField(JTextField textField) { 302 this.textField = textField; 303 } 304 } 305