Mercurial > projects > dwt2
comparison org.eclipse.draw2d/src/org/eclipse/draw2d/CheckBox.d @ 12:bc29606a740c
Added dwt-addons in original directory structure of eclipse.org
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 14 Mar 2009 18:23:29 +0100 |
parents | |
children | dbfb303e8fb0 |
comparison
equal
deleted
inserted
replaced
11:43904fec5dca | 12:bc29606a740c |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2005 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 org.eclipse.draw2d.CheckBox; | |
14 | |
15 import java.lang.all; | |
16 import org.eclipse.swt.dwthelper.ByteArrayInputStream; | |
17 | |
18 import org.eclipse.swt.graphics.Image; | |
19 import org.eclipse.swt.graphics.ImageData; | |
20 import org.eclipse.draw2d.Toggle; | |
21 import org.eclipse.draw2d.Label; | |
22 import org.eclipse.draw2d.ChangeListener; | |
23 import org.eclipse.draw2d.ChangeEvent; | |
24 import org.eclipse.draw2d.ButtonModel; | |
25 | |
26 /** | |
27 * A Checkbox is a toggle figure which toggles between the checked and unchecked figures | |
28 * to simulate a check box. A check box contains a text label to represent it. | |
29 */ | |
30 public final class CheckBox | |
31 : Toggle | |
32 { | |
33 | |
34 private Label label = null; | |
35 | |
36 private static Image | |
37 UNCHECKED_, | |
38 CHECKED_; | |
39 | |
40 package static Image UNCHECKED(){ | |
41 if( UNCHECKED_ is null ){ | |
42 synchronized( CheckBox.classinfo ){ | |
43 if( UNCHECKED_ is null ){ | |
44 UNCHECKED_ = createImage( getImportData!("org.eclipse.draw2d.checkboxenabledoff.gif")); | |
45 } | |
46 } | |
47 } | |
48 assert( UNCHECKED_ ); | |
49 return UNCHECKED_; | |
50 } | |
51 package static Image CHECKED(){ | |
52 if( CHECKED_ is null ){ | |
53 synchronized( CheckBox.classinfo ){ | |
54 if( CHECKED_ is null ){ | |
55 CHECKED_ = createImage( getImportData!("org.eclipse.draw2d.checkboxenabledon.gif")); | |
56 } | |
57 } | |
58 } | |
59 assert( CHECKED_ ); | |
60 return CHECKED_; | |
61 } | |
62 | |
63 private static Image createImage( ImportData importdata ) { | |
64 Image image = new Image(null, new ImageData(new ByteArrayInputStream( cast(byte[]) importdata.data))); | |
65 return image; | |
66 } | |
67 | |
68 /** | |
69 * Constructs a CheckBox with no text. | |
70 * | |
71 * @since 2.0 | |
72 */ | |
73 public this() { | |
74 this(""); //$NON-NLS-1$ | |
75 } | |
76 | |
77 /** | |
78 * Constructs a CheckBox with the passed text in its label. | |
79 * @param text The label text | |
80 * @since 2.0 | |
81 */ | |
82 public this(String text) { | |
83 setContents(label = new Label(text, UNCHECKED)); | |
84 } | |
85 | |
86 /** | |
87 * Adjusts CheckBox's icon depending on selection status. | |
88 * | |
89 * @since 2.0 | |
90 */ | |
91 protected void handleSelectionChanged() { | |
92 if (isSelected()) | |
93 label.setIcon(CHECKED); | |
94 else | |
95 label.setIcon(UNCHECKED); | |
96 } | |
97 | |
98 /** | |
99 * Initializes this Clickable by setting a default model and adding a clickable event | |
100 * handler for that model. Also adds a ChangeListener to update icon when selection | |
101 * status changes. | |
102 * | |
103 * @since 2.0 | |
104 */ | |
105 protected void init() { | |
106 super.init(); | |
107 addChangeListener(new class() ChangeListener { | |
108 public void handleStateChanged(ChangeEvent changeEvent) { | |
109 if (changeEvent.getPropertyName().equals(ButtonModel.SELECTED_PROPERTY)) | |
110 handleSelectionChanged(); | |
111 } | |
112 }); | |
113 } | |
114 | |
115 } |