Package org.dcarew.hudson.utils

Source Code of org.dcarew.hudson.utils.LabelContributionItem

/*
* Copyright (c) 2009
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dcarew.hudson.utils;

import java.util.HashMap;
import java.util.Map;

import org.dcarew.hudson.HudsonPlugin;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.ContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IMenuCreator;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ToolBar;

// TODO: add hyperlinks to the tooltips

/**
* A ContributionItem for the application's status line.
*
* @author Devon Carew
*/
public class LabelContributionItem
  extends ContributionItem
{
  private Label          label;
 
  private IAction          hudsonAction;
  private IMenuCreator       menuCreator;
 
  private IAction          doubleClickAction;
  private String          tooltip = "";
  private ImageDescriptor      imageDescriptor = HudsonPlugin.getImageDescriptor("resources/icons/hudson.gif");
  private String          text;
 
  private static Map        imageDescriptionMap = new HashMap();
 
 
  public LabelContributionItem(String id, IMenuCreator menuCreator)
  {
    super(id);
   
    this.menuCreator = menuCreator;
  }
 
  public void fill(Composite composite)
  {
    Composite parent = new Composite(composite, SWT.NONE);
    GridLayout layout = new GridLayout(3, false);
    layout.marginWidth = 0;
    layout.marginHeight = 0;
    parent.setLayout(layout);
   
    // separator
    Label sep = new Label(parent, SWT.SEPARATOR);
    sep.setLayoutData(new GridData(GridData.FILL_VERTICAL));
   
    // action menu
    hudsonAction = new Action("", IAction.AS_DROP_DOWN_MENU) {
      public void run() {
        handleDoubleClick();
      }
    };
    hudsonAction.setMenuCreator(menuCreator);
    hudsonAction.setImageDescriptor(imageDescriptor);
   
    ToolBar toolbar = new ToolBar(parent, SWT.HORIZONTAL);
    IContributionItem contributionItem = new ActionContributionItem(hudsonAction);
    contributionItem.fill(toolbar, -1);
    toolbar.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER));
   
    // project count label
    label = new Label(parent, SWT.LEFT);
    label.setText("000 projects");
    label.setVisible(false);
    label.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER));
   
    setTooltip(tooltip);
   
    if (text != null)
      setText(text);
  }
 
  public void setDoubleClickAction(IAction doubleClickAction)
  {
    this.doubleClickAction = doubleClickAction;
  }
 
  public void setText(String text)
  {
    this.text = text;
   
    if (!label.isDisposed())
    {
      label.setText(text);
     
      if (!label.isVisible())
        label.setVisible(true);
    }
  }
 
  public void setImage(Image image)
  {
    if (!imageDescriptionMap.containsKey(image))
    {
      if (image == null)
        imageDescriptionMap.put(image, null);
      else
        imageDescriptionMap.put(image, new ImageImageDescription(image));
    }
   
    ImageDescriptor imageDescriptor = (ImageDescriptor)imageDescriptionMap.get(image);
   
    this.imageDescriptor = imageDescriptor;
   
    hudsonAction.setImageDescriptor(imageDescriptor);
  }
 
  public void setTooltip(String tooltip)
  {
    this.tooltip = tooltip;
   
    if (!label.isDisposed())
    {
      hudsonAction.setToolTipText(tooltip);
      label.setToolTipText(tooltip);
    }
  }
 
  private void handleDoubleClick()
  {
    if (doubleClickAction != null)
      doubleClickAction.run();
  }
 
  /*private static class CustomToolTip
    extends ToolTip
  {
    private String   text = "";
    private Label   tooltipLabel;
   
    public CustomToolTip(Control control)
    {
      super(control, NO_RECREATE, false);
    }
   
    public void setToolTipText(String tooltip)
    {
      if (tooltip == null)
        tooltip = "";
     
      this.text = tooltip;
     
      if (tooltipLabel != null && !tooltipLabel.isDisposed())
      {
        tooltipLabel.setText(tooltip);
       
        tooltipLabel.getShell().pack();
      }
    }
   
    protected Composite createToolTipContentArea(Event event, Composite parent)
    {
      Composite composite = new Composite(parent, SWT.NONE);
      composite.setLayout(new GridLayout());
     
      tooltipLabel = new Label(composite, SWT.NONE);
      tooltipLabel.setText(text);
     
      return composite;
    }
  }*/
 
}
 
TOP

Related Classes of org.dcarew.hudson.utils.LabelContributionItem

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.