KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > action > ControlContribution


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.action;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Control;
17 import org.eclipse.swt.widgets.Menu;
18 import org.eclipse.swt.widgets.ToolBar;
19 import org.eclipse.swt.widgets.ToolItem;
20
21 /**
22  * An abstract contribution item implementation for adding an arbitrary
23  * SWT control to a tool bar.
24  * Note, however, that these items cannot be contributed to menu bars.
25  * <p>
26  * The <code>createControl</code> framework method must be implemented
27  * by concrete subclasses.
28  * </p>
29  */

30 public abstract class ControlContribution extends ContributionItem {
31     /**
32      * Creates a control contribution item with the given id.
33      *
34      * @param id the contribution item id
35      */

36     protected ControlContribution(String JavaDoc id) {
37         super(id);
38     }
39
40     /**
41      * Computes the width of the given control which is being added
42      * to a tool bar. This is needed to determine the width of the tool bar item
43      * containing the given control.
44      * <p>
45      * The default implementation of this framework method returns
46      * <code>control.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x</code>.
47      * Subclasses may override if required.
48      * </p>
49      *
50      * @param control the control being added
51      * @return the width of the control
52      */

53     protected int computeWidth(Control control) {
54         return control.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x;
55     }
56
57     /**
58      * Creates and returns the control for this contribution item
59      * under the given parent composite.
60      * <p>
61      * This framework method must be implemented by concrete
62      * subclasses.
63      * </p>
64      *
65      * @param parent the parent composite
66      * @return the new control
67      */

68     protected abstract Control createControl(Composite parent);
69
70     /**
71      * The control item implementation of this <code>IContributionItem</code>
72      * method calls the <code>createControl</code> framework method.
73      * Subclasses must implement <code>createControl</code> rather than
74      * overriding this method.
75      */

76     public final void fill(Composite parent) {
77         createControl(parent);
78     }
79
80     /**
81      * The control item implementation of this <code>IContributionItem</code>
82      * method throws an exception since controls cannot be added to menus.
83      */

84     public final void fill(Menu parent, int index) {
85         Assert.isTrue(false, "Can't add a control to a menu");//$NON-NLS-1$
86
}
87
88     /**
89      * The control item implementation of this <code>IContributionItem</code>
90      * method calls the <code>createControl</code> framework method to
91      * create a control under the given parent, and then creates
92      * a new tool item to hold it.
93      * Subclasses must implement <code>createControl</code> rather than
94      * overriding this method.
95      */

96     public final void fill(ToolBar parent, int index) {
97         Control control = createControl(parent);
98         ToolItem ti = new ToolItem(parent, SWT.SEPARATOR, index);
99         ti.setControl(control);
100         ti.setWidth(computeWidth(control));
101     }
102 }
103
Popular Tags