1 package winterwell.markdown.preferences;
3 import org.eclipse.jface.preference.BooleanFieldEditor;
4 import org.eclipse.jface.preference.ColorFieldEditor;
5 import org.eclipse.jface.preference.FieldEditor;
6 import org.eclipse.jface.preference.FieldEditorPreferencePage;
7 import org.eclipse.jface.preference.IPreferenceStore;
8 import org.eclipse.jface.preference.PreferenceConverter;
9 import org.eclipse.jface.preference.StringFieldEditor;
10 import org.eclipse.swt.graphics.RGB;
11 import org.eclipse.swt.widgets.Composite;
12 import org.eclipse.ui.IWorkbench;
13 import org.eclipse.ui.IWorkbenchPreferencePage;
15 import winterwell.markdown.Activator;
18 * This class represents a preference page that
19 * is contributed to the Preferences dialog. By
20 * subclassing <samp>FieldEditorPreferencePage</samp>, we
21 * can use the field support built into JFace that allows
22 * us to create a page that is small and knows how to
23 * save, restore and apply itself.
25 * This page is used to modify preferences only. They
26 * are stored in the preference store that belongs to
27 * the main plug-in class. That way, preferences can
28 * be accessed directly via the preference store.
31 public class MarkdownPreferencePage
32 extends FieldEditorPreferencePage
33 implements IWorkbenchPreferencePage {
35 public static final String PREF_FOLDING = "Pref_Folding";
36 public static final String PREF_WORD_WRAP = "Pref_WordWrap";
37 public static final String PREF_TASK_TAGS = "Pref_TaskTagsOn";
38 public static final String PREF_TASK_TAGS_DEFINED = "Pref_TaskTags";
39 public static final String PREF_SECTION_NUMBERS = "Pref_SectionNumbers";
41 public static final String PREF_MARKDOWN_COMMAND = "Pref_Markdown_Command";
42 private static final String MARKDOWNJ = "(use built-in MarkdownJ converter)";
45 public static final String PREF_DEFUALT = "Pref_Default";
46 public static final String PREF_COMMENT = "Pref_Comment";
47 public static final String PREF_HEADER = "Pref_Header";
48 public static final String PREF_LINK = "Pref_Link";
49 public static final String PREF_CODE = "Pref_Code";
50 public static final String PREF_CODE_BG = "Pref_Code_Background";
52 public static final String PREF_GITHUB_SYNTAX = "Pref_Github_Syntax";
53 public static final String PREF_MULTIMARKDOWN_METADATA = "Pref_MultiMarkdown_Metadata";
55 private static final RGB DEF_DEFAULT = new RGB(0, 0, 0);
56 private static final RGB DEF_COMMENT = new RGB(128, 0, 0);
57 private static final RGB DEF_HEADER = new RGB(0, 128, 0);
58 private static final RGB DEF_LINK = new RGB(106, 131, 199);
59 private static final RGB DEF_CODE = new RGB(0, 0, 0);
60 private static final RGB DEF_CODE_BG = new RGB(244,244,244);
62 public MarkdownPreferencePage() {
64 IPreferenceStore pStore = Activator.getDefault().getPreferenceStore();
65 setDefaultPreferences(pStore);
66 setPreferenceStore(pStore);
67 setDescription("Settings for the Markdown text editor. See also the general text editor preferences.");
70 public static void setDefaultPreferences(IPreferenceStore pStore) {
71 pStore.setDefault(PREF_WORD_WRAP, false);
72 pStore.setDefault(PREF_FOLDING, true);
73 pStore.setDefault(PREF_TASK_TAGS, true);
74 pStore.setDefault(PREF_TASK_TAGS_DEFINED, "TODO,FIXME,??");
75 pStore.setDefault(PREF_MARKDOWN_COMMAND, MARKDOWNJ);
76 pStore.setDefault(PREF_SECTION_NUMBERS, true);
77 pStore.setDefault(PREF_GITHUB_SYNTAX, true);
78 pStore.setDefault(PREF_MULTIMARKDOWN_METADATA, false);
80 PreferenceConverter.setDefault(pStore, PREF_DEFUALT, DEF_DEFAULT);
81 PreferenceConverter.setDefault(pStore, PREF_COMMENT, DEF_COMMENT);
82 PreferenceConverter.setDefault(pStore, PREF_HEADER, DEF_HEADER);
83 PreferenceConverter.setDefault(pStore, PREF_LINK, DEF_LINK);
84 PreferenceConverter.setDefault(pStore, PREF_CODE, DEF_CODE);
85 PreferenceConverter.setDefault(pStore, PREF_CODE_BG, DEF_CODE_BG);
89 * Creates the field editors. Field editors are abstractions of
90 * the common GUI blocks needed to manipulate various types
91 * of preferences. Each field editor knows how to save and
95 public void createFieldEditors() {
97 BooleanFieldEditor fd = new BooleanFieldEditor(PREF_WORD_WRAP,
98 "Soft word wrapping \r\n"
99 +"Note: may cause line numbers and related \r\n" +
100 "functionality to act a bit strangely",
101 getFieldEditorParent());
104 fd = new BooleanFieldEditor(PREF_TASK_TAGS,
105 "Manage tasks using task tags \r\n" +
106 "If true, this will add and delete tags in sync with edits.",
107 getFieldEditorParent());
109 StringFieldEditor tags = new StringFieldEditor(PREF_TASK_TAGS_DEFINED,
110 "Task tags\nComma separated list of recognised task tags.", getFieldEditorParent());
113 fd = new BooleanFieldEditor(PREF_FOLDING,
114 "Document folding, a.k.a. outline support",
115 getFieldEditorParent());
118 // addField(new DummyField() {
119 // protected void makeComponent(Composite parent) {
120 // Label label = new Label(parent, 0);
121 // label.setText("Hello!");
122 // GridData gd = new GridData(100, 20);
123 // label.setLayoutData(gd);
126 StringFieldEditor cmd = new StringFieldEditor(PREF_MARKDOWN_COMMAND,
127 "UNSTABLE: Command-line to run Markdown.\r\n" +
128 "This should take in a file and output to std-out.\n" +
129 "Leave blank to use the built-in Java converter.", getFieldEditorParent());
132 ColorFieldEditor def = new ColorFieldEditor(PREF_DEFUALT, "Default text", getFieldEditorParent());
135 ColorFieldEditor com = new ColorFieldEditor(PREF_COMMENT, "Comment", getFieldEditorParent());
138 ColorFieldEditor link = new ColorFieldEditor(PREF_LINK, "Link", getFieldEditorParent());
141 ColorFieldEditor head = new ColorFieldEditor(PREF_HEADER, "Header and List indicator", getFieldEditorParent());
144 ColorFieldEditor code = new ColorFieldEditor(PREF_CODE, "Code", getFieldEditorParent());
147 ColorFieldEditor codeBg = new ColorFieldEditor(PREF_CODE_BG, "Code Background", getFieldEditorParent());
151 * Fields for the preview window
154 // Github Syntax support
155 fd = new BooleanFieldEditor(PREF_GITHUB_SYNTAX,
156 "Support Github Syntax",
157 getFieldEditorParent());
160 // Multi-Markdown support
161 fd = new BooleanFieldEditor(PREF_MULTIMARKDOWN_METADATA,
162 "Support Multi-Markdown Metadata",
163 getFieldEditorParent());
168 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
170 public void init(IWorkbench workbench) {
174 public static boolean wordWrap() {
175 IPreferenceStore pStore = Activator.getDefault().getPreferenceStore();
176 if (! pStore.contains(MarkdownPreferencePage.PREF_WORD_WRAP)) {
179 return pStore.getBoolean(MarkdownPreferencePage.PREF_WORD_WRAP);
184 abstract class DummyField extends FieldEditor {
186 protected void adjustForNumColumns(int numColumns) {
190 protected void doFillIntoGrid(Composite parent, int numColumns) {
191 makeComponent(parent);
193 abstract protected void makeComponent(Composite parent);
196 protected void doLoad() {
200 protected void doLoadDefault() {
205 protected void doStore() {
210 public int getNumberOfControls() {