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.common.customcomp; 17 18 import com.intellij.ui.JBColor; 19 import com.intellij.ui.components.JBList; 20 import com.intellij.ui.components.JBPanel; 21 import com.intellij.ui.components.JBScrollPane; 22 import net.miginfocom.swing.MigLayout; 23 import ohos.devtools.views.common.UtConstant; 24 import org.apache.commons.lang.StringUtils; 25 26 import javax.swing.BorderFactory; 27 import javax.swing.event.DocumentEvent; 28 import javax.swing.event.DocumentListener; 29 import javax.swing.event.ListSelectionEvent; 30 import javax.swing.event.ListSelectionListener; 31 import java.awt.BorderLayout; 32 import java.awt.Font; 33 import java.awt.event.MouseAdapter; 34 import java.awt.event.MouseEvent; 35 import java.util.List; 36 37 /** 38 * CustomSearchComBox 39 * 40 * @since 2021/5/19 16:39 41 */ 42 public class CustomSearchComBox extends JBPanel { 43 private CustomTextField selectedProcessTextField; 44 private CustomTextField searchField; 45 private JBList processList; 46 private JBScrollPane processScrollPane; 47 private SelectedTextFileLister selectedTextFileListener; 48 private CustomListFilerModel myListModel = new CustomListFilerModel(); 49 50 /** 51 * CustomSearchComBox 52 * 53 * @param deviceId deviceId 54 * @param isDistributedPanel isDistributedPanel 55 */ CustomSearchComBox(int deviceId, boolean isDistributedPanel)56 public CustomSearchComBox(int deviceId, boolean isDistributedPanel) { 57 selectedProcessTextField = new CustomTextField("device"); 58 selectedProcessTextField.setName(UtConstant.UT_DEVICE_PROCESS_PANEL_PROCESS_NAME + deviceId); 59 selectedProcessTextField.setBackground(JBColor.background()); 60 selectedProcessTextField.setOpaque(true); 61 selectedProcessTextField.setForeground(JBColor.foreground().brighter()); 62 selectedProcessTextField.setEditable(false); 63 selectedProcessTextField.setBorder(BorderFactory.createLineBorder(JBColor.background().darker(), 1)); 64 searchField = new CustomTextField("press"); 65 searchField.setBackground(JBColor.background()); 66 searchField.setOpaque(true); 67 searchField.setForeground(JBColor.foreground().brighter()); 68 searchField.setVisible(false); 69 searchField.setName(UtConstant.UT_DEVICE_PROCESS_PANEL_SEARCH_FIELD); 70 processList = new JBList(myListModel); 71 processList.setBackground(JBColor.background()); 72 processList.setOpaque(true); 73 processList.setForeground(JBColor.foreground().brighter()); 74 processList.setVisible(false); 75 processList.setFont(new Font("PingFang SC", Font.PLAIN, 14)); 76 processList.setName(UtConstant.UT_DEVICE_PROCESS_PANEL_TABLE + deviceId); 77 processScrollPane = new JBScrollPane(processList); 78 processScrollPane.setVisible(false); 79 if (isDistributedPanel) { 80 this.setLayout(new BorderLayout()); 81 this.add(selectedProcessTextField, BorderLayout.NORTH); 82 this.add(searchField, BorderLayout.CENTER); 83 this.add(processScrollPane, BorderLayout.SOUTH); 84 } else { 85 this.setLayout(new MigLayout("insets 5", "[grow,fill]", "[grow,fill][grow,fill][grow,fill]")); 86 this.add(selectedProcessTextField, "growx,growy,span"); 87 this.add(searchField, "growx,growy,span"); 88 this.add(processScrollPane, "growx,growy,span"); 89 } 90 selectedProcessTextField.addMouseListener(new MouseAdapter() { 91 @Override 92 public void mouseClicked(MouseEvent mouseEvent) { 93 if (mouseEvent.getClickCount() == 1) { 94 if (searchField.isShowing() && processList.isShowing()) { 95 hidden(); 96 return; 97 } else { 98 selectedTextFileListener.textFileClick(); 99 showProcess(); 100 } 101 } 102 } 103 }); 104 addProcessListListen(); 105 addProcessPanelEvent(); 106 } 107 hidden()108 private void hidden() { 109 myListModel.clear(); 110 searchField.setVisible(false); 111 processScrollPane.setVisible(false); 112 processList.setVisible(false); 113 this.revalidate(); 114 this.repaint(); 115 } 116 showProcess()117 private void showProcess() { 118 searchField.setVisible(true); 119 processList.setVisible(true); 120 processScrollPane.setVisible(true); 121 this.revalidate(); 122 this.repaint(); 123 } 124 addProcessListListen()125 private void addProcessListListen() { 126 processList.addListSelectionListener(new ListSelectionListener() { 127 @Override 128 public void valueChanged(ListSelectionEvent event) { 129 Object selectedValue = processList.getSelectedValue(); 130 if (selectedValue instanceof String) { 131 String value = (String) selectedValue; 132 if (StringUtils.isNotBlank(value)) { 133 selectedProcessTextField.setText(value); 134 hidden(); 135 } 136 } 137 if (processList.getSelectedIndex() == 0) { 138 processList.setSelectedValue(null, true); 139 } 140 } 141 }); 142 } 143 addProcessPanelEvent()144 private void addProcessPanelEvent() { 145 searchField.getDocument().addDocumentListener(new DocumentListener() { 146 @Override 147 public void insertUpdate(DocumentEvent documentEvent) { 148 String text = searchField.getText(); 149 myListModel.refilter(text); 150 } 151 152 @Override 153 public void removeUpdate(DocumentEvent documentEvent) { 154 String text = searchField.getText(); 155 myListModel.refilter(text); 156 } 157 158 @Override 159 public void changedUpdate(DocumentEvent exception) { 160 } 161 }); 162 } 163 164 /** 165 * setSelectedTextFileListener 166 * 167 * @param selectedTextFileListener selectedTextFileListener 168 */ setSelectedTextFileListener(SelectedTextFileLister selectedTextFileListener)169 public void setSelectedTextFileListener(SelectedTextFileLister selectedTextFileListener) { 170 this.selectedTextFileListener = selectedTextFileListener; 171 } 172 173 /** 174 * Refresh process information 175 * 176 * @param processNames processNames 177 */ refreshProcess(List<String> processNames)178 public void refreshProcess(List<String> processNames) { 179 myListModel.clear(); 180 myListModel.addAll(processNames); 181 } 182 183 /** 184 * getSelectedProcessName 185 * 186 * @return String 187 */ getSelectedProcessName()188 public String getSelectedProcessName() { 189 return selectedProcessTextField.getText(); 190 } 191 192 /** 193 * getSelectedProcessTextFiled 194 * 195 * @return String 196 */ getSelectedProcessTextFiled()197 public CustomTextField getSelectedProcessTextFiled() { 198 return selectedProcessTextField; 199 } 200 201 /** 202 * clearSelectedName 203 */ clearSelectedName()204 public void clearSelectedName() { 205 selectedProcessTextField.setText(""); 206 if (!myListModel.isEmpty()) { 207 myListModel.clear(); 208 } 209 } 210 211 } 212