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,
80 PREF_NEWLINE_EMPTY_BLOCK, PREF_LINE_SPLIT,
81 PREF_STYLE_COMPACT_ASSIGNEMENT, PREF_TAB_CHAR, PREF_TAB_SIZE
86 * Gets the currently configured tab size
87 * @deprecated Inline to avoid reference to preference page
89 public static int getTabSize() {
90 String string= (String) PHPCore.getOptions().get(PREF_TAB_SIZE);
91 return getPositiveIntValue(string, 4);
95 * Gets the current compating assignement configuration
96 * @deprecated Inline to avoid reference to preference page
98 public static boolean isCompactingAssignment() {
99 return COMPACT.equals(PHPCore.getOptions().get(PREF_STYLE_COMPACT_ASSIGNEMENT));
103 * Gets the current compating assignement configuration
104 * @deprecated Inline to avoid reference to preference page
106 public static boolean useSpaces() {
107 return SPACE.equals(PHPCore.getOptions().get(PREF_TAB_CHAR));
111 private static int getPositiveIntValue(String string, int dflt) {
113 int i= Integer.parseInt(string);
117 } catch (NumberFormatException e) {
122 private static class ControlData {
124 private String[] fValues;
126 public ControlData(String key, String[] values) {
131 public String getKey() {
135 public String getValue(boolean selection) {
136 int index= selection ? 0 : 1;
137 return fValues[index];
140 public String getValue(int index) {
141 return fValues[index];
144 public int getSelection(String value) {
145 for (int i= 0; i < fValues.length; i++) {
146 if (value.equals(fValues[i])) {
150 throw new IllegalArgumentException();
154 private Hashtable fWorkingValues;
156 private ArrayList fCheckBoxes;
157 private ArrayList fTextBoxes;
159 private SelectionListener fButtonSelectionListener;
160 private ModifyListener fTextModifyListener;
162 private String fPreviewText;
163 private IDocument fPreviewDocument;
165 private Text fTabSizeTextBox;
166 // private SourceViewer fSourceViewer;
169 public CodeFormatterPreferencePage() {
170 setPreferenceStore(PHPeclipsePlugin.getDefault().getPreferenceStore());
171 setDescription(PHPUIMessages.getString("CodeFormatterPreferencePage.description")); //$NON-NLS-1$
173 fWorkingValues= PHPCore.getOptions();
174 fCheckBoxes= new ArrayList();
175 fTextBoxes= new ArrayList();
177 fButtonSelectionListener= new SelectionListener() {
178 public void widgetDefaultSelected(SelectionEvent e) {}
180 public void widgetSelected(SelectionEvent e) {
181 if (!e.widget.isDisposed()) {
182 controlChanged((Button) e.widget);
187 fTextModifyListener= new ModifyListener() {
188 public void modifyText(ModifyEvent e) {
189 if (!e.widget.isDisposed()) {
190 textChanged((Text) e.widget);
195 fPreviewDocument= new Document();
196 fPreviewText= loadPreviewFile("CodeFormatterPreviewCode.txt"); //$NON-NLS-1$
200 * @see IWorkbenchPreferencePage#init()
202 public void init(IWorkbench workbench) {
206 * @see PreferencePage#createControl(Composite)
208 public void createControl(Composite parent) {
209 super.createControl(parent);
210 // WorkbenchHelp.setHelp(getControl(), IJavaHelpContextIds.CODEFORMATTER_PREFERENCE_PAGE);
214 * @see PreferencePage#createContents(Composite)
216 protected Control createContents(Composite parent) {
218 GridLayout layout= new GridLayout();
219 layout.marginHeight= 0;
220 layout.marginWidth= 0;
222 Composite composite= new Composite(parent, SWT.NONE);
223 composite.setLayout(layout);
226 TabFolder folder= new TabFolder(composite, SWT.NONE);
227 folder.setLayout(new TabFolderLayout());
228 folder.setLayoutData(new GridData(GridData.FILL_BOTH));
230 String[] insertNotInsert= new String[] { INSERT, DO_NOT_INSERT };
232 layout= new GridLayout();
233 layout.numColumns= 2;
235 Composite newlineComposite= new Composite(folder, SWT.NULL);
236 newlineComposite.setLayout(layout);
238 String label= PHPUIMessages.getString("CodeFormatterPreferencePage.newline_opening_braces.label"); //$NON-NLS-1$
239 addCheckBox(newlineComposite, label, PREF_NEWLINE_OPENING_BRACES, insertNotInsert);
241 label= PHPUIMessages.getString("CodeFormatterPreferencePage.newline_control_statement.label"); //$NON-NLS-1$
242 addCheckBox(newlineComposite, label, PREF_NEWLINE_CONTROL_STATEMENT, insertNotInsert);
244 label= PHPUIMessages.getString("CodeFormatterPreferencePage.newline_clear_lines"); //$NON-NLS-1$
245 addCheckBox(newlineComposite, label, PREF_NEWLINE_CLEAR_ALL, new String[] { CLEAR_ALL, PRESERVE_ONE } );
247 // label= PHPUIMessages.getString("CodeFormatterPreferencePage.newline_else_if.label"); //$NON-NLS-1$
248 // addCheckBox(newlineComposite, label, PREF_NEWLINE_ELSE_IF, insertNotInsert);
250 label= PHPUIMessages.getString("CodeFormatterPreferencePage.newline_empty_block.label"); //$NON-NLS-1$
251 addCheckBox(newlineComposite, label, PREF_NEWLINE_EMPTY_BLOCK, insertNotInsert);
253 layout= new GridLayout();
254 layout.numColumns= 2;
256 Composite lineSplittingComposite= new Composite(folder, SWT.NULL);
257 lineSplittingComposite.setLayout(layout);
259 label= PHPUIMessages.getString("CodeFormatterPreferencePage.split_line.label"); //$NON-NLS-1$
260 addTextField(lineSplittingComposite, label, PREF_LINE_SPLIT);
262 layout= new GridLayout();
263 layout.numColumns= 2;
265 Composite styleComposite= new Composite(folder, SWT.NULL);
266 styleComposite.setLayout(layout);
268 label= PHPUIMessages.getString("CodeFormatterPreferencePage.style_compact_assignement.label"); //$NON-NLS-1$
269 addCheckBox(styleComposite, label, PREF_STYLE_COMPACT_ASSIGNEMENT, new String[] { COMPACT, NORMAL } );
271 label= PHPUIMessages.getString("CodeFormatterPreferencePage.tab_char.label"); //$NON-NLS-1$
272 addCheckBox(styleComposite, label, PREF_TAB_CHAR, new String[] { TAB, SPACE } );
274 label= PHPUIMessages.getString("CodeFormatterPreferencePage.tab_size.label"); //$NON-NLS-1$
275 fTabSizeTextBox= addTextField(styleComposite, label, PREF_TAB_SIZE);
277 TabItem item= new TabItem(folder, SWT.NONE);
278 item.setText(PHPUIMessages.getString("CodeFormatterPreferencePage.tab.newline.tabtitle")); //$NON-NLS-1$
279 item.setControl(newlineComposite);
281 item= new TabItem(folder, SWT.NONE);
282 item.setText(PHPUIMessages.getString("CodeFormatterPreferencePage.tab.linesplit.tabtitle")); //$NON-NLS-1$
283 item.setControl(lineSplittingComposite);
285 item= new TabItem(folder, SWT.NONE);
286 item.setText(PHPUIMessages.getString("CodeFormatterPreferencePage.tab.style.tabtitle")); //$NON-NLS-1$
287 item.setControl(styleComposite);
289 // fSourceViewer= createPreview(parent);
296 // private SourceViewer createPreview(Composite parent) {
297 // SourceViewer previewViewer= new SourceViewer(parent, null, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
298 // JavaTextTools tools= JavaPlugin.getDefault().getJavaTextTools();
299 // previewViewer.configure(new PHPSourceViewerConfiguration(tools, null));
300 // previewViewer.getTextWidget().setFont(JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT));
301 // previewViewer.getTextWidget().setTabs(getPositiveIntValue((String) fWorkingValues.get(PREF_TAB_SIZE), 0));
302 // previewViewer.setEditable(false);
303 // previewViewer.setDocument(fPreviewDocument);
304 // Control control= previewViewer.getControl();
305 // GridData gdata= new GridData(GridData.FILL_BOTH);
306 // gdata.widthHint= convertWidthInCharsToPixels(30);
307 // gdata.heightHint= convertHeightInCharsToPixels(5);
308 // control.setLayoutData(gdata);
309 // return previewViewer;
313 private Button addCheckBox(Composite parent, String label, String key, String[] values) {
314 ControlData data= new ControlData(key, values);
316 GridData gd= new GridData(GridData.FILL_HORIZONTAL);
317 gd.horizontalSpan= 2;
319 Button checkBox= new Button(parent, SWT.CHECK);
320 checkBox.setText(label);
321 checkBox.setData(data);
322 checkBox.setLayoutData(gd);
324 String currValue= (String)fWorkingValues.get(key);
325 checkBox.setSelection(data.getSelection(currValue) == 0);
326 checkBox.addSelectionListener(fButtonSelectionListener);
328 fCheckBoxes.add(checkBox);
332 private Text addTextField(Composite parent, String label, String key) {
333 Label labelControl= new Label(parent, SWT.NONE);
334 labelControl.setText(label);
335 labelControl.setLayoutData(new GridData());
337 Text textBox= new Text(parent, SWT.BORDER | SWT.SINGLE);
338 textBox.setData(key);
339 textBox.setLayoutData(new GridData());
341 String currValue= (String)fWorkingValues.get(key);
342 textBox.setText(String.valueOf(getPositiveIntValue(currValue, 1)));
343 textBox.setTextLimit(3);
344 textBox.addModifyListener(fTextModifyListener);
346 GridData gd= new GridData();
347 gd.widthHint= convertWidthInCharsToPixels(5);
348 textBox.setLayoutData(gd);
350 fTextBoxes.add(textBox);
354 private void controlChanged(Button button) {
355 ControlData data= (ControlData) button.getData();
356 boolean selection= button.getSelection();
357 String newValue= data.getValue(selection);
358 fWorkingValues.put(data.getKey(), newValue);
361 if (PREF_TAB_CHAR.equals(data.getKey())) {
362 updateStatus(new StatusInfo());
364 fTabSizeTextBox.setText((String)fWorkingValues.get(PREF_TAB_SIZE));
369 private void textChanged(Text textControl) {
370 String key= (String) textControl.getData();
371 String number= textControl.getText();
372 IStatus status= validatePositiveNumber(number);
373 if (!status.matches(IStatus.ERROR)) {
374 fWorkingValues.put(key, number);
376 // if (PREF_TAB_SIZE.equals(key)) {
377 // fSourceViewer.getTextWidget().setTabs(getPositiveIntValue(number, 0));
379 updateStatus(status);
385 * @see IPreferencePage#performOk()
387 public boolean performOk() {
388 String[] allKeys= getAllKeys();
389 // preserve other options
391 Hashtable actualOptions= PHPCore.getOptions();
392 for (int i= 0; i < allKeys.length; i++) {
393 String key= allKeys[i];
394 String val= (String) fWorkingValues.get(key);
395 actualOptions.put(key, val);
397 PHPCore.setOptions(actualOptions);
398 PHPeclipsePlugin.getDefault().savePluginPreferences();
399 return super.performOk();
403 * @see PreferencePage#performDefaults()
405 protected void performDefaults() {
406 fWorkingValues= PHPCore.getDefaultOptions();
408 super.performDefaults();
411 private String loadPreviewFile(String filename) {
412 String separator= System.getProperty("line.separator"); //$NON-NLS-1$
413 StringBuffer btxt= new StringBuffer(512);
414 BufferedReader rin= null;
416 rin= new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(filename)));
418 while ((line= rin.readLine()) != null) {
420 btxt.append(separator);
422 } catch (IOException io) {
423 PHPeclipsePlugin.log(io);
426 try { rin.close(); } catch (IOException e) {}
429 return btxt.toString();
433 private void updatePreview() {
434 ICodeFormatter formatter= ToolFactory.createDefaultCodeFormatter(fWorkingValues);
435 fPreviewDocument.set(formatter.format(fPreviewText, 0, null, "\n")); //$NON-NLS-1$
438 private void updateControls() {
440 for (int i= fCheckBoxes.size() - 1; i >= 0; i--) {
441 Button curr= (Button) fCheckBoxes.get(i);
442 ControlData data= (ControlData) curr.getData();
444 String currValue= (String) fWorkingValues.get(data.getKey());
445 curr.setSelection(data.getSelection(currValue) == 0);
447 for (int i= fTextBoxes.size() - 1; i >= 0; i--) {
448 Text curr= (Text) fTextBoxes.get(i);
449 String key= (String) curr.getData();
450 String currValue= (String) fWorkingValues.get(key);
451 curr.setText(currValue);
455 private IStatus validatePositiveNumber(String number) {
456 StatusInfo status= new StatusInfo();
457 if (number.length() == 0) {
458 status.setError(PHPUIMessages.getString("CodeFormatterPreferencePage.empty_input")); //$NON-NLS-1$
461 int value= Integer.parseInt(number);
463 status.setError(PHPUIMessages.getFormattedString("CodeFormatterPreferencePage.invalid_input", number)); //$NON-NLS-1$
465 } catch (NumberFormatException e) {
466 status.setError(PHPUIMessages.getFormattedString("CodeFormatterPreferencePage.invalid_input", number)); //$NON-NLS-1$
473 private void updateStatus(IStatus status) {
474 if (!status.matches(IStatus.ERROR)) {
475 // look if there are more severe errors
476 for (int i= 0; i < fTextBoxes.size(); i++) {
477 Text curr= (Text) fTextBoxes.get(i);
478 if (!(curr == fTabSizeTextBox && usesTabs())) {
479 IStatus currStatus= validatePositiveNumber(curr.getText());
480 status= StatusUtil.getMoreSevere(currStatus, status);
484 setValid(!status.matches(IStatus.ERROR));
485 StatusUtil.applyToStatusLine(this, status);
488 private boolean usesTabs() {
489 return TAB.equals(fWorkingValues.get(PREF_TAB_CHAR));