001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Sebastian Davids <sdavids@gmx.de> - bug 132479 - [FieldAssist] Field assist example improvements
011: *******************************************************************************/package org.eclipse.ui.examples.fieldassist;
012:
013: import java.text.MessageFormat;
014:
015: import org.eclipse.core.runtime.IStatus;
016: import org.eclipse.core.runtime.Status;
017: import org.eclipse.jface.bindings.keys.KeyStroke;
018: import org.eclipse.jface.bindings.keys.ParseException;
019: import org.eclipse.jface.dialogs.Dialog;
020: import org.eclipse.jface.dialogs.IDialogConstants;
021: import org.eclipse.jface.dialogs.MessageDialog;
022: import org.eclipse.jface.dialogs.StatusDialog;
023: import org.eclipse.jface.fieldassist.AutoCompleteField;
024: import org.eclipse.jface.fieldassist.ComboContentAdapter;
025: import org.eclipse.jface.fieldassist.ContentProposalAdapter;
026: import org.eclipse.jface.fieldassist.ControlDecoration;
027: import org.eclipse.jface.fieldassist.FieldDecoration;
028: import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
029: import org.eclipse.jface.fieldassist.IContentProposal;
030: import org.eclipse.jface.fieldassist.IContentProposalProvider;
031: import org.eclipse.jface.fieldassist.IControlContentAdapter;
032: import org.eclipse.jface.fieldassist.TextContentAdapter;
033: import org.eclipse.jface.preference.IPreferenceStore;
034: import org.eclipse.osgi.util.NLS;
035: import org.eclipse.swt.SWT;
036: import org.eclipse.swt.events.MenuDetectEvent;
037: import org.eclipse.swt.events.MenuDetectListener;
038: import org.eclipse.swt.events.ModifyEvent;
039: import org.eclipse.swt.events.ModifyListener;
040: import org.eclipse.swt.events.SelectionEvent;
041: import org.eclipse.swt.events.SelectionListener;
042: import org.eclipse.swt.graphics.Rectangle;
043: import org.eclipse.swt.layout.GridData;
044: import org.eclipse.swt.layout.GridLayout;
045: import org.eclipse.swt.widgets.Combo;
046: import org.eclipse.swt.widgets.Composite;
047: import org.eclipse.swt.widgets.Control;
048: import org.eclipse.swt.widgets.Group;
049: import org.eclipse.swt.widgets.Label;
050: import org.eclipse.swt.widgets.Menu;
051: import org.eclipse.swt.widgets.MenuItem;
052: import org.eclipse.swt.widgets.Shell;
053: import org.eclipse.swt.widgets.Spinner;
054: import org.eclipse.swt.widgets.Text;
055: import org.eclipse.ui.examples.fieldassist.preferences.PreferenceConstants;
056:
057: /**
058: * Example dialog that shows different field assist capabilities.
059: */
060: public class FieldAssistTestDialog extends StatusDialog {
061:
062: class SpinnerContentAdapter implements IControlContentAdapter {
063: // We are only implementing this for our internal use, not for
064: // content assist, so many of the methods are ignored.
065: public String getControlContents(Control control) {
066: return new Integer(((Spinner) control).getSelection())
067: .toString();
068: }
069:
070: public void setControlContents(Control control, String text,
071: int cursorPosition) {
072: // ignore
073: }
074:
075: public void insertControlContents(Control control, String text,
076: int cursorPosition) {
077: // ignore
078: }
079:
080: public int getCursorPosition(Control control) {
081: // ignore
082: return 0;
083: }
084:
085: public Rectangle getInsertionBounds(Control control) {
086: return control.getBounds();
087: }
088:
089: public void setCursorPosition(Control control, int index) {
090: // ignore
091: }
092: }
093:
094: abstract class SmartField {
095: ControlDecoration controlDecoration;
096:
097: Control control;
098:
099: IControlContentAdapter contentAdapter;
100:
101: FieldDecoration errorDecoration, warningDecoration;
102:
103: SmartField(ControlDecoration dec, Control control,
104: IControlContentAdapter adapter) {
105: this .controlDecoration = dec;
106: this .contentAdapter = adapter;
107: this .control = control;
108: }
109:
110: boolean isRequiredField() {
111: return true;
112: }
113:
114: boolean hasQuickFix() {
115: return false;
116: }
117:
118: void quickFix() {
119: // do nothing
120: }
121:
122: boolean hasContentAssist() {
123: return false;
124: }
125:
126: void dispose() {
127: // do nothing
128: }
129:
130: FieldDecoration getErrorDecoration() {
131: if (errorDecoration == null) {
132: FieldDecoration standardError;
133: if (hasQuickFix()) {
134: standardError = FieldDecorationRegistry
135: .getDefault()
136: .getFieldDecoration(
137: FieldDecorationRegistry.DEC_ERROR_QUICKFIX);
138: } else {
139: standardError = FieldDecorationRegistry
140: .getDefault().getFieldDecoration(
141: FieldDecorationRegistry.DEC_ERROR);
142: }
143: if (getErrorMessage() == null) {
144: errorDecoration = standardError;
145: } else {
146: errorDecoration = new FieldDecoration(standardError
147: .getImage(), getErrorMessage());
148: }
149: }
150: return errorDecoration;
151:
152: }
153:
154: FieldDecoration getWarningDecoration() {
155: if (warningDecoration == null) {
156: FieldDecoration standardWarning = FieldDecorationRegistry
157: .getDefault().getFieldDecoration(
158: FieldDecorationRegistry.DEC_WARNING);
159: if (getWarningMessage() == null) {
160: warningDecoration = standardWarning;
161: } else {
162: warningDecoration = new FieldDecoration(
163: standardWarning.getImage(),
164: getWarningMessage());
165: }
166: }
167: return warningDecoration;
168: }
169:
170: String getContents() {
171: return contentAdapter.getControlContents(control);
172: }
173:
174: void setContents(String contents) {
175: contentAdapter.setControlContents(control, contents,
176: contents.length());
177: }
178:
179: abstract boolean isValid();
180:
181: abstract boolean isWarning();
182:
183: String getErrorMessage() {
184: return null;
185: }
186:
187: String getWarningMessage() {
188: return null;
189: }
190:
191: }
192:
193: class UserField extends SmartField {
194: Menu quickFixMenu;
195:
196: UserField(ControlDecoration dec, Control control,
197: IControlContentAdapter adapter) {
198: super (dec, control, adapter);
199: }
200:
201: boolean isValid() {
202: String contents = getContents();
203: for (int i = 0; i < contents.length(); i++) {
204: if (!Character.isLetter(contents.charAt(i))) {
205: return false;
206: }
207: }
208: return true;
209: }
210:
211: String getErrorMessage() {
212: return TaskAssistExampleMessages.ExampleDialog_UserError;
213: }
214:
215: boolean isWarning() {
216: return getContents()
217: .equals(
218: TaskAssistExampleMessages.ExampleDialog_WarningName);
219: }
220:
221: String getWarningMessage() {
222: return TaskAssistExampleMessages.ExampleDialog_UserWarning;
223: }
224:
225: boolean hasContentAssist() {
226: return true;
227: }
228:
229: boolean hasQuickFix() {
230: return true;
231: }
232:
233: void quickFix() {
234: String contents = getContents();
235: StringBuffer lettersOnly = new StringBuffer();
236: int length = contents.length();
237: for (int i = 0; i < length;) {
238: char ch = contents.charAt(i++);
239: if (Character.isLetter(ch)) {
240: lettersOnly.append(ch);
241: }
242: }
243: setContents(lettersOnly.toString());
244: }
245:
246: void dispose() {
247: if (quickFixMenu != null) {
248: quickFixMenu.dispose();
249: quickFixMenu = null;
250: }
251: }
252: }
253:
254: class AgeField extends SmartField {
255:
256: AgeField(ControlDecoration dec, Control control,
257: IControlContentAdapter adapter) {
258: super (dec, control, adapter);
259: }
260:
261: boolean isValid() {
262: // We seed the spinner with valid values always.
263: return true;
264: }
265:
266: boolean isWarning() {
267: Spinner spinner = (Spinner) control;
268: return spinner.getSelection() > 65;
269: }
270:
271: String getWarningMessage() {
272: return TaskAssistExampleMessages.ExampleDialog_AgeWarning;
273: }
274: }
275:
276: String[] validUsers = { "tom", "dick", "harry", "ferdinand", "tim",
277: "teresa", "tori", "daniela", "aaron", "kevin", "tod",
278: "mike", "kim", "eric", "paul" };
279:
280: String triggerKey;
281:
282: String username;
283:
284: boolean showErrorDecoration, showErrorMessage,
285: showWarningDecoration, showRequiredFieldDecoration,
286: showRequiredFieldLabelIndicator, showSecondaryPopup,
287: showContentAssist;
288:
289: int marginWidth;
290:
291: UserField textField, comboField;
292:
293: /**
294: * Open the example dialog.
295: *
296: * @param parent
297: * the parent shell
298: * @param username
299: * the default username
300: */
301: public FieldAssistTestDialog(Shell parent, String username) {
302: super (parent);
303: setTitle(TaskAssistExampleMessages.ExampleDialog_Title);
304: this .username = username;
305: getPreferenceValues();
306: }
307:
308: protected Control createDialogArea(Composite parent) {
309:
310: Composite outer = (Composite) super .createDialogArea(parent);
311:
312: initializeDialogUnits(outer);
313: createSecurityGroup(outer);
314:
315: // Create a simple field to show how field assist can be used for
316: // autocomplete.
317: Group autoComplete = new Group(outer, SWT.NONE);
318: autoComplete.setLayoutData(new GridData(GridData.FILL_BOTH));
319: GridLayout layout = new GridLayout();
320: layout.numColumns = 2;
321: layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
322: layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
323: layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
324: layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
325: autoComplete.setLayout(layout);
326: autoComplete
327: .setText(TaskAssistExampleMessages.ExampleDialog_AutoCompleteGroup);
328:
329: Label label = new Label(autoComplete, SWT.LEFT);
330: label.setText(TaskAssistExampleMessages.ExampleDialog_UserName);
331:
332: // Create an auto-complete field representing a user name
333: Text text = new Text(autoComplete, SWT.BORDER);
334: text.setLayoutData(getFieldGridData());
335: new AutoCompleteField(text, new TextContentAdapter(),
336: validUsers);
337:
338: Dialog.applyDialogFont(outer);
339:
340: return outer;
341: }
342:
343: private void getPreferenceValues() {
344: IPreferenceStore store = FieldAssistPlugin.getDefault()
345: .getPreferenceStore();
346: showErrorMessage = store
347: .getBoolean(PreferenceConstants.PREF_SHOWERRORMESSAGE);
348: showErrorDecoration = store
349: .getBoolean(PreferenceConstants.PREF_SHOWERRORDECORATION);
350: showWarningDecoration = store
351: .getBoolean(PreferenceConstants.PREF_SHOWWARNINGDECORATION);
352: showRequiredFieldDecoration = store
353: .getBoolean(PreferenceConstants.PREF_SHOWREQUIREDFIELDDECORATION);
354: showRequiredFieldLabelIndicator = store
355: .getBoolean(PreferenceConstants.PREF_SHOWREQUIREDFIELDLABELINDICATOR);
356: showSecondaryPopup = store
357: .getBoolean(PreferenceConstants.PREF_SHOWSECONDARYPOPUP);
358: showContentAssist = store
359: .getBoolean(PreferenceConstants.PREF_SHOWCONTENTPROPOSALCUE);
360: triggerKey = store
361: .getString(PreferenceConstants.PREF_CONTENTASSISTKEY);
362: marginWidth = store
363: .getInt(PreferenceConstants.PREF_DECORATOR_MARGINWIDTH);
364: }
365:
366: FieldDecoration getCueDecoration() {
367: // We use our own decoration which is based on the JFace version.
368: FieldDecorationRegistry registry = FieldDecorationRegistry
369: .getDefault();
370: FieldDecoration dec = registry
371: .getFieldDecoration(FieldAssistPlugin.DEC_CONTENTASSIST);
372: if (dec == null) {
373: // Get the standard one. We use its image and our own customized
374: // text.
375: FieldDecoration standardDecoration = registry
376: .getFieldDecoration(FieldDecorationRegistry.DEC_CONTENT_PROPOSAL);
377: registry
378: .registerFieldDecoration(
379: FieldAssistPlugin.DEC_CONTENTASSIST,
380: NLS
381: .bind(
382: TaskAssistExampleMessages.Decorator_ContentAssist,
383: triggerKey),
384: standardDecoration.getImage());
385: dec = registry
386: .getFieldDecoration(FieldAssistPlugin.DEC_CONTENTASSIST);
387: }
388: return dec;
389: }
390:
391: FieldDecoration getWarningDecoration() {
392: return FieldDecorationRegistry.getDefault().getFieldDecoration(
393: FieldDecorationRegistry.DEC_WARNING);
394: }
395:
396: void handleModify(SmartField smartField) {
397: // Error indicator supercedes all others
398: if (!smartField.isValid()) {
399: showError(smartField);
400: } else {
401: hideError(smartField);
402: if (smartField.isWarning()) {
403: showWarning(smartField);
404: } else {
405: hideWarning(smartField);
406: if (showContentAssist && smartField.hasContentAssist()) {
407: showContentAssistDecoration(smartField, true);
408: } else {
409: showContentAssistDecoration(smartField, false);
410: showRequiredFieldDecoration(smartField,
411: showRequiredFieldDecoration);
412: }
413: }
414: }
415: }
416:
417: GridData getFieldGridData() {
418: int margin = FieldDecorationRegistry.getDefault()
419: .getMaximumDecorationWidth();
420: GridData data = new GridData();
421: data.horizontalAlignment = SWT.FILL;
422: data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH + margin;
423: data.horizontalIndent = margin;
424: data.grabExcessHorizontalSpace = true;
425: return data;
426:
427: }
428:
429: void showError(SmartField smartField) {
430: FieldDecoration dec = smartField.getErrorDecoration();
431: if (showErrorMessage) {
432: updateStatus(new Status(IStatus.ERROR,
433: "org.eclipse.examples.contentassist", 0, dec //$NON-NLS-1$
434: .getDescription(), null));
435: }
436: if (showErrorDecoration) {
437: showErrorDecoration(smartField, true);
438: }
439: }
440:
441: void hideError(SmartField smartField) {
442: if (showErrorMessage) {
443: this .updateStatus(Status.OK_STATUS);
444: }
445: if (showErrorDecoration) {
446: showErrorDecoration(smartField, false);
447: }
448: }
449:
450: void showWarning(SmartField smartField) {
451: if (showWarningDecoration) {
452: showWarningDecoration(smartField, true);
453: }
454: }
455:
456: void hideWarning(SmartField smartField) {
457: if (showWarningDecoration) {
458: showWarningDecoration(smartField, false);
459: }
460: }
461:
462: void installContentProposalAdapter(Control control,
463: IControlContentAdapter contentAdapter) {
464: IPreferenceStore store = FieldAssistPlugin.getDefault()
465: .getPreferenceStore();
466: boolean propagate = store
467: .getBoolean(PreferenceConstants.PREF_CONTENTASSISTKEY_PROPAGATE);
468: KeyStroke keyStroke;
469: char[] autoActivationCharacters = null;
470: int autoActivationDelay = store
471: .getInt(PreferenceConstants.PREF_CONTENTASSISTDELAY);
472:
473: if (triggerKey
474: .equals(PreferenceConstants.PREF_CONTENTASSISTKEYAUTO)) {
475: // null means automatically assist when character typed
476: keyStroke = null;
477: } else if (triggerKey
478: .equals(PreferenceConstants.PREF_CONTENTASSISTKEYAUTOSUBSET)) {
479: keyStroke = null;
480: autoActivationCharacters = new char[] { 't', 'd' };
481: } else {
482: try {
483: keyStroke = KeyStroke.getInstance(triggerKey);
484: } catch (ParseException e) {
485: keyStroke = KeyStroke.getInstance(SWT.F10);
486: }
487: }
488:
489: ContentProposalAdapter adapter = new ContentProposalAdapter(
490: control, contentAdapter, getContentProposalProvider(),
491: keyStroke, autoActivationCharacters) {
492: public void closeProposalPopup() {
493: closeProposalPopup();
494: }
495: };
496: adapter.setAutoActivationDelay(autoActivationDelay);
497: adapter.setPropagateKeys(propagate);
498: adapter.setFilterStyle(getContentAssistFilterStyle());
499: adapter
500: .setProposalAcceptanceStyle(getContentAssistAcceptance());
501: }
502:
503: private IContentProposalProvider getContentProposalProvider() {
504: return new IContentProposalProvider() {
505: public IContentProposal[] getProposals(String contents,
506: int position) {
507: IContentProposal[] proposals = new IContentProposal[validUsers.length];
508: for (int i = 0; i < validUsers.length; i++) {
509: final String user = validUsers[i];
510: proposals[i] = new IContentProposal() {
511: public String getContent() {
512: return user;
513: }
514:
515: public String getLabel() {
516: return user;
517: }
518:
519: public String getDescription() {
520: if (showSecondaryPopup)
521: return MessageFormat
522: .format(
523: TaskAssistExampleMessages.ExampleDialog_ProposalDescription,
524: new String[] { user });
525: return null;
526: }
527:
528: public int getCursorPosition() {
529: return user.length();
530: }
531: };
532: }
533: return proposals;
534: }
535: };
536: }
537:
538: private int getContentAssistAcceptance() {
539: IPreferenceStore store = FieldAssistPlugin.getDefault()
540: .getPreferenceStore();
541: String acceptanceStyle = store
542: .getString(PreferenceConstants.PREF_CONTENTASSISTRESULT);
543: if (acceptanceStyle
544: .equals(PreferenceConstants.PREF_CONTENTASSISTRESULT_INSERT))
545: return ContentProposalAdapter.PROPOSAL_INSERT;
546: if (acceptanceStyle
547: .equals(PreferenceConstants.PREF_CONTENTASSISTRESULT_REPLACE))
548: return ContentProposalAdapter.PROPOSAL_REPLACE;
549: return ContentProposalAdapter.PROPOSAL_IGNORE;
550: }
551:
552: private int getContentAssistFilterStyle() {
553: IPreferenceStore store = FieldAssistPlugin.getDefault()
554: .getPreferenceStore();
555: String acceptanceStyle = store
556: .getString(PreferenceConstants.PREF_CONTENTASSISTFILTER);
557: if (acceptanceStyle
558: .equals(PreferenceConstants.PREF_CONTENTASSISTFILTER_CHAR))
559: return ContentProposalAdapter.FILTER_CHARACTER;
560: if (acceptanceStyle
561: .equals(PreferenceConstants.PREF_CONTENTASSISTFILTER_CUMULATIVE))
562: return ContentProposalAdapter.FILTER_CUMULATIVE;
563: return ContentProposalAdapter.FILTER_NONE;
564: }
565:
566: void addRequiredFieldIndicator(Label label) {
567: String text = label.getText();
568: // This concatenation could be done by a field assist helper.
569: text = text.concat("*");
570: label.setText(text);
571: }
572:
573: FieldDecoration getRequiredFieldDecoration() {
574: return FieldDecorationRegistry.getDefault().getFieldDecoration(
575: FieldDecorationRegistry.DEC_REQUIRED);
576: }
577:
578: int getDecorationLocationBits() {
579: IPreferenceStore store = FieldAssistPlugin.getDefault()
580: .getPreferenceStore();
581: int bits = 0;
582: String vert = store
583: .getString(PreferenceConstants.PREF_DECORATOR_VERTICALLOCATION);
584: if (vert
585: .equals(PreferenceConstants.PREF_DECORATOR_VERTICALLOCATION_BOTTOM)) {
586: bits = SWT.BOTTOM;
587: } else if (vert
588: .equals(PreferenceConstants.PREF_DECORATOR_VERTICALLOCATION_CENTER)) {
589: bits = SWT.CENTER;
590: } else {
591: bits = SWT.TOP;
592: }
593:
594: String horz = store
595: .getString(PreferenceConstants.PREF_DECORATOR_HORIZONTALLOCATION);
596: if (horz
597: .equals(PreferenceConstants.PREF_DECORATOR_HORIZONTALLOCATION_RIGHT)) {
598: bits |= SWT.RIGHT;
599: } else {
600: bits |= SWT.LEFT;
601: }
602: return bits;
603: }
604:
605: void createSecurityGroup(Composite parent) {
606:
607: Group main = new Group(parent, SWT.NONE);
608: main.setLayoutData(new GridData(GridData.FILL_BOTH));
609: main
610: .setText(TaskAssistExampleMessages.ExampleDialog_SecurityGroup);
611: GridLayout layout = new GridLayout();
612: layout.numColumns = 2;
613: layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
614: layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
615: layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
616: layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
617: main.setLayout(layout);
618:
619: Label label = new Label(main, SWT.LEFT);
620: label.setText(TaskAssistExampleMessages.ExampleDialog_UserName);
621:
622: // Create a field representing a user name
623: Text text = new Text(main, SWT.BORDER);
624: ControlDecoration dec = new ControlDecoration(text,
625: getDecorationLocationBits());
626: dec.setMarginWidth(marginWidth);
627: dec.addSelectionListener(new SelectionListener() {
628: public void widgetSelected(SelectionEvent event) {
629: MessageDialog
630: .openInformation(
631: getShell(),
632: TaskAssistExampleMessages.ExampleDialog_SelectionTitle,
633: TaskAssistExampleMessages.ExampleDialog_SelectionMessage);
634: }
635:
636: public void widgetDefaultSelected(SelectionEvent e) {
637: // Nothing on default select
638: }
639: });
640:
641: textField = new UserField(dec, text, new TextContentAdapter());
642: dec.addMenuDetectListener(new MenuDetectListener() {
643: public void menuDetected(MenuDetectEvent event) {
644: // no quick fix if we aren't in error state.
645: if (textField.isValid()) {
646: return;
647: }
648: if (textField.quickFixMenu == null) {
649: textField.quickFixMenu = createQuickFixMenu(textField);
650: }
651: textField.quickFixMenu.setLocation(event.x, event.y);
652: textField.quickFixMenu.setVisible(true);
653: }
654: });
655: if (showRequiredFieldLabelIndicator
656: && textField.isRequiredField()) {
657: addRequiredFieldIndicator(label);
658: }
659: text.addModifyListener(new ModifyListener() {
660: public void modifyText(ModifyEvent event) {
661: handleModify(textField);
662: }
663: });
664:
665: text.setText(username);
666: installContentProposalAdapter(text, new TextContentAdapter());
667: text.setLayoutData(getFieldGridData());
668:
669: label = new Label(main, SWT.LEFT);
670: label
671: .setText(TaskAssistExampleMessages.ExampleDialog_ComboUserName);
672:
673: // Create a combo field representing a user name
674: Combo combo = new Combo(main, SWT.BORDER | SWT.DROP_DOWN);
675: dec = new ControlDecoration(combo, getDecorationLocationBits());
676: dec.setMarginWidth(marginWidth);
677: comboField = new UserField(dec, combo,
678: new ComboContentAdapter());
679:
680: dec.addMenuDetectListener(new MenuDetectListener() {
681: public void menuDetected(MenuDetectEvent event) {
682: // no quick fix if we aren't in error state.
683: if (comboField.isValid()) {
684: return;
685: }
686: if (comboField.quickFixMenu == null) {
687: comboField.quickFixMenu = createQuickFixMenu(comboField);
688: }
689: comboField.quickFixMenu.setLocation(event.x, event.y);
690: comboField.quickFixMenu.setVisible(true);
691: }
692: });
693: dec.addSelectionListener(new SelectionListener() {
694: public void widgetDefaultSelected(SelectionEvent event) {
695: MessageDialog
696: .openInformation(
697: getShell(),
698: TaskAssistExampleMessages.ExampleDialog_DefaultSelectionTitle,
699: TaskAssistExampleMessages.ExampleDialog_DefaultSelectionMessage);
700: }
701:
702: public void widgetSelected(SelectionEvent e) {
703: // Do nothing on selection
704: }
705: });
706:
707: if (showRequiredFieldLabelIndicator) {
708: addRequiredFieldIndicator(label);
709: }
710: combo.addModifyListener(new ModifyListener() {
711: public void modifyText(ModifyEvent event) {
712: handleModify(comboField);
713: }
714: });
715:
716: combo.setText(username);
717: combo.setItems(validUsers);
718: combo.setLayoutData(getFieldGridData());
719: installContentProposalAdapter(combo, new ComboContentAdapter());
720:
721: // Create a spinner representing a user age
722: label = new Label(main, SWT.LEFT);
723: label.setText(TaskAssistExampleMessages.ExampleDialog_Age);
724:
725: Spinner spinner = new Spinner(main, SWT.BORDER);
726: dec = new ControlDecoration(spinner,
727: getDecorationLocationBits());
728: dec.setMarginWidth(marginWidth);
729:
730: if (showRequiredFieldLabelIndicator) {
731: addRequiredFieldIndicator(label);
732: }
733: final SmartField spinnerField = new AgeField(dec, spinner,
734: new SpinnerContentAdapter());
735: spinner.addModifyListener(new ModifyListener() {
736: public void modifyText(ModifyEvent event) {
737: handleModify(spinnerField);
738: }
739: });
740: spinner.setSelection(40);
741: spinner.setLayoutData(getFieldGridData());
742:
743: // This field has no decorator
744: label = new Label(main, SWT.LEFT);
745: label.setText(TaskAssistExampleMessages.ExampleDialog_Password);
746: text = new Text(main, SWT.BORDER);
747: text.setText("******"); //$NON-NLS-1$
748: text.setLayoutData(getFieldGridData());
749: if (showRequiredFieldLabelIndicator) {
750: addRequiredFieldIndicator(label);
751: }
752: }
753:
754: Menu createQuickFixMenu(final SmartField field) {
755: Menu newMenu = new Menu(field.control);
756: MenuItem item = new MenuItem(newMenu, SWT.PUSH);
757: item
758: .setText(TaskAssistExampleMessages.ExampleDialog_DecorationMenuItem);
759: item.addSelectionListener(new SelectionListener() {
760: public void widgetSelected(SelectionEvent event) {
761: field.quickFix();
762: }
763:
764: public void widgetDefaultSelected(SelectionEvent event) {
765:
766: }
767: });
768: return newMenu;
769: }
770:
771: void showErrorDecoration(SmartField smartField, boolean show) {
772: FieldDecoration dec = smartField.getErrorDecoration();
773: ControlDecoration cd = smartField.controlDecoration;
774: if (show) {
775: cd.setImage(dec.getImage());
776: cd.setDescriptionText(dec.getDescription());
777: cd.setShowOnlyOnFocus(false);
778: cd.show();
779: } else {
780: cd.hide();
781: }
782: }
783:
784: void showWarningDecoration(SmartField smartField, boolean show) {
785: FieldDecoration dec = smartField.getWarningDecoration();
786: ControlDecoration cd = smartField.controlDecoration;
787: if (show) {
788: cd.setImage(dec.getImage());
789: cd.setDescriptionText(dec.getDescription());
790: cd.setShowOnlyOnFocus(false);
791: cd.show();
792: } else {
793: cd.hide();
794: }
795: }
796:
797: void showRequiredFieldDecoration(SmartField smartField, boolean show) {
798: FieldDecoration dec = getRequiredFieldDecoration();
799: ControlDecoration cd = smartField.controlDecoration;
800: if (show) {
801: cd.setImage(dec.getImage());
802: cd.setDescriptionText(dec.getDescription());
803: cd.setShowOnlyOnFocus(false);
804: cd.show();
805: } else {
806: cd.hide();
807: }
808: }
809:
810: void showContentAssistDecoration(SmartField smartField, boolean show) {
811: FieldDecoration dec = getCueDecoration();
812: ControlDecoration cd = smartField.controlDecoration;
813: if (show) {
814: cd.setImage(dec.getImage());
815: cd.setDescriptionText(dec.getDescription());
816: cd.setShowOnlyOnFocus(true);
817: cd.show();
818: } else {
819: cd.hide();
820: }
821: }
822:
823: public boolean close() {
824: textField.dispose();
825: comboField.dispose();
826: return super.close();
827: }
828: }
|