KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > formatter > AlreadyExistsDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.jdt.internal.ui.preferences.formatter;
12
13 import org.eclipse.core.runtime.IStatus;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.ModifyEvent;
17 import org.eclipse.swt.events.ModifyListener;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.events.SelectionListener;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.swt.widgets.Text;
28
29 import org.eclipse.jface.dialogs.IDialogConstants;
30 import org.eclipse.jface.dialogs.StatusDialog;
31
32 import org.eclipse.jdt.internal.corext.util.Messages;
33
34 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
35 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.CustomProfile;
36
37 /**
38  * The dialog to rename an imported profile.
39  */

40 public class AlreadyExistsDialog extends StatusDialog {
41     
42     private Composite fComposite;
43     protected Text fNameText;
44     private Button fRenameRadio, fOverwriteRadio;
45     
46     private final int NUM_COLUMNS= 2;
47     
48     private final StatusInfo fOk;
49     private final StatusInfo fEmpty;
50     private final StatusInfo fDuplicate;
51     
52     private final CustomProfile fProfile;
53     private final ProfileManager fProfileManager;
54     
55     public AlreadyExistsDialog(Shell parentShell, CustomProfile profile, ProfileManager profileManager) {
56         super(parentShell);
57         fProfile= profile;
58         fProfileManager= profileManager;
59         fOk= new StatusInfo();
60         fDuplicate= new StatusInfo(IStatus.ERROR, FormatterMessages.AlreadyExistsDialog_message_profile_already_exists);
61         fEmpty= new StatusInfo(IStatus.ERROR, FormatterMessages.AlreadyExistsDialog_message_profile_name_empty);
62         
63         setHelpAvailable(false);
64     }
65     
66     
67     public void create() {
68         super.create();
69         setTitle(FormatterMessages.AlreadyExistsDialog_dialog_title);
70     }
71     
72     public Control createDialogArea(Composite parent) {
73                 
74         initializeComposite(parent);
75         
76         createLabel(Messages.format(FormatterMessages.AlreadyExistsDialog_dialog_label, fProfile.getName()));
77
78         fRenameRadio= createRadioButton(FormatterMessages.AlreadyExistsDialog_rename_radio_button_desc);
79         fNameText= createTextField();
80
81         fOverwriteRadio= createRadioButton(FormatterMessages.AlreadyExistsDialog_overwrite_radio_button_desc);
82
83         fRenameRadio.setSelection(true);
84         
85         fNameText.setText(fProfile.getName());
86         fNameText.setSelection(0, fProfile.getName().length());
87         fNameText.setFocus();
88         
89         fNameText.addModifyListener( new ModifyListener() {
90             public void modifyText(ModifyEvent e) {
91                 doValidation();
92             }
93         });
94         
95         fRenameRadio.addSelectionListener(new SelectionListener() {
96             public void widgetSelected(SelectionEvent e) {
97                 fNameText.setEnabled(true);
98                 fNameText.setFocus();
99                 fNameText.setSelection(0, fNameText.getText().length());
100                 doValidation();
101             }
102             public void widgetDefaultSelected(SelectionEvent e) {
103             }
104         });
105         
106         fOverwriteRadio.addSelectionListener(new SelectionListener() {
107             public void widgetSelected(SelectionEvent e) {
108                 fNameText.setEnabled(false);
109                 doValidation();
110             }
111             public void widgetDefaultSelected(SelectionEvent e) {
112             }
113         });
114         
115         updateStatus(fDuplicate);
116         
117         applyDialogFont(fComposite);
118         
119         return fComposite;
120     }
121     
122     private void initializeComposite(Composite parent) {
123         fComposite= new Composite(parent, SWT.NULL);
124         
125         final GridLayout layout= new GridLayout();
126         layout.marginHeight= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
127         layout.marginWidth= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
128         layout.verticalSpacing= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
129         layout.horizontalSpacing= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
130         layout.numColumns= NUM_COLUMNS;
131         
132         fComposite.setLayout(layout);
133     }
134     
135     private Label createLabel(String JavaDoc text) {
136         final GridData gd= new GridData(GridData.FILL_HORIZONTAL);
137         gd.horizontalSpan= NUM_COLUMNS;
138         gd.widthHint= convertWidthInCharsToPixels(60);
139         final Label label= new Label(fComposite, SWT.WRAP);
140         label.setText(text);
141         label.setLayoutData(gd);
142         return label;
143     }
144     
145     private Button createRadioButton(String JavaDoc text) {
146         final GridData gd = new GridData();
147         gd.horizontalSpan = NUM_COLUMNS;
148         gd.widthHint= convertWidthInCharsToPixels(60);
149         final Button radio= new Button(fComposite, SWT.RADIO);
150         radio.setLayoutData(gd);
151         radio.setText(text);
152         return radio;
153     }
154     
155     private Text createTextField() {
156         final GridData gd = new GridData( GridData.FILL_HORIZONTAL);
157         gd.horizontalSpan= NUM_COLUMNS;
158         gd.horizontalIndent= 15;
159         final Text text= new Text(fComposite, SWT.SINGLE | SWT.BORDER);
160         text.setLayoutData(gd);
161         return text;
162     }
163
164
165     /**
166      * Validate the current settings
167      */

168     protected void doValidation() {
169
170         if (fOverwriteRadio.getSelection()) {
171             updateStatus(fOk);
172             return;
173         }
174
175         final String JavaDoc name= fNameText.getText().trim();
176         
177         if (name.length() == 0) {
178             updateStatus(fEmpty);
179             return;
180         }
181         
182         if (fProfileManager.containsName(name)) {
183             updateStatus(fDuplicate);
184             return;
185         }
186         
187         updateStatus(fOk);
188     }
189     
190     
191     protected void okPressed() {
192         if (!getStatus().isOK())
193             return;
194         if (fRenameRadio.getSelection())
195             fProfile.rename(fNameText.getText().trim(), fProfileManager);
196         super.okPressed();
197     }
198 }
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
Popular Tags