1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.atest.widget;
17 
18 import org.jetbrains.annotations.NotNull;
19 
20 import javax.swing.*;
21 import javax.swing.event.DocumentEvent;
22 import javax.swing.event.DocumentListener;
23 import javax.swing.text.JTextComponent;
24 
25 /** A controller for Atest target with checkboxes. */
26 public class AtestFastInputController {
27 
28     public static final String RUN_ON_HOST_POST = " --host";
29     public static final String TEST_MAPPING_POST = " --test-mapping";
30     public static final String SKIP_BUILD_POST = " -it";
31     private JComboBox mTestTarget;
32     private JCheckBox mRunOnHost;
33     private JCheckBox mTestMapping;
34     private JCheckBox mSkipBuild;
35 
36     /**
37      * Initializes the UI components.
38      *
39      * @param testTarget a JComboBox used to input test command.
40      * @param runOnHost a JCheckBox used to turn on/off run on host.
41      * @param testMapping a JCheckBox used to turn on/off test mapping.
42      * @param skipBuild a JCheckBox used to turn on/off skip build.
43      */
AtestFastInputController( @otNull JComboBox testTarget, @NotNull JCheckBox runOnHost, @NotNull JCheckBox testMapping, @NotNull JCheckBox skipBuild)44     public AtestFastInputController(
45             @NotNull JComboBox testTarget,
46             @NotNull JCheckBox runOnHost,
47             @NotNull JCheckBox testMapping,
48             @NotNull JCheckBox skipBuild) {
49         mTestTarget = testTarget;
50         mRunOnHost = runOnHost;
51         mTestMapping = testMapping;
52         mSkipBuild = skipBuild;
53     }
54     /**
55      * Links all checkbox to test target.
56      *
57      * <p>e.g. if users select run on host check box, the test target appends --host. if users type
58      * "--host" in test target, the run on host check box is selected.
59      */
linkCheckBoxWithTestTarget()60     public void linkCheckBoxWithTestTarget() {
61         mRunOnHost.addActionListener(e -> setCheckbox(mRunOnHost, RUN_ON_HOST_POST));
62         mTestMapping.addActionListener(e -> setCheckbox(mTestMapping, TEST_MAPPING_POST));
63         mSkipBuild.addActionListener(e -> setCheckbox(mSkipBuild, SKIP_BUILD_POST));
64         final JTextComponent tc = (JTextComponent) mTestTarget.getEditor().getEditorComponent();
65         tc.getDocument()
66                 .addDocumentListener(
67                         new DocumentListener() {
68 
69                             /**
70                              * Gives notification that there was an insert into the document. The
71                              * range given by the DocumentEvent bounds the freshly inserted region.
72                              *
73                              * @param e the document event
74                              */
75                             @Override
76                             public void insertUpdate(DocumentEvent e) {
77                                 checkAllTestTarget();
78                             }
79 
80                             /**
81                              * Gives notification that a portion of the document has been removed.
82                              * The range is given in terms of what the view last saw (that is,
83                              * before updating sticky positions).
84                              *
85                              * @param e the document event
86                              */
87                             @Override
88                             public void removeUpdate(DocumentEvent e) {
89                                 checkAllTestTarget();
90                             }
91 
92                             /**
93                              * Gives notification that an attribute or set of attributes changed.
94                              *
95                              * @param e the document event
96                              */
97                             @Override
98                             public void changedUpdate(DocumentEvent e) {
99                                 checkAllTestTarget();
100                             }
101                         });
102     }
103 
104     /** Checks test target if it contains key words of all checkboxes. */
checkAllTestTarget()105     private void checkAllTestTarget() {
106         String testTarget = mTestTarget.getEditor().getItem().toString();
107         checkTestTarget(testTarget, mRunOnHost, RUN_ON_HOST_POST);
108         checkTestTarget(testTarget, mTestMapping, TEST_MAPPING_POST);
109         checkTestTarget(testTarget, mSkipBuild, SKIP_BUILD_POST);
110     }
111 
112     /**
113      * Sets the checkbox behavior.
114      *
115      * <p>if users select check box, the test target appends the corresponding postfix.
116      *
117      * @param checkbox the JCheckBox to set.
118      * @param postfix the corresponding postfix of the JCheckBox.
119      */
setCheckbox(JCheckBox checkbox, String postfix)120     private void setCheckbox(JCheckBox checkbox, String postfix) {
121         String testTarget = mTestTarget.getEditor().getItem().toString();
122         if (checkbox.isSelected()) {
123             testTarget = testTarget.concat(postfix);
124         } else {
125             testTarget = testTarget.replace(postfix, "");
126         }
127         mTestTarget.setSelectedItem(testTarget);
128     }
129 
130     /**
131      * Checks test target if it contains key words of checkbox.
132      *
133      * <p>if users type the keyword in test target, the corresponding checkbox is selected.
134      *
135      * @param testTarget the string in test target.
136      * @param checkbox the JCheckBox to set.
137      * @param postfix the corresponding postfix of the JCheckBox.
138      */
checkTestTarget(String testTarget, JCheckBox checkbox, String postfix)139     private void checkTestTarget(String testTarget, JCheckBox checkbox, String postfix) {
140         if (testTarget.contains(postfix)) {
141             checkbox.setSelected(true);
142         } else {
143             checkbox.setSelected(false);
144         }
145     }
146 }
147