2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.ui.preferences;
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;
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;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.jface.preference.PreferencePage;
24 import org.eclipse.jface.text.Document;
25 import org.eclipse.jface.text.IDocument;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.ModifyEvent;
28 import org.eclipse.swt.events.ModifyListener;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.events.SelectionListener;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.swt.widgets.Label;
37 import org.eclipse.swt.widgets.TabFolder;
38 import org.eclipse.swt.widgets.TabItem;
39 import org.eclipse.swt.widgets.Text;
40 import org.eclipse.ui.IWorkbench;
41 import org.eclipse.ui.IWorkbenchPreferencePage;
44 * The page for setting code formatter options
46 public class CodeFormatterPreferencePage extends PreferencePage implements
47 IWorkbenchPreferencePage {
49 // Preference store keys, see PHPCore.getOptions
50 private static final String PREF_NEWLINE_OPENING_BRACES = JavaCore.FORMATTER_NEWLINE_OPENING_BRACE;
52 private static final String PREF_NEWLINE_CONTROL_STATEMENT = JavaCore.FORMATTER_NEWLINE_CONTROL;
54 private static final String PREF_NEWLINE_CLEAR_ALL = JavaCore.FORMATTER_CLEAR_BLANK_LINES;
56 // private static final String PREF_NEWLINE_ELSE_IF=
57 // PHPCore.FORMATTER_NEWLINE_ELSE_IF;
58 private static final String PREF_NEWLINE_EMPTY_BLOCK = JavaCore.FORMATTER_NEWLINE_EMPTY_BLOCK;
60 private static final String PREF_LINE_SPLIT = JavaCore.FORMATTER_LINE_SPLIT;
62 private static final String PREF_STYLE_COMPACT_ASSIGNEMENT = JavaCore.FORMATTER_COMPACT_ASSIGNMENT;
64 private static final String PREF_STYLE_COMPACT_STRING_CONCATENATION = JavaCore.FORMATTER_COMPACT_STRING_CONCATENATION;
66 private static final String PREF_STYLE_COMPACT_ARRAYS = JavaCore.FORMATTER_COMPACT_ARRAYS;
68 private static final String PREF_TAB_CHAR = JavaCore.FORMATTER_TAB_CHAR;
70 private static final String PREF_TAB_SIZE = JavaCore.FORMATTER_TAB_SIZE;
73 private static final String INSERT = JavaCore.INSERT;
75 private static final String DO_NOT_INSERT = JavaCore.DO_NOT_INSERT;
77 private static final String COMPACT = JavaCore.COMPACT;
79 private static final String NORMAL = JavaCore.NORMAL;
81 private static final String TAB = JavaCore.TAB;
83 private static final String SPACE = JavaCore.SPACE;
85 private static final String CLEAR_ALL = JavaCore.CLEAR_ALL;
87 private static final String PRESERVE_ONE = JavaCore.PRESERVE_ONE;
89 private static String[] getAllKeys() {
90 return new String[] { PREF_NEWLINE_OPENING_BRACES,
91 PREF_NEWLINE_CONTROL_STATEMENT, PREF_NEWLINE_CLEAR_ALL,
92 // PREF_NEWLINE_ELSE_IF,
93 PREF_NEWLINE_EMPTY_BLOCK, PREF_LINE_SPLIT,
94 PREF_STYLE_COMPACT_ASSIGNEMENT, PREF_STYLE_COMPACT_STRING_CONCATENATION,
95 PREF_STYLE_COMPACT_ARRAYS,
96 PREF_TAB_CHAR, PREF_TAB_SIZE };
100 * Gets the currently configured tab size
102 * @deprecated Inline to avoid reference to preference page
104 public static int getTabSize() {
105 String string = (String) JavaCore.getOptions().get(PREF_TAB_SIZE);
106 return getPositiveIntValue(string, 4);
110 * Gets the current compating assignement configuration
112 * @deprecated Inline to avoid reference to preference page
114 public static boolean isCompactingAssignment() {
115 return COMPACT.equals(JavaCore.getOptions().get(
116 PREF_STYLE_COMPACT_ASSIGNEMENT));
120 * Gets the current compating assignement configuration
122 * @deprecated Inline to avoid reference to preference page
124 public static boolean useSpaces() {
125 return SPACE.equals(JavaCore.getOptions().get(PREF_TAB_CHAR));
128 private static int getPositiveIntValue(String string, int dflt) {
130 int i = Integer.parseInt(string);
134 } catch (NumberFormatException e) {
139 private static class ControlData {
142 private String[] fValues;
144 public ControlData(String key, String[] values) {
149 public String getKey() {
153 public String getValue(boolean selection) {
154 int index = selection ? 0 : 1;
155 return fValues[index];
158 public String getValue(int index) {
159 return fValues[index];
162 public int getSelection(String value) {
163 for (int i = 0; i < fValues.length; i++) {
164 if (value.equals(fValues[i])) {
168 throw new IllegalArgumentException();
172 private Hashtable fWorkingValues;
174 private ArrayList fCheckBoxes;
176 private ArrayList fTextBoxes;
178 private SelectionListener fButtonSelectionListener;
180 private ModifyListener fTextModifyListener;
182 private String fPreviewText;
184 private IDocument fPreviewDocument;
186 private Text fTabSizeTextBox;
188 // private SourceViewer fSourceViewer;
190 public CodeFormatterPreferencePage() {
191 setPreferenceStore(PHPeclipsePlugin.getDefault().getPreferenceStore());
192 setDescription(PHPUIMessages
193 .getString("CodeFormatterPreferencePage.description")); //$NON-NLS-1$
195 fWorkingValues = JavaCore.getOptions();
196 fCheckBoxes = new ArrayList();
197 fTextBoxes = new ArrayList();
199 fButtonSelectionListener = new SelectionListener() {
200 public void widgetDefaultSelected(SelectionEvent e) {
203 public void widgetSelected(SelectionEvent e) {
204 if (!e.widget.isDisposed()) {
205 controlChanged((Button) e.widget);
210 fTextModifyListener = new ModifyListener() {
211 public void modifyText(ModifyEvent e) {
212 if (!e.widget.isDisposed()) {
213 textChanged((Text) e.widget);
218 fPreviewDocument = new Document();
219 fPreviewText = loadPreviewFile("CodeFormatterPreviewCode.txt"); //$NON-NLS-1$
223 * @see IWorkbenchPreferencePage#init()
225 public void init(IWorkbench workbench) {
229 * @see PreferencePage#createControl(Composite)
231 public void createControl(Composite parent) {
232 super.createControl(parent);
233 // WorkbenchHelp.setHelp(getControl(),
234 // IJavaHelpContextIds.CODEFORMATTER_PREFERENCE_PAGE);
238 * @see PreferencePage#createContents(Composite)
240 protected Control createContents(Composite parent) {
242 GridLayout layout = new GridLayout();
243 layout.marginHeight = 0;
244 layout.marginWidth = 0;
246 Composite composite = new Composite(parent, SWT.NONE);
247 composite.setLayout(layout);
249 TabFolder folder = new TabFolder(composite, SWT.NONE);
250 folder.setLayout(new TabFolderLayout());
251 folder.setLayoutData(new GridData(GridData.FILL_BOTH));
253 String[] insertNotInsert = new String[] { INSERT, DO_NOT_INSERT };
255 layout = new GridLayout();
256 layout.numColumns = 2;
258 Composite newlineComposite = new Composite(folder, SWT.NULL);
259 newlineComposite.setLayout(layout);
261 String label = PHPUIMessages
262 .getString("CodeFormatterPreferencePage.newline_opening_braces.label"); //$NON-NLS-1$
263 addCheckBox(newlineComposite, label, PREF_NEWLINE_OPENING_BRACES,
266 label = PHPUIMessages
267 .getString("CodeFormatterPreferencePage.newline_control_statement.label"); //$NON-NLS-1$
268 addCheckBox(newlineComposite, label, PREF_NEWLINE_CONTROL_STATEMENT,
271 label = PHPUIMessages
272 .getString("CodeFormatterPreferencePage.newline_clear_lines"); //$NON-NLS-1$
273 addCheckBox(newlineComposite, label, PREF_NEWLINE_CLEAR_ALL,
274 new String[] { CLEAR_ALL, PRESERVE_ONE });
277 // PHPUIMessages.getString("CodeFormatterPreferencePage.newline_else_if.label");
279 // addCheckBox(newlineComposite, label, PREF_NEWLINE_ELSE_IF,
282 label = PHPUIMessages
283 .getString("CodeFormatterPreferencePage.newline_empty_block.label"); //$NON-NLS-1$
284 addCheckBox(newlineComposite, label, PREF_NEWLINE_EMPTY_BLOCK,
287 layout = new GridLayout();
288 layout.numColumns = 2;
290 Composite lineSplittingComposite = new Composite(folder, SWT.NULL);
291 lineSplittingComposite.setLayout(layout);
293 label = PHPUIMessages
294 .getString("CodeFormatterPreferencePage.split_line.label"); //$NON-NLS-1$
295 addTextField(lineSplittingComposite, label, PREF_LINE_SPLIT);
297 layout = new GridLayout();
298 layout.numColumns = 2;
300 Composite styleComposite = new Composite(folder, SWT.NULL);
301 styleComposite.setLayout(layout);
303 label = PHPUIMessages
304 .getString("CodeFormatterPreferencePage.style_compact_assignement.label"); //$NON-NLS-1$
305 addCheckBox(styleComposite, label, PREF_STYLE_COMPACT_ASSIGNEMENT,
306 new String[] { COMPACT, NORMAL });
308 label = PHPUIMessages
309 .getString("CodeFormatterPreferencePage.style_compact_string_concatenation.label"); //$NON-NLS-1$
310 addCheckBox(styleComposite, label, PREF_STYLE_COMPACT_STRING_CONCATENATION,
311 new String[] { COMPACT, NORMAL });
313 label = PHPUIMessages
314 .getString("CodeFormatterPreferencePage.style_compact_arrays.label"); //$NON-NLS-1$
315 addCheckBox(styleComposite, label, PREF_STYLE_COMPACT_ARRAYS,
316 new String[] { COMPACT, NORMAL });
318 label = PHPUIMessages
319 .getString("CodeFormatterPreferencePage.tab_char.label"); //$NON-NLS-1$
320 addCheckBox(styleComposite, label, PREF_TAB_CHAR, new String[] { TAB,
323 label = PHPUIMessages
324 .getString("CodeFormatterPreferencePage.tab_size.label"); //$NON-NLS-1$
325 fTabSizeTextBox = addTextField(styleComposite, label, PREF_TAB_SIZE);
327 TabItem item = new TabItem(folder, SWT.NONE);
328 item.setText(PHPUIMessages
329 .getString("CodeFormatterPreferencePage.tab.newline.tabtitle")); //$NON-NLS-1$
330 item.setControl(newlineComposite);
332 item = new TabItem(folder, SWT.NONE);
334 .setText(PHPUIMessages
335 .getString("CodeFormatterPreferencePage.tab.linesplit.tabtitle")); //$NON-NLS-1$
336 item.setControl(lineSplittingComposite);
338 item = new TabItem(folder, SWT.NONE);
339 item.setText(PHPUIMessages
340 .getString("CodeFormatterPreferencePage.tab.style.tabtitle")); //$NON-NLS-1$
341 item.setControl(styleComposite);
343 // fSourceViewer= createPreview(parent);
350 // private SourceViewer createPreview(Composite parent) {
351 // SourceViewer previewViewer= new SourceViewer(parent, null, SWT.V_SCROLL |
352 // SWT.H_SCROLL | SWT.BORDER);
353 // JavaTextTools tools= JavaPlugin.getDefault().getJavaTextTools();
354 // previewViewer.configure(new PHPSourceViewerConfiguration(tools, null));
355 // previewViewer.getTextWidget().setFont(JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT));
356 // previewViewer.getTextWidget().setTabs(getPositiveIntValue((String)
357 // fWorkingValues.get(PREF_TAB_SIZE), 0));
358 // previewViewer.setEditable(false);
359 // previewViewer.setDocument(fPreviewDocument);
360 // Control control= previewViewer.getControl();
361 // GridData gdata= new GridData(GridData.FILL_BOTH);
362 // gdata.widthHint= convertWidthInCharsToPixels(30);
363 // gdata.heightHint= convertHeightInCharsToPixels(5);
364 // control.setLayoutData(gdata);
365 // return previewViewer;
368 private Button addCheckBox(Composite parent, String label, String key,
370 ControlData data = new ControlData(key, values);
372 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
373 gd.horizontalSpan = 2;
375 Button checkBox = new Button(parent, SWT.CHECK);
376 checkBox.setText(label);
377 checkBox.setData(data);
378 checkBox.setLayoutData(gd);
380 String currValue = (String) fWorkingValues.get(key);
381 checkBox.setSelection(data.getSelection(currValue) == 0);
382 checkBox.addSelectionListener(fButtonSelectionListener);
384 fCheckBoxes.add(checkBox);
388 private Text addTextField(Composite parent, String label, String key) {
389 Label labelControl = new Label(parent, SWT.NONE);
390 labelControl.setText(label);
391 labelControl.setLayoutData(new GridData());
393 Text textBox = new Text(parent, SWT.BORDER | SWT.SINGLE);
394 textBox.setData(key);
395 textBox.setLayoutData(new GridData());
397 String currValue = (String) fWorkingValues.get(key);
398 textBox.setText(String.valueOf(getPositiveIntValue(currValue, 1)));
399 textBox.setTextLimit(3);
400 textBox.addModifyListener(fTextModifyListener);
402 GridData gd = new GridData();
403 gd.widthHint = convertWidthInCharsToPixels(5);
404 textBox.setLayoutData(gd);
406 fTextBoxes.add(textBox);
410 private void controlChanged(Button button) {
411 ControlData data = (ControlData) button.getData();
412 boolean selection = button.getSelection();
413 String newValue = data.getValue(selection);
414 fWorkingValues.put(data.getKey(), newValue);
417 if (PREF_TAB_CHAR.equals(data.getKey())) {
418 updateStatus(new StatusInfo());
420 fTabSizeTextBox.setText((String) fWorkingValues
421 .get(PREF_TAB_SIZE));
426 private void textChanged(Text textControl) {
427 String key = (String) textControl.getData();
428 String number = textControl.getText();
429 IStatus status = validatePositiveNumber(number);
430 if (!status.matches(IStatus.ERROR)) {
431 fWorkingValues.put(key, number);
433 // if (PREF_TAB_SIZE.equals(key)) {
434 // fSourceViewer.getTextWidget().setTabs(getPositiveIntValue(number,
437 updateStatus(status);
442 * @see IPreferencePage#performOk()
444 public boolean performOk() {
445 String[] allKeys = getAllKeys();
446 // preserve other options
448 Hashtable actualOptions = JavaCore.getOptions();
449 for (int i = 0; i < allKeys.length; i++) {
450 String key = allKeys[i];
451 String val = (String) fWorkingValues.get(key);
452 actualOptions.put(key, val);
454 JavaCore.setOptions(actualOptions);
455 PHPeclipsePlugin.getDefault().savePluginPreferences();
456 return super.performOk();
460 * @see PreferencePage#performDefaults()
462 protected void performDefaults() {
463 fWorkingValues = JavaCore.getDefaultOptions();
465 super.performDefaults();
468 private String loadPreviewFile(String filename) {
469 String separator = System.getProperty("line.separator"); //$NON-NLS-1$
470 StringBuffer btxt = new StringBuffer(512);
471 BufferedReader rin = null;
473 rin = new BufferedReader(new InputStreamReader(getClass()
474 .getResourceAsStream(filename)));
476 while ((line = rin.readLine()) != null) {
478 btxt.append(separator);
480 } catch (IOException io) {
481 PHPeclipsePlugin.log(io);
486 } catch (IOException e) {
490 return btxt.toString();
493 private void updatePreview() {
494 ICodeFormatter formatter = ToolFactory
495 .createDefaultCodeFormatter(fWorkingValues);
496 fPreviewDocument.set(formatter.format(fPreviewText, 0, null, "\n")); //$NON-NLS-1$
499 private void updateControls() {
501 for (int i = fCheckBoxes.size() - 1; i >= 0; i--) {
502 Button curr = (Button) fCheckBoxes.get(i);
503 ControlData data = (ControlData) curr.getData();
505 String currValue = (String) fWorkingValues.get(data.getKey());
506 curr.setSelection(data.getSelection(currValue) == 0);
508 for (int i = fTextBoxes.size() - 1; i >= 0; i--) {
509 Text curr = (Text) fTextBoxes.get(i);
510 String key = (String) curr.getData();
511 String currValue = (String) fWorkingValues.get(key);
512 curr.setText(currValue);
516 private IStatus validatePositiveNumber(String number) {
517 StatusInfo status = new StatusInfo();
518 if (number.length() == 0) {
519 status.setError(PHPUIMessages
520 .getString("CodeFormatterPreferencePage.empty_input")); //$NON-NLS-1$
523 int value = Integer.parseInt(number);
526 .setError(PHPUIMessages
528 "CodeFormatterPreferencePage.invalid_input", number)); //$NON-NLS-1$
530 } catch (NumberFormatException e) {
531 status.setError(PHPUIMessages.getFormattedString(
532 "CodeFormatterPreferencePage.invalid_input", number)); //$NON-NLS-1$
538 private void updateStatus(IStatus status) {
539 if (!status.matches(IStatus.ERROR)) {
540 // look if there are more severe errors
541 for (int i = 0; i < fTextBoxes.size(); i++) {
542 Text curr = (Text) fTextBoxes.get(i);
543 if (!(curr == fTabSizeTextBox && usesTabs())) {
544 IStatus currStatus = validatePositiveNumber(curr.getText());
545 status = StatusUtil.getMoreSevere(currStatus, status);
549 setValid(!status.matches(IStatus.ERROR));
550 StatusUtil.applyToStatusLine(this, status);
553 private boolean usesTabs() {
554 return TAB.equals(fWorkingValues.get(PREF_TAB_CHAR));