]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/winterwell.markdown/src/winterwell/markdown/preferences/MarkdownPreferencePage.java
Tycho compilation changes for SVN version also.
[simantics/platform.git] / bundles / winterwell.markdown / src / winterwell / markdown / preferences / MarkdownPreferencePage.java
1 package winterwell.markdown.preferences;
2
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;
14
15 import winterwell.markdown.Activator;
16
17 /**
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.
24  * <p>
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.
29  */
30
31 public class MarkdownPreferencePage
32         extends FieldEditorPreferencePage
33         implements IWorkbenchPreferencePage {
34
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";
40
41         public static final String PREF_MARKDOWN_COMMAND = "Pref_Markdown_Command";
42         private static final String MARKDOWNJ = "(use built-in MarkdownJ converter)";
43         
44
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";
51         
52         public static final String PREF_GITHUB_SYNTAX = "Pref_Github_Syntax";
53         public static final String PREF_MULTIMARKDOWN_METADATA = "Pref_MultiMarkdown_Metadata";
54         
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);
61         
62         public MarkdownPreferencePage() {
63                 super(GRID);
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.");
68         }
69         
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);
79                 
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);
86         }
87         
88         /**
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
92          * restore itself.
93          */
94         @Override
95         public void createFieldEditors() {
96                 // Word wrap
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());
102                 addField(fd);
103                 // Task tags
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());
108                 addField(fd);           
109                 StringFieldEditor tags = new StringFieldEditor(PREF_TASK_TAGS_DEFINED,
110                                 "Task tags\nComma separated list of recognised task tags.", getFieldEditorParent());
111                 addField(tags);
112                 // Code folding
113                 fd = new BooleanFieldEditor(PREF_FOLDING,
114                                 "Document folding, a.k.a. outline support",
115                                 getFieldEditorParent());
116                 addField(fd);
117                 // Command line         
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);
124 //                      }                       
125 //              });
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());             
130                 addField(cmd);
131                 
132                 ColorFieldEditor def = new ColorFieldEditor(PREF_DEFUALT, "Default text", getFieldEditorParent());
133                 addField(def);
134                 
135                 ColorFieldEditor com = new ColorFieldEditor(PREF_COMMENT, "Comment", getFieldEditorParent());
136                 addField(com);
137                 
138                 ColorFieldEditor link = new ColorFieldEditor(PREF_LINK, "Link", getFieldEditorParent());
139                 addField(link);
140                 
141                 ColorFieldEditor head = new ColorFieldEditor(PREF_HEADER, "Header and List indicator", getFieldEditorParent());
142                 addField(head);
143                 
144                 ColorFieldEditor code = new ColorFieldEditor(PREF_CODE, "Code", getFieldEditorParent());
145                 addField(code);
146
147                 ColorFieldEditor codeBg = new ColorFieldEditor(PREF_CODE_BG, "Code Background", getFieldEditorParent());
148                 addField(codeBg);
149                 
150                 /*
151                  * Fields for the preview window
152                  */
153
154                 // Github Syntax support
155                 fd = new BooleanFieldEditor(PREF_GITHUB_SYNTAX,
156                                 "Support Github Syntax",
157                                 getFieldEditorParent());
158                 addField(fd);
159
160                 // Multi-Markdown support
161                 fd = new BooleanFieldEditor(PREF_MULTIMARKDOWN_METADATA,
162                                 "Support Multi-Markdown Metadata",
163                                 getFieldEditorParent());
164                 addField(fd);
165         }
166
167         /* (non-Javadoc)
168          * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
169          */
170         public void init(IWorkbench workbench) {
171                 
172         }
173
174         public static boolean wordWrap() {
175                 IPreferenceStore pStore = Activator.getDefault().getPreferenceStore();
176                 if (! pStore.contains(MarkdownPreferencePage.PREF_WORD_WRAP)) {
177                         return false;
178                 }
179                 return pStore.getBoolean(MarkdownPreferencePage.PREF_WORD_WRAP);                
180         }
181         
182 }
183
184 abstract class DummyField extends FieldEditor {
185         @Override
186         protected void adjustForNumColumns(int numColumns) {
187                 // do nothing
188         }
189         @Override
190         protected void doFillIntoGrid(Composite parent, int numColumns) {
191                 makeComponent(parent);
192         }
193         abstract protected void makeComponent(Composite parent);
194         
195         @Override
196         protected void doLoad() {
197                 // 
198         }
199         @Override
200         protected void doLoadDefault() {
201                 // 
202         }
203
204         @Override
205         protected void doStore() {
206                 // 
207         }
208
209         @Override
210         public int getNumberOfControls() {
211                 return 1;
212         }
213         
214 }