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