bd714b31667a1b747a013aad675520f784981305
[phpeclipse.git] /
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.ui.preferences;
6
7 import java.io.BufferedReader;
8 import java.io.IOException;
9 import java.io.InputStreamReader;
10 import java.util.ArrayList;
11 import java.util.Hashtable;
12
13 import net.sourceforge.phpdt.core.ICodeFormatter;
14 import net.sourceforge.phpdt.core.ToolFactory;
15 import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
16 import net.sourceforge.phpdt.internal.ui.dialog.StatusInfo;
17 import net.sourceforge.phpdt.internal.ui.dialog.StatusUtil;
18 import net.sourceforge.phpdt.internal.ui.util.TabFolderLayout;
19 import net.sourceforge.phpeclipse.PHPCore;
20 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
21 import net.sourceforge.phpeclipse.phpeditor.PHPSourceViewerConfiguration;
22
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.jface.preference.PreferencePage;
25 import org.eclipse.jface.resource.JFaceResources;
26 import org.eclipse.jface.text.Document;
27 import org.eclipse.jface.text.IDocument;
28 import org.eclipse.jface.text.source.SourceViewer;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.events.ModifyEvent;
31 import org.eclipse.swt.events.ModifyListener;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.events.SelectionListener;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Button;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Control;
39 import org.eclipse.swt.widgets.Label;
40 import org.eclipse.swt.widgets.TabFolder;
41 import org.eclipse.swt.widgets.TabItem;
42 import org.eclipse.swt.widgets.Text;
43 import org.eclipse.ui.IWorkbench;
44 import org.eclipse.ui.IWorkbenchPreferencePage;
45
46 /*
47  * The page for setting code formatter options
48  */
49 public class CodeFormatterPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
50
51         // Preference store keys, see PHPCore.getOptions
52         private static final String PREF_NEWLINE_OPENING_BRACES= PHPCore.FORMATTER_NEWLINE_OPENING_BRACE; 
53         private static final String PREF_NEWLINE_CONTROL_STATEMENT= PHPCore.FORMATTER_NEWLINE_CONTROL;
54         private static final String PREF_NEWLINE_CLEAR_ALL= PHPCore.FORMATTER_CLEAR_BLANK_LINES;
55         private static final String PREF_NEWLINE_ELSE_IF= PHPCore.FORMATTER_NEWLINE_ELSE_IF;
56         private static final String PREF_NEWLINE_EMPTY_BLOCK= PHPCore.FORMATTER_NEWLINE_EMPTY_BLOCK;
57         private static final String PREF_LINE_SPLIT= PHPCore.FORMATTER_LINE_SPLIT;
58         private static final String PREF_STYLE_COMPACT_ASSIGNEMENT= PHPCore.FORMATTER_COMPACT_ASSIGNMENT;
59         private static final String PREF_TAB_CHAR= PHPCore.FORMATTER_TAB_CHAR;
60         private static final String PREF_TAB_SIZE= PHPCore.FORMATTER_TAB_SIZE;
61
62         // values
63         private static final String INSERT=  PHPCore.INSERT;
64         private static final String DO_NOT_INSERT= PHPCore.DO_NOT_INSERT;
65         
66         private static final String COMPACT= PHPCore.COMPACT;
67         private static final String NORMAL= PHPCore.NORMAL;
68         
69         private static final String TAB= PHPCore.TAB;
70         private static final String SPACE= PHPCore.SPACE;
71         
72         private static final String CLEAR_ALL= PHPCore.CLEAR_ALL;
73         private static final String PRESERVE_ONE= PHPCore.PRESERVE_ONE;
74         
75
76         private static String[] getAllKeys() {
77                 return new String[] {
78                         PREF_NEWLINE_OPENING_BRACES, PREF_NEWLINE_CONTROL_STATEMENT, PREF_NEWLINE_CLEAR_ALL,
79                         PREF_NEWLINE_ELSE_IF, PREF_NEWLINE_EMPTY_BLOCK, PREF_LINE_SPLIT,
80                         PREF_STYLE_COMPACT_ASSIGNEMENT, PREF_TAB_CHAR, PREF_TAB_SIZE
81                 };      
82         }
83         
84         /**
85          * Gets the currently configured tab size
86          * @deprecated Inline to avoid reference to preference page
87          */
88         public static int getTabSize() {
89                 String string= (String) PHPCore.getOptions().get(PREF_TAB_SIZE);
90                 return getPositiveIntValue(string, 4);
91         }
92         
93         /**
94          * Gets the current compating assignement configuration
95          * @deprecated Inline to avoid reference to preference page
96          */     
97         public static boolean isCompactingAssignment() {
98                 return COMPACT.equals(PHPCore.getOptions().get(PREF_STYLE_COMPACT_ASSIGNEMENT));
99         }
100         
101         /**
102          * Gets the current compating assignement configuration
103          * @deprecated Inline to avoid reference to preference page
104          */     
105         public static boolean useSpaces() {
106                 return SPACE.equals(PHPCore.getOptions().get(PREF_TAB_CHAR));
107         }       
108         
109         
110         private static int getPositiveIntValue(String string, int dflt) {
111                 try {
112                         int i= Integer.parseInt(string);
113                         if (i >= 0) {
114                                 return i;
115                         }
116                 } catch (NumberFormatException e) {
117                 }
118                 return dflt;
119         }       
120                 
121         private static class ControlData {
122                 private String fKey;
123                 private String[] fValues;
124                 
125                 public ControlData(String key, String[] values) {
126                         fKey= key;
127                         fValues= values;
128                 }
129                 
130                 public String getKey() {
131                         return fKey;
132                 }
133                 
134                 public String getValue(boolean selection) {
135                         int index= selection ? 0 : 1;
136                         return fValues[index];
137                 }
138                 
139                 public String getValue(int index) {
140                         return fValues[index];
141                 }               
142                 
143                 public int getSelection(String value) {
144                         for (int i= 0; i < fValues.length; i++) {
145                                 if (value.equals(fValues[i])) {
146                                         return i;
147                                 }
148                         }
149                         throw new IllegalArgumentException();
150                 }
151         }
152         
153         private Hashtable fWorkingValues;
154
155         private ArrayList fCheckBoxes;
156         private ArrayList fTextBoxes;
157         
158         private SelectionListener fButtonSelectionListener;
159         private ModifyListener fTextModifyListener;
160         
161         private String fPreviewText;
162         private IDocument fPreviewDocument;
163         
164         private Text fTabSizeTextBox;
165  // private SourceViewer fSourceViewer;
166         
167
168         public CodeFormatterPreferencePage() {
169                 setPreferenceStore(PHPeclipsePlugin.getDefault().getPreferenceStore());         
170                 setDescription(PHPUIMessages.getString("CodeFormatterPreferencePage.description")); //$NON-NLS-1$
171         
172                 fWorkingValues= PHPCore.getOptions();
173                 fCheckBoxes= new ArrayList();
174                 fTextBoxes= new ArrayList();
175                 
176                 fButtonSelectionListener= new SelectionListener() {
177                         public void widgetDefaultSelected(SelectionEvent e) {}
178
179                         public void widgetSelected(SelectionEvent e) {
180                                 if (!e.widget.isDisposed()) {
181                                         controlChanged((Button) e.widget);
182                                 }
183                         }
184                 };
185                 
186                 fTextModifyListener= new ModifyListener() {
187                         public void modifyText(ModifyEvent e) {
188                                 if (!e.widget.isDisposed()) {
189                                         textChanged((Text) e.widget);
190                                 }
191                         }
192                 };
193                 
194                 fPreviewDocument= new Document();
195                 fPreviewText= loadPreviewFile("CodeFormatterPreviewCode.txt");  //$NON-NLS-1$   
196         }
197
198         /*
199          * @see IWorkbenchPreferencePage#init()
200          */     
201         public void init(IWorkbench workbench) {
202         }
203
204         /*
205          * @see PreferencePage#createControl(Composite)
206          */
207         public void createControl(Composite parent) {
208                 super.createControl(parent);
209 //              WorkbenchHelp.setHelp(getControl(), IJavaHelpContextIds.CODEFORMATTER_PREFERENCE_PAGE);
210         }       
211
212         /*
213          * @see PreferencePage#createContents(Composite)
214          */
215         protected Control createContents(Composite parent) {
216                 
217                 GridLayout layout= new GridLayout();
218                 layout.marginHeight= 0;
219                 layout.marginWidth= 0;
220                 
221                 Composite composite= new Composite(parent, SWT.NONE);
222                 composite.setLayout(layout);
223                                 
224                         
225                 TabFolder folder= new TabFolder(composite, SWT.NONE);
226                 folder.setLayout(new TabFolderLayout());        
227                 folder.setLayoutData(new GridData(GridData.FILL_BOTH));
228                 
229                 String[] insertNotInsert= new String[] { INSERT, DO_NOT_INSERT };
230                 
231                 layout= new GridLayout();
232                 layout.numColumns= 2;
233                 
234                 Composite newlineComposite= new Composite(folder, SWT.NULL);
235                 newlineComposite.setLayout(layout);
236
237                 String label= PHPUIMessages.getString("CodeFormatterPreferencePage.newline_opening_braces.label"); //$NON-NLS-1$
238                 addCheckBox(newlineComposite, label, PREF_NEWLINE_OPENING_BRACES, insertNotInsert);     
239                 
240                 label= PHPUIMessages.getString("CodeFormatterPreferencePage.newline_control_statement.label"); //$NON-NLS-1$
241                 addCheckBox(newlineComposite, label, PREF_NEWLINE_CONTROL_STATEMENT, insertNotInsert);  
242
243                 label= PHPUIMessages.getString("CodeFormatterPreferencePage.newline_clear_lines"); //$NON-NLS-1$
244                 addCheckBox(newlineComposite, label, PREF_NEWLINE_CLEAR_ALL, new String[] { CLEAR_ALL, PRESERVE_ONE } );        
245
246                 label= PHPUIMessages.getString("CodeFormatterPreferencePage.newline_else_if.label"); //$NON-NLS-1$
247                 addCheckBox(newlineComposite, label, PREF_NEWLINE_ELSE_IF, insertNotInsert);    
248
249                 label= PHPUIMessages.getString("CodeFormatterPreferencePage.newline_empty_block.label"); //$NON-NLS-1$
250                 addCheckBox(newlineComposite, label, PREF_NEWLINE_EMPTY_BLOCK, insertNotInsert);        
251                 
252                 layout= new GridLayout();
253                 layout.numColumns= 2;   
254                 
255                 Composite lineSplittingComposite= new Composite(folder, SWT.NULL);
256                 lineSplittingComposite.setLayout(layout);
257
258                 label= PHPUIMessages.getString("CodeFormatterPreferencePage.split_line.label"); //$NON-NLS-1$
259                 addTextField(lineSplittingComposite, label, PREF_LINE_SPLIT);
260
261                 layout= new GridLayout();
262                 layout.numColumns= 2;   
263                 
264                 Composite styleComposite= new Composite(folder, SWT.NULL);
265                 styleComposite.setLayout(layout);
266                 
267                 label= PHPUIMessages.getString("CodeFormatterPreferencePage.style_compact_assignement.label"); //$NON-NLS-1$
268                 addCheckBox(styleComposite, label, PREF_STYLE_COMPACT_ASSIGNEMENT, new String[] { COMPACT, NORMAL } );          
269
270                 label= PHPUIMessages.getString("CodeFormatterPreferencePage.tab_char.label"); //$NON-NLS-1$
271                 addCheckBox(styleComposite, label, PREF_TAB_CHAR, new String[] { TAB, SPACE } );                
272
273                 label= PHPUIMessages.getString("CodeFormatterPreferencePage.tab_size.label"); //$NON-NLS-1$
274                 fTabSizeTextBox= addTextField(styleComposite, label, PREF_TAB_SIZE);            
275
276                 TabItem item= new TabItem(folder, SWT.NONE);
277                 item.setText(PHPUIMessages.getString("CodeFormatterPreferencePage.tab.newline.tabtitle")); //$NON-NLS-1$
278                 item.setControl(newlineComposite);
279
280                 item= new TabItem(folder, SWT.NONE);
281                 item.setText(PHPUIMessages.getString("CodeFormatterPreferencePage.tab.linesplit.tabtitle")); //$NON-NLS-1$
282                 item.setControl(lineSplittingComposite);
283                 
284                 item= new TabItem(folder, SWT.NONE);
285                 item.setText(PHPUIMessages.getString("CodeFormatterPreferencePage.tab.style.tabtitle")); //$NON-NLS-1$
286                 item.setControl(styleComposite);                
287                 
288 //              fSourceViewer= createPreview(parent);
289                         
290                 updatePreview();
291                                         
292                 return composite;
293         }
294         
295 //      private SourceViewer createPreview(Composite parent) {
296 //              SourceViewer previewViewer= new SourceViewer(parent, null, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
297 //              JavaTextTools tools= JavaPlugin.getDefault().getJavaTextTools();
298 //              previewViewer.configure(new PHPSourceViewerConfiguration(tools, null));
299 //              previewViewer.getTextWidget().setFont(JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT));
300 //              previewViewer.getTextWidget().setTabs(getPositiveIntValue((String) fWorkingValues.get(PREF_TAB_SIZE), 0));
301 //              previewViewer.setEditable(false);
302 //              previewViewer.setDocument(fPreviewDocument);
303 //              Control control= previewViewer.getControl();
304 //              GridData gdata= new GridData(GridData.FILL_BOTH);
305 //              gdata.widthHint= convertWidthInCharsToPixels(30);
306 //              gdata.heightHint= convertHeightInCharsToPixels(5);
307 //              control.setLayoutData(gdata);
308 //              return previewViewer;
309 //      }
310
311         
312         private Button addCheckBox(Composite parent, String label, String key, String[] values) {
313                 ControlData data= new ControlData(key, values);
314                 
315                 GridData gd= new GridData(GridData.FILL_HORIZONTAL);
316                 gd.horizontalSpan= 2;
317                 
318                 Button checkBox= new Button(parent, SWT.CHECK);
319                 checkBox.setText(label);
320                 checkBox.setData(data);
321                 checkBox.setLayoutData(gd);
322                 
323                 String currValue= (String)fWorkingValues.get(key);      
324                 checkBox.setSelection(data.getSelection(currValue) == 0);
325                 checkBox.addSelectionListener(fButtonSelectionListener);
326                 
327                 fCheckBoxes.add(checkBox);
328                 return checkBox;
329         }
330         
331         private Text addTextField(Composite parent, String label, String key) { 
332                 Label labelControl= new Label(parent, SWT.NONE);
333                 labelControl.setText(label);
334                 labelControl.setLayoutData(new GridData());
335                                 
336                 Text textBox= new Text(parent, SWT.BORDER | SWT.SINGLE);
337                 textBox.setData(key);
338                 textBox.setLayoutData(new GridData());
339                 
340                 String currValue= (String)fWorkingValues.get(key);      
341                 textBox.setText(String.valueOf(getPositiveIntValue(currValue, 1)));
342                 textBox.setTextLimit(3);
343                 textBox.addModifyListener(fTextModifyListener);
344
345                 GridData gd= new GridData();
346                 gd.widthHint= convertWidthInCharsToPixels(5);
347                 textBox.setLayoutData(gd);
348
349                 fTextBoxes.add(textBox);
350                 return textBox;
351         }       
352         
353         private void controlChanged(Button button) {
354                 ControlData data= (ControlData) button.getData();
355                 boolean selection= button.getSelection();
356                 String newValue= data.getValue(selection);      
357                 fWorkingValues.put(data.getKey(), newValue);
358                 updatePreview();
359                 
360                 if (PREF_TAB_CHAR.equals(data.getKey())) {
361                         updateStatus(new StatusInfo());
362                         if (selection) {
363                                 fTabSizeTextBox.setText((String)fWorkingValues.get(PREF_TAB_SIZE));
364                         }
365                 }
366         }
367         
368         private void textChanged(Text textControl) {
369                 String key= (String) textControl.getData();
370                 String number= textControl.getText();
371                 IStatus status= validatePositiveNumber(number);
372                 if (!status.matches(IStatus.ERROR)) {
373                         fWorkingValues.put(key, number);
374                 }
375 //              if (PREF_TAB_SIZE.equals(key)) {
376 //                      fSourceViewer.getTextWidget().setTabs(getPositiveIntValue(number, 0));
377 //              }                       
378                 updateStatus(status);
379                 updatePreview();
380         }
381                 
382         
383         /*
384          * @see IPreferencePage#performOk()
385          */
386         public boolean performOk() {
387                 String[] allKeys= getAllKeys();
388                 // preserve other options
389                 // store in JCore
390                 Hashtable actualOptions= PHPCore.getOptions();
391                 for (int i= 0; i < allKeys.length; i++) {
392                         String key= allKeys[i];
393                         String val=  (String) fWorkingValues.get(key);
394                         actualOptions.put(key, val);
395                 }
396                 PHPCore.setOptions(actualOptions);
397                 PHPeclipsePlugin.getDefault().savePluginPreferences();
398                 return super.performOk();
399         }       
400         
401         /*
402          * @see PreferencePage#performDefaults()
403          */
404         protected void performDefaults() {
405                 fWorkingValues= PHPCore.getDefaultOptions();
406                 updateControls();
407                 super.performDefaults();
408         }
409
410         private String loadPreviewFile(String filename) {
411                 String separator= System.getProperty("line.separator"); //$NON-NLS-1$
412                 StringBuffer btxt= new StringBuffer(512);
413                 BufferedReader rin= null;
414                 try {
415                         rin= new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(filename)));
416                         String line;
417                         while ((line= rin.readLine()) != null) {
418                                 btxt.append(line);
419                                 btxt.append(separator);
420                         }
421                 } catch (IOException io) {
422                         PHPeclipsePlugin.log(io);
423                 } finally {
424                         if (rin != null) {
425                                 try { rin.close(); } catch (IOException e) {}
426                         }
427                 }
428                 return btxt.toString();
429         }
430
431
432         private void updatePreview() {
433                 ICodeFormatter formatter= ToolFactory.createDefaultCodeFormatter(fWorkingValues);
434                 fPreviewDocument.set(formatter.format(fPreviewText, 0, null, "\n")); //$NON-NLS-1$
435         }       
436         
437         private void updateControls() {
438                 // update the UI
439                 for (int i= fCheckBoxes.size() - 1; i >= 0; i--) {
440                         Button curr= (Button) fCheckBoxes.get(i);
441                         ControlData data= (ControlData) curr.getData();
442                                         
443                         String currValue= (String) fWorkingValues.get(data.getKey());   
444                         curr.setSelection(data.getSelection(currValue) == 0);                   
445                 }
446                 for (int i= fTextBoxes.size() - 1; i >= 0; i--) {
447                         Text curr= (Text) fTextBoxes.get(i);
448                         String key= (String) curr.getData();            
449                         String currValue= (String) fWorkingValues.get(key);
450                         curr.setText(currValue);
451                 }
452         }
453         
454         private IStatus validatePositiveNumber(String number) {
455                 StatusInfo status= new StatusInfo();
456                 if (number.length() == 0) {
457                         status.setError(PHPUIMessages.getString("CodeFormatterPreferencePage.empty_input")); //$NON-NLS-1$
458                 } else {
459                         try {
460                                 int value= Integer.parseInt(number);
461                                 if (value < 0) {
462                                         status.setError(PHPUIMessages.getFormattedString("CodeFormatterPreferencePage.invalid_input", number)); //$NON-NLS-1$
463                                 }
464                         } catch (NumberFormatException e) {
465                                 status.setError(PHPUIMessages.getFormattedString("CodeFormatterPreferencePage.invalid_input", number)); //$NON-NLS-1$
466                         }
467                 }
468                 return status;
469         }
470                         
471         
472         private void updateStatus(IStatus status) {
473                 if (!status.matches(IStatus.ERROR)) {
474                         // look if there are more severe errors
475                         for (int i= 0; i < fTextBoxes.size(); i++) {
476                                 Text curr= (Text) fTextBoxes.get(i);
477                                 if (!(curr == fTabSizeTextBox && usesTabs())) {
478                                         IStatus currStatus= validatePositiveNumber(curr.getText());
479                                         status= StatusUtil.getMoreSevere(currStatus, status);
480                                 }
481                         }
482                 }       
483                 setValid(!status.matches(IStatus.ERROR));
484                 StatusUtil.applyToStatusLine(this, status);
485         }
486         
487         private boolean usesTabs() {
488                 return TAB.equals(fWorkingValues.get(PREF_TAB_CHAR));
489         }
490                 
491
492 }
493
494