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.ToolFactory;
15 import net.sourceforge.phpdt.internal.ui.PHPUIMessages;
16 import net.sourceforge.phpdt.internal.ui.dialog.StatusInfo;
17 import net.sourceforge.phpdt.internal.ui.dialog.StatusUtil;
18 import net.sourceforge.phpdt.internal.ui.util.TabFolderLayout;
19 import net.sourceforge.phpeclipse.PHPCore;
20 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
21 import net.sourceforge.phpeclipse.phpeditor.PHPSourceViewerConfiguration;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.jface.preference.PreferencePage;
25 import org.eclipse.jface.resource.JFaceResources;
26 import org.eclipse.jface.text.Document;
27 import org.eclipse.jface.text.IDocument;
28 import org.eclipse.jface.text.source.SourceViewer;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.events.ModifyEvent;
31 import org.eclipse.swt.events.ModifyListener;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.events.SelectionListener;
34 import org.eclipse.swt.layout.GridData;
35 import org.eclipse.swt.layout.GridLayout;
36 import org.eclipse.swt.widgets.Button;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Control;
39 import org.eclipse.swt.widgets.Label;
40 import org.eclipse.swt.widgets.TabFolder;
41 import org.eclipse.swt.widgets.TabItem;
42 import org.eclipse.swt.widgets.Text;
43 import org.eclipse.ui.IWorkbench;
44 import org.eclipse.ui.IWorkbenchPreferencePage;
47 * The page for setting code formatter options
49 public class CodeFormatterPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
51 // Preference store keys, see PHPCore.getOptions
52 private static final String PREF_NEWLINE_OPENING_BRACES= PHPCore.FORMATTER_NEWLINE_OPENING_BRACE;
53 private static final String PREF_NEWLINE_CONTROL_STATEMENT= PHPCore.FORMATTER_NEWLINE_CONTROL;
54 private static final String PREF_NEWLINE_CLEAR_ALL= PHPCore.FORMATTER_CLEAR_BLANK_LINES;
55 private static final String PREF_NEWLINE_ELSE_IF= PHPCore.FORMATTER_NEWLINE_ELSE_IF;
56 private static final String PREF_NEWLINE_EMPTY_BLOCK= PHPCore.FORMATTER_NEWLINE_EMPTY_BLOCK;
57 private static final String PREF_LINE_SPLIT= PHPCore.FORMATTER_LINE_SPLIT;
58 private static final String PREF_STYLE_COMPACT_ASSIGNEMENT= PHPCore.FORMATTER_COMPACT_ASSIGNMENT;
59 private static final String PREF_TAB_CHAR= PHPCore.FORMATTER_TAB_CHAR;
60 private static final String PREF_TAB_SIZE= PHPCore.FORMATTER_TAB_SIZE;
63 private static final String INSERT= PHPCore.INSERT;
64 private static final String DO_NOT_INSERT= PHPCore.DO_NOT_INSERT;
66 private static final String COMPACT= PHPCore.COMPACT;
67 private static final String NORMAL= PHPCore.NORMAL;
69 private static final String TAB= PHPCore.TAB;
70 private static final String SPACE= PHPCore.SPACE;
72 private static final String CLEAR_ALL= PHPCore.CLEAR_ALL;
73 private static final String PRESERVE_ONE= PHPCore.PRESERVE_ONE;
76 private static String[] getAllKeys() {
78 PREF_NEWLINE_OPENING_BRACES, PREF_NEWLINE_CONTROL_STATEMENT, PREF_NEWLINE_CLEAR_ALL,
79 PREF_NEWLINE_ELSE_IF, PREF_NEWLINE_EMPTY_BLOCK, PREF_LINE_SPLIT,
80 PREF_STYLE_COMPACT_ASSIGNEMENT, PREF_TAB_CHAR, PREF_TAB_SIZE
85 * Gets the currently configured tab size
86 * @deprecated Inline to avoid reference to preference page
88 public static int getTabSize() {
89 String string= (String) PHPCore.getOptions().get(PREF_TAB_SIZE);
90 return getPositiveIntValue(string, 4);
94 * Gets the current compating assignement configuration
95 * @deprecated Inline to avoid reference to preference page
97 public static boolean isCompactingAssignment() {
98 return COMPACT.equals(PHPCore.getOptions().get(PREF_STYLE_COMPACT_ASSIGNEMENT));
102 * Gets the current compating assignement configuration
103 * @deprecated Inline to avoid reference to preference page
105 public static boolean useSpaces() {
106 return SPACE.equals(PHPCore.getOptions().get(PREF_TAB_CHAR));
110 private static int getPositiveIntValue(String string, int dflt) {
112 int i= Integer.parseInt(string);
116 } catch (NumberFormatException e) {
121 private static class ControlData {
123 private String[] fValues;
125 public ControlData(String key, String[] values) {
130 public String getKey() {
134 public String getValue(boolean selection) {
135 int index= selection ? 0 : 1;
136 return fValues[index];
139 public String getValue(int index) {
140 return fValues[index];
143 public int getSelection(String value) {
144 for (int i= 0; i < fValues.length; i++) {
145 if (value.equals(fValues[i])) {
149 throw new IllegalArgumentException();
153 private Hashtable fWorkingValues;
155 private ArrayList fCheckBoxes;
156 private ArrayList fTextBoxes;
158 private SelectionListener fButtonSelectionListener;
159 private ModifyListener fTextModifyListener;
161 private String fPreviewText;
162 private IDocument fPreviewDocument;
164 private Text fTabSizeTextBox;
165 // private SourceViewer fSourceViewer;
168 public CodeFormatterPreferencePage() {
169 setPreferenceStore(PHPeclipsePlugin.getDefault().getPreferenceStore());
170 setDescription(PHPUIMessages.getString("CodeFormatterPreferencePage.description")); //$NON-NLS-1$
172 fWorkingValues= PHPCore.getOptions();
173 fCheckBoxes= new ArrayList();
174 fTextBoxes= new ArrayList();
176 fButtonSelectionListener= new SelectionListener() {
177 public void widgetDefaultSelected(SelectionEvent e) {}
179 public void widgetSelected(SelectionEvent e) {
180 if (!e.widget.isDisposed()) {
181 controlChanged((Button) e.widget);
186 fTextModifyListener= new ModifyListener() {
187 public void modifyText(ModifyEvent e) {
188 if (!e.widget.isDisposed()) {
189 textChanged((Text) e.widget);
194 fPreviewDocument= new Document();
195 fPreviewText= loadPreviewFile("CodeFormatterPreviewCode.txt"); //$NON-NLS-1$
199 * @see IWorkbenchPreferencePage#init()
201 public void init(IWorkbench workbench) {
205 * @see PreferencePage#createControl(Composite)
207 public void createControl(Composite parent) {
208 super.createControl(parent);
209 // WorkbenchHelp.setHelp(getControl(), IJavaHelpContextIds.CODEFORMATTER_PREFERENCE_PAGE);
213 * @see PreferencePage#createContents(Composite)
215 protected Control createContents(Composite parent) {
217 GridLayout layout= new GridLayout();
218 layout.marginHeight= 0;
219 layout.marginWidth= 0;
221 Composite composite= new Composite(parent, SWT.NONE);
222 composite.setLayout(layout);
225 TabFolder folder= new TabFolder(composite, SWT.NONE);
226 folder.setLayout(new TabFolderLayout());
227 folder.setLayoutData(new GridData(GridData.FILL_BOTH));
229 String[] insertNotInsert= new String[] { INSERT, DO_NOT_INSERT };
231 layout= new GridLayout();
232 layout.numColumns= 2;
234 Composite newlineComposite= new Composite(folder, SWT.NULL);
235 newlineComposite.setLayout(layout);
237 String label= PHPUIMessages.getString("CodeFormatterPreferencePage.newline_opening_braces.label"); //$NON-NLS-1$
238 addCheckBox(newlineComposite, label, PREF_NEWLINE_OPENING_BRACES, insertNotInsert);
240 label= PHPUIMessages.getString("CodeFormatterPreferencePage.newline_control_statement.label"); //$NON-NLS-1$
241 addCheckBox(newlineComposite, label, PREF_NEWLINE_CONTROL_STATEMENT, insertNotInsert);
243 label= PHPUIMessages.getString("CodeFormatterPreferencePage.newline_clear_lines"); //$NON-NLS-1$
244 addCheckBox(newlineComposite, label, PREF_NEWLINE_CLEAR_ALL, new String[] { CLEAR_ALL, PRESERVE_ONE } );
246 label= PHPUIMessages.getString("CodeFormatterPreferencePage.newline_else_if.label"); //$NON-NLS-1$
247 addCheckBox(newlineComposite, label, PREF_NEWLINE_ELSE_IF, insertNotInsert);
249 label= PHPUIMessages.getString("CodeFormatterPreferencePage.newline_empty_block.label"); //$NON-NLS-1$
250 addCheckBox(newlineComposite, label, PREF_NEWLINE_EMPTY_BLOCK, insertNotInsert);
252 layout= new GridLayout();
253 layout.numColumns= 2;
255 Composite lineSplittingComposite= new Composite(folder, SWT.NULL);
256 lineSplittingComposite.setLayout(layout);
258 label= PHPUIMessages.getString("CodeFormatterPreferencePage.split_line.label"); //$NON-NLS-1$
259 addTextField(lineSplittingComposite, label, PREF_LINE_SPLIT);
261 layout= new GridLayout();
262 layout.numColumns= 2;
264 Composite styleComposite= new Composite(folder, SWT.NULL);
265 styleComposite.setLayout(layout);
267 label= PHPUIMessages.getString("CodeFormatterPreferencePage.style_compact_assignement.label"); //$NON-NLS-1$
268 addCheckBox(styleComposite, label, PREF_STYLE_COMPACT_ASSIGNEMENT, new String[] { COMPACT, NORMAL } );
270 label= PHPUIMessages.getString("CodeFormatterPreferencePage.tab_char.label"); //$NON-NLS-1$
271 addCheckBox(styleComposite, label, PREF_TAB_CHAR, new String[] { TAB, SPACE } );
273 label= PHPUIMessages.getString("CodeFormatterPreferencePage.tab_size.label"); //$NON-NLS-1$
274 fTabSizeTextBox= addTextField(styleComposite, label, PREF_TAB_SIZE);
276 TabItem item= new TabItem(folder, SWT.NONE);
277 item.setText(PHPUIMessages.getString("CodeFormatterPreferencePage.tab.newline.tabtitle")); //$NON-NLS-1$
278 item.setControl(newlineComposite);
280 item= new TabItem(folder, SWT.NONE);
281 item.setText(PHPUIMessages.getString("CodeFormatterPreferencePage.tab.linesplit.tabtitle")); //$NON-NLS-1$
282 item.setControl(lineSplittingComposite);
284 item= new TabItem(folder, SWT.NONE);
285 item.setText(PHPUIMessages.getString("CodeFormatterPreferencePage.tab.style.tabtitle")); //$NON-NLS-1$
286 item.setControl(styleComposite);
288 // fSourceViewer= createPreview(parent);
295 // private SourceViewer createPreview(Composite parent) {
296 // SourceViewer previewViewer= new SourceViewer(parent, null, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
297 // JavaTextTools tools= JavaPlugin.getDefault().getJavaTextTools();
298 // previewViewer.configure(new PHPSourceViewerConfiguration(tools, null));
299 // previewViewer.getTextWidget().setFont(JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT));
300 // previewViewer.getTextWidget().setTabs(getPositiveIntValue((String) fWorkingValues.get(PREF_TAB_SIZE), 0));
301 // previewViewer.setEditable(false);
302 // previewViewer.setDocument(fPreviewDocument);
303 // Control control= previewViewer.getControl();
304 // GridData gdata= new GridData(GridData.FILL_BOTH);
305 // gdata.widthHint= convertWidthInCharsToPixels(30);
306 // gdata.heightHint= convertHeightInCharsToPixels(5);
307 // control.setLayoutData(gdata);
308 // return previewViewer;
312 private Button addCheckBox(Composite parent, String label, String key, String[] values) {
313 ControlData data= new ControlData(key, values);
315 GridData gd= new GridData(GridData.FILL_HORIZONTAL);
316 gd.horizontalSpan= 2;
318 Button checkBox= new Button(parent, SWT.CHECK);
319 checkBox.setText(label);
320 checkBox.setData(data);
321 checkBox.setLayoutData(gd);
323 String currValue= (String)fWorkingValues.get(key);
324 checkBox.setSelection(data.getSelection(currValue) == 0);
325 checkBox.addSelectionListener(fButtonSelectionListener);
327 fCheckBoxes.add(checkBox);
331 private Text addTextField(Composite parent, String label, String key) {
332 Label labelControl= new Label(parent, SWT.NONE);
333 labelControl.setText(label);
334 labelControl.setLayoutData(new GridData());
336 Text textBox= new Text(parent, SWT.BORDER | SWT.SINGLE);
337 textBox.setData(key);
338 textBox.setLayoutData(new GridData());
340 String currValue= (String)fWorkingValues.get(key);
341 textBox.setText(String.valueOf(getPositiveIntValue(currValue, 1)));
342 textBox.setTextLimit(3);
343 textBox.addModifyListener(fTextModifyListener);
345 GridData gd= new GridData();
346 gd.widthHint= convertWidthInCharsToPixels(5);
347 textBox.setLayoutData(gd);
349 fTextBoxes.add(textBox);
353 private void controlChanged(Button button) {
354 ControlData data= (ControlData) button.getData();
355 boolean selection= button.getSelection();
356 String newValue= data.getValue(selection);
357 fWorkingValues.put(data.getKey(), newValue);
360 if (PREF_TAB_CHAR.equals(data.getKey())) {
361 updateStatus(new StatusInfo());
363 fTabSizeTextBox.setText((String)fWorkingValues.get(PREF_TAB_SIZE));
368 private void textChanged(Text textControl) {
369 String key= (String) textControl.getData();
370 String number= textControl.getText();
371 IStatus status= validatePositiveNumber(number);
372 if (!status.matches(IStatus.ERROR)) {
373 fWorkingValues.put(key, number);
375 // if (PREF_TAB_SIZE.equals(key)) {
376 // fSourceViewer.getTextWidget().setTabs(getPositiveIntValue(number, 0));
378 updateStatus(status);
384 * @see IPreferencePage#performOk()
386 public boolean performOk() {
387 String[] allKeys= getAllKeys();
388 // preserve other options
390 Hashtable actualOptions= PHPCore.getOptions();
391 for (int i= 0; i < allKeys.length; i++) {
392 String key= allKeys[i];
393 String val= (String) fWorkingValues.get(key);
394 actualOptions.put(key, val);
396 PHPCore.setOptions(actualOptions);
397 PHPeclipsePlugin.getDefault().savePluginPreferences();
398 return super.performOk();
402 * @see PreferencePage#performDefaults()
404 protected void performDefaults() {
405 fWorkingValues= PHPCore.getDefaultOptions();
407 super.performDefaults();
410 private String loadPreviewFile(String filename) {
411 String separator= System.getProperty("line.separator"); //$NON-NLS-1$
412 StringBuffer btxt= new StringBuffer(512);
413 BufferedReader rin= null;
415 rin= new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(filename)));
417 while ((line= rin.readLine()) != null) {
419 btxt.append(separator);
421 } catch (IOException io) {
422 PHPeclipsePlugin.log(io);
425 try { rin.close(); } catch (IOException e) {}
428 return btxt.toString();
432 private void updatePreview() {
433 ICodeFormatter formatter= ToolFactory.createDefaultCodeFormatter(fWorkingValues);
434 fPreviewDocument.set(formatter.format(fPreviewText, 0, null, "\n")); //$NON-NLS-1$
437 private void updateControls() {
439 for (int i= fCheckBoxes.size() - 1; i >= 0; i--) {
440 Button curr= (Button) fCheckBoxes.get(i);
441 ControlData data= (ControlData) curr.getData();
443 String currValue= (String) fWorkingValues.get(data.getKey());
444 curr.setSelection(data.getSelection(currValue) == 0);
446 for (int i= fTextBoxes.size() - 1; i >= 0; i--) {
447 Text curr= (Text) fTextBoxes.get(i);
448 String key= (String) curr.getData();
449 String currValue= (String) fWorkingValues.get(key);
450 curr.setText(currValue);
454 private IStatus validatePositiveNumber(String number) {
455 StatusInfo status= new StatusInfo();
456 if (number.length() == 0) {
457 status.setError(PHPUIMessages.getString("CodeFormatterPreferencePage.empty_input")); //$NON-NLS-1$
460 int value= Integer.parseInt(number);
462 status.setError(PHPUIMessages.getFormattedString("CodeFormatterPreferencePage.invalid_input", number)); //$NON-NLS-1$
464 } catch (NumberFormatException e) {
465 status.setError(PHPUIMessages.getFormattedString("CodeFormatterPreferencePage.invalid_input", number)); //$NON-NLS-1$
472 private void updateStatus(IStatus status) {
473 if (!status.matches(IStatus.ERROR)) {
474 // look if there are more severe errors
475 for (int i= 0; i < fTextBoxes.size(); i++) {
476 Text curr= (Text) fTextBoxes.get(i);
477 if (!(curr == fTabSizeTextBox && usesTabs())) {
478 IStatus currStatus= validatePositiveNumber(curr.getText());
479 status= StatusUtil.getMoreSevere(currStatus, status);
483 setValid(!status.matches(IStatus.ERROR));
484 StatusUtil.applyToStatusLine(this, status);
487 private boolean usesTabs() {
488 return TAB.equals(fWorkingValues.get(PREF_TAB_CHAR));