KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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  * Aaron Luchko, aluchko@redhat.com - 105926 [Formatter] Exporting Unnamed profile fails silently
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.ui.preferences.formatter;
13
14 import org.eclipse.core.runtime.IStatus;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.ModifyEvent;
18 import org.eclipse.swt.events.ModifyListener;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Label;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.swt.widgets.Text;
26
27 import org.eclipse.jface.dialogs.IDialogConstants;
28 import org.eclipse.jface.dialogs.StatusDialog;
29
30 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
31 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.Profile;
32 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.SharedProfile;
33
34 /**
35  * The dialog to rename a new profile.
36  */

37 public class RenameProfileDialog extends StatusDialog {
38     
39     private Label fNameLabel;
40     private Text fNameText;
41     
42     private final StatusInfo fOk;
43     private final StatusInfo fEmpty;
44     private final StatusInfo fDuplicate;
45     private final StatusInfo fNoMessage;
46
47     private final Profile fProfile;
48     private final ProfileManager fManager;
49     private Profile fRenamedProfile;
50     
51     public RenameProfileDialog(Shell parentShell, Profile profile, ProfileManager manager) {
52         super(parentShell);
53         fManager= manager;
54         setTitle(FormatterMessages.RenameProfileDialog_dialog_title);
55         fProfile= profile;
56         fOk= new StatusInfo();
57         fDuplicate= new StatusInfo(IStatus.ERROR, FormatterMessages.RenameProfileDialog_status_message_profile_with_this_name_already_exists);
58         fEmpty= new StatusInfo(IStatus.ERROR, FormatterMessages.RenameProfileDialog_status_message_profile_name_empty);
59         fNoMessage= new StatusInfo(IStatus.ERROR, new String JavaDoc());
60     }
61     
62     public Control createDialogArea(Composite parent) {
63                 
64         final int numColumns= 2;
65         
66         GridLayout layout= new GridLayout();
67         layout.marginHeight= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
68         layout.marginWidth= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
69         layout.verticalSpacing= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
70         layout.horizontalSpacing= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
71         layout.numColumns= numColumns;
72         
73         final Composite composite= new Composite(parent, SWT.NULL);
74         composite.setLayout(layout);
75         
76         // Create "Please enter a new name:" label
77
GridData gd = new GridData();
78         gd.horizontalSpan = numColumns;
79         gd.widthHint= convertWidthInCharsToPixels(60);
80         fNameLabel = new Label(composite, SWT.NONE);
81         fNameLabel.setText(FormatterMessages.RenameProfileDialog_dialog_label_enter_a_new_name);
82         fNameLabel.setLayoutData(gd);
83         
84         // Create text field to enter name
85
gd = new GridData( GridData.FILL_HORIZONTAL);
86         gd.horizontalSpan= numColumns;
87         fNameText= new Text(composite, SWT.SINGLE | SWT.BORDER);
88         if (fProfile instanceof SharedProfile) {
89             fNameText.setText(fProfile.getName());
90         }
91         fNameText.setSelection(0, fProfile.getName().length());
92         fNameText.setLayoutData(gd);
93         fNameText.addModifyListener( new ModifyListener() {
94             public void modifyText(ModifyEvent e) {
95                 doValidation();
96             }
97         });
98         fNameText.setText(fProfile.getName());
99         fNameText.selectAll();
100         
101         applyDialogFont(composite);
102         
103         return composite;
104     }
105
106
107     /**
108      * Validate the current settings.
109      */

110     protected void doValidation() {
111         final String JavaDoc name= fNameText.getText().trim();
112         
113         if (name.length() == 0) {
114             updateStatus(fEmpty);
115             return;
116         }
117         
118         if (name.equals(fProfile.getName())) {
119             updateStatus(fNoMessage);
120             return;
121         }
122         
123         if (fManager.containsName(name)) {
124             updateStatus(fDuplicate);
125             return;
126         }
127         
128         updateStatus(fOk);
129     }
130     
131     public Profile getRenamedProfile() {
132         return fRenamedProfile;
133     }
134     
135     
136     protected void okPressed() {
137         if (!getStatus().isOK())
138             return;
139         fRenamedProfile= fProfile.rename(fNameText.getText(), fManager);
140         super.okPressed();
141     }
142 }
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
Popular Tags