Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2010 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.hierarchyviewer.util;
     18 
     19 import com.android.hierarchyviewerlib.actions.ImageAction;
     20 
     21 import org.eclipse.jface.action.Action;
     22 import org.eclipse.jface.util.IPropertyChangeListener;
     23 import org.eclipse.jface.util.PropertyChangeEvent;
     24 import org.eclipse.swt.SWT;
     25 import org.eclipse.swt.events.SelectionEvent;
     26 import org.eclipse.swt.events.SelectionListener;
     27 import org.eclipse.swt.widgets.Button;
     28 import org.eclipse.swt.widgets.Composite;
     29 
     30 public class ActionButton implements IPropertyChangeListener, SelectionListener {
     31     private Button mButton;
     32 
     33     private Action mAction;
     34 
     35     public ActionButton(Composite parent, ImageAction action) {
     36         this.mAction = (Action) action;
     37         if (this.mAction.getStyle() == Action.AS_CHECK_BOX) {
     38             mButton = new Button(parent, SWT.CHECK);
     39         } else {
     40             mButton = new Button(parent, SWT.PUSH);
     41         }
     42         mButton.setText(action.getText());
     43         mButton.setImage(action.getImage());
     44         this.mAction.addPropertyChangeListener(this);
     45         mButton.addSelectionListener(this);
     46         mButton.setToolTipText(action.getToolTipText());
     47         mButton.setEnabled(this.mAction.isEnabled());
     48     }
     49 
     50     public void propertyChange(PropertyChangeEvent e) {
     51         if (e.getProperty().toUpperCase().equals("ENABLED")) { //$NON-NLS-1$
     52             mButton.setEnabled((Boolean) e.getNewValue());
     53         } else if (e.getProperty().toUpperCase().equals("CHECKED")) { //$NON-NLS-1$
     54             mButton.setSelection(mAction.isChecked());
     55         }
     56     }
     57 
     58     public void setLayoutData(Object data) {
     59         mButton.setLayoutData(data);
     60     }
     61 
     62     public void widgetDefaultSelected(SelectionEvent e) {
     63         // pass
     64     }
     65 
     66     public void widgetSelected(SelectionEvent e) {
     67         if (mAction.getStyle() == Action.AS_CHECK_BOX) {
     68             mAction.setChecked(mButton.getSelection());
     69         }
     70         mAction.run();
     71     }
     72 
     73     public void addSelectionListener(SelectionListener listener) {
     74         mButton.addSelectionListener(listener);
     75     }
     76 
     77     public void setVisible(boolean visible) {
     78         mButton.setVisible(visible);
     79     }
     80 }
     81