KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > variables > details > DetailPaneMaxLengthDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation 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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.actions.variables.details;
12
13 import org.eclipse.debug.internal.ui.DebugUIPlugin;
14 import org.eclipse.debug.internal.ui.IDebugHelpContextIds;
15 import org.eclipse.debug.internal.ui.views.variables.VariablesViewMessages;
16 import org.eclipse.debug.ui.IDebugUIConstants;
17 import org.eclipse.jface.dialogs.IDialogConstants;
18 import org.eclipse.jface.dialogs.IDialogSettings;
19 import org.eclipse.jface.dialogs.IInputValidator;
20 import org.eclipse.jface.dialogs.TrayDialog;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.ModifyEvent;
23 import org.eclipse.swt.events.ModifyListener;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.swt.widgets.Text;
30 import org.eclipse.ui.PlatformUI;
31
32 /**
33  * Provides a dialog for changing the maximum length allowed in the detail pane
34  *
35  * @since 3.0
36  */

37 public class DetailPaneMaxLengthDialog extends TrayDialog {
38
39     private static final String JavaDoc SETTINGS_ID = DebugUIPlugin.getUniqueIdentifier() + ".MAX_DETAILS_LENGTH_DIALOG"; //$NON-NLS-1$
40

41     private Text fTextWidget;
42     private Text fErrorTextWidget;
43     private String JavaDoc fErrorMessage;
44     private String JavaDoc fValue;
45     private IInputValidator fValidator;
46     
47     /**
48      * Constructs a new dialog on the given shell.
49      *
50      * @param parent shell
51      */

52     public DetailPaneMaxLengthDialog(Shell parent) {
53         super(parent);
54         setShellStyle(getShellStyle() | SWT.RESIZE);
55         fValue = Integer.toString(DebugUIPlugin.getDefault().getPreferenceStore().getInt(IDebugUIConstants.PREF_MAX_DETAIL_LENGTH));
56         fValidator = new IInputValidator() {
57                     public String JavaDoc isValid(String JavaDoc newText) {
58                         try {
59                             int num = Integer.parseInt(newText);
60                             if (num < 0) {
61                                 return VariablesViewMessages.DetailPaneMaxLengthDialog_2;
62                             }
63                         } catch (NumberFormatException JavaDoc e) {
64                             return VariablesViewMessages.DetailPaneMaxLengthDialog_3;
65                         }
66                         return null;
67                     }
68                 
69                 };
70     }
71
72     /* (non-Javadoc)
73      * @see org.eclipse.ui.dialogs.SelectionDialog#getDialogBoundsSettings()
74      */

75     protected IDialogSettings getDialogBoundsSettings() {
76         IDialogSettings settings = DebugUIPlugin.getDefault().getDialogSettings();
77         IDialogSettings section = settings.getSection(SETTINGS_ID);
78         if (section == null) {
79             section = settings.addNewSection(SETTINGS_ID);
80         }
81         return section;
82     }
83     
84     /* (non-Javadoc)
85      * @see org.eclipse.jface.dialogs.Dialog#createContents(org.eclipse.swt.widgets.Composite)
86      */

87     protected Control createContents(Composite parent) {
88         getShell().setText(VariablesViewMessages.DetailPaneMaxLengthDialog_0);
89         Control contents = super.createContents(parent);
90         PlatformUI.getWorkbench().getHelpSystem().setHelp(getDialogArea(), IDebugHelpContextIds.DETAIL_PANE_MAX_LENGTH_ACTION);
91         return contents;
92     }
93
94     /* (non-Javadoc)
95      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
96      */

97     protected Control createDialogArea(Composite parent) {
98         Composite composite = (Composite) super.createDialogArea(parent);
99         Label label = new Label(composite, SWT.WRAP);
100         label.setText(VariablesViewMessages.DetailPaneMaxLengthDialog_1);
101         GridData data = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_CENTER);
102         data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
103         label.setLayoutData(data);
104         label.setFont(parent.getFont());
105         fTextWidget = new Text(composite, SWT.SINGLE | SWT.BORDER);
106         fTextWidget.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
107         fTextWidget.setText(fValue);
108         fTextWidget.addModifyListener(new ModifyListener() {
109             public void modifyText(ModifyEvent e) {
110                 validateInput();
111                 fValue = fTextWidget.getText();
112             }
113         });
114         fErrorTextWidget = new Text(composite, SWT.READ_ONLY);
115         fErrorTextWidget.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
116                 | GridData.HORIZONTAL_ALIGN_FILL));
117         fErrorTextWidget.setBackground(fErrorTextWidget.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
118         setErrorMessage(fErrorMessage);
119         applyDialogFont(composite);
120         return composite;
121     }
122
123     /* (non-Javadoc)
124      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
125      */

126     protected void okPressed() {
127         String JavaDoc text = getValue();
128         try {
129             DebugUIPlugin.getDefault().getPreferenceStore().setValue(IDebugUIConstants.PREF_MAX_DETAIL_LENGTH, Integer.parseInt(text));
130         }
131         catch (NumberFormatException JavaDoc e) {
132             DebugUIPlugin.log(e);
133         }
134         super.okPressed();
135     }
136     
137     /**
138      * Returns the string typed into this input dialog.
139      *
140      * @return the input string
141      * @since 3.3
142      */

143     public String JavaDoc getValue() {
144         return fValue;
145     }
146     
147     /**
148      * Validates the current input
149      * @since 3.3
150      */

151     private void validateInput() {
152         String JavaDoc errorMessage = null;
153         if (fValidator != null) {
154             errorMessage = fValidator.isValid(fTextWidget.getText());
155         }
156         setErrorMessage(errorMessage);
157     }
158     
159     /**
160      * Sets the current error message or none if null
161      * @param errorMessage
162      * @since 3.3
163      */

164     public void setErrorMessage(String JavaDoc errorMessage) {
165         fErrorMessage = errorMessage;
166         if (fErrorTextWidget != null && !fErrorTextWidget.isDisposed()) {
167             fErrorTextWidget.setText(errorMessage == null ? "" : errorMessage); //$NON-NLS-1$
168
fErrorTextWidget.getParent().update();
169             // Access the ok button by id, in case clients have overridden button creation.
170
// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=113643
171
Control button = getButton(IDialogConstants.OK_ID);
172             if (button != null) {
173                 button.setEnabled(errorMessage == null);
174             }
175         }
176     }
177 }
178
Popular Tags