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