KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > SmartTypingConfigurationBlock


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
12 package org.eclipse.jdt.internal.ui.preferences;
13
14 import org.eclipse.core.runtime.Assert;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.DisposeListener;
18 import org.eclipse.swt.events.SelectionAdapter;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.graphics.Point;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.swt.widgets.Link;
27
28 import org.eclipse.jface.dialogs.Dialog;
29 import org.eclipse.jface.preference.IPreferenceStore;
30 import org.eclipse.jface.util.IPropertyChangeListener;
31 import org.eclipse.jface.util.PropertyChangeEvent;
32
33 import org.eclipse.ui.dialogs.PreferencesUtil;
34
35 import org.eclipse.jdt.core.JavaCore;
36 import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
37
38 import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil;
39 import org.eclipse.jdt.internal.corext.util.Messages;
40
41 import org.eclipse.jdt.ui.PreferenceConstants;
42
43 import org.eclipse.jdt.internal.ui.JavaPlugin;
44
45 /**
46  * Configures Java Editor typing preferences.
47  *
48  * @since 3.1
49  */

50 class SmartTypingConfigurationBlock extends AbstractConfigurationBlock {
51
52     public SmartTypingConfigurationBlock(OverlayPreferenceStore store) {
53         super(store);
54         
55         store.addKeys(createOverlayStoreKeys());
56     }
57     
58     private OverlayPreferenceStore.OverlayKey[] createOverlayStoreKeys() {
59         
60         return new OverlayPreferenceStore.OverlayKey[] {
61                 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_SMART_PASTE),
62                 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_IMPORTS_ON_PASTE),
63                 
64                 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_CLOSE_STRINGS),
65                 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_CLOSE_BRACKETS),
66                 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_CLOSE_BRACES),
67                 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_CLOSE_JAVADOCS),
68                 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_WRAP_STRINGS),
69                 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_ESCAPE_STRINGS),
70                 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_ADD_JAVADOC_TAGS),
71                 
72                 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_SMART_SEMICOLON),
73                 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_SMART_TAB),
74                 new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_SMART_OPENING_BRACE),
75         };
76     }
77
78     /**
79      * Creates page for mark occurrences preferences.
80      *
81      * @param parent the parent composite
82      * @return the control for the preference page
83      */

84     public Control createControl(Composite parent) {
85         ScrolledPageContent scrolled= new ScrolledPageContent(parent, SWT.H_SCROLL | SWT.V_SCROLL);
86         scrolled.setExpandHorizontal(true);
87         scrolled.setExpandVertical(true);
88         
89         Composite control= new Composite(scrolled, SWT.NONE);
90         GridLayout layout= new GridLayout();
91         layout.marginWidth= 0;
92         layout.marginHeight= 0;
93         control.setLayout(layout);
94
95         Composite composite;
96         
97         composite= createSubsection(control, null, PreferencesMessages.SmartTypingConfigurationBlock_autoclose_title);
98         addAutoclosingSection(composite);
99         
100         composite= createSubsection(control, null, PreferencesMessages.SmartTypingConfigurationBlock_automove_title);
101         addAutopositionSection(composite);
102         
103         composite= createSubsection(control, null, PreferencesMessages.SmartTypingConfigurationBlock_tabs_title);
104         addTabSection(composite);
105
106         composite= createSubsection(control, null, PreferencesMessages.SmartTypingConfigurationBlock_pasting_title);
107         addPasteSection(composite);
108         
109         composite= createSubsection(control, null, PreferencesMessages.SmartTypingConfigurationBlock_strings_title);
110         addStringsSection(composite);
111
112         scrolled.setContent(control);
113         final Point size= control.computeSize(SWT.DEFAULT, SWT.DEFAULT);
114         scrolled.setMinSize(size.x, size.y);
115         return scrolled;
116     }
117
118     private void addStringsSection(Composite composite) {
119         GridLayout layout= new GridLayout();
120         composite.setLayout(layout);
121
122         String JavaDoc label;
123         Button master, slave;
124         label= PreferencesMessages.JavaEditorPreferencePage_wrapStrings;
125         master= addCheckBox(composite, label, PreferenceConstants.EDITOR_WRAP_STRINGS, 0);
126         
127         label= PreferencesMessages.JavaEditorPreferencePage_escapeStrings;
128         slave= addCheckBox(composite, label, PreferenceConstants.EDITOR_ESCAPE_STRINGS, 0);
129         createDependency(master, slave);
130     }
131
132     private void addPasteSection(Composite composite) {
133         GridLayout layout= new GridLayout();
134         composite.setLayout(layout);
135
136         String JavaDoc label;
137         label= PreferencesMessages.JavaEditorPreferencePage_smartPaste;
138         addCheckBox(composite, label, PreferenceConstants.EDITOR_SMART_PASTE, 0);
139
140         label= PreferencesMessages.JavaEditorPreferencePage_importsOnPaste;
141         addCheckBox(composite, label, PreferenceConstants.EDITOR_IMPORTS_ON_PASTE, 0);
142     }
143
144     private void addTabSection(Composite composite) {
145         GridLayout layout= new GridLayout();
146         composite.setLayout(layout);
147
148         String JavaDoc label;
149         label= PreferencesMessages.JavaEditorPreferencePage_typing_smartTab;
150         addCheckBox(composite, label, PreferenceConstants.EDITOR_SMART_TAB, 0);
151         
152         createMessage(composite);
153     }
154
155     private void addAutopositionSection(Composite composite) {
156         
157         GridLayout layout= new GridLayout();
158         composite.setLayout(layout);
159
160         String JavaDoc label;
161         
162         label= PreferencesMessages.JavaEditorPreferencePage_typing_smartSemicolon;
163         addCheckBox(composite, label, PreferenceConstants.EDITOR_SMART_SEMICOLON, 0);
164         
165         label= PreferencesMessages.JavaEditorPreferencePage_typing_smartOpeningBrace;
166         addCheckBox(composite, label, PreferenceConstants.EDITOR_SMART_OPENING_BRACE, 0);
167     }
168
169     private void addAutoclosingSection(Composite composite) {
170         
171         GridLayout layout= new GridLayout();
172         layout.numColumns= 1;
173         composite.setLayout(layout);
174
175         String JavaDoc label;
176         Button master, slave;
177
178         label= PreferencesMessages.JavaEditorPreferencePage_closeStrings;
179         addCheckBox(composite, label, PreferenceConstants.EDITOR_CLOSE_STRINGS, 0);
180
181         label= PreferencesMessages.JavaEditorPreferencePage_closeBrackets;
182         addCheckBox(composite, label, PreferenceConstants.EDITOR_CLOSE_BRACKETS, 0);
183
184         label= PreferencesMessages.JavaEditorPreferencePage_closeBraces;
185         addCheckBox(composite, label, PreferenceConstants.EDITOR_CLOSE_BRACES, 0);
186
187         label= PreferencesMessages.JavaEditorPreferencePage_closeJavaDocs;
188         master= addCheckBox(composite, label, PreferenceConstants.EDITOR_CLOSE_JAVADOCS, 0);
189
190         label= PreferencesMessages.JavaEditorPreferencePage_addJavaDocTags;
191         slave= addCheckBox(composite, label, PreferenceConstants.EDITOR_ADD_JAVADOC_TAGS, 0);
192         createDependency(master, slave);
193     }
194     
195     private void createMessage(final Composite composite) {
196         // TODO create a link with an argument, so the formatter preference page can open the
197
// current profile automatically.
198
String JavaDoc linkTooltip= PreferencesMessages.SmartTypingConfigurationBlock_tabs_message_tooltip;
199         String JavaDoc text;
200         String JavaDoc indentMode= JavaPlugin.getDefault().getCombinedPreferenceStore().getString(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR);
201         if (JavaCore.TAB.equals(indentMode))
202             text= Messages.format(PreferencesMessages.SmartTypingConfigurationBlock_tabs_message_tab_text, new String JavaDoc[] {Integer.toString(getTabDisplaySize())});
203         else
204             text= Messages.format(PreferencesMessages.SmartTypingConfigurationBlock_tabs_message_others_text, new String JavaDoc[] {Integer.toString(getTabDisplaySize()), Integer.toString(getIndentSize()), getIndentMode()});
205         
206         final Link link= new Link(composite, SWT.NONE);
207         link.setText(text);
208         link.setToolTipText(linkTooltip);
209         GridData gd= new GridData(SWT.FILL, SWT.BEGINNING, true, false);
210         gd.widthHint= 300; // don't get wider initially
211
link.setLayoutData(gd);
212         link.addSelectionListener(new SelectionAdapter() {
213             public void widgetSelected(SelectionEvent e) {
214                 PreferencesUtil.createPreferenceDialogOn(link.getShell(), "org.eclipse.jdt.ui.preferences.CodeFormatterPreferencePage", null, null); //$NON-NLS-1$
215
}
216         });
217         
218         final IPreferenceStore combinedStore= JavaPlugin.getDefault().getCombinedPreferenceStore();
219         final IPropertyChangeListener propertyChangeListener= new IPropertyChangeListener() {
220             private boolean fHasRun= false;
221             public void propertyChange(PropertyChangeEvent event) {
222                 if (fHasRun)
223                     return;
224                 if (composite.isDisposed())
225                     return;
226                 String JavaDoc property= event.getProperty();
227                 if (DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR.equals(property)
228                         || DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE.equals(property)) {
229                     fHasRun= true;
230                     link.dispose();
231                     createMessage(composite);
232                     Dialog.applyDialogFont(composite);
233                     composite.redraw();
234                     composite.layout();
235                 }
236             }
237         };
238         combinedStore.addPropertyChangeListener(propertyChangeListener);
239         link.addDisposeListener(new DisposeListener() {
240                 public void widgetDisposed(org.eclipse.swt.events.DisposeEvent e) {
241                     combinedStore.removePropertyChangeListener(propertyChangeListener);
242                 }
243         });
244     }
245
246     private String JavaDoc getIndentMode() {
247         String JavaDoc indentMode= JavaPlugin.getDefault().getCombinedPreferenceStore().getString(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR);
248         
249         if (JavaCore.SPACE.equals(indentMode))
250             return PreferencesMessages.SmartTypingConfigurationBlock_tabs_message_spaces;
251         
252         if (JavaCore.TAB.equals(indentMode))
253             return PreferencesMessages.SmartTypingConfigurationBlock_tabs_message_tabs;
254         
255         if (DefaultCodeFormatterConstants.MIXED.equals(indentMode))
256             return PreferencesMessages.SmartTypingConfigurationBlock_tabs_message_tabsAndSpaces;
257
258         Assert.isTrue(false, "Illegal indent mode - must not happen"); //$NON-NLS-1$
259
return null;
260     }
261
262     private int getIndentSize() {
263         return CodeFormatterUtil.getIndentWidth(null);
264     }
265     
266     private int getTabDisplaySize() {
267         return CodeFormatterUtil.getTabWidth(null);
268     }
269
270 }
271
Popular Tags