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