|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 package com.nokia.carbide.cpp.internal.pi.button.ui; |
|
19 |
|
20 import org.eclipse.jface.dialogs.IDialogConstants; |
|
21 import org.eclipse.jface.dialogs.TrayDialog; |
|
22 import org.eclipse.jface.layout.GridDataFactory; |
|
23 import org.eclipse.jface.layout.GridLayoutFactory; |
|
24 import org.eclipse.swt.SWT; |
|
25 import org.eclipse.swt.events.FocusAdapter; |
|
26 import org.eclipse.swt.events.FocusEvent; |
|
27 import org.eclipse.swt.events.ModifyEvent; |
|
28 import org.eclipse.swt.events.ModifyListener; |
|
29 import org.eclipse.swt.events.SelectionAdapter; |
|
30 import org.eclipse.swt.events.SelectionEvent; |
|
31 import org.eclipse.swt.widgets.Composite; |
|
32 import org.eclipse.swt.widgets.Control; |
|
33 import org.eclipse.swt.widgets.Label; |
|
34 import org.eclipse.swt.widgets.Shell; |
|
35 import org.eclipse.swt.widgets.Text; |
|
36 import org.eclipse.ui.PlatformUI; |
|
37 |
|
38 import com.nokia.carbide.cpp.pi.button.ComNokiaCarbidePiButtonHelpIDs; |
|
39 import com.nokia.carbide.cpp.pi.editors.PIPageEditor; |
|
40 |
|
41 public class BupMapAddDialog extends TrayDialog { |
|
42 |
|
43 private int oldEventKeyCode; |
|
44 private String oldEventEnumString; |
|
45 private String oldEventLabel; |
|
46 private int eventKeyCode; |
|
47 private String eventEnumString; |
|
48 private String eventLabel; |
|
49 private ModifyCachedBupEventMap map; |
|
50 |
|
51 // control |
|
52 private Composite composite = null; |
|
53 private Composite labelAndValueComposite = null; |
|
54 |
|
55 private Label keyCodeLabel = null; |
|
56 private Text keyCodeValueText = null; |
|
57 |
|
58 private Label enumLabel = null; |
|
59 private Text enumValueText = null; |
|
60 |
|
61 private Label labelLabel = null; |
|
62 private Text labelText = null; |
|
63 |
|
64 private Label messageLabel = null; |
|
65 |
|
66 private Label hexNeeds0x = null; |
|
67 |
|
68 public BupMapAddDialog(Shell shell, ModifyCachedBupEventMap cachedMap, int keyCode, String enumString, String label) { |
|
69 super(shell); |
|
70 |
|
71 // store original values |
|
72 oldEventKeyCode = eventKeyCode = keyCode; |
|
73 oldEventEnumString = eventEnumString = enumString; |
|
74 oldEventLabel = eventLabel = label; |
|
75 map = cachedMap; |
|
76 } |
|
77 |
|
78 public Control createDialogArea(Composite parent) { |
|
79 getShell().setText(Messages.getString("BupMapAddDialog.addMapping")); //$NON-NLS-1$ |
|
80 |
|
81 composite = new Composite(parent, SWT.NONE); |
|
82 GridDataFactory.fillDefaults().applyTo(composite); |
|
83 GridLayoutFactory.fillDefaults().margins(5, 5).applyTo(composite); |
|
84 |
|
85 PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, ComNokiaCarbidePiButtonHelpIDs.PI_BUTTON_ADD_DIALOG); |
|
86 |
|
87 labelAndValueComposite = new Composite(composite, SWT.NONE); |
|
88 GridDataFactory.fillDefaults().applyTo(labelAndValueComposite); |
|
89 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(labelAndValueComposite); |
|
90 |
|
91 messageLabel = new Label(composite, SWT.NONE); |
|
92 GridDataFactory.fillDefaults().indent(15,0).grab(true, true).hint(200, 15).applyTo(messageLabel); |
|
93 messageLabel.setFont(PIPageEditor.helvetica_9); |
|
94 |
|
95 hexNeeds0x = new Label(composite, SWT.NONE); |
|
96 GridDataFactory.fillDefaults().indent(15,0).grab(true, true).hint(200, 15).applyTo(hexNeeds0x); |
|
97 hexNeeds0x.setText(Messages.getString("BupMapAddDialog.startHexWith0x")); //$NON-NLS-1$ |
|
98 hexNeeds0x.setFont(PIPageEditor.helvetica_9); |
|
99 |
|
100 keyCodeLabel = new Label(labelAndValueComposite, SWT.NONE); |
|
101 GridDataFactory.fillDefaults().grab(true, false).applyTo(keyCodeLabel); |
|
102 keyCodeLabel.setFont(PIPageEditor.helvetica_9); |
|
103 keyCodeLabel.setText(Messages.getString("BupMapAddDialog.value")); //$NON-NLS-1$ |
|
104 |
|
105 keyCodeValueText = new Text(labelAndValueComposite, SWT.BORDER); |
|
106 GridDataFactory.fillDefaults().grab(true, true).hint(200, 15).applyTo(keyCodeValueText); |
|
107 keyCodeValueText.setFont(PIPageEditor.helvetica_9); |
|
108 keyCodeValueText.setText(""); //$NON-NLS-1$ |
|
109 // add the listener(s) |
|
110 keyCodeValueText.addModifyListener(new ModifyListener(){ |
|
111 public void modifyText(ModifyEvent arg0) { |
|
112 validateKeyCode(); |
|
113 } |
|
114 }); |
|
115 |
|
116 enumLabel = new Label(labelAndValueComposite, SWT.NONE); |
|
117 GridDataFactory.fillDefaults().grab(true, false).applyTo(enumLabel); |
|
118 enumLabel.setFont(PIPageEditor.helvetica_9); |
|
119 enumLabel.setText(Messages.getString("BupMapAddDialog.tKeyCode")); //$NON-NLS-1$ |
|
120 |
|
121 enumValueText = new Text(labelAndValueComposite, SWT.BORDER); |
|
122 enumValueText.setFont(PIPageEditor.helvetica_9); |
|
123 GridDataFactory.fillDefaults().grab(true, true).hint(200, 15).applyTo(enumValueText); |
|
124 // add the listener(s) |
|
125 enumValueText.addSelectionListener(new SelectionAdapter() { |
|
126 public void widgetSelected(SelectionEvent e) { |
|
127 eventEnumString = enumValueText.getText(); |
|
128 } |
|
129 }); |
|
130 enumValueText.addFocusListener(new FocusAdapter() { |
|
131 public void focusLost(FocusEvent e) { |
|
132 eventEnumString = enumValueText.getText(); |
|
133 } |
|
134 }); |
|
135 enumValueText.setText(eventEnumString); |
|
136 |
|
137 labelLabel = new Label(labelAndValueComposite, SWT.NONE); |
|
138 GridDataFactory.fillDefaults().grab(true, false).applyTo(labelLabel); |
|
139 labelLabel.setFont(PIPageEditor.helvetica_9); |
|
140 labelLabel.setText(Messages.getString("BupMapAddDialog.label")); //$NON-NLS-1$ |
|
141 |
|
142 labelText = new Text(labelAndValueComposite, SWT.BORDER); |
|
143 GridDataFactory.fillDefaults().grab(true, true).hint(200, 15).applyTo(labelText); |
|
144 labelText.setFont(PIPageEditor.helvetica_9); |
|
145 // add the listener(s) |
|
146 labelText.addSelectionListener(new SelectionAdapter() { |
|
147 public void widgetSelected(SelectionEvent e) { |
|
148 eventLabel = labelText.getText(); |
|
149 } |
|
150 }); |
|
151 labelText.addFocusListener(new FocusAdapter() { |
|
152 public void focusLost(FocusEvent e) { |
|
153 eventLabel = labelText.getText(); |
|
154 } |
|
155 }); |
|
156 labelText.setText(eventLabel); |
|
157 |
|
158 return composite; |
|
159 } |
|
160 |
|
161 /** |
|
162 * |
|
163 */ |
|
164 protected void validateKeyCode() { |
|
165 int keyCode = -1; |
|
166 String message = ""; //$NON-NLS-1$ |
|
167 String keyCodeString = keyCodeValueText.getText(); |
|
168 messageLabel.setForeground(getShell().getDisplay().getSystemColor(SWT.COLOR_BLACK)); |
|
169 |
|
170 if (keyCodeString.length() == 0) |
|
171 message = " "; //$NON-NLS-1$ |
|
172 else { |
|
173 try { |
|
174 keyCode = Integer.parseInt(keyCodeString); |
|
175 } catch (NumberFormatException e) { |
|
176 try { |
|
177 if (keyCodeString.startsWith("0x")) { //$NON-NLS-1$ |
|
178 keyCode = Integer.parseInt(keyCodeString.substring(2), 16); |
|
179 } else { |
|
180 Integer.parseInt(keyCodeString, 16); |
|
181 message = Messages.getString("BupMapAddDialog.hexNeeds0x") + keyCodeValueText.getText(); //$NON-NLS-1$ |
|
182 messageLabel.setForeground(getShell().getDisplay().getSystemColor(SWT.COLOR_RED)); |
|
183 } |
|
184 } catch (NumberFormatException e2) { |
|
185 message = keyCodeValueText.getText() + Messages.getString("BupMapAddDialog.invalidNumber"); //$NON-NLS-1$ |
|
186 messageLabel.setForeground(getShell().getDisplay().getSystemColor(SWT.COLOR_RED)); |
|
187 } |
|
188 } |
|
189 if (keyCode >= 0) { |
|
190 eventKeyCode = keyCode; |
|
191 if (map.getKeyCodeSet().contains(keyCode)) { |
|
192 message = keyCode + Messages.getString("BupMapAddDialog.existsInMap1") //$NON-NLS-1$ |
|
193 + Integer.toHexString(keyCode) + Messages.getString("BupMapAddDialog.existsInMap2"); //$NON-NLS-1$ |
|
194 messageLabel.setForeground(getShell().getDisplay().getSystemColor(SWT.COLOR_RED)); |
|
195 } |
|
196 } |
|
197 } |
|
198 messageLabel.setText(message); |
|
199 messageLabel.setVisible(!message.equals("")); //$NON-NLS-1$ |
|
200 getButton(IDialogConstants.OK_ID).setEnabled(message.equals("")); //$NON-NLS-1$ |
|
201 } |
|
202 |
|
203 public int getKeyCode() { |
|
204 return eventKeyCode; |
|
205 } |
|
206 |
|
207 public String getEnumString() { |
|
208 if (eventEnumString == null) { |
|
209 return ""; //$NON-NLS-1$ |
|
210 } |
|
211 return eventEnumString; |
|
212 } |
|
213 |
|
214 public String getLabel() { |
|
215 if (eventLabel == null) { |
|
216 return ""; //$NON-NLS-1$ |
|
217 } |
|
218 return eventLabel; |
|
219 } |
|
220 |
|
221 protected void okPressed() { |
|
222 super.okPressed(); |
|
223 } |
|
224 |
|
225 protected void cancelPressed() { |
|
226 eventKeyCode = oldEventKeyCode; |
|
227 eventEnumString = oldEventEnumString; |
|
228 eventLabel = oldEventLabel; |
|
229 super.cancelPressed(); |
|
230 } |
|
231 |
|
232 protected void createButtonsForButtonBar(Composite parent) { |
|
233 super.createButtonsForButtonBar(parent); |
|
234 // now buttons are there, we can disable key |
|
235 getButton(IDialogConstants.OK_ID).setEnabled(false); |
|
236 } |
|
237 } |