KickJava   Java API By Example, From Geeks To Geeks.

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


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.jdt.internal.ui.preferences.formatter;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.core.runtime.Status;
25 import org.eclipse.core.runtime.content.IContentType;
26
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.events.SelectionListener;
30 import org.eclipse.swt.graphics.Point;
31 import org.eclipse.swt.graphics.Rectangle;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.swt.widgets.FileDialog;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Shell;
40 import org.eclipse.swt.widgets.TabFolder;
41 import org.eclipse.swt.widgets.TabItem;
42
43 import org.eclipse.jface.dialogs.IDialogConstants;
44 import org.eclipse.jface.dialogs.IDialogSettings;
45 import org.eclipse.jface.dialogs.MessageDialog;
46 import org.eclipse.jface.dialogs.StatusDialog;
47
48 import org.eclipse.jdt.internal.corext.util.Messages;
49
50 import org.eclipse.jdt.ui.JavaUI;
51
52 import org.eclipse.jdt.internal.ui.JavaPlugin;
53 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
54 import org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialogTabPage.IModificationListener;
55 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.CustomProfile;
56 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.Profile;
57 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
58 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField;
59 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener;
60 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField;
61
62 public abstract class ModifyDialog extends StatusDialog implements IModificationListener {
63     
64     /**
65      * The keys to retrieve the preferred area from the dialog settings.
66      */

67     private static final String JavaDoc DS_KEY_PREFERRED_WIDTH= "modify_dialog.preferred_width"; //$NON-NLS-1$
68
private static final String JavaDoc DS_KEY_PREFERRED_HEIGHT= "modify_dialog.preferred_height"; //$NON-NLS-1$
69
private static final String JavaDoc DS_KEY_PREFERRED_X= "modify_dialog.preferred_x"; //$NON-NLS-1$
70
private static final String JavaDoc DS_KEY_PREFERRED_Y= "modify_dialog.preferred_y"; //$NON-NLS-1$
71

72     
73     /**
74      * The key to store the number (beginning at 0) of the tab page which had the
75      * focus last time.
76      */

77     private static final String JavaDoc DS_KEY_LAST_FOCUS= "modify_dialog.last_focus"; //$NON-NLS-1$
78

79     private static final int APPLAY_BUTTON_ID= IDialogConstants.CLIENT_ID;
80     private static final int SAVE_BUTTON_ID= IDialogConstants.CLIENT_ID + 1;
81
82     private final String JavaDoc fKeyPreferredWidth;
83     private final String JavaDoc fKeyPreferredHight;
84     private final String JavaDoc fKeyPreferredX;
85     private final String JavaDoc fKeyPreferredY;
86     private final String JavaDoc fKeyLastFocus;
87     private final String JavaDoc fLastSaveLoadPathKey;
88     private final ProfileStore fProfileStore;
89     private final boolean fNewProfile;
90     private Profile fProfile;
91     private final Map JavaDoc fWorkingValues;
92     private final List JavaDoc fTabPages;
93     private final IDialogSettings fDialogSettings;
94     private TabFolder fTabFolder;
95     private final ProfileManager fProfileManager;
96     private Button fApplyButton;
97     private Button fSaveButton;
98     private StringDialogField fProfileNameField;
99
100     public ModifyDialog(Shell parentShell, Profile profile, ProfileManager profileManager, ProfileStore profileStore, boolean newProfile, String JavaDoc dialogPreferencesKey, String JavaDoc lastSavePathKey) {
101         super(parentShell);
102         
103         fProfileStore= profileStore;
104         fLastSaveLoadPathKey= lastSavePathKey;
105
106         fKeyPreferredWidth= JavaUI.ID_PLUGIN + dialogPreferencesKey + DS_KEY_PREFERRED_WIDTH;
107         fKeyPreferredHight= JavaUI.ID_PLUGIN + dialogPreferencesKey + DS_KEY_PREFERRED_HEIGHT;
108         fKeyPreferredX= JavaUI.ID_PLUGIN + dialogPreferencesKey + DS_KEY_PREFERRED_X;
109         fKeyPreferredY= JavaUI.ID_PLUGIN + dialogPreferencesKey + DS_KEY_PREFERRED_Y;
110         fKeyLastFocus= JavaUI.ID_PLUGIN + dialogPreferencesKey + DS_KEY_LAST_FOCUS;
111
112         fProfileManager= profileManager;
113         fNewProfile= newProfile;
114         setShellStyle(getShellStyle() | SWT.RESIZE | SWT.MAX );
115                 
116         fProfile= profile;
117         setTitle(Messages.format(FormatterMessages.ModifyDialog_dialog_title, profile.getName()));
118         fWorkingValues= new HashMap JavaDoc(fProfile.getSettings());
119         setStatusLineAboveButtons(false);
120         fTabPages= new ArrayList JavaDoc();
121         fDialogSettings= JavaPlugin.getDefault().getDialogSettings();
122     }
123
124     protected abstract void addPages(Map JavaDoc values);
125
126     public void create() {
127         super.create();
128         int lastFocusNr= 0;
129         try {
130             lastFocusNr= fDialogSettings.getInt(fKeyLastFocus);
131             if (lastFocusNr < 0) lastFocusNr= 0;
132             if (lastFocusNr > fTabPages.size() - 1) lastFocusNr= fTabPages.size() - 1;
133         } catch (NumberFormatException JavaDoc x) {
134             lastFocusNr= 0;
135         }
136         
137         if (!fNewProfile) {
138             fTabFolder.setSelection(lastFocusNr);
139             ((ModifyDialogTabPage)fTabFolder.getSelection()[0].getData()).setInitialFocus();
140         }
141     }
142
143     protected Control createDialogArea(Composite parent) {
144         
145         final Composite composite= (Composite)super.createDialogArea(parent);
146         
147         Composite nameComposite= new Composite(composite, SWT.NONE);
148         nameComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
149         nameComposite.setLayout(new GridLayout(3, false));
150         
151         fProfileNameField= new StringDialogField();
152         fProfileNameField.setLabelText(FormatterMessages.ModifyDialog_ProfileName_Label);
153         fProfileNameField.setText(fProfile.getName());
154         fProfileNameField.getLabelControl(nameComposite).setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
155         fProfileNameField.getTextControl(nameComposite).setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
156         fProfileNameField.setDialogFieldListener(new IDialogFieldListener() {
157             public void dialogFieldChanged(DialogField field) {
158                 doValidate();
159             }
160         });
161         
162         fSaveButton= createButton(nameComposite, SAVE_BUTTON_ID, FormatterMessages.ModifyDialog_Export_Button, false);
163         
164         fTabFolder = new TabFolder(composite, SWT.NONE);
165         fTabFolder.setFont(composite.getFont());
166         fTabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
167
168         addPages(fWorkingValues);
169
170         applyDialogFont(composite);
171         
172         fTabFolder.addSelectionListener(new SelectionListener() {
173             public void widgetDefaultSelected(SelectionEvent e) {}
174             public void widgetSelected(SelectionEvent e) {
175                 final TabItem tabItem= (TabItem)e.item;
176                 final ModifyDialogTabPage page= (ModifyDialogTabPage)tabItem.getData();
177                 // page.fSashForm.setWeights();
178
fDialogSettings.put(fKeyLastFocus, fTabPages.indexOf(page));
179                 page.makeVisible();
180             }
181         });
182         
183         doValidate();
184         
185         return composite;
186     }
187     
188     public void updateStatus(IStatus status) {
189         if (status == null) {
190             doValidate();
191         } else {
192             super.updateStatus(status);
193         }
194     }
195
196     /* (non-Javadoc)
197      * @see org.eclipse.jface.window.Window#getInitialSize()
198      */

199     protected Point getInitialSize() {
200         Point initialSize= super.getInitialSize();
201         try {
202             int lastWidth= fDialogSettings.getInt(fKeyPreferredWidth);
203             if (initialSize.x > lastWidth)
204                 lastWidth= initialSize.x;
205             int lastHeight= fDialogSettings.getInt(fKeyPreferredHight);
206             if (initialSize.y > lastHeight)
207                 lastHeight= initialSize.y;
208             return new Point(lastWidth, lastHeight);
209         } catch (NumberFormatException JavaDoc ex) {
210         }
211         return initialSize;
212     }
213     
214     /* (non-Javadoc)
215      * @see org.eclipse.jface.window.Window#getInitialLocation(org.eclipse.swt.graphics.Point)
216      */

217     protected Point getInitialLocation(Point initialSize) {
218         try {
219             return new Point(fDialogSettings.getInt(fKeyPreferredX), fDialogSettings.getInt(fKeyPreferredY));
220         } catch (NumberFormatException JavaDoc ex) {
221             return super.getInitialLocation(initialSize);
222         }
223     }
224         
225     public boolean close() {
226         final Rectangle shell= getShell().getBounds();
227
228         fDialogSettings.put(fKeyPreferredWidth, shell.width);
229         fDialogSettings.put(fKeyPreferredHight, shell.height);
230         fDialogSettings.put(fKeyPreferredX, shell.x);
231         fDialogSettings.put(fKeyPreferredY, shell.y);
232
233         return super.close();
234     }
235     
236     /* (non-Javadoc)
237      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
238      */

239     protected void okPressed() {
240         applyPressed();
241         super.okPressed();
242     }
243     
244     protected void buttonPressed(int buttonId) {
245         if (buttonId == APPLAY_BUTTON_ID) {
246             applyPressed();
247             setTitle(Messages.format(FormatterMessages.ModifyDialog_dialog_title, fProfile.getName()));
248         } else if (buttonId == SAVE_BUTTON_ID) {
249             saveButtonPressed();
250         } else {
251             super.buttonPressed(buttonId);
252         }
253     }
254     
255     private void applyPressed() {
256         if (!fProfile.getName().equals(fProfileNameField.getText())) {
257             fProfile= fProfile.rename(fProfileNameField.getText(), fProfileManager);
258         }
259         fProfile.setSettings(new HashMap JavaDoc(fWorkingValues));
260         fProfileManager.setSelected(fProfile);
261         doValidate();
262     }
263     
264     private void saveButtonPressed() {
265         Profile selected= new CustomProfile(fProfileNameField.getText(), new HashMap JavaDoc(fWorkingValues), fProfile.getVersion(), fProfileManager.getProfileVersioner().getProfileKind());
266         
267         final FileDialog dialog= new FileDialog(getShell(), SWT.SAVE);
268         dialog.setText(FormatterMessages.CodingStyleConfigurationBlock_save_profile_dialog_title);
269         dialog.setFilterExtensions(new String JavaDoc [] {"*.xml"}); //$NON-NLS-1$
270

271         final String JavaDoc lastPath= JavaPlugin.getDefault().getDialogSettings().get(fLastSaveLoadPathKey + ".savepath"); //$NON-NLS-1$
272
if (lastPath != null) {
273             dialog.setFilterPath(lastPath);
274         }
275         final String JavaDoc path= dialog.open();
276         if (path == null)
277             return;
278
279         JavaPlugin.getDefault().getDialogSettings().put(fLastSaveLoadPathKey + ".savepath", dialog.getFilterPath()); //$NON-NLS-1$
280

281         final File JavaDoc file= new File JavaDoc(path);
282         if (file.exists() && !MessageDialog.openQuestion(getShell(), FormatterMessages.CodingStyleConfigurationBlock_save_profile_overwrite_title, Messages.format(FormatterMessages.CodingStyleConfigurationBlock_save_profile_overwrite_message, path))) {
283             return;
284         }
285         String JavaDoc encoding= ProfileStore.ENCODING;
286         final IContentType type= Platform.getContentTypeManager().getContentType("org.eclipse.core.runtime.xml"); //$NON-NLS-1$
287
if (type != null)
288             encoding= type.getDefaultCharset();
289         final Collection JavaDoc profiles= new ArrayList JavaDoc();
290         profiles.add(selected);
291         try {
292             fProfileStore.writeProfilesToFile(profiles, file, encoding);
293         } catch (CoreException e) {
294             final String JavaDoc title= FormatterMessages.CodingStyleConfigurationBlock_save_profile_error_title;
295             final String JavaDoc message= FormatterMessages.CodingStyleConfigurationBlock_save_profile_error_message;
296             ExceptionHandler.handle(e, getShell(), title, message);
297         }
298     }
299     
300     protected void createButtonsForButtonBar(Composite parent) {
301         fApplyButton= createButton(parent, APPLAY_BUTTON_ID, FormatterMessages.ModifyDialog_apply_button, false);
302         fApplyButton.setEnabled(false);
303         
304         GridLayout layout= (GridLayout) parent.getLayout();
305         layout.numColumns++;
306         layout.makeColumnsEqualWidth= false;
307         Label label= new Label(parent, SWT.NONE);
308         GridData data= new GridData();
309         data.widthHint= layout.horizontalSpacing;
310         label.setLayoutData(data);
311         super.createButtonsForButtonBar(parent);
312     }
313
314     protected final void addTabPage(String JavaDoc title, ModifyDialogTabPage tabPage) {
315         final TabItem tabItem= new TabItem(fTabFolder, SWT.NONE);
316         applyDialogFont(tabItem.getControl());
317         tabItem.setText(title);
318         tabItem.setData(tabPage);
319         tabItem.setControl(tabPage.createContents(fTabFolder));
320         fTabPages.add(tabPage);
321     }
322
323     public void valuesModified() {
324         doValidate();
325     }
326
327     protected void updateButtonsEnableState(IStatus status) {
328         super.updateButtonsEnableState(status);
329         if (fApplyButton != null && !fApplyButton.isDisposed()) {
330             fApplyButton.setEnabled(hasChanges() && !status.matches(IStatus.ERROR));
331         }
332         if (fSaveButton != null && !fSaveButton.isDisposed()) {
333             fSaveButton.setEnabled(!validateProfileName().matches(IStatus.ERROR));
334         }
335     }
336     
337     private void doValidate() {
338         IStatus status= validateProfileName();
339         if (status.matches(IStatus.ERROR)) {
340             updateStatus(status);
341             return;
342         }
343         
344         String JavaDoc name= fProfileNameField.getText().trim();
345         if (!name.equals(fProfile.getName()) && fProfileManager.containsName(name)) {
346             updateStatus(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, FormatterMessages.ModifyDialog_Duplicate_Status));
347             return;
348         }
349         
350         if (fProfile.isBuiltInProfile() || fProfile.isSharedProfile()) {
351             updateStatus(new Status(IStatus.INFO, JavaUI.ID_PLUGIN, FormatterMessages.ModifyDialog_NewCreated_Status));
352             return;
353         }
354
355         updateStatus(StatusInfo.OK_STATUS);
356     }
357
358     private IStatus validateProfileName() {
359         final String JavaDoc name= fProfileNameField.getText().trim();
360         
361         if (fProfile.isBuiltInProfile()) {
362             if (fProfile.getName().equals(name)) {
363                 return new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, FormatterMessages.ModifyDialog_BuiltIn_Status);
364             }
365         }
366         
367         if (fProfile.isSharedProfile()) {
368             if (fProfile.getName().equals(name)) {
369                 return new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, FormatterMessages.ModifyDialog_Shared_Status);
370             }
371         }
372         
373         if (name.length() == 0) {
374             return new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, FormatterMessages.ModifyDialog_EmptyName_Status);
375         }
376         
377         return StatusInfo.OK_STATUS;
378     }
379
380     private boolean hasChanges() {
381         if (!fProfileNameField.getText().trim().equals(fProfile.getName()))
382             return true;
383         
384         Iterator JavaDoc iter= fProfile.getSettings().entrySet().iterator();
385         for (;iter.hasNext();) {
386             Map.Entry JavaDoc curr= (Map.Entry JavaDoc) iter.next();
387             if (!fWorkingValues.get(curr.getKey()).equals(curr.getValue())) {
388                 return true;
389             }
390         }
391         return false;
392     }
393     
394 }
395
Popular Tags