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
21 import net.sourceforge.phpeclipse.ui.WebUI;
\r
23 import org.eclipse.core.runtime.IStatus;
\r
24 import org.eclipse.jface.preference.PreferencePage;
\r
25 import org.eclipse.jface.text.Document;
\r
26 import org.eclipse.jface.text.IDocument;
\r
27 import org.eclipse.swt.SWT;
\r
28 import org.eclipse.swt.events.ModifyEvent;
\r
29 import org.eclipse.swt.events.ModifyListener;
\r
30 import org.eclipse.swt.events.SelectionEvent;
\r
31 import org.eclipse.swt.events.SelectionListener;
\r
32 import org.eclipse.swt.layout.GridData;
\r
33 import org.eclipse.swt.layout.GridLayout;
\r
34 import org.eclipse.swt.widgets.Button;
\r
35 import org.eclipse.swt.widgets.Composite;
\r
36 import org.eclipse.swt.widgets.Control;
\r
37 import org.eclipse.swt.widgets.Label;
\r
38 import org.eclipse.swt.widgets.TabFolder;
\r
39 import org.eclipse.swt.widgets.TabItem;
\r
40 import org.eclipse.swt.widgets.Text;
\r
41 import org.eclipse.ui.IWorkbench;
\r
42 import org.eclipse.ui.IWorkbenchPreferencePage;
\r
45 * The page for setting code formatter options
\r
47 public class CodeFormatterPreferencePage extends PreferencePage implements
\r
48 IWorkbenchPreferencePage {
\r
50 // Preference store keys, see PHPCore.getOptions
\r
51 private static final String PREF_NEWLINE_OPENING_BRACES = JavaCore.FORMATTER_NEWLINE_OPENING_BRACE;
\r
53 private static final String PREF_NEWLINE_CONTROL_STATEMENT = JavaCore.FORMATTER_NEWLINE_CONTROL;
\r
55 private static final String PREF_NEWLINE_CLEAR_ALL = JavaCore.FORMATTER_CLEAR_BLANK_LINES;
\r
57 // private static final String PREF_NEWLINE_ELSE_IF=
\r
58 // PHPCore.FORMATTER_NEWLINE_ELSE_IF;
\r
59 private static final String PREF_NEWLINE_EMPTY_BLOCK = JavaCore.FORMATTER_NEWLINE_EMPTY_BLOCK;
\r
61 private static final String PREF_LINE_SPLIT = JavaCore.FORMATTER_LINE_SPLIT;
\r
63 private static final String PREF_STYLE_COMPACT_ASSIGNEMENT = JavaCore.FORMATTER_COMPACT_ASSIGNMENT;
\r
65 private static final String PREF_STYLE_COMPACT_STRING_CONCATENATION = JavaCore.FORMATTER_COMPACT_STRING_CONCATENATION;
\r
67 private static final String PREF_STYLE_COMPACT_ARRAYS = JavaCore.FORMATTER_COMPACT_ARRAYS;
\r
69 private static final String PREF_TAB_CHAR = JavaCore.FORMATTER_TAB_CHAR;
\r
71 private static final String PREF_TAB_SIZE = JavaCore.FORMATTER_TAB_SIZE;
\r
74 private static final String INSERT = JavaCore.INSERT;
\r
76 private static final String DO_NOT_INSERT = JavaCore.DO_NOT_INSERT;
\r
78 private static final String COMPACT = JavaCore.COMPACT;
\r
80 private static final String NORMAL = JavaCore.NORMAL;
\r
82 private static final String TAB = JavaCore.TAB;
\r
84 private static final String SPACE = JavaCore.SPACE;
\r
86 private static final String CLEAR_ALL = JavaCore.CLEAR_ALL;
\r
88 private static final String PRESERVE_ONE = JavaCore.PRESERVE_ONE;
\r
90 private static String[] getAllKeys() {
\r
91 return new String[] { PREF_NEWLINE_OPENING_BRACES,
\r
92 PREF_NEWLINE_CONTROL_STATEMENT, PREF_NEWLINE_CLEAR_ALL,
\r
93 // PREF_NEWLINE_ELSE_IF,
\r
94 PREF_NEWLINE_EMPTY_BLOCK, PREF_LINE_SPLIT,
\r
95 PREF_STYLE_COMPACT_ASSIGNEMENT, PREF_STYLE_COMPACT_STRING_CONCATENATION,
\r
96 PREF_STYLE_COMPACT_ARRAYS,
\r
97 PREF_TAB_CHAR, PREF_TAB_SIZE };
\r
101 * Gets the currently configured tab size
\r
103 * @deprecated Inline to avoid reference to preference page
\r
105 public static int getTabSize() {
\r
106 String string = (String) JavaCore.getOptions().get(PREF_TAB_SIZE);
\r
107 return getPositiveIntValue(string, 4);
\r
111 * Gets the current compating assignement configuration
\r
113 * @deprecated Inline to avoid reference to preference page
\r
115 public static boolean isCompactingAssignment() {
\r
116 return COMPACT.equals(JavaCore.getOptions().get(
\r
117 PREF_STYLE_COMPACT_ASSIGNEMENT));
\r
121 * Gets the current compating assignement configuration
\r
123 * @deprecated Inline to avoid reference to preference page
\r
125 public static boolean useSpaces() {
\r
126 return SPACE.equals(JavaCore.getOptions().get(PREF_TAB_CHAR));
\r
129 private static int getPositiveIntValue(String string, int dflt) {
\r
131 int i = Integer.parseInt(string);
\r
135 } catch (NumberFormatException e) {
\r
140 private static class ControlData {
\r
141 private String fKey;
\r
143 private String[] fValues;
\r
145 public ControlData(String key, String[] values) {
\r
150 public String getKey() {
\r
154 public String getValue(boolean selection) {
\r
155 int index = selection ? 0 : 1;
\r
156 return fValues[index];
\r
159 public String getValue(int index) {
\r
160 return fValues[index];
\r
163 public int getSelection(String value) {
\r
164 for (int i = 0; i < fValues.length; i++) {
\r
165 if (value.equals(fValues[i])) {
\r
169 throw new IllegalArgumentException();
\r
173 private Hashtable fWorkingValues;
\r
175 private ArrayList fCheckBoxes;
\r
177 private ArrayList fTextBoxes;
\r
179 private SelectionListener fButtonSelectionListener;
\r
181 private ModifyListener fTextModifyListener;
\r
183 private String fPreviewText;
\r
185 private IDocument fPreviewDocument;
\r
187 private Text fTabSizeTextBox;
\r
189 // private SourceViewer fSourceViewer;
\r
191 public CodeFormatterPreferencePage() {
\r
192 setPreferenceStore(WebUI.getDefault().getPreferenceStore());
\r
193 setDescription(PHPUIMessages
\r
194 .getString("CodeFormatterPreferencePage.description")); //$NON-NLS-1$
\r
196 fWorkingValues = JavaCore.getOptions();
\r
197 fCheckBoxes = new ArrayList();
\r
198 fTextBoxes = new ArrayList();
\r
200 fButtonSelectionListener = new SelectionListener() {
\r
201 public void widgetDefaultSelected(SelectionEvent e) {
\r
204 public void widgetSelected(SelectionEvent e) {
\r
205 if (!e.widget.isDisposed()) {
\r
206 controlChanged((Button) e.widget);
\r
211 fTextModifyListener = new ModifyListener() {
\r
212 public void modifyText(ModifyEvent e) {
\r
213 if (!e.widget.isDisposed()) {
\r
214 textChanged((Text) e.widget);
\r
219 fPreviewDocument = new Document();
\r
220 fPreviewText = loadPreviewFile("CodeFormatterPreviewCode.txt"); //$NON-NLS-1$
\r
224 * @see IWorkbenchPreferencePage#init()
\r
226 public void init(IWorkbench workbench) {
\r
230 * @see PreferencePage#createControl(Composite)
\r
232 public void createControl(Composite parent) {
\r
233 super.createControl(parent);
\r
234 // WorkbenchHelp.setHelp(getControl(),
\r
235 // IJavaHelpContextIds.CODEFORMATTER_PREFERENCE_PAGE);
\r
239 * @see PreferencePage#createContents(Composite)
\r
241 protected Control createContents(Composite parent) {
\r
243 GridLayout layout = new GridLayout();
\r
244 layout.marginHeight = 0;
\r
245 layout.marginWidth = 0;
\r
247 Composite composite = new Composite(parent, SWT.NONE);
\r
248 composite.setLayout(layout);
\r
250 TabFolder folder = new TabFolder(composite, SWT.NONE);
\r
251 folder.setLayout(new TabFolderLayout());
\r
252 folder.setLayoutData(new GridData(GridData.FILL_BOTH));
\r
254 String[] insertNotInsert = new String[] { INSERT, DO_NOT_INSERT };
\r
256 layout = new GridLayout();
\r
257 layout.numColumns = 2;
\r
259 Composite newlineComposite = new Composite(folder, SWT.NULL);
\r
260 newlineComposite.setLayout(layout);
\r
262 String label = PHPUIMessages
\r
263 .getString("CodeFormatterPreferencePage.newline_opening_braces.label"); //$NON-NLS-1$
\r
264 addCheckBox(newlineComposite, label, PREF_NEWLINE_OPENING_BRACES,
\r
267 label = PHPUIMessages
\r
268 .getString("CodeFormatterPreferencePage.newline_control_statement.label"); //$NON-NLS-1$
\r
269 addCheckBox(newlineComposite, label, PREF_NEWLINE_CONTROL_STATEMENT,
\r
272 label = PHPUIMessages
\r
273 .getString("CodeFormatterPreferencePage.newline_clear_lines"); //$NON-NLS-1$
\r
274 addCheckBox(newlineComposite, label, PREF_NEWLINE_CLEAR_ALL,
\r
275 new String[] { CLEAR_ALL, PRESERVE_ONE });
\r
278 // PHPUIMessages.getString("CodeFormatterPreferencePage.newline_else_if.label");
\r
280 // addCheckBox(newlineComposite, label, PREF_NEWLINE_ELSE_IF,
\r
281 // insertNotInsert);
\r
283 label = PHPUIMessages
\r
284 .getString("CodeFormatterPreferencePage.newline_empty_block.label"); //$NON-NLS-1$
\r
285 addCheckBox(newlineComposite, label, PREF_NEWLINE_EMPTY_BLOCK,
\r
288 layout = new GridLayout();
\r
289 layout.numColumns = 2;
\r
291 Composite lineSplittingComposite = new Composite(folder, SWT.NULL);
\r
292 lineSplittingComposite.setLayout(layout);
\r
294 label = PHPUIMessages
\r
295 .getString("CodeFormatterPreferencePage.split_line.label"); //$NON-NLS-1$
\r
296 addTextField(lineSplittingComposite, label, PREF_LINE_SPLIT);
\r
298 layout = new GridLayout();
\r
299 layout.numColumns = 2;
\r
301 Composite styleComposite = new Composite(folder, SWT.NULL);
\r
302 styleComposite.setLayout(layout);
\r
304 label = PHPUIMessages
\r
305 .getString("CodeFormatterPreferencePage.style_compact_assignement.label"); //$NON-NLS-1$
\r
306 addCheckBox(styleComposite, label, PREF_STYLE_COMPACT_ASSIGNEMENT,
\r
307 new String[] { COMPACT, NORMAL });
\r
309 label = PHPUIMessages
\r
310 .getString("CodeFormatterPreferencePage.style_compact_string_concatenation.label"); //$NON-NLS-1$
\r
311 addCheckBox(styleComposite, label, PREF_STYLE_COMPACT_STRING_CONCATENATION,
\r
312 new String[] { COMPACT, NORMAL });
\r
314 label = PHPUIMessages
\r
315 .getString("CodeFormatterPreferencePage.style_compact_arrays.label"); //$NON-NLS-1$
\r
316 addCheckBox(styleComposite, label, PREF_STYLE_COMPACT_ARRAYS,
\r
317 new String[] { COMPACT, NORMAL });
\r
319 label = PHPUIMessages
\r
320 .getString("CodeFormatterPreferencePage.tab_char.label"); //$NON-NLS-1$
\r
321 addCheckBox(styleComposite, label, PREF_TAB_CHAR, new String[] { TAB,
\r
324 label = PHPUIMessages
\r
325 .getString("CodeFormatterPreferencePage.tab_size.label"); //$NON-NLS-1$
\r
326 fTabSizeTextBox = addTextField(styleComposite, label, PREF_TAB_SIZE);
\r
328 TabItem item = new TabItem(folder, SWT.NONE);
\r
329 item.setText(PHPUIMessages
\r
330 .getString("CodeFormatterPreferencePage.tab.newline.tabtitle")); //$NON-NLS-1$
\r
331 item.setControl(newlineComposite);
\r
333 item = new TabItem(folder, SWT.NONE);
\r
335 .setText(PHPUIMessages
\r
336 .getString("CodeFormatterPreferencePage.tab.linesplit.tabtitle")); //$NON-NLS-1$
\r
337 item.setControl(lineSplittingComposite);
\r
339 item = new TabItem(folder, SWT.NONE);
\r
340 item.setText(PHPUIMessages
\r
341 .getString("CodeFormatterPreferencePage.tab.style.tabtitle")); //$NON-NLS-1$
\r
342 item.setControl(styleComposite);
\r
344 // fSourceViewer= createPreview(parent);
\r
351 // private SourceViewer createPreview(Composite parent) {
\r
352 // SourceViewer previewViewer= new SourceViewer(parent, null, SWT.V_SCROLL |
\r
353 // SWT.H_SCROLL | SWT.BORDER);
\r
354 // JavaTextTools tools= JavaPlugin.getDefault().getJavaTextTools();
\r
355 // previewViewer.configure(new PHPSourceViewerConfiguration(tools, null));
\r
356 // previewViewer.getTextWidget().setFont(JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT));
\r
357 // previewViewer.getTextWidget().setTabs(getPositiveIntValue((String)
\r
358 // fWorkingValues.get(PREF_TAB_SIZE), 0));
\r
359 // previewViewer.setEditable(false);
\r
360 // previewViewer.setDocument(fPreviewDocument);
\r
361 // Control control= previewViewer.getControl();
\r
362 // GridData gdata= new GridData(GridData.FILL_BOTH);
\r
363 // gdata.widthHint= convertWidthInCharsToPixels(30);
\r
364 // gdata.heightHint= convertHeightInCharsToPixels(5);
\r
365 // control.setLayoutData(gdata);
\r
366 // return previewViewer;
\r
369 private Button addCheckBox(Composite parent, String label, String key,
\r
371 ControlData data = new ControlData(key, values);
\r
373 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
\r
374 gd.horizontalSpan = 2;
\r
376 Button checkBox = new Button(parent, SWT.CHECK);
\r
377 checkBox.setText(label);
\r
378 checkBox.setData(data);
\r
379 checkBox.setLayoutData(gd);
\r
381 String currValue = (String) fWorkingValues.get(key);
\r
382 checkBox.setSelection(data.getSelection(currValue) == 0);
\r
383 checkBox.addSelectionListener(fButtonSelectionListener);
\r
385 fCheckBoxes.add(checkBox);
\r
389 private Text addTextField(Composite parent, String label, String key) {
\r
390 Label labelControl = new Label(parent, SWT.NONE);
\r
391 labelControl.setText(label);
\r
392 labelControl.setLayoutData(new GridData());
\r
394 Text textBox = new Text(parent, SWT.BORDER | SWT.SINGLE);
\r
395 textBox.setData(key);
\r
396 textBox.setLayoutData(new GridData());
\r
398 String currValue = (String) fWorkingValues.get(key);
\r
399 textBox.setText(String.valueOf(getPositiveIntValue(currValue, 1)));
\r
400 textBox.setTextLimit(3);
\r
401 textBox.addModifyListener(fTextModifyListener);
\r
403 GridData gd = new GridData();
\r
404 gd.widthHint = convertWidthInCharsToPixels(5);
\r
405 textBox.setLayoutData(gd);
\r
407 fTextBoxes.add(textBox);
\r
411 private void controlChanged(Button button) {
\r
412 ControlData data = (ControlData) button.getData();
\r
413 boolean selection = button.getSelection();
\r
414 String newValue = data.getValue(selection);
\r
415 fWorkingValues.put(data.getKey(), newValue);
\r
418 if (PREF_TAB_CHAR.equals(data.getKey())) {
\r
419 updateStatus(new StatusInfo());
\r
421 fTabSizeTextBox.setText((String) fWorkingValues
\r
422 .get(PREF_TAB_SIZE));
\r
427 private void textChanged(Text textControl) {
\r
428 String key = (String) textControl.getData();
\r
429 String number = textControl.getText();
\r
430 IStatus status = validatePositiveNumber(number);
\r
431 if (!status.matches(IStatus.ERROR)) {
\r
432 fWorkingValues.put(key, number);
\r
434 // if (PREF_TAB_SIZE.equals(key)) {
\r
435 // fSourceViewer.getTextWidget().setTabs(getPositiveIntValue(number,
\r
438 updateStatus(status);
\r
443 * @see IPreferencePage#performOk()
\r
445 public boolean performOk() {
\r
446 String[] allKeys = getAllKeys();
\r
447 // preserve other options
\r
449 Hashtable actualOptions = JavaCore.getOptions();
\r
450 for (int i = 0; i < allKeys.length; i++) {
\r
451 String key = allKeys[i];
\r
452 String val = (String) fWorkingValues.get(key);
\r
453 actualOptions.put(key, val);
\r
455 JavaCore.setOptions(actualOptions);
\r
456 WebUI.getDefault().savePluginPreferences();
\r
457 return super.performOk();
\r
461 * @see PreferencePage#performDefaults()
\r
463 protected void performDefaults() {
\r
464 fWorkingValues = JavaCore.getDefaultOptions();
\r
466 super.performDefaults();
\r
469 private String loadPreviewFile(String filename) {
\r
470 String separator = System.getProperty("line.separator"); //$NON-NLS-1$
\r
471 StringBuffer btxt = new StringBuffer(512);
\r
472 BufferedReader rin = null;
\r
474 rin = new BufferedReader(new InputStreamReader(getClass()
\r
475 .getResourceAsStream(filename)));
\r
477 while ((line = rin.readLine()) != null) {
\r
479 btxt.append(separator);
\r
481 } catch (IOException io) {
\r
487 } catch (IOException e) {
\r
491 return btxt.toString();
\r
494 private void updatePreview() {
\r
495 ICodeFormatter formatter = ToolFactory
\r
496 .createDefaultCodeFormatter(fWorkingValues);
\r
497 fPreviewDocument.set(formatter.format(fPreviewText, 0, null, "\n")); //$NON-NLS-1$
\r
500 private void updateControls() {
\r
502 for (int i = fCheckBoxes.size() - 1; i >= 0; i--) {
\r
503 Button curr = (Button) fCheckBoxes.get(i);
\r
504 ControlData data = (ControlData) curr.getData();
\r
506 String currValue = (String) fWorkingValues.get(data.getKey());
\r
507 curr.setSelection(data.getSelection(currValue) == 0);
\r
509 for (int i = fTextBoxes.size() - 1; i >= 0; i--) {
\r
510 Text curr = (Text) fTextBoxes.get(i);
\r
511 String key = (String) curr.getData();
\r
512 String currValue = (String) fWorkingValues.get(key);
\r
513 curr.setText(currValue);
\r
517 private IStatus validatePositiveNumber(String number) {
\r
518 StatusInfo status = new StatusInfo();
\r
519 if (number.length() == 0) {
\r
520 status.setError(PHPUIMessages
\r
521 .getString("CodeFormatterPreferencePage.empty_input")); //$NON-NLS-1$
\r
524 int value = Integer.parseInt(number);
\r
527 .setError(PHPUIMessages
\r
528 .getFormattedString(
\r
529 "CodeFormatterPreferencePage.invalid_input", number)); //$NON-NLS-1$
\r
531 } catch (NumberFormatException e) {
\r
532 status.setError(PHPUIMessages.getFormattedString(
\r
533 "CodeFormatterPreferencePage.invalid_input", number)); //$NON-NLS-1$
\r
539 private void updateStatus(IStatus status) {
\r
540 if (!status.matches(IStatus.ERROR)) {
\r
541 // look if there are more severe errors
\r
542 for (int i = 0; i < fTextBoxes.size(); i++) {
\r
543 Text curr = (Text) fTextBoxes.get(i);
\r
544 if (!(curr == fTabSizeTextBox && usesTabs())) {
\r
545 IStatus currStatus = validatePositiveNumber(curr.getText());
\r
546 status = StatusUtil.getMoreSevere(currStatus, status);
\r
550 setValid(!status.matches(IStatus.ERROR));
\r
551 StatusUtil.applyToStatusLine(this, status);
\r
554 private boolean usesTabs() {
\r
555 return TAB.equals(fWorkingValues.get(PREF_TAB_CHAR));
\r