KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashMap JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.runtime.IStatus;
18
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.events.ModifyEvent;
21 import org.eclipse.swt.events.ModifyListener;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.events.SelectionListener;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Button;
27 import org.eclipse.swt.widgets.Combo;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.swt.widgets.Text;
33
34 import org.eclipse.jface.dialogs.IDialogConstants;
35 import org.eclipse.jface.dialogs.IDialogSettings;
36 import org.eclipse.jface.dialogs.StatusDialog;
37
38 import org.eclipse.jdt.ui.JavaUI;
39
40 import org.eclipse.jdt.internal.ui.JavaPlugin;
41 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
42 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.CustomProfile;
43 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.Profile;
44
45 /**
46  * The dialog to create a new profile.
47  */

48 public class CreateProfileDialog extends StatusDialog {
49     
50     
51     private static final String JavaDoc PREF_OPEN_EDIT_DIALOG= JavaUI.ID_PLUGIN + ".codeformatter.create_profile_dialog.open_edit"; //$NON-NLS-1$
52

53     
54     private Text fNameText;
55     private Combo fProfileCombo;
56     private Button fEditCheckbox;
57     
58     private final static StatusInfo fOk= new StatusInfo();
59     private final static StatusInfo fEmpty= new StatusInfo(IStatus.ERROR, FormatterMessages.CreateProfileDialog_status_message_profile_name_is_empty);
60     private final static StatusInfo fDuplicate= new StatusInfo(IStatus.ERROR, FormatterMessages.CreateProfileDialog_status_message_profile_with_this_name_already_exists);
61
62     private final ProfileManager fProfileManager;
63     private final List JavaDoc fSortedProfiles;
64     private final String JavaDoc [] fSortedNames;
65     
66     private CustomProfile fCreatedProfile;
67     protected boolean fOpenEditDialog;
68
69
70     private final IProfileVersioner fProfileVersioner;
71     
72     public CreateProfileDialog(Shell parentShell, ProfileManager profileManager, IProfileVersioner profileVersioner) {
73         super(parentShell);
74         fProfileManager= profileManager;
75         fProfileVersioner= profileVersioner;
76         fSortedProfiles= fProfileManager.getSortedProfiles();
77         fSortedNames= fProfileManager.getSortedDisplayNames();
78     }
79     
80     
81     public void create() {
82         super.create();
83         setTitle(FormatterMessages.CreateProfileDialog_dialog_title);
84     }
85     
86     public Control createDialogArea(Composite parent) {
87                 
88         final int numColumns= 2;
89         
90         GridData gd;
91         
92         final GridLayout layout= new GridLayout(numColumns, false);
93         layout.marginHeight= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
94         layout.marginWidth= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
95         layout.verticalSpacing= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
96         layout.horizontalSpacing= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
97
98         final Composite composite= new Composite(parent, SWT.NONE);
99         composite.setLayout(layout);
100         
101         // Create "Profile name:" label
102
gd = new GridData(GridData.FILL_HORIZONTAL);
103         gd.horizontalSpan = numColumns;
104         gd.widthHint= convertWidthInCharsToPixels(60);
105         final Label nameLabel = new Label(composite, SWT.WRAP);
106         nameLabel.setText(FormatterMessages.CreateProfileDialog_profile_name_label_text);
107         nameLabel.setLayoutData(gd);
108         
109         // Create text field to enter name
110
gd = new GridData( GridData.FILL_HORIZONTAL);
111         gd.horizontalSpan= numColumns;
112         fNameText= new Text(composite, SWT.SINGLE | SWT.BORDER);
113         fNameText.setLayoutData(gd);
114         fNameText.addModifyListener( new ModifyListener() {
115             public void modifyText(ModifyEvent e) {
116                 doValidation();
117             }
118         });
119         
120         // Create "Initialize settings ..." label
121
gd = new GridData();
122         gd.horizontalSpan = numColumns;
123         Label profileLabel = new Label(composite, SWT.WRAP);
124         profileLabel.setText(FormatterMessages.CreateProfileDialog_base_profile_label_text);
125         profileLabel.setLayoutData(gd);
126         
127         gd= new GridData(GridData.FILL_HORIZONTAL);
128         gd.horizontalSpan= numColumns;
129         fProfileCombo = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
130         fProfileCombo.setLayoutData(gd);
131         
132         
133         // "Open the edit dialog now" checkbox
134
gd= new GridData();
135         gd.horizontalSpan= numColumns;
136         fEditCheckbox= new Button(composite, SWT.CHECK);
137         fEditCheckbox.setText(FormatterMessages.CreateProfileDialog_open_edit_dialog_checkbox_text);
138         fEditCheckbox.addSelectionListener(new SelectionListener() {
139             public void widgetSelected(SelectionEvent e) {
140                 fOpenEditDialog= ((Button)e.widget).getSelection();
141             }
142             public void widgetDefaultSelected(SelectionEvent e) {
143             }
144         });
145         
146         final IDialogSettings dialogSettings= JavaPlugin.getDefault().getDialogSettings();//.get(PREF_OPEN_EDIT_DIALOG);
147
if (dialogSettings.get(PREF_OPEN_EDIT_DIALOG) != null) {
148             fOpenEditDialog= dialogSettings.getBoolean(PREF_OPEN_EDIT_DIALOG);
149         } else {
150             fOpenEditDialog= true;
151         }
152         fEditCheckbox.setSelection(fOpenEditDialog);
153         
154         fProfileCombo.setItems(fSortedNames);
155         fProfileCombo.setText(fProfileManager.getDefaultProfile().getName());
156         updateStatus(fEmpty);
157         
158         applyDialogFont(composite);
159         
160         fNameText.setFocus();
161         
162         return composite;
163     }
164
165
166     /**
167      * Validate the current settings
168      */

169     protected void doValidation() {
170         final String JavaDoc name= fNameText.getText().trim();
171         
172         if (fProfileManager.containsName(name)) {
173             updateStatus(fDuplicate);
174             return;
175         }
176         if (name.length() == 0) {
177             updateStatus(fEmpty);
178             return;
179         }
180         updateStatus(fOk);
181     }
182     
183     
184     protected void okPressed() {
185         if (!getStatus().isOK())
186             return;
187
188         JavaPlugin.getDefault().getDialogSettings().put(PREF_OPEN_EDIT_DIALOG, fOpenEditDialog);
189
190         final Map JavaDoc baseSettings= new HashMap JavaDoc(((Profile)fSortedProfiles.get(fProfileCombo.getSelectionIndex())).getSettings());
191         final String JavaDoc profileName= fNameText.getText();
192         
193         fCreatedProfile= new CustomProfile(profileName, baseSettings, fProfileVersioner.getCurrentVersion(), fProfileVersioner.getProfileKind());
194         fProfileManager.addProfile(fCreatedProfile);
195         super.okPressed();
196     }
197     
198     public final CustomProfile getCreatedProfile() {
199         return fCreatedProfile;
200     }
201     
202     public final boolean openEditDialog() {
203         return fOpenEditDialog;
204     }
205 }
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
Popular Tags