• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 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 
17 package com.android.ddmuilib.logcat;
18 
19 import com.android.ddmuilib.ImageLoader;
20 
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.ModifyEvent;
23 import org.eclipse.swt.events.ModifyListener;
24 import org.eclipse.swt.events.SelectionAdapter;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.graphics.Rectangle;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Combo;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Dialog;
33 import org.eclipse.swt.widgets.Display;
34 import org.eclipse.swt.widgets.Event;
35 import org.eclipse.swt.widgets.Label;
36 import org.eclipse.swt.widgets.Listener;
37 import org.eclipse.swt.widgets.Shell;
38 import org.eclipse.swt.widgets.Text;
39 
40 /**
41  * Small dialog box to edit a static port number.
42  */
43 public class EditFilterDialog extends Dialog {
44 
45     private static final int DLG_WIDTH = 400;
46     private static final int DLG_HEIGHT = 250;
47 
48     private Shell mParent;
49 
50     private Shell mShell;
51 
52     private boolean mOk = false;
53 
54     /**
55      * Filter being edited or created
56      */
57     private LogFilter mFilter;
58 
59     private String mName;
60     private String mTag;
61     private String mPid;
62 
63     /** Log level as an index of the drop-down combo
64      * @see getLogLevel
65      * @see getComboIndex
66      */
67     private int mLogLevel;
68 
69     private Button mOkButton;
70 
71     private Label mPidWarning;
72 
EditFilterDialog(Shell parent)73     public EditFilterDialog(Shell parent) {
74         super(parent, SWT.DIALOG_TRIM | SWT.BORDER | SWT.APPLICATION_MODAL);
75     }
76 
EditFilterDialog(Shell shell, LogFilter filter)77     public EditFilterDialog(Shell shell, LogFilter filter) {
78         this(shell);
79         mFilter = filter;
80     }
81 
82     /**
83      * Opens the dialog. The method will return when the user closes the dialog
84      * somehow.
85      *
86      * @return true if ok was pressed, false if cancelled.
87      */
open()88     public boolean open() {
89         createUI();
90 
91         if (mParent == null || mShell == null) {
92             return false;
93         }
94 
95         mShell.setMinimumSize(DLG_WIDTH, DLG_HEIGHT);
96         Rectangle r = mParent.getBounds();
97         // get the center new top left.
98         int cx = r.x + r.width/2;
99         int x = cx - DLG_WIDTH / 2;
100         int cy = r.y + r.height/2;
101         int y = cy - DLG_HEIGHT / 2;
102         mShell.setBounds(x, y, DLG_WIDTH, DLG_HEIGHT);
103 
104         mShell.open();
105 
106         Display display = mParent.getDisplay();
107         while (!mShell.isDisposed()) {
108             if (!display.readAndDispatch())
109                 display.sleep();
110         }
111 
112         // we're quitting with OK.
113         // Lets update the filter if needed
114         if (mOk) {
115             // if it was a "Create filter" action we need to create it first.
116             if (mFilter == null) {
117                 mFilter = new LogFilter(mName);
118             }
119 
120             // setup the filter
121             mFilter.setTagMode(mTag);
122 
123             if (mPid != null && mPid.length() > 0) {
124                 mFilter.setPidMode(Integer.parseInt(mPid));
125             } else {
126                 mFilter.setPidMode(-1);
127             }
128 
129             mFilter.setLogLevel(getLogLevel(mLogLevel));
130         }
131 
132         return mOk;
133     }
134 
getFilter()135     public LogFilter getFilter() {
136         return mFilter;
137     }
138 
createUI()139     private void createUI() {
140         mParent = getParent();
141         mShell = new Shell(mParent, getStyle());
142         mShell.setText("Log Filter");
143 
144         mShell.setLayout(new GridLayout(1, false));
145 
146         mShell.addListener(SWT.Close, new Listener() {
147             public void handleEvent(Event event) {
148             }
149         });
150 
151         // top part with the filter name
152         Composite nameComposite = new Composite(mShell, SWT.NONE);
153         nameComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
154         nameComposite.setLayout(new GridLayout(2, false));
155 
156         Label l = new Label(nameComposite, SWT.NONE);
157         l.setText("Filter Name:");
158 
159         final Text filterNameText = new Text(nameComposite,
160                 SWT.SINGLE | SWT.BORDER);
161         if (mFilter != null) {
162             mName = mFilter.getName();
163             if (mName != null) {
164                 filterNameText.setText(mName);
165             }
166         }
167         filterNameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
168         filterNameText.addModifyListener(new ModifyListener() {
169             public void modifyText(ModifyEvent e) {
170                 mName = filterNameText.getText().trim();
171                 validate();
172             }
173         });
174 
175         // separator
176         l = new Label(mShell, SWT.SEPARATOR | SWT.HORIZONTAL);
177         l.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
178 
179 
180         // center part with the filter parameters
181         Composite main = new Composite(mShell, SWT.NONE);
182         main.setLayoutData(new GridData(GridData.FILL_BOTH));
183         main.setLayout(new GridLayout(3, false));
184 
185         l = new Label(main, SWT.NONE);
186         l.setText("by Log Tag:");
187 
188         final Text tagText = new Text(main, SWT.SINGLE | SWT.BORDER);
189         if (mFilter != null) {
190             mTag = mFilter.getTagFilter();
191             if (mTag != null) {
192                 tagText.setText(mTag);
193             }
194         }
195         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
196         gd.horizontalSpan = 2;
197         tagText.setLayoutData(gd);
198         tagText.addModifyListener(new ModifyListener() {
199             public void modifyText(ModifyEvent e) {
200                 mTag = tagText.getText().trim();
201                 validate();
202             }
203         });
204 
205         l = new Label(main, SWT.NONE);
206         l.setText("by pid:");
207 
208         final Text pidText = new Text(main, SWT.SINGLE | SWT.BORDER);
209         if (mFilter != null) {
210             if (mFilter.getPidFilter() != -1) {
211                 mPid = Integer.toString(mFilter.getPidFilter());
212             } else {
213                 mPid = "";
214             }
215             pidText.setText(mPid);
216         }
217         pidText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
218         pidText.addModifyListener(new ModifyListener() {
219             public void modifyText(ModifyEvent e) {
220                 mPid = pidText.getText().trim();
221                 validate();
222             }
223         });
224 
225         mPidWarning = new Label(main, SWT.NONE);
226         mPidWarning.setImage(ImageLoader.getDdmUiLibLoader().loadImage("empty.png", // $NON-NLS-1$
227                 mShell.getDisplay()));
228 
229         l = new Label(main, SWT.NONE);
230         l.setText("by Log level:");
231 
232         final Combo logCombo = new Combo(main, SWT.DROP_DOWN | SWT.READ_ONLY);
233         gd = new GridData(GridData.FILL_HORIZONTAL);
234         gd.horizontalSpan = 2;
235         logCombo.setLayoutData(gd);
236 
237         // add the labels
238         logCombo.add("<none>");
239         logCombo.add("Error");
240         logCombo.add("Warning");
241         logCombo.add("Info");
242         logCombo.add("Debug");
243         logCombo.add("Verbose");
244 
245         if (mFilter != null) {
246             mLogLevel = getComboIndex(mFilter.getLogLevel());
247             logCombo.select(mLogLevel);
248         } else {
249             logCombo.select(0);
250         }
251 
252         logCombo.addSelectionListener(new SelectionAdapter() {
253             @Override
254             public void widgetSelected(SelectionEvent e) {
255                 // get the selection
256                 mLogLevel = logCombo.getSelectionIndex();
257                 validate();
258             }
259         });
260 
261         // separator
262         l = new Label(mShell, SWT.SEPARATOR | SWT.HORIZONTAL);
263         l.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
264 
265         // bottom part with the ok/cancel
266         Composite bottomComp = new Composite(mShell, SWT.NONE);
267         bottomComp
268                 .setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
269         bottomComp.setLayout(new GridLayout(2, true));
270 
271         mOkButton = new Button(bottomComp, SWT.NONE);
272         mOkButton.setText("OK");
273         mOkButton.addSelectionListener(new SelectionAdapter() {
274             @Override
275             public void widgetSelected(SelectionEvent e) {
276                 mOk = true;
277                 mShell.close();
278             }
279         });
280         mOkButton.setEnabled(false);
281         mShell.setDefaultButton(mOkButton);
282 
283         Button cancelButton = new Button(bottomComp, SWT.NONE);
284         cancelButton.setText("Cancel");
285         cancelButton.addSelectionListener(new SelectionAdapter() {
286             @Override
287             public void widgetSelected(SelectionEvent e) {
288                 mShell.close();
289             }
290         });
291 
292         validate();
293     }
294 
295     /**
296      * Returns the log level from a combo index.
297      * @param index the Combo index
298      * @return a log level valid for the Log class.
299      */
getLogLevel(int index)300     protected int getLogLevel(int index) {
301         if (index == 0) {
302             return -1;
303         }
304 
305         return 7 - index;
306     }
307 
308     /**
309      * Returns the index in the combo that matches the log level
310      * @param logLevel The Log level.
311      * @return the combo index
312      */
getComboIndex(int logLevel)313     private int getComboIndex(int logLevel) {
314         if (logLevel == -1) {
315             return 0;
316         }
317 
318         return 7 - logLevel;
319     }
320 
321     /**
322      * Validates the content of the 2 text fields and enable/disable "ok", while
323      * setting up the warning/error message.
324      */
validate()325     private void validate() {
326 
327         // then we check it only contains digits.
328         if (mPid != null) {
329             if (mPid.matches("[0-9]*") == false) { // $NON-NLS-1$
330                 mOkButton.setEnabled(false);
331                 mPidWarning.setImage(ImageLoader.getDdmUiLibLoader().loadImage(
332                         "warning.png", // $NON-NLS-1$
333                         mShell.getDisplay()));
334                 return;
335             } else {
336                 mPidWarning.setImage(ImageLoader.getDdmUiLibLoader().loadImage(
337                         "empty.png", // $NON-NLS-1$
338                         mShell.getDisplay()));
339             }
340         }
341 
342         if (mName == null || mName.length() == 0) {
343             mOkButton.setEnabled(false);
344             return;
345         }
346 
347         mOkButton.setEnabled(true);
348     }
349 }
350