upload tizen1.0 source
[sdk/ide/product.git] / org.eclipse.cdt.ui / src / org / eclipse / cdt / ui / templateengine / uitree / uiwidgets / UIBooleanWidget.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2008 Symbian Software Limited 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  * Bala Torati (Symbian) - Initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.cdt.ui.templateengine.uitree.uiwidgets;
12
13 import java.util.HashMap;
14 import java.util.Map;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.Button;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Label;
24
25 import org.eclipse.cdt.ui.templateengine.uitree.InputUIElement;
26 import org.eclipse.cdt.ui.templateengine.uitree.UIAttributes;
27 import org.eclipse.cdt.ui.templateengine.uitree.UIElement;
28
29
30 /**
31  * This gives a Label and Boolean widget.
32  */
33 public class UIBooleanWidget extends InputUIElement {
34         /**
35          * Boolean widget.
36          */
37         protected Button button;
38
39         /**
40          * Label of this widget.
41          */
42         protected Label label;
43
44         /**
45          * Composite to which this widget control is added.
46          */
47         protected UIComposite uiComposite;
48         
49         private boolean booleanValue;
50
51         /**
52          * Constructor.
53          * 
54          * @param uiAttributes
55          *            attribute associated with this widget.
56          */
57         public UIBooleanWidget(UIAttributes uiAttributes, boolean defaultValue) {
58                 super(uiAttributes);
59                 this.booleanValue= defaultValue;
60         }
61
62         /**
63          * @return HashMap which contains the values in the Boolean Widget.
64          */
65         @Override
66         public Map<String, String> getValues() {
67                 Map<String, String> values = new HashMap<String, String>();
68                 values.put(uiAttributes.get(InputUIElement.ID), Boolean.toString(booleanValue));
69                 return values;
70         }
71
72         /*
73          * @see org.eclipse.cdt.ui.templateengine.uitree.UIElement#setValues(java.util.Map)
74          */
75         @Override
76         public void setValues(Map<String, String> valueMap) {
77                 booleanValue = new Boolean(valueMap.get(uiAttributes.get(InputUIElement.ID))).booleanValue();
78         }
79
80         /*
81          * @see org.eclipse.cdt.ui.templateengine.uitree.UIElement#createWidgets(org.eclipse.cdt.ui.templateengine.uitree.uiwidgets.UIComposite)
82          */
83         @Override
84         public void createWidgets(UIComposite uiComposite) {
85                 GridData gridData = null;
86                 this.uiComposite = uiComposite;
87
88                 label = new Label(uiComposite, SWT.LEFT);
89                 label.setText(uiAttributes.get(InputUIElement.WIDGETLABEL));
90
91                 if (uiAttributes.get(InputUIElement.DESCRIPTION) != null){
92                         String tipText = uiAttributes.get(UIElement.DESCRIPTION);
93                         tipText = tipText.replaceAll("\\\\r\\\\n", "\r\n"); //$NON-NLS-1$ //$NON-NLS-2$
94                         label.setToolTipText(tipText);
95                 }
96                 gridData = new GridData();
97                 gridData.horizontalAlignment = GridData.FILL;
98                 gridData.grabExcessHorizontalSpace = true;
99                 Composite booleanContainer = new Composite(uiComposite, SWT.NONE);
100                 GridData gridcData = new GridData(GridData.FILL_HORIZONTAL);
101                 booleanContainer.setLayout(new GridLayout());
102                 booleanContainer.setLayoutData(gridcData);
103                 button = new Button(booleanContainer, SWT.CHECK);
104                 button.setData(".uid", uiAttributes.get(UIElement.ID)); //$NON-NLS-1$
105                 button.setSelection(new Boolean(booleanValue).booleanValue());
106                 button.addSelectionListener(new SelectionAdapter() {
107                         @Override
108                         public void widgetSelected(SelectionEvent e) {
109                                 booleanValue = button.getSelection();
110                         }
111                 });
112         }
113
114         /*
115          * @see org.eclipse.cdt.ui.templateengine.uitree.UIElement#isValid()
116          */
117         @Override
118         public boolean isValid() {
119                 boolean retVal= true;
120                 String mandatory= uiAttributes.get(InputUIElement.MANDATORY);
121                 if (!booleanValue && Boolean.parseBoolean(mandatory)) {
122                         retVal= false;
123                 }
124                 return retVal;
125         }
126
127         /*
128          * @see org.eclipse.cdt.ui.templateengine.uitree.UIElement#disposeWidget()
129          */
130         @Override
131         public void disposeWidget() {
132                 label.dispose();
133                 button.dispose();
134         }
135 }