001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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: *******************************************************************************/package org.eclipse.jdt.internal.ui.preferences;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: import org.eclipse.swt.SWT;
016: import org.eclipse.swt.layout.GridLayout;
017: import org.eclipse.swt.widgets.Composite;
018: import org.eclipse.swt.widgets.Control;
019: import org.eclipse.swt.widgets.Shell;
020:
021: import org.eclipse.jface.dialogs.StatusDialog;
022:
023: import org.eclipse.ui.PlatformUI;
024:
025: import org.eclipse.jdt.core.JavaCore;
026:
027: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
028: import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
029: import org.eclipse.jdt.internal.ui.preferences.TodoTaskConfigurationBlock.TodoTask;
030: import org.eclipse.jdt.internal.ui.wizards.dialogfields.ComboDialogField;
031: import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
032: import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
033: import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil;
034: import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField;
035:
036: /**
037: * Dialog to enter a na new task tag
038: */
039: public class TodoTaskInputDialog extends StatusDialog {
040:
041: private class CompilerTodoTaskInputAdapter implements
042: IDialogFieldListener {
043: public void dialogFieldChanged(DialogField field) {
044: doValidation();
045: }
046: }
047:
048: private StringDialogField fNameDialogField;
049: private ComboDialogField fPriorityDialogField;
050:
051: private List fExistingNames;
052:
053: public TodoTaskInputDialog(Shell parent, TodoTask task,
054: List existingEntries) {
055: super (parent);
056:
057: fExistingNames = new ArrayList(existingEntries.size());
058: for (int i = 0; i < existingEntries.size(); i++) {
059: TodoTask curr = (TodoTask) existingEntries.get(i);
060: if (!curr.equals(task)) {
061: fExistingNames.add(curr.name);
062: }
063: }
064:
065: if (task == null) {
066: setTitle(PreferencesMessages.TodoTaskInputDialog_new_title);
067: } else {
068: setTitle(PreferencesMessages.TodoTaskInputDialog_edit_title);
069: }
070:
071: CompilerTodoTaskInputAdapter adapter = new CompilerTodoTaskInputAdapter();
072:
073: fNameDialogField = new StringDialogField();
074: fNameDialogField
075: .setLabelText(PreferencesMessages.TodoTaskInputDialog_name_label);
076: fNameDialogField.setDialogFieldListener(adapter);
077:
078: fNameDialogField.setText((task != null) ? task.name : ""); //$NON-NLS-1$
079:
080: String[] items = new String[] {
081: PreferencesMessages.TodoTaskInputDialog_priority_high,
082: PreferencesMessages.TodoTaskInputDialog_priority_normal,
083: PreferencesMessages.TodoTaskInputDialog_priority_low };
084:
085: fPriorityDialogField = new ComboDialogField(SWT.READ_ONLY);
086: fPriorityDialogField
087: .setLabelText(PreferencesMessages.TodoTaskInputDialog_priority_label);
088: fPriorityDialogField.setItems(items);
089: if (task != null) {
090: if (JavaCore.COMPILER_TASK_PRIORITY_HIGH
091: .equals(task.priority)) {
092: fPriorityDialogField.selectItem(0);
093: } else if (JavaCore.COMPILER_TASK_PRIORITY_NORMAL
094: .equals(task.priority)) {
095: fPriorityDialogField.selectItem(1);
096: } else {
097: fPriorityDialogField.selectItem(2);
098: }
099: } else {
100: fPriorityDialogField.selectItem(1);
101: }
102: }
103:
104: public TodoTask getResult() {
105: TodoTask task = new TodoTask();
106: task.name = fNameDialogField.getText().trim();
107: switch (fPriorityDialogField.getSelectionIndex()) {
108: case 0:
109: task.priority = JavaCore.COMPILER_TASK_PRIORITY_HIGH;
110: break;
111: case 1:
112: task.priority = JavaCore.COMPILER_TASK_PRIORITY_NORMAL;
113: break;
114: default:
115: task.priority = JavaCore.COMPILER_TASK_PRIORITY_LOW;
116: break;
117: }
118: return task;
119: }
120:
121: protected Control createDialogArea(Composite parent) {
122: Composite composite = (Composite) super
123: .createDialogArea(parent);
124:
125: Composite inner = new Composite(composite, SWT.NONE);
126: GridLayout layout = new GridLayout();
127: layout.marginHeight = 0;
128: layout.marginWidth = 0;
129: layout.numColumns = 2;
130: inner.setLayout(layout);
131:
132: fNameDialogField.doFillIntoGrid(inner, 2);
133: fPriorityDialogField.doFillIntoGrid(inner, 2);
134:
135: LayoutUtil.setHorizontalGrabbing(fNameDialogField
136: .getTextControl(null));
137: LayoutUtil.setWidthHint(fNameDialogField.getTextControl(null),
138: convertWidthInCharsToPixels(45));
139:
140: fNameDialogField.postSetFocusOnDialogField(parent.getDisplay());
141:
142: applyDialogFont(composite);
143:
144: PlatformUI.getWorkbench().getHelpSystem().setHelp(composite,
145: IJavaHelpContextIds.TASK_TAG_INPUT_DIALOG);
146:
147: return composite;
148: }
149:
150: private void doValidation() {
151: StatusInfo status = new StatusInfo();
152: String newText = fNameDialogField.getText();
153: if (newText.length() == 0) {
154: status
155: .setError(PreferencesMessages.TodoTaskInputDialog_error_enterName);
156: } else {
157: if (newText.indexOf(',') != -1) {
158: status
159: .setError(PreferencesMessages.TodoTaskInputDialog_error_comma);
160: } else if (fExistingNames.contains(newText)) {
161: status
162: .setError(PreferencesMessages.TodoTaskInputDialog_error_entryExists);
163: } else if (Character.isWhitespace(newText.charAt(0))
164: || Character.isWhitespace(newText.charAt(newText
165: .length() - 1))) {
166: status
167: .setError(PreferencesMessages.TodoTaskInputDialog_error_noSpace);
168: }
169: }
170: updateStatus(status);
171: }
172:
173: /*
174: * @see org.eclipse.jface.window.Window#configureShell(Shell)
175: */
176: protected void configureShell(Shell newShell) {
177: super.configureShell(newShell);
178: PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell,
179: IJavaHelpContextIds.TODO_TASK_INPUT_DIALOG);
180: }
181: }
|