KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > stringsubstitution > PromptingResolver


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 Matt Conway 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  * Matt Conway - initial implementation
10  * IBM Corporation - integration and code cleanup
11  *******************************************************************************/

12 package org.eclipse.debug.internal.ui.stringsubstitution;
13
14 import com.ibm.icu.text.MessageFormat;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.core.variables.IDynamicVariable;
20 import org.eclipse.core.variables.IDynamicVariableResolver;
21 import org.eclipse.debug.core.DebugException;
22 import org.eclipse.debug.internal.ui.DebugUIPlugin;
23 import org.eclipse.swt.widgets.Shell;
24
25 /**
26  * Base implementation for variable resolvers that prompt the user
27  * for their value.
28  */

29 abstract class PromptingResolver implements IDynamicVariableResolver {
30
31     /**
32      * A hint that helps the user choose their input. If a prompt
33      * hint is provider the user will be prompted:
34      * Please input a value for <code>promptHint</code>
35      */

36     protected String JavaDoc promptHint = null;
37     /**
38      * The prompt displayed to the user.
39      */

40     protected String JavaDoc dialogMessage = null;
41     /**
42      * The default value selected when the prompt is displayed
43      */

44     protected String JavaDoc defaultValue = null;
45     /**
46      * The last value chosen by the user for this variable
47      */

48     protected String JavaDoc lastValue = null;
49     /**
50      * The result returned from the prompt dialog
51      */

52     protected String JavaDoc dialogResultString = null;
53     
54     /**
55      * Presents the user with the appropriate prompt for the variable to be expanded
56      * and sets the <code>dialogResultString</code> based on the user's selection.
57      */

58     public abstract void prompt();
59
60     /**
61      * Initializes values displayed when the user is prompted. If
62      * a prompt hint and default value are supplied in the given
63      * variable value, these are extracted for presentation
64      *
65      * @param varValue the value of the variable from which the prompt
66      * hint and default value will be extracted
67      */

68     protected void setupDialog(String JavaDoc varValue) {
69         promptHint = null;
70         defaultValue = null;
71         dialogResultString = null;
72         if (varValue != null) {
73             int idx = varValue.indexOf(':');
74             if (idx != -1) {
75                 promptHint = varValue.substring(0, idx);
76                 defaultValue = varValue.substring(idx + 1);
77             } else {
78                 promptHint = varValue;
79             }
80         }
81
82         if (promptHint != null) {
83             dialogMessage = MessageFormat.format(StringSubstitutionMessages.PromptExpanderBase_0, new String JavaDoc[] {promptHint});
84         } else {
85             dialogMessage = StringSubstitutionMessages.PromptExpanderBase_1;
86         }
87     }
88     
89     /* (non-Javadoc)
90      * @see org.eclipse.debug.internal.core.stringsubstitution.IContextVariableResolver#resolveValue(org.eclipse.debug.internal.core.stringsubstitution.IContextVariable, java.lang.String)
91      */

92     public String JavaDoc resolveValue(IDynamicVariable variable, String JavaDoc argument) throws CoreException {
93         String JavaDoc value = null;
94         setupDialog(argument);
95
96         DebugUIPlugin.getStandardDisplay().syncExec(new Runnable JavaDoc() {
97             public void run() {
98                 prompt();
99             }
100         });
101         if (dialogResultString != null) {
102             value = dialogResultString;
103             lastValue = dialogResultString;
104         } else {
105             // dialogResultString == null means prompt was cancelled
106
throw new DebugException(new Status(IStatus.CANCEL, DebugUIPlugin.getUniqueIdentifier(), IStatus.CANCEL, MessageFormat.format(StringSubstitutionMessages.PromptingResolver_0, new String JavaDoc[] { variable.getName() }), null));
107         }
108         return value;
109     }
110     
111     protected Shell getShell() {
112         return DebugUIPlugin.getShell();
113     }
114
115 }
116
Popular Tags