All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.jface.action.ControlContribution Maven / Gradle / Ivy

There is a newer version: 3.29.0
Show newest version
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jface.action;

import org.eclipse.core.runtime.Assert;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

/**
 * An abstract contribution item implementation for adding an arbitrary 
 * SWT control to a tool bar. 
 * Note, however, that these items cannot be contributed to menu bars.
 * 

* The createControl framework method must be implemented * by concrete subclasses. *

*/ public abstract class ControlContribution extends ContributionItem { /** * Creates a control contribution item with the given id. * * @param id the contribution item id */ protected ControlContribution(String id) { super(id); } /** * Computes the width of the given control which is being added * to a tool bar. This is needed to determine the width of the tool bar item * containing the given control. *

* The default implementation of this framework method returns * control.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x. * Subclasses may override if required. *

* * @param control the control being added * @return the width of the control */ protected int computeWidth(Control control) { return control.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x; } /** * Creates and returns the control for this contribution item * under the given parent composite. *

* This framework method must be implemented by concrete * subclasses. *

* * @param parent the parent composite * @return the new control */ protected abstract Control createControl(Composite parent); /** * The control item implementation of this IContributionItem * method calls the createControl framework method. * Subclasses must implement createControl rather than * overriding this method. */ public final void fill(Composite parent) { createControl(parent); } /** * The control item implementation of this IContributionItem * method throws an exception since controls cannot be added to menus. */ public final void fill(Menu parent, int index) { Assert.isTrue(false, "Can't add a control to a menu");//$NON-NLS-1$ } /** * The control item implementation of this IContributionItem * method calls the createControl framework method to * create a control under the given parent, and then creates * a new tool item to hold it. * Subclasses must implement createControl rather than * overriding this method. */ public final void fill(ToolBar parent, int index) { Control control = createControl(parent); ToolItem ti = new ToolItem(parent, SWT.SEPARATOR, index); ti.setControl(control); ti.setWidth(computeWidth(control)); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy