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;
21 import net.sourceforge.phpeclipse.ui.WebUI;
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;
45 * The page for setting code formatter options
47 public class CodeFormatterPreferencePage extends PreferencePage implements
48 IWorkbenchPreferencePage {
50 // Preference store keys, see PHPCore.getOptions
51 private static final String PREF_NEWLINE_OPENING_BRACES = JavaCore.FORMATTER_NEWLINE_OPENING_BRACE;
53 private static final String PREF_NEWLINE_CONTROL_STATEMENT = JavaCore.FORMATTER_NEWLINE_CONTROL;
55 private static final String PREF_NEWLINE_CLEAR_ALL = JavaCore.FORMATTER_CLEAR_BLANK_LINES;
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;
61 private static final String PREF_LINE_SPLIT = JavaCore.FORMATTER_LINE_SPLIT;
63 private static final String PREF_STYLE_COMPACT_ASSIGNEMENT = JavaCore.FORMATTER_COMPACT_ASSIGNMENT;
65 private static final String PREF_STYLE_COMPACT_STRING_CONCATENATION = JavaCore.FORMATTER_COMPACT_STRING_CONCATENATION;
67 private static final String PREF_STYLE_COMPACT_ARRAYS = JavaCore.FORMATTER_COMPACT_ARRAYS;
69 private static final String PREF_TAB_CHAR = JavaCore.FORMATTER_TAB_CHAR;
71 private static final String PREF_TAB_SIZE = JavaCore.FORMATTER_TAB_SIZE;
74 private static final String INSERT = JavaCore.INSERT;
76 private static final String DO_NOT_INSERT = JavaCore.DO_NOT_INSERT;
78 private static final String COMPACT = JavaCore.COMPACT;
80 private static final String NORMAL = JavaCore.NORMAL;
82 private static final String TAB = JavaCore.TAB;
84 private static final String SPACE = JavaCore.SPACE;
86 private static final String CLEAR_ALL = JavaCore.CLEAR_ALL;
88 private static final String PRESERVE_ONE = JavaCore.PRESERVE_ONE;
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 };
101 * Gets the currently configured tab size
103 * @deprecated Inline to avoid reference to preference page
105 public static int getTabSize() {
106 String string = (String) JavaCore.getOptions().get(PREF_TAB_SIZE);
107 return getPositiveIntValue(string, 4);
111 * Gets the current compating assignement configuration
113 * @deprecated Inline to avoid reference to preference page
115 public static boolean isCompactingAssignment() {
116 return COMPACT.equals(JavaCore.getOptions().get(
117 PREF_STYLE_COMPACT_ASSIGNEMENT));
121 * Gets the current compating assignement configuration
123 * @deprecated Inline to avoid reference to preference page
125 public static boolean useSpaces() {
126 return SPACE.equals(JavaCore.getOptions().get(PREF_TAB_CHAR));
129 private static int getPositiveIntValue(String string, int dflt) {
131 int i = Integer.parseInt(string);
135 } catch (NumberFormatException e) {
140 private static class ControlData {
143 private String[] fValues;
145 public ControlData(String key, String[] values) {
150 public String getKey() {
154 public String getValue(boolean selection) {
155 int index = selection ? 0 : 1;
156 return fValues[index];
159 public String getValue(int index) {
160 return fValues[index];
163 public int getSelection(String value) {
164 for (int i = 0; i < fValues.length; i++) {
165 if (value.equals(fValues[i])) {
169 throw new IllegalArgumentException();
173 private Hashtable fWorkingValues;
175 private ArrayList fCheckBoxes;
177 private ArrayList fTextBoxes;
179 private SelectionListener fButtonSelectionListener;
181 private ModifyListener fTextModifyListener;
183 private String fPreviewText;
185 private IDocument fPreviewDocument;
187 private Text fTabSizeTextBox;
189 // private SourceViewer fSourceViewer;
191 public CodeFormatterPreferencePage() {
192 setPreferenceStore(WebUI.getDefault().getPreferenceStore());
193 setDescription(PHPUIMessages
194 .getString("CodeFormatterPreferencePage.description")); //$NON-NLS-1$
196 fWorkingValues = JavaCore.getOptions();
197 fCheckBoxes = new ArrayList();
198 fTextBoxes = new ArrayList();
200 fButtonSelectionListener = new SelectionListener() {
201 public void widgetDefaultSelected(SelectionEvent e) {
204 public void widgetSelected(SelectionEvent e) {
205 if (!e.widget.isDisposed()) {
206 controlChanged((Button) e.widget);
211 fTextModifyListener = new ModifyListener() {
212 public void modifyText(ModifyEvent e) {
213 if (!e.widget.isDisposed()) {
214 textChanged((Text) e.widget);
219 fPreviewDocument = new Document();
220 fPreviewText = loadPreviewFile("CodeFormatterPreviewCode.txt"); //$NON-NLS-1$
224 * @see IWorkbenchPreferencePage#init()
226 public void init(IWorkbench workbench) {
230 * @see PreferencePage#createControl(Composite)
232 public void createControl(Composite parent) {
233 super.createControl(parent);
234 // WorkbenchHelp.setHelp(getControl(),
235 // IJavaHelpContextIds.CODEFORMATTER_PREFERENCE_PAGE);
239 * @see PreferencePage#createContents(Composite)
241 protected Control createContents(Composite parent) {
243 GridLayout layout = new GridLayout();
244 layout.marginHeight = 0;
245 layout.marginWidth = 0;
247 Composite composite = new Composite(parent, SWT.NONE);
248 composite.setLayout(layout);
250 TabFolder folder = new TabFolder(composite, SWT.NONE);
251 folder.setLayout(new TabFolderLayout());
252 folder.setLayoutData(new GridData(GridData.FILL_BOTH));
254 String[] insertNotInsert = new String[] { INSERT, DO_NOT_INSERT };
256 layout = new GridLayout();
257 layout.numColumns = 2;
259 Composite newlineComposite = new Composite(folder, SWT.NULL);
260 newlineComposite.setLayout(layout);
262 String label = PHPUIMessages
263 .getString("CodeFormatterPreferencePage.newline_opening_braces.label"); //$NON-NLS-1$
264 addCheckBox(newlineComposite, label, PREF_NEWLINE_OPENING_BRACES,
267 label = PHPUIMessages
268 .getString("CodeFormatterPreferencePage.newline_control_statement.label"); //$NON-NLS-1$
269 addCheckBox(newlineComposite, label, PREF_NEWLINE_CONTROL_STATEMENT,
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 });
278 // PHPUIMessages.getString("CodeFormatterPreferencePage.newline_else_if.label");
280 // addCheckBox(newlineComposite, label, PREF_NEWLINE_ELSE_IF,
283 label = PHPUIMessages
284 .getString("CodeFormatterPreferencePage.newline_empty_block.label"); //$NON-NLS-1$
285 addCheckBox(newlineComposite, label, PREF_NEWLINE_EMPTY_BLOCK,
288 layout = new GridLayout();
289 layout.numColumns = 2;
291 Composite lineSplittingComposite = new Composite(folder, SWT.NULL);
292 lineSplittingComposite.setLayout(layout);
294 label = PHPUIMessages
295 .getString("CodeFormatterPreferencePage.split_line.label"); //$NON-NLS-1$
296 addTextField(lineSplittingComposite, label, PREF_LINE_SPLIT);
298 layout = new GridLayout();
299 layout.numColumns = 2;
301 Composite styleComposite = new Composite(folder, SWT.NULL);
302 styleComposite.setLayout(layout);
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 });
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 });
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 });
319 label = PHPUIMessages
320 .getString("CodeFormatterPreferencePage.tab_char.label"); //$NON-NLS-1$
321 addCheckBox(styleComposite, label, PREF_TAB_CHAR, new String[] { TAB,
324 label = PHPUIMessages
325 .getString("CodeFormatterPreferencePage.tab_size.label"); //$NON-NLS-1$
326 fTabSizeTextBox = addTextField(styleComposite, label, PREF_TAB_SIZE);
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);
333 item = new TabItem(folder, SWT.NONE);
335 .setText(PHPUIMessages
336 .getString("CodeFormatterPreferencePage.tab.linesplit.tabtitle")); //$NON-NLS-1$
337 item.setControl(lineSplittingComposite);
339 item = new TabItem(folder, SWT.NONE);
340 item.setText(PHPUIMessages
341 .getString("CodeFormatterPreferencePage.tab.style.tabtitle")); //$NON-NLS-1$
342 item.setControl(styleComposite);
344 // fSourceViewer= createPreview(parent);
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;
369 private Button addCheckBox(Composite parent, String label, String key,
371 ControlData data = new ControlData(key, values);
373 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
374 gd.horizontalSpan = 2;
376 Button checkBox = new Button(parent, SWT.CHECK);
377 checkBox.setText(label);
378 checkBox.setData(data);
379 checkBox.setLayoutData(gd);
381 String currValue = (String) fWorkingValues.get(key);
382 checkBox.setSelection(data.getSelection(currValue) == 0);
383 checkBox.addSelectionListener(fButtonSelectionListener);
385 fCheckBoxes.add(checkBox);
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());
394 Text textBox = new Text(parent, SWT.BORDER | SWT.SINGLE);
395 textBox.setData(key);
396 textBox.setLayoutData(new GridData());
398 String currValue = (String) fWorkingValues.get(key);
399 textBox.setText(String.valueOf(getPositiveIntValue(currValue, 1)));
400 textBox.setTextLimit(3);
401 textBox.addModifyListener(fTextModifyListener);
403 GridData gd = new GridData();
404 gd.widthHint = convertWidthInCharsToPixels(5);
405 textBox.setLayoutData(gd);
407 fTextBoxes.add(textBox);
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);
418 if (PREF_TAB_CHAR.equals(data.getKey())) {
419 updateStatus(new StatusInfo());
421 fTabSizeTextBox.setText((String) fWorkingValues
422 .get(PREF_TAB_SIZE));
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);
434 // if (PREF_TAB_SIZE.equals(key)) {
435 // fSourceViewer.getTextWidget().setTabs(getPositiveIntValue(number,
438 updateStatus(status);
443 * @see IPreferencePage#performOk()
445 public boolean performOk() {
446 String[] allKeys = getAllKeys();
447 // preserve other options
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);
455 JavaCore.setOptions(actualOptions);
456 WebUI.getDefault().savePluginPreferences();
457 return super.performOk();
461 * @see PreferencePage#performDefaults()
463 protected void performDefaults() {
464 fWorkingValues = JavaCore.getDefaultOptions();
466 super.performDefaults();
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;
474 rin = new BufferedReader(new InputStreamReader(getClass()
475 .getResourceAsStream(filename)));
477 while ((line = rin.readLine()) != null) {
479 btxt.append(separator);
481 } catch (IOException io) {
487 } catch (IOException e) {
491 return btxt.toString();
494 private void updatePreview() {
495 ICodeFormatter formatter = ToolFactory
496 .createDefaultCodeFormatter(fWorkingValues);
497 fPreviewDocument.set(formatter.format(fPreviewText, 0, null, "\n")); //$NON-NLS-1$
500 private void updateControls() {
502 for (int i = fCheckBoxes.size() - 1; i >= 0; i--) {
503 Button curr = (Button) fCheckBoxes.get(i);
504 ControlData data = (ControlData) curr.getData();
506 String currValue = (String) fWorkingValues.get(data.getKey());
507 curr.setSelection(data.getSelection(currValue) == 0);
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);
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$
524 int value = Integer.parseInt(number);
527 .setError(PHPUIMessages
529 "CodeFormatterPreferencePage.invalid_input", number)); //$NON-NLS-1$
531 } catch (NumberFormatException e) {
532 status.setError(PHPUIMessages.getFormattedString(
533 "CodeFormatterPreferencePage.invalid_input", number)); //$NON-NLS-1$
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);
550 setValid(!status.matches(IStatus.ERROR));
551 StatusUtil.applyToStatusLine(this, status);
554 private boolean usesTabs() {
555 return TAB.equals(fWorkingValues.get(PREF_TAB_CHAR));