• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ide.eclipse.adt.internal.preferences;
18 
19 import static com.android.ide.common.xml.XmlAttributeSortOrder.ALPHABETICAL;
20 import static com.android.ide.common.xml.XmlAttributeSortOrder.LOGICAL;
21 import static com.android.ide.common.xml.XmlAttributeSortOrder.NO_SORTING;
22 
23 import com.android.ide.eclipse.adt.AdtPlugin;
24 import com.android.sdkuilib.internal.widgets.ResolutionChooserDialog;
25 
26 import org.eclipse.jface.preference.BooleanFieldEditor;
27 import org.eclipse.jface.preference.FieldEditorPreferencePage;
28 import org.eclipse.jface.preference.RadioGroupFieldEditor;
29 import org.eclipse.jface.preference.StringButtonFieldEditor;
30 import org.eclipse.jface.window.Window;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.ui.IWorkbench;
36 import org.eclipse.ui.IWorkbenchPreferencePage;
37 
38 /**
39  * Preference page for the editors.
40  */
41 public class EditorsPage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
42     private BooleanFieldEditor mIndentEditor;
43     private BooleanFieldEditor mRemoveEmptyEditor;
44     private BooleanFieldEditor mOneAttrPerLineEditor;
45     private BooleanFieldEditor mSpaceBeforeCloseEditor;
46     private BooleanFieldEditor mFormatGuiXmlEditor;
47 
48     /**
49      * Constructs a new Android editors preference page
50      */
EditorsPage()51     public EditorsPage() {
52         super(GRID);
53         setPreferenceStore(AdtPlugin.getDefault().getPreferenceStore());
54     }
55 
56     @Override
init(IWorkbench workbench)57     public void init(IWorkbench workbench) {
58         // pass
59     }
60 
61     @Override
createFieldEditors()62     protected void createFieldEditors() {
63         Composite parent = getFieldEditorParent();
64 
65         addField(new DensityFieldEditor(AdtPrefs.PREFS_MONITOR_DENSITY,
66                 "Monitor Density", parent));
67 
68         final MyBooleanFieldEditor editor = new MyBooleanFieldEditor(
69                 AdtPrefs.PREFS_USE_CUSTOM_XML_FORMATTER,
70                 "Format XML files using the standard Android XML style rather than the \n" +
71                 "configured Eclipse XML style (additional options below)",
72                 parent);
73         addField(editor);
74 
75         // Add a listener which fires whenever the checkbox for the custom formatter
76         // is toggled -- this will be used to enable/disable the formatting related options
77         // on the page. To do this we subclass the BooleanFieldEditor to make the protected
78         // method getChangeControl public (so we can access it and add a listener on it).
79         // This is pretty ugly but I found several posts in the Eclipse forums asking
80         // how to do it and they were all unanswered. (No, calling setPropertyChangeListener
81         // does not work.)
82         Button checkbox = editor.getChangeControl(parent);
83         checkbox.addSelectionListener(new SelectionAdapter() {
84             @Override
85             public void widgetSelected(SelectionEvent e) {
86                 updateCustomFormattingOptions(editor.getBooleanValue());
87             }
88         });
89 
90         mIndentEditor = new BooleanFieldEditor(AdtPrefs.PREFS_USE_ECLIPSE_INDENT,
91                 "Use Eclipse setting for indentation width and space or tab character "
92                 + "indentation \n(Android default is 4 space characters)",
93                 parent);
94         addField(mIndentEditor);
95 
96         mRemoveEmptyEditor = new BooleanFieldEditor(AdtPrefs.PREVS_REMOVE_EMPTY_LINES,
97                 "Always remove empty lines between elements",
98                 parent);
99         addField(mRemoveEmptyEditor);
100 
101         mOneAttrPerLineEditor = new BooleanFieldEditor(AdtPrefs.PREFS_ONE_ATTR_PER_LINE,
102                 "Allow single attributes to appear on the same line as their elements",
103                 parent);
104         addField(mOneAttrPerLineEditor);
105 
106         mSpaceBeforeCloseEditor = new BooleanFieldEditor(AdtPrefs.PREFS_SPACE_BEFORE_CLOSE,
107                 "Add a space before the > or /> in opening tags",
108                 parent);
109         addField(mSpaceBeforeCloseEditor);
110 
111         addField(new RadioGroupFieldEditor(AdtPrefs.PREFS_ATTRIBUTE_SORT,
112                 "Sort Attributes", 1,
113                 new String[][] {
114                     { "&Logical (id, style, layout attributes, remaining attributes alphabetically)",
115                         LOGICAL.key },
116                     { "&Alphabetical", ALPHABETICAL.key },
117                     { "&None", NO_SORTING.key },
118                 },
119                 parent, true));
120 
121         mFormatGuiXmlEditor = new BooleanFieldEditor(AdtPrefs.PREFS_FORMAT_GUI_XML,
122                 "Automatically format the XML edited by the visual layout editor",
123                 parent);
124         addField(mFormatGuiXmlEditor);
125 
126         addField(new BooleanFieldEditor(AdtPrefs.PREFS_FORMAT_ON_SAVE,
127                 "Format on Save",
128                 parent));
129 
130         addField(new BooleanFieldEditor(AdtPrefs.PREFS_SHARED_LAYOUT_EDITOR,
131                 "Use a single layout editor for all configuration variations of a layout",
132                 parent));
133 
134         boolean enabled = getPreferenceStore().getBoolean(AdtPrefs.PREFS_USE_CUSTOM_XML_FORMATTER);
135         updateCustomFormattingOptions(enabled);
136     }
137 
updateCustomFormattingOptions(boolean enabled)138     private void updateCustomFormattingOptions(boolean enabled) {
139         Composite parent = getFieldEditorParent();
140         mIndentEditor.setEnabled(enabled, parent);
141         mRemoveEmptyEditor.setEnabled(enabled, parent);
142         mOneAttrPerLineEditor.setEnabled(enabled, parent);
143         mSpaceBeforeCloseEditor.setEnabled(enabled, parent);
144         mFormatGuiXmlEditor.setEnabled(enabled, parent);
145     }
146 
147     /**
148      * Overridden solely so that I can get access to the checkbox button to listen to
149      * state changes
150      */
151     private class MyBooleanFieldEditor extends BooleanFieldEditor {
MyBooleanFieldEditor(String name, String label, Composite parent)152         public MyBooleanFieldEditor(String name, String label, Composite parent) {
153             super(name, label, parent);
154         }
155         @Override
getChangeControl(Composite parent)156         protected Button getChangeControl(Composite parent) {
157             return super.getChangeControl(parent);
158         }
159     }
160 
161     /**
162      * Custom {@link StringButtonFieldEditor} to call out to {@link ResolutionChooserDialog}
163      * when the button is called.
164      */
165     private static class DensityFieldEditor extends StringButtonFieldEditor {
166 
DensityFieldEditor(String name, String labelText, Composite parent)167         public DensityFieldEditor(String name, String labelText, Composite parent) {
168             super(name, labelText, parent);
169             setChangeButtonText("Compute...");
170         }
171 
172         @Override
changePressed()173         protected String changePressed() {
174             ResolutionChooserDialog dialog = new ResolutionChooserDialog(getShell());
175             if (dialog.open() == Window.OK) {
176                 return Integer.toString(dialog.getDensity());
177             }
178 
179             return null;
180         }
181     }
182 }
183