• 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.actions;
18 
19 import org.eclipse.swt.events.SelectionAdapter;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.events.SelectionListener;
22 import org.eclipse.swt.widgets.ToolBar;
23 import org.eclipse.swt.widgets.ToolItem;
24 
25 /**
26  * Wrapper around {@link ToolItem} to implement {@link ICommonAction}
27  */
28 public class ToolItemAction implements ICommonAction {
29     public ToolItem item;
30 
ToolItemAction(ToolBar parent, int style)31     public ToolItemAction(ToolBar parent, int style) {
32         item = new ToolItem(parent, style);
33     }
34 
35     /**
36      * Sets the enabled state of this action.
37      * @param enabled <code>true</code> to enable, and
38      *   <code>false</code> to disable
39      * @see ICommonAction#setChecked(boolean)
40      */
setChecked(boolean checked)41     public void setChecked(boolean checked) {
42         item.setSelection(checked);
43     }
44 
45     /**
46      * Sets the enabled state of this action.
47      * @param enabled <code>true</code> to enable, and
48      *   <code>false</code> to disable
49      * @see ICommonAction#setEnabled(boolean)
50      */
setEnabled(boolean enabled)51     public void setEnabled(boolean enabled) {
52         item.setEnabled(enabled);
53     }
54 
55     /**
56      * Sets the {@link Runnable} that will be executed when the action is triggered (through
57      * {@link SelectionListener#widgetSelected(SelectionEvent)} on the wrapped {@link ToolItem}).
58      * @see ICommonAction#setRunnable(Runnable)
59      */
setRunnable(final Runnable runnable)60     public void setRunnable(final Runnable runnable) {
61         item.addSelectionListener(new SelectionAdapter() {
62             @Override
63             public void widgetSelected(SelectionEvent e) {
64                 runnable.run();
65             }
66         });
67     }
68 }
69