KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-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.List JavaDoc;
17 import java.util.Observable JavaDoc;
18 import java.util.Observer JavaDoc;
19
20 import org.eclipse.core.runtime.CoreException;
21
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.SelectionEvent;
24 import org.eclipse.swt.events.SelectionListener;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Button;
28 import org.eclipse.swt.widgets.Combo;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.FileDialog;
31 import org.eclipse.swt.widgets.Label;
32
33 import org.eclipse.jface.dialogs.IDialogConstants;
34 import org.eclipse.jface.dialogs.MessageDialog;
35 import org.eclipse.jface.preference.IPreferenceStore;
36 import org.eclipse.jface.window.Window;
37
38 import org.eclipse.jdt.ui.JavaUI;
39
40 import org.eclipse.jdt.internal.ui.JavaPlugin;
41 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.CustomProfile;
42 import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.Profile;
43 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
44 import org.eclipse.jdt.internal.ui.util.PixelConverter;
45 import org.eclipse.jdt.internal.ui.util.SWTUtil;
46
47
48
49 /**
50  * The code formatter preference page.
51  */

52
53 public class CodingStyleConfigurationBlock {
54     
55     private static final String JavaDoc PREF_LASTLOADPATH= JavaUI.ID_PLUGIN + ".codeformatter.loadpath"; //$NON-NLS-1$
56
private static final String JavaDoc PREF_LASTSAVEPATH= JavaUI.ID_PLUGIN + ".codeformatter.savepath"; //$NON-NLS-1$
57

58     
59     private class StoreUpdater implements Observer JavaDoc {
60         
61         public StoreUpdater() {
62             fProfileManager.addObserver(this);
63         }
64
65         public void update(Observable JavaDoc o, Object JavaDoc arg) {
66             final int value= ((Integer JavaDoc)arg).intValue();
67             switch (value) {
68             case ProfileManager.PROFILE_DELETED_EVENT:
69             case ProfileManager.PROFILE_RENAMED_EVENT:
70             case ProfileManager.PROFILE_CREATED_EVENT:
71             case ProfileManager.SETTINGS_CHANGED_EVENT:
72                 try {
73                     ProfileStore.writeProfiles(fProfileManager.getSortedProfiles());
74                 } catch (CoreException x) {
75                     JavaPlugin.log(x);
76                 }
77             }
78         }
79     }
80     
81     
82
83     private class ProfileComboController implements Observer JavaDoc, SelectionListener {
84         
85         private final List JavaDoc fSortedProfiles;
86         
87         public ProfileComboController() {
88             fSortedProfiles= fProfileManager.getSortedProfiles();
89             fProfileCombo.addSelectionListener(this);
90             fProfileManager.addObserver(this);
91             updateProfiles();
92             updateSelection();
93         }
94         
95         public void widgetSelected(SelectionEvent e) {
96             final int index= fProfileCombo.getSelectionIndex();
97             fProfileManager.setSelected((Profile)fSortedProfiles.get(index));
98         }
99
100         public void widgetDefaultSelected(SelectionEvent e) {}
101
102         public void update(Observable JavaDoc o, Object JavaDoc arg) {
103             if (arg == null) return;
104             final int value= ((Integer JavaDoc)arg).intValue();
105             switch (value) {
106                 case ProfileManager.PROFILE_CREATED_EVENT:
107                 case ProfileManager.PROFILE_DELETED_EVENT:
108                 case ProfileManager.PROFILE_RENAMED_EVENT:
109                     updateProfiles();
110                 case ProfileManager.SELECTION_CHANGED_EVENT:
111                     updateSelection();
112             }
113         }
114         
115         private void updateProfiles() {
116             fProfileCombo.setItems(fProfileManager.getSortedNames());
117         }
118
119         private void updateSelection() {
120             fProfileCombo.setText(fProfileManager.getSelected().getName());
121         }
122     }
123     
124     private class ButtonController implements Observer JavaDoc, SelectionListener {
125         
126         public ButtonController() {
127             fProfileManager.addObserver(this);
128             fNewButton.addSelectionListener(this);
129             fRenameButton.addSelectionListener(this);
130             fEditButton.addSelectionListener(this);
131             fDeleteButton.addSelectionListener(this);
132             fSaveButton.addSelectionListener(this);
133             fLoadButton.addSelectionListener(this);
134             update(fProfileManager, null);
135         }
136
137         public void update(Observable JavaDoc o, Object JavaDoc arg) {
138             final boolean state= ((ProfileManager)o).getSelected() instanceof CustomProfile;
139             fEditButton.setText(state ? FormatterMessages.getString("CodingStyleConfigurationBlock.edit_button.desc") //$NON-NLS-1$
140
: FormatterMessages.getString("CodingStyleConfigurationBlock.show_button.desc")); //$NON-NLS-1$
141
fDeleteButton.setEnabled(state);
142             fSaveButton.setEnabled(state);
143             fRenameButton.setEnabled(state);
144         }
145
146         public void widgetSelected(SelectionEvent e) {
147             final Button button= (Button)e.widget;
148             if (button == fSaveButton)
149                 saveButtonPressed();
150             else if (button == fEditButton)
151                 modifyButtonPressed();
152             else if (button == fDeleteButton)
153                 deleteButtonPressed();
154             else if (button == fNewButton)
155                 newButtonPressed();
156             else if (button == fLoadButton)
157                 loadButtonPressed();
158             else if (button == fRenameButton)
159                 renameButtonPressed();
160         }
161         
162         public void widgetDefaultSelected(SelectionEvent e) {
163         }
164         
165         private void renameButtonPressed() {
166             if (!(fProfileManager.getSelected() instanceof CustomProfile)) return;
167             final CustomProfile profile= (CustomProfile)fProfileManager.getSelected();
168             final RenameProfileDialog renameDialog= new RenameProfileDialog(fComposite.getShell(), profile, fProfileManager);
169             renameDialog.open();
170         }
171         
172         private void modifyButtonPressed() {
173             final ModifyDialog modifyDialog= new ModifyDialog(fComposite.getShell(), fProfileManager.getSelected(), fProfileManager, false);
174             modifyDialog.open();
175         }
176         
177         private void deleteButtonPressed() {
178             if (MessageDialog.openQuestion(
179                 fComposite.getShell(),
180                 FormatterMessages.getString("CodingStyleConfigurationBlock.delete_confirmation.title"), //$NON-NLS-1$
181
FormatterMessages.getFormattedString("CodingStyleConfigurationBlock.delete_confirmation.question", fProfileManager.getSelected().getName()))) { //$NON-NLS-1$
182
fProfileManager.deleteSelected();
183             }
184         }
185         
186         private void newButtonPressed() {
187             final CreateProfileDialog p= new CreateProfileDialog(fComposite.getShell(), fProfileManager);
188             if (p.open() != Window.OK)
189                 return;
190             if (!p.openEditDialog())
191                 return;
192             final ModifyDialog modifyDialog= new ModifyDialog(fComposite.getShell(), p.getCreatedProfile(), fProfileManager, true);
193             modifyDialog.open();
194         }
195         
196         private void saveButtonPressed() {
197             final FileDialog dialog= new FileDialog(fComposite.getShell(), SWT.SAVE);
198             dialog.setText(FormatterMessages.getString("CodingStyleConfigurationBlock.save_profile.dialog.title")); //$NON-NLS-1$
199
dialog.setFilterExtensions(new String JavaDoc [] {"*.xml"}); //$NON-NLS-1$
200

201             final String JavaDoc lastPath= JavaPlugin.getDefault().getDialogSettings().get(PREF_LASTSAVEPATH);
202             if (lastPath != null) {
203                 dialog.setFilterPath(lastPath);
204             }
205             final String JavaDoc path= dialog.open();
206             if (path == null)
207                 return;
208             
209             JavaPlugin.getDefault().getDialogSettings().put(PREF_LASTSAVEPATH, dialog.getFilterPath());
210             
211             final File JavaDoc file= new File JavaDoc(path);
212             final Collection JavaDoc profiles= new ArrayList JavaDoc();
213             profiles.add(fProfileManager.getSelected());
214             try {
215                 ProfileStore.writeProfilesToFile(profiles, file);
216             } catch (CoreException e) {
217                 final String JavaDoc title= FormatterMessages.getString("CodingStyleConfigurationBlock.save_profile.error.title"); //$NON-NLS-1$
218
final String JavaDoc message= FormatterMessages.getString("CodingStyleConfigurationBlock.save_profile.error.message"); //$NON-NLS-1$
219
ExceptionHandler.handle(e, fComposite.getShell(), title, message);
220             }
221         }
222         
223         private void loadButtonPressed() {
224             final FileDialog dialog= new FileDialog(fComposite.getShell(), SWT.OPEN);
225             dialog.setText(FormatterMessages.getString("CodingStyleConfigurationBlock.load_profile.dialog.title")); //$NON-NLS-1$
226
dialog.setFilterExtensions(new String JavaDoc [] {"*.xml"}); //$NON-NLS-1$
227
final String JavaDoc lastPath= JavaPlugin.getDefault().getDialogSettings().get(PREF_LASTLOADPATH);
228             if (lastPath != null) {
229                 dialog.setFilterPath(lastPath);
230             }
231             final String JavaDoc path= dialog.open();
232             if (path == null)
233                 return;
234             JavaPlugin.getDefault().getDialogSettings().put(PREF_LASTLOADPATH, dialog.getFilterPath());
235             
236             final File JavaDoc file= new File JavaDoc(path);
237             Collection JavaDoc profiles= null;
238             try {
239                 profiles= ProfileStore.readProfilesFromFile(file);
240             } catch (CoreException e) {
241                 final String JavaDoc title= FormatterMessages.getString("CodingStyleConfigurationBlock.load_profile.error.title"); //$NON-NLS-1$
242
final String JavaDoc message= FormatterMessages.getString("CodingStyleConfigurationBlock.load_profile.error.message"); //$NON-NLS-1$
243
ExceptionHandler.handle(e, fComposite.getShell(), title, message);
244             }
245             if (profiles == null || profiles.isEmpty())
246                 return;
247             
248             final CustomProfile profile= (CustomProfile)profiles.iterator().next();
249             
250             if (ProfileVersioner.getVersionStatus(profile) > 0) {
251                 final String JavaDoc title= FormatterMessages.getString("CodingStyleConfigurationBlock.load_profile.error_too_new.title"); //$NON-NLS-1$
252
final String JavaDoc message= FormatterMessages.getString("CodingStyleConfigurationBlock.load_profile.error_too_new.message"); //$NON-NLS-1$
253
MessageDialog.openWarning(fComposite.getShell(), title, message);
254             }
255             
256             if (fProfileManager.containsName(profile.getName())) {
257                 final AlreadyExistsDialog aeDialog= new AlreadyExistsDialog(fComposite.getShell(), profile, fProfileManager);
258                 if (aeDialog.open() != Window.OK)
259                     return;
260             }
261             ProfileVersioner.updateAndComplete(profile);
262             fProfileManager.addProfile(profile);
263         }
264     }
265     
266     private class PreviewController implements Observer JavaDoc {
267
268         public PreviewController() {
269             fProfileManager.addObserver(this);
270             fJavaPreview.setWorkingValues(fProfileManager.getSelected().getSettings());
271             fJavaPreview.update();
272         }
273         
274         public void update(Observable JavaDoc o, Object JavaDoc arg) {
275             final int value= ((Integer JavaDoc)arg).intValue();
276             switch (value) {
277                 case ProfileManager.PROFILE_CREATED_EVENT:
278                 case ProfileManager.PROFILE_DELETED_EVENT:
279                 case ProfileManager.SELECTION_CHANGED_EVENT:
280                 case ProfileManager.SETTINGS_CHANGED_EVENT:
281                     fJavaPreview.setWorkingValues(((ProfileManager)o).getSelected().getSettings());
282                     fJavaPreview.update();
283             }
284         }
285         
286     }
287
288     
289     /**
290      * Some Java source code used for preview.
291      */

292     private final static String JavaDoc PREVIEW=
293         "/**\n* " + //$NON-NLS-1$
294
FormatterMessages.getString("CodingStyleConfigurationBlock.preview.title") + //$NON-NLS-1$
295
"\n*/\n\n" + //$NON-NLS-1$
296
"package mypackage; import java.util.LinkedList; public class MyIntStack {" + //$NON-NLS-1$
297
"private final LinkedList fStack;" + //$NON-NLS-1$
298
"public MyIntStack(){fStack= new LinkedList();}" + //$NON-NLS-1$
299
"public int pop(){return ((Integer)fStack.removeFirst()).intValue();}" + //$NON-NLS-1$
300
"public void push(int elem){fStack.addFirst(new Integer(elem));}" + //$NON-NLS-1$
301
"public boolean isEmpty() {return fStack.isEmpty();}" + //$NON-NLS-1$
302
"}"; //$NON-NLS-1$
303

304
305     /**
306      * The GUI controls
307      */

308     protected Composite fComposite;
309     protected Combo fProfileCombo;
310     protected Button fEditButton;
311     protected Button fRenameButton;
312     protected Button fDeleteButton;
313     protected Button fNewButton;
314     protected Button fLoadButton;
315     protected Button fSaveButton;
316     
317     /**
318      * The ProfileManager, the model of this page.
319      */

320     protected final ProfileManager fProfileManager;
321     
322     /**
323      * The JavaPreview.
324      */

325     protected CompilationUnitPreview fJavaPreview;
326     private PixelConverter fPixConv;
327
328     
329     /**
330      * Create a new <code>CodeFormatterPreferencePage</code>.
331      */

332     public CodingStyleConfigurationBlock() {
333         List JavaDoc profiles= null;
334         try {
335             profiles= ProfileStore.readProfiles();
336         } catch (CoreException e) {
337             JavaPlugin.log(e);
338         }
339         
340         if (profiles == null)
341             profiles= new ArrayList JavaDoc();
342         
343         fProfileManager= new ProfileManager(profiles);
344
345         new StoreUpdater();
346     }
347
348     /**
349      * Create the contents
350      */

351     public Composite createContents(Composite parent) {
352
353         final int numColumns = 5;
354         
355         fPixConv = new PixelConverter(parent);
356         fComposite = createComposite(parent, numColumns, false);
357
358         fProfileCombo= createProfileCombo(fComposite, numColumns - 3, fPixConv.convertWidthInCharsToPixels(20));
359         fEditButton= createButton(fComposite, FormatterMessages.getString("CodingStyleConfigurationBlock.edit_button.desc"), GridData.HORIZONTAL_ALIGN_BEGINNING); //$NON-NLS-1$
360
fRenameButton= createButton(fComposite, FormatterMessages.getString("CodingStyleConfigurationBlock.rename_button.desc"), GridData.HORIZONTAL_ALIGN_BEGINNING); //$NON-NLS-1$
361
fDeleteButton= createButton(fComposite, FormatterMessages.getString("CodingStyleConfigurationBlock.remove_button.desc"), GridData.HORIZONTAL_ALIGN_BEGINNING); //$NON-NLS-1$
362

363         final Composite group= createComposite(fComposite, 4, false);
364         final GridData groupData= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
365         groupData.horizontalSpan= numColumns;
366         group.setLayoutData(groupData);
367
368         fNewButton= createButton(group, FormatterMessages.getString("CodingStyleConfigurationBlock.new_button.desc"), GridData.HORIZONTAL_ALIGN_BEGINNING); //$NON-NLS-1$
369
((GridData)createLabel(group, "", 1).getLayoutData()).grabExcessHorizontalSpace= true; //$NON-NLS-1$
370
fLoadButton= createButton(group, FormatterMessages.getString("CodingStyleConfigurationBlock.load_button.desc"), GridData.HORIZONTAL_ALIGN_END); //$NON-NLS-1$
371
fSaveButton= createButton(group, FormatterMessages.getString("CodingStyleConfigurationBlock.save_button.desc"), GridData.HORIZONTAL_ALIGN_END); //$NON-NLS-1$
372

373         createLabel(fComposite, FormatterMessages.getString("CodingStyleConfigurationBlock.preview_label.text"), numColumns); //$NON-NLS-1$
374
configurePreview(fComposite, numColumns);
375         
376         new ButtonController();
377         new ProfileComboController();
378         new PreviewController();
379         
380         return fComposite;
381     }
382
383     
384     private static Button createButton(Composite composite, String JavaDoc text, final int style) {
385         final Button button= new Button(composite, SWT.PUSH);
386         button.setText(text);
387
388         final GridData gd= new GridData(style);
389         gd.widthHint= SWTUtil.getButtonWidthHint(button);
390         gd.heightHint= SWTUtil.getButtonHeightHint(button);
391         button.setLayoutData(gd);
392         return button;
393     }
394     
395     private static Combo createProfileCombo(Composite composite, int span, int widthHint) {
396         final GridData gd = new GridData(GridData.FILL_HORIZONTAL);
397         gd.horizontalSpan = span;
398         gd.widthHint= widthHint;
399
400         final Combo combo= new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY );
401         combo.setLayoutData(gd);
402         return combo;
403     }
404     
405     private Label createLabel(Composite composite, String JavaDoc text, int numColumns) {
406         final GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
407         gd.horizontalSpan = numColumns;
408         gd.widthHint= 0;
409
410         final Label label = new Label(composite, SWT.WRAP);
411         label.setText(text);
412         label.setLayoutData(gd);
413         return label;
414     }
415     
416     private Composite createComposite(Composite parent, int numColumns, boolean margins) {
417         final Composite composite = new Composite(parent, SWT.NONE);
418         final GridLayout layout = new GridLayout(numColumns, false);
419         if (margins) {
420             layout.marginHeight= fPixConv.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
421             layout.marginWidth= fPixConv.convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
422         } else {
423             layout.marginHeight = 0;
424             layout.marginWidth = 0;
425         }
426         layout.horizontalSpacing= fPixConv.convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
427         layout.verticalSpacing= fPixConv.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
428         composite.setLayout(layout);
429         return composite;
430     }
431     
432     private void configurePreview(Composite composite, int numColumns) {
433         fJavaPreview= new CompilationUnitPreview(fProfileManager.getSelected().getSettings(), composite);
434         fJavaPreview.setPreviewText(PREVIEW);
435         
436         final GridData gd = new GridData(GridData.FILL_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL);
437         gd.horizontalSpan = numColumns;
438         gd.verticalSpan= 7;
439         gd.widthHint = 0;
440         gd.heightHint = 0;
441         fJavaPreview.getControl().setLayoutData(gd);
442     }
443
444     protected IPreferenceStore doGetPreferenceStore() {
445         return JavaPlugin.getDefault().getPreferenceStore();
446     }
447     
448     public void performOk() {
449         fProfileManager.commitChanges();
450     }
451     
452     public void performDefaults() {
453         Profile profile= fProfileManager.getProfile(ProfileManager.JAVA_PROFILE);
454         if (profile != null) {
455             int defaultIndex= fProfileManager.getSortedProfiles().indexOf(profile);
456             if (defaultIndex != -1) {
457                 fProfileCombo.select(defaultIndex);
458                 fProfileManager.setSelected(profile);
459             }
460         }
461     }
462     
463
464 }
465
Popular Tags