KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > apt > ui > internal > preferences > ProcessorOptionInputDialog


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 BEA Systems, Inc.
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  * wharley@bea.com - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.apt.ui.internal.preferences;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.jdt.apt.core.util.AptConfig;
18 import org.eclipse.jdt.apt.ui.internal.preferences.AptConfigurationBlock.ProcessorOption;
19 import org.eclipse.jdt.apt.ui.internal.util.IAptHelpContextIds;
20 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
21 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
22 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
23 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
24 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField;
25 import org.eclipse.jface.dialogs.StatusDialog;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.ui.PlatformUI;
32
33 /**
34  * Dialog to edit or add an APT processor option
35  */

36 public class ProcessorOptionInputDialog extends StatusDialog {
37     
38     private class FieldAdapter implements IDialogFieldListener {
39         public void dialogFieldChanged(DialogField field) {
40             doValidation();
41         }
42     }
43     
44     private StringDialogField fKeyField;
45     private StringDialogField fValueField;
46     
47     private List JavaDoc<String JavaDoc> fExistingNames;
48         
49     public ProcessorOptionInputDialog(Shell parent, ProcessorOption option, List JavaDoc<ProcessorOption> existingEntries) {
50         super(parent);
51         
52         fExistingNames= new ArrayList JavaDoc<String JavaDoc>(existingEntries.size());
53         for (ProcessorOption o : existingEntries) {
54             if (!o.equals(option)) {
55                 fExistingNames.add(o.key);
56             }
57         }
58         
59         if (option == null) {
60             setTitle(Messages.ProcessorOptionInputDialog_newProcessorOption);
61         } else {
62             setTitle(Messages.ProcessorOptionInputDialog_editProcessorOption);
63         }
64
65         FieldAdapter adapter= new FieldAdapter();
66
67         fKeyField= new StringDialogField();
68         fKeyField.setLabelText(Messages.ProcessorOptionInputDialog_key);
69         fKeyField.setDialogFieldListener(adapter);
70         
71         fValueField= new StringDialogField();
72         fValueField.setLabelText(Messages.ProcessorOptionInputDialog_value);
73         fValueField.setDialogFieldListener(adapter);
74         
75         fKeyField.setText((option != null) ? option.key : ""); //$NON-NLS-1$
76
fValueField.setText((option != null) ? option.value : ""); //$NON-NLS-1$
77
}
78     
79     public ProcessorOption getResult() {
80         ProcessorOption option = new ProcessorOption();
81         option.key= fKeyField.getText().trim();
82         option.value= fValueField.getText().trim();
83         
84         return option;
85     }
86     
87     protected Control createDialogArea(Composite parent) {
88         Composite composite= (Composite) super.createDialogArea(parent);
89         
90         Composite inner= new Composite(composite, SWT.NONE);
91         GridLayout layout= new GridLayout();
92         layout.marginHeight= 0;
93         layout.marginWidth= 0;
94         layout.numColumns= 2;
95         inner.setLayout(layout);
96         
97         fKeyField.doFillIntoGrid(inner, 2);
98         fValueField.doFillIntoGrid(inner, 2);
99         
100         LayoutUtil.setHorizontalGrabbing(fKeyField.getTextControl(null));
101         LayoutUtil.setWidthHint(fKeyField.getTextControl(null), convertWidthInCharsToPixels(50));
102         LayoutUtil.setHorizontalGrabbing(fValueField.getTextControl(null));
103         LayoutUtil.setWidthHint(fValueField.getTextControl(null), convertWidthInCharsToPixels(50));
104         
105         fKeyField.postSetFocusOnDialogField(parent.getDisplay());
106         
107         applyDialogFont(composite);
108         return composite;
109     }
110         
111     private void doValidation() {
112         StatusInfo status= new StatusInfo();
113         String JavaDoc newKey= fKeyField.getText();
114         String JavaDoc newVal= fValueField.getText();
115         // TODO: thorough validation of both key and value
116
if (newKey.length() == 0) {
117             status.setError(Messages.ProcessorOptionInputDialog_emptyKey);
118         } else if (fExistingNames.contains(newKey)) {
119             status.setError(Messages.ProcessorOptionInputDialog_keyAlreadyInUse);
120         } else if (newVal.indexOf('=') >= 0) {
121             status.setError(Messages.ProcessorOptionInputDialog_equalsSignNotValid);
122         } else if (AptConfig.isAutomaticProcessorOption(newKey)) {
123             status.setWarning(Messages.AptConfigurationBlock_warningIgnoredOptions);
124         }
125         updateStatus(status);
126     }
127
128     /*
129      * @see org.eclipse.jface.window.Window#configureShell(Shell)
130      */

131     protected void configureShell(Shell newShell) {
132         super.configureShell(newShell);
133         PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IAptHelpContextIds.PROCESSOR_OPTION_INPUT_DIALOG);
134     }
135 }
136
Popular Tags