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_TAB_CHAR = JavaCore.FORMATTER_TAB_CHAR;
66 private static final String PREF_TAB_SIZE = JavaCore.FORMATTER_TAB_SIZE;
69 private static final String INSERT = JavaCore.INSERT;
71 private static final String DO_NOT_INSERT = JavaCore.DO_NOT_INSERT;
73 private static final String COMPACT = JavaCore.COMPACT;
75 private static final String NORMAL = JavaCore.NORMAL;
77 private static final String TAB = JavaCore.TAB;
79 private static final String SPACE = JavaCore.SPACE;
81 private static final String CLEAR_ALL = JavaCore.CLEAR_ALL;
83 private static final String PRESERVE_ONE = JavaCore.PRESERVE_ONE;
85 private static String[] getAllKeys() {
86 return new String[] { PREF_NEWLINE_OPENING_BRACES,
87 PREF_NEWLINE_CONTROL_STATEMENT, PREF_NEWLINE_CLEAR_ALL,
88 // PREF_NEWLINE_ELSE_IF,
89 PREF_NEWLINE_EMPTY_BLOCK, PREF_LINE_SPLIT,
90 PREF_STYLE_COMPACT_ASSIGNEMENT, PREF_TAB_CHAR, PREF_TAB_SIZE };
94 * Gets the currently configured tab size
96 * @deprecated Inline to avoid reference to preference page
98 public static int getTabSize() {
99 String string = (String) JavaCore.getOptions().get(PREF_TAB_SIZE);
100 return getPositiveIntValue(string, 4);
104 * Gets the current compating assignement configuration
106 * @deprecated Inline to avoid reference to preference page
108 public static boolean isCompactingAssignment() {
109 return COMPACT.equals(JavaCore.getOptions().get(
110 PREF_STYLE_COMPACT_ASSIGNEMENT));
114 * Gets the current compating assignement configuration
116 * @deprecated Inline to avoid reference to preference page
118 public static boolean useSpaces() {
119 return SPACE.equals(JavaCore.getOptions().get(PREF_TAB_CHAR));
122 private static int getPositiveIntValue(String string, int dflt) {
124 int i = Integer.parseInt(string);
128 } catch (NumberFormatException e) {
133 private static class ControlData {
136 private String[] fValues;
138 public ControlData(String key, String[] values) {
143 public String getKey() {
147 public String getValue(boolean selection) {
148 int index = selection ? 0 : 1;
149 return fValues[index];
152 public String getValue(int index) {
153 return fValues[index];
156 public int getSelection(String value) {
157 for (int i = 0; i < fValues.length; i++) {
158 if (value.equals(fValues[i])) {
162 throw new IllegalArgumentException();
166 private Hashtable fWorkingValues;
168 private ArrayList fCheckBoxes;
170 private ArrayList fTextBoxes;
172 private SelectionListener fButtonSelectionListener;
174 private ModifyListener fTextModifyListener;
176 private String fPreviewText;
178 private IDocument fPreviewDocument;
180 private Text fTabSizeTextBox;
182 // private SourceViewer fSourceViewer;
184 public CodeFormatterPreferencePage() {
185 setPreferenceStore(PHPeclipsePlugin.getDefault().getPreferenceStore());
186 setDescription(PHPUIMessages
187 .getString("CodeFormatterPreferencePage.description")); //$NON-NLS-1$
189 fWorkingValues = JavaCore.getOptions();
190 fCheckBoxes = new ArrayList();
191 fTextBoxes = new ArrayList();
193 fButtonSelectionListener = new SelectionListener() {
194 public void widgetDefaultSelected(SelectionEvent e) {
197 public void widgetSelected(SelectionEvent e) {
198 if (!e.widget.isDisposed()) {
199 controlChanged((Button) e.widget);
204 fTextModifyListener = new ModifyListener() {
205 public void modifyText(ModifyEvent e) {
206 if (!e.widget.isDisposed()) {
207 textChanged((Text) e.widget);
212 fPreviewDocument = new Document();
213 fPreviewText = loadPreviewFile("CodeFormatterPreviewCode.txt"); //$NON-NLS-1$
217 * @see IWorkbenchPreferencePage#init()
219 public void init(IWorkbench workbench) {
223 * @see PreferencePage#createControl(Composite)
225 public void createControl(Composite parent) {
226 super.createControl(parent);
227 // WorkbenchHelp.setHelp(getControl(),
228 // IJavaHelpContextIds.CODEFORMATTER_PREFERENCE_PAGE);
232 * @see PreferencePage#createContents(Composite)
234 protected Control createContents(Composite parent) {
236 GridLayout layout = new GridLayout();
237 layout.marginHeight = 0;
238 layout.marginWidth = 0;
240 Composite composite = new Composite(parent, SWT.NONE);
241 composite.setLayout(layout);
243 TabFolder folder = new TabFolder(composite, SWT.NONE);
244 folder.setLayout(new TabFolderLayout());
245 folder.setLayoutData(new GridData(GridData.FILL_BOTH));
247 String[] insertNotInsert = new String[] { INSERT, DO_NOT_INSERT };
249 layout = new GridLayout();
250 layout.numColumns = 2;
252 Composite newlineComposite = new Composite(folder, SWT.NULL);
253 newlineComposite.setLayout(layout);
255 String label = PHPUIMessages
256 .getString("CodeFormatterPreferencePage.newline_opening_braces.label"); //$NON-NLS-1$
257 addCheckBox(newlineComposite, label, PREF_NEWLINE_OPENING_BRACES,
260 label = PHPUIMessages
261 .getString("CodeFormatterPreferencePage.newline_control_statement.label"); //$NON-NLS-1$
262 addCheckBox(newlineComposite, label, PREF_NEWLINE_CONTROL_STATEMENT,
265 label = PHPUIMessages
266 .getString("CodeFormatterPreferencePage.newline_clear_lines"); //$NON-NLS-1$
267 addCheckBox(newlineComposite, label, PREF_NEWLINE_CLEAR_ALL,
268 new String[] { CLEAR_ALL, PRESERVE_ONE });
271 // PHPUIMessages.getString("CodeFormatterPreferencePage.newline_else_if.label");
273 // addCheckBox(newlineComposite, label, PREF_NEWLINE_ELSE_IF,
276 label = PHPUIMessages
277 .getString("CodeFormatterPreferencePage.newline_empty_block.label"); //$NON-NLS-1$
278 addCheckBox(newlineComposite, label, PREF_NEWLINE_EMPTY_BLOCK,
281 layout = new GridLayout();
282 layout.numColumns = 2;
284 Composite lineSplittingComposite = new Composite(folder, SWT.NULL);
285 lineSplittingComposite.setLayout(layout);
287 label = PHPUIMessages
288 .getString("CodeFormatterPreferencePage.split_line.label"); //$NON-NLS-1$
289 addTextField(lineSplittingComposite, label, PREF_LINE_SPLIT);
291 layout = new GridLayout();
292 layout.numColumns = 2;
294 Composite styleComposite = new Composite(folder, SWT.NULL);
295 styleComposite.setLayout(layout);
297 label = PHPUIMessages
298 .getString("CodeFormatterPreferencePage.style_compact_assignement.label"); //$NON-NLS-1$
299 addCheckBox(styleComposite, label, PREF_STYLE_COMPACT_ASSIGNEMENT,
300 new String[] { COMPACT, NORMAL });
302 label = PHPUIMessages
303 .getString("CodeFormatterPreferencePage.tab_char.label"); //$NON-NLS-1$
304 addCheckBox(styleComposite, label, PREF_TAB_CHAR, new String[] { TAB,
307 label = PHPUIMessages
308 .getString("CodeFormatterPreferencePage.tab_size.label"); //$NON-NLS-1$
309 fTabSizeTextBox = addTextField(styleComposite, label, PREF_TAB_SIZE);
311 TabItem item = new TabItem(folder, SWT.NONE);
312 item.setText(PHPUIMessages
313 .getString("CodeFormatterPreferencePage.tab.newline.tabtitle")); //$NON-NLS-1$
314 item.setControl(newlineComposite);
316 item = new TabItem(folder, SWT.NONE);
318 .setText(PHPUIMessages
319 .getString("CodeFormatterPreferencePage.tab.linesplit.tabtitle")); //$NON-NLS-1$
320 item.setControl(lineSplittingComposite);
322 item = new TabItem(folder, SWT.NONE);
323 item.setText(PHPUIMessages
324 .getString("CodeFormatterPreferencePage.tab.style.tabtitle")); //$NON-NLS-1$
325 item.setControl(styleComposite);
327 // fSourceViewer= createPreview(parent);
334 // private SourceViewer createPreview(Composite parent) {
335 // SourceViewer previewViewer= new SourceViewer(parent, null, SWT.V_SCROLL |
336 // SWT.H_SCROLL | SWT.BORDER);
337 // JavaTextTools tools= JavaPlugin.getDefault().getJavaTextTools();
338 // previewViewer.configure(new PHPSourceViewerConfiguration(tools, null));
339 // previewViewer.getTextWidget().setFont(JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT));
340 // previewViewer.getTextWidget().setTabs(getPositiveIntValue((String)
341 // fWorkingValues.get(PREF_TAB_SIZE), 0));
342 // previewViewer.setEditable(false);
343 // previewViewer.setDocument(fPreviewDocument);
344 // Control control= previewViewer.getControl();
345 // GridData gdata= new GridData(GridData.FILL_BOTH);
346 // gdata.widthHint= convertWidthInCharsToPixels(30);
347 // gdata.heightHint= convertHeightInCharsToPixels(5);
348 // control.setLayoutData(gdata);
349 // return previewViewer;
352 private Button addCheckBox(Composite parent, String label, String key,
354 ControlData data = new ControlData(key, values);
356 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
357 gd.horizontalSpan = 2;
359 Button checkBox = new Button(parent, SWT.CHECK);
360 checkBox.setText(label);
361 checkBox.setData(data);
362 checkBox.setLayoutData(gd);
364 String currValue = (String) fWorkingValues.get(key);
365 checkBox.setSelection(data.getSelection(currValue) == 0);
366 checkBox.addSelectionListener(fButtonSelectionListener);
368 fCheckBoxes.add(checkBox);
372 private Text addTextField(Composite parent, String label, String key) {
373 Label labelControl = new Label(parent, SWT.NONE);
374 labelControl.setText(label);
375 labelControl.setLayoutData(new GridData());
377 Text textBox = new Text(parent, SWT.BORDER | SWT.SINGLE);
378 textBox.setData(key);
379 textBox.setLayoutData(new GridData());
381 String currValue = (String) fWorkingValues.get(key);
382 textBox.setText(String.valueOf(getPositiveIntValue(currValue, 1)));
383 textBox.setTextLimit(3);
384 textBox.addModifyListener(fTextModifyListener);
386 GridData gd = new GridData();
387 gd.widthHint = convertWidthInCharsToPixels(5);
388 textBox.setLayoutData(gd);
390 fTextBoxes.add(textBox);
394 private void controlChanged(Button button) {
395 ControlData data = (ControlData) button.getData();
396 boolean selection = button.getSelection();
397 String newValue = data.getValue(selection);
398 fWorkingValues.put(data.getKey(), newValue);
401 if (PREF_TAB_CHAR.equals(data.getKey())) {
402 updateStatus(new StatusInfo());
404 fTabSizeTextBox.setText((String) fWorkingValues
405 .get(PREF_TAB_SIZE));
410 private void textChanged(Text textControl) {
411 String key = (String) textControl.getData();
412 String number = textControl.getText();
413 IStatus status = validatePositiveNumber(number);
414 if (!status.matches(IStatus.ERROR)) {
415 fWorkingValues.put(key, number);
417 // if (PREF_TAB_SIZE.equals(key)) {
418 // fSourceViewer.getTextWidget().setTabs(getPositiveIntValue(number,
421 updateStatus(status);
426 * @see IPreferencePage#performOk()
428 public boolean performOk() {
429 String[] allKeys = getAllKeys();
430 // preserve other options
432 Hashtable actualOptions = JavaCore.getOptions();
433 for (int i = 0; i < allKeys.length; i++) {
434 String key = allKeys[i];
435 String val = (String) fWorkingValues.get(key);
436 actualOptions.put(key, val);
438 JavaCore.setOptions(actualOptions);
439 PHPeclipsePlugin.getDefault().savePluginPreferences();
440 return super.performOk();
444 * @see PreferencePage#performDefaults()
446 protected void performDefaults() {
447 fWorkingValues = JavaCore.getDefaultOptions();
449 super.performDefaults();
452 private String loadPreviewFile(String filename) {
453 String separator = System.getProperty("line.separator"); //$NON-NLS-1$
454 StringBuffer btxt = new StringBuffer(512);
455 BufferedReader rin = null;
457 rin = new BufferedReader(new InputStreamReader(getClass()
458 .getResourceAsStream(filename)));
460 while ((line = rin.readLine()) != null) {
462 btxt.append(separator);
464 } catch (IOException io) {
465 PHPeclipsePlugin.log(io);
470 } catch (IOException e) {
474 return btxt.toString();
477 private void updatePreview() {
478 ICodeFormatter formatter = ToolFactory
479 .createDefaultCodeFormatter(fWorkingValues);
480 fPreviewDocument.set(formatter.format(fPreviewText, 0, null, "\n")); //$NON-NLS-1$
483 private void updateControls() {
485 for (int i = fCheckBoxes.size() - 1; i >= 0; i--) {
486 Button curr = (Button) fCheckBoxes.get(i);
487 ControlData data = (ControlData) curr.getData();
489 String currValue = (String) fWorkingValues.get(data.getKey());
490 curr.setSelection(data.getSelection(currValue) == 0);
492 for (int i = fTextBoxes.size() - 1; i >= 0; i--) {
493 Text curr = (Text) fTextBoxes.get(i);
494 String key = (String) curr.getData();
495 String currValue = (String) fWorkingValues.get(key);
496 curr.setText(currValue);
500 private IStatus validatePositiveNumber(String number) {
501 StatusInfo status = new StatusInfo();
502 if (number.length() == 0) {
503 status.setError(PHPUIMessages
504 .getString("CodeFormatterPreferencePage.empty_input")); //$NON-NLS-1$
507 int value = Integer.parseInt(number);
510 .setError(PHPUIMessages
512 "CodeFormatterPreferencePage.invalid_input", number)); //$NON-NLS-1$
514 } catch (NumberFormatException e) {
515 status.setError(PHPUIMessages.getFormattedString(
516 "CodeFormatterPreferencePage.invalid_input", number)); //$NON-NLS-1$
522 private void updateStatus(IStatus status) {
523 if (!status.matches(IStatus.ERROR)) {
524 // look if there are more severe errors
525 for (int i = 0; i < fTextBoxes.size(); i++) {
526 Text curr = (Text) fTextBoxes.get(i);
527 if (!(curr == fTabSizeTextBox && usesTabs())) {
528 IStatus currStatus = validatePositiveNumber(curr.getText());
529 status = StatusUtil.getMoreSevere(currStatus, status);
533 setValid(!status.matches(IStatus.ERROR));
534 StatusUtil.applyToStatusLine(this, status);
537 private boolean usesTabs() {
538 return TAB.equals(fWorkingValues.get(PREF_TAB_CHAR));