Mercurial > projects > dwt-addons
comparison dwtx/jface/action/ControlContribution.d @ 24:eb6b3e6de869
ControlContribution
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 03 Apr 2008 00:48:42 +0200 |
parents | |
children | ea8ff534f622 |
comparison
equal
deleted
inserted
replaced
23:1451821c3e00 | 24:eb6b3e6de869 |
---|---|
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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.action.ControlContribution; | |
14 | |
15 import dwtx.jface.action.ContributionItem; | |
16 | |
17 import dwt.DWT; | |
18 import dwt.widgets.Composite; | |
19 import dwt.widgets.Control; | |
20 import dwt.widgets.Menu; | |
21 import dwt.widgets.ToolBar; | |
22 import dwt.widgets.ToolItem; | |
23 import dwtx.core.runtime.Assert; | |
24 | |
25 import dwt.dwthelper.utils; | |
26 | |
27 /** | |
28 * An abstract contribution item implementation for adding an arbitrary | |
29 * DWT control to a tool bar. | |
30 * Note, however, that these items cannot be contributed to menu bars. | |
31 * <p> | |
32 * The <code>createControl</code> framework method must be implemented | |
33 * by concrete subclasses. | |
34 * </p> | |
35 */ | |
36 public abstract class ControlContribution : ContributionItem { | |
37 /** | |
38 * Creates a control contribution item with the given id. | |
39 * | |
40 * @param id the contribution item id | |
41 */ | |
42 protected this(String id) { | |
43 super(id); | |
44 } | |
45 | |
46 /** | |
47 * Computes the width of the given control which is being added | |
48 * to a tool bar. This is needed to determine the width of the tool bar item | |
49 * containing the given control. | |
50 * <p> | |
51 * The default implementation of this framework method returns | |
52 * <code>control.computeSize(DWT.DEFAULT, DWT.DEFAULT, true).x</code>. | |
53 * Subclasses may override if required. | |
54 * </p> | |
55 * | |
56 * @param control the control being added | |
57 * @return the width of the control | |
58 */ | |
59 protected int computeWidth(Control control) { | |
60 return control.computeSize(DWT.DEFAULT, DWT.DEFAULT, true).x; | |
61 } | |
62 | |
63 /** | |
64 * Creates and returns the control for this contribution item | |
65 * under the given parent composite. | |
66 * <p> | |
67 * This framework method must be implemented by concrete | |
68 * subclasses. | |
69 * </p> | |
70 * | |
71 * @param parent the parent composite | |
72 * @return the new control | |
73 */ | |
74 protected abstract Control createControl(Composite parent); | |
75 | |
76 /** | |
77 * The control item implementation of this <code>IContributionItem</code> | |
78 * method calls the <code>createControl</code> framework method. | |
79 * Subclasses must implement <code>createControl</code> rather than | |
80 * overriding this method. | |
81 */ | |
82 public final void fill(Composite parent) { | |
83 createControl(parent); | |
84 } | |
85 | |
86 /** | |
87 * The control item implementation of this <code>IContributionItem</code> | |
88 * method throws an exception since controls cannot be added to menus. | |
89 */ | |
90 public final void fill(Menu parent, int index) { | |
91 Assert.isTrue(false, "Can't add a control to a menu");//$NON-NLS-1$ | |
92 } | |
93 | |
94 /** | |
95 * The control item implementation of this <code>IContributionItem</code> | |
96 * method calls the <code>createControl</code> framework method to | |
97 * create a control under the given parent, and then creates | |
98 * a new tool item to hold it. | |
99 * Subclasses must implement <code>createControl</code> rather than | |
100 * overriding this method. | |
101 */ | |
102 public final void fill(ToolBar parent, int index) { | |
103 Control control = createControl(parent); | |
104 ToolItem ti = new ToolItem(parent, DWT.SEPARATOR, index); | |
105 ti.setControl(control); | |
106 ti.setWidth(computeWidth(control)); | |
107 } | |
108 } |