]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/preferences/formatter/ModifyDialogTabPage.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / preferences / formatter / ModifyDialogTabPage.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.jdt.internal.ui.preferences.formatter;
12
13import java.util.ArrayList;
14import java.util.HashMap;
15import java.util.List;
16import java.util.Map;
17import java.util.Observable;
18import java.util.Observer;
19
20import org.eclipse.swt.SWT;
21import org.eclipse.swt.custom.SashForm;
22import org.eclipse.swt.custom.ScrolledComposite;
23import org.eclipse.swt.events.ControlEvent;
24import org.eclipse.swt.events.ControlListener;
25import org.eclipse.swt.events.FocusAdapter;
26import org.eclipse.swt.events.FocusEvent;
27import org.eclipse.swt.events.FocusListener;
28import org.eclipse.swt.events.ModifyEvent;
29import org.eclipse.swt.events.ModifyListener;
30import org.eclipse.swt.events.SelectionAdapter;
31import org.eclipse.swt.events.SelectionEvent;
32import org.eclipse.swt.graphics.Point;
33import org.eclipse.swt.graphics.Rectangle;
34import org.eclipse.swt.layout.GridData;
35import org.eclipse.swt.layout.GridLayout;
36import org.eclipse.swt.widgets.Button;
37import org.eclipse.swt.widgets.Combo;
38import org.eclipse.swt.widgets.Composite;
39import org.eclipse.swt.widgets.Control;
40import org.eclipse.swt.widgets.Event;
41import org.eclipse.swt.widgets.Group;
42import org.eclipse.swt.widgets.Label;
43import org.eclipse.swt.widgets.Layout;
44import org.eclipse.swt.widgets.Listener;
45import org.eclipse.swt.widgets.Text;
46
47import org.eclipse.core.runtime.IStatus;
48import org.eclipse.core.runtime.Status;
49
50import org.eclipse.jface.dialogs.IDialogConstants;
51import org.eclipse.jface.dialogs.IDialogSettings;
52import org.eclipse.jface.dialogs.IInputValidator;
53import org.eclipse.jface.layout.PixelConverter;
54
55import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
56
57import org.eclipse.jdt.internal.corext.util.Messages;
58
59import org.eclipse.jdt.ui.JavaUI;
60
61import org.eclipse.jdt.internal.ui.JavaPlugin;
62import org.eclipse.jdt.internal.ui.preferences.cleanup.CleanUpTabPage;
63import org.eclipse.jdt.internal.ui.preferences.formatter.CommentsTabPage.OrController;
64import org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialogTabPage.CheckboxPreference;
65import org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialogTabPage.ComboPreference;
66import org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialogTabPage.NumberPreference;
67import org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialogTabPage.Preference;
68import org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialogTabPage.RadioPreference;
69import org.eclipse.jdt.internal.ui.preferences.formatter.ModifyDialogTabPage.StringPreference;
70import org.eclipse.jdt.internal.ui.preferences.formatter.WhiteSpaceTabPage.JavaElementComponent;
71import org.eclipse.jdt.internal.ui.util.SWTUtil;
72
73
74public abstract class ModifyDialogTabPage implements IModifyDialogTabPage {
75
76
77 /**
78 * This is the default listener for any of the Preference
79 * classes. It is added by the respective factory methods and
80 * updates the page's preview on each change.
81 */
82 protected final Observer fUpdater= new Observer() {
83 public void update(Observable o, Object arg) {
84 doUpdatePreview();
85 notifyValuesModified();
86 }
87 };
88
89
90 /**
91 * The base class of all Preference classes. A preference class provides a wrapper
92 * around one or more SWT widgets and handles the input of values for some key.
93 * On each change, the new value is written to the map and the listeners are notified.
94 */
95 protected abstract class Preference extends Observable {
96 private final Map<String, String> fPreferences;
97 private boolean fEnabled;
98 private String fKey;
99
100 /**
101 * Create a new Preference.
102 * @param preferences The map where the value is written.
103 * @param key The key for which a value is managed.
104 */
105 public Preference(Map<String, String> preferences, String key) {
106 fPreferences= preferences;
107 fEnabled= true;
108 fKey= key;
109 }
110 /**
111 * @return Gets the map of this Preference.
112 */
113 protected final Map<String, String> getPreferences() {
114 return fPreferences;
115 }
116
117 /**
118 * Set the enabled state of all SWT widgets of this preference.
119 * @param enabled new value
120 */
121 public final void setEnabled(boolean enabled) {
122 fEnabled= enabled;
123 updateWidget();
124 }
125
126 /**
127 * @return Gets the enabled state of all SWT widgets of this Preference.
128 */
129 public final boolean getEnabled() {
130 return fEnabled;
131 }
132
133 /**
134 * Set the key which is used to store the value.
135 * @param key New value
136 */
137 public final void setKey(String key) {
138 if (key == null || !fKey.equals(key)) {
139 fKey= key;
140 updateWidget();
141 }
142 }
143 /**
144 * @return Gets the currently used key which is used to store the value.
145 */
146 public final String getKey() {
147 return fKey;
148 }
149
150 /**
151 * Returns the main control of a preference, which is mainly used to
152 * manage the focus. This may be <code>null</code> if the preference doesn't
153 * have a control which is able to have the focus.
154 * @return The main control
155 */
156 public abstract Control getControl();
157
158 /**
159 * To be implemented in subclasses. Update the SWT widgets when the state
160 * of this object has changed (enabled, key, ...).
161 */
162 protected abstract void updateWidget();
163 public void generated_8795498692018477341(int numColumns, LineWrappingTabPage linewrappingtabpage) {
164 Control control;
165 GridData layoutData;
166 control= getControl();
167 control.setVisible(false);
168 layoutData= (GridData)control.getLayoutData();
169 layoutData.exclude= true;
170 layoutData.horizontalAlignment= SWT.BEGINNING;
171 layoutData.horizontalSpan= numColumns - 1;
172 layoutData.grabExcessHorizontalSpace= false;
173 linewrappingtabpage.fCatchCategory.addPreference(this);
174 }
175 }
176
177 /**
178 * Wrapper around a checkbox and a label.
179 */
180 protected class ButtonPreference extends Preference {
181 private final String[] fValues;
182 private final Button fCheckbox;
183
184 /**
185 * Create a new CheckboxPreference.
186 * @param composite The composite on which the SWT widgets are added.
187 * @param numColumns The number of columns in the composite's GridLayout.
188 * @param preferences The map to store the values.
189 * @param key The key to store the values.
190 * @param values An array of two elements indicating the values to store on unchecked/checked.
191 * @param text The label text for this Preference.
192 * @param style SWT style flag for the button
193 */
194 public ButtonPreference(Composite composite, int numColumns,
195 Map<String, String> preferences, String key,
196 String [] values, String text, int style) {
197 super(preferences, key);
198 if (values == null || text == null)
199 throw new IllegalArgumentException(FormatterMessages.ModifyDialogTabPage_error_msg_values_text_unassigned);
200 fValues= values;
201
202 fCheckbox= new Button(composite, style);
203 fCheckbox.setText(text);
204 fCheckbox.setLayoutData(createGridData(numColumns, GridData.FILL_HORIZONTAL, SWT.DEFAULT));
205 fCheckbox.setFont(composite.getFont());
206
207 updateWidget();
208
209 fCheckbox.addSelectionListener(new SelectionAdapter() {
210 @Override
211 public void widgetSelected(SelectionEvent e) {
212 checkboxChecked(((Button)e.widget).getSelection());
213 }
214 });
215 }
216
217 protected void checkboxChecked(boolean state) {
218 getPreferences().put(getKey(), state ? fValues[1] : fValues[0]);
219 setChanged();
220 notifyObservers();
221 }
222
223 @Override
224 protected void updateWidget() {
225 if (getKey() != null) {
226 fCheckbox.setEnabled(getEnabled());
227 fCheckbox.setSelection(getChecked());
228 } else {
229 fCheckbox.setSelection(false);
230 fCheckbox.setEnabled(false);
231 }
232 }
233
234 public boolean getChecked() {
235 return fValues[1].equals(getPreferences().get(getKey()));
236 }
237
238 public void setChecked(boolean checked) {
239 getPreferences().put(getKey(), checked ? fValues[1] : fValues[0]);
240 updateWidget();
241 checkboxChecked(checked);
242 }
243
244 @Override
245 public Control getControl() {
246 return fCheckbox;
247 }
248 }
249
250 protected final class CheckboxPreference extends ButtonPreference {
251 public CheckboxPreference(Composite composite, int numColumns, Map<String, String> preferences, String key, String[] values, String text) {
252 super(composite, numColumns, preferences, key, values, text, SWT.CHECK);
253 }
254
255 public void generated_4873144067629493899(final RadioPreference sortAllPref, final Button nullRadio, final Label warningImage, final Label warningLabel) {
256 addObserver(new Observer() {
257 public void update(Observable o, Object arg) {
258 nullRadio.setEnabled(getChecked());
259
260 boolean warningEnabled= getChecked() && sortAllPref.getChecked();
261 warningImage.setEnabled(warningEnabled);
262 warningLabel.setEnabled(warningEnabled);
263 }
264 });
265 }
266
267 public void generated_7214163981621660305(final CleanUpTabPage cleanuptabpage) {
268 addObserver(new Observer() {
269 public void update(Observable o, Object arg) {
270 if (getChecked()) {
271 cleanuptabpage.setSelectedCleanUpCount(cleanuptabpage.fSelectedCount + 1);
272 } else {
273 cleanuptabpage.setSelectedCleanUpCount(cleanuptabpage.fSelectedCount - 1);
274 }
275 }
276 });
277 if (getChecked()) {
278 cleanuptabpage.setSelectedCleanUpCount(cleanuptabpage.fSelectedCount + 1);
279 }
280 }
281
282 public void generated_6958012264297418394(final CheckboxPreference overrideInterfacePref) {
283 overrideInterfacePref.setEnabled(getEnabled() && getChecked());
284 }
285
286 public CheckboxPreference generated_5729534999395002544(ModifyDialogTabPage modifydialogtabpage) {
287 modifydialogtabpage.fDefaultFocusManager.add(this);
288 addObserver(modifydialogtabpage.fUpdater);
289 return this;
290 }
291
292 public ArrayList<CheckboxPreference> generated_1735756286725459971(final CheckboxPreference javadoc, final CheckboxPreference header, final Group blockSettingsGroup, final CheckboxPreference nlBoundariesBlock, final CheckboxPreference blankLinesBlock) {
293 ArrayList<CheckboxPreference> blockMasters= new ArrayList<CheckboxPreference>();
294 blockMasters.add(this);
295 blockMasters.add(header);
296
297 ArrayList<Object> blockSlaves= new ArrayList<Object>();
298 blockSlaves.add(blockSettingsGroup);
299 blockSlaves.add(nlBoundariesBlock);
300 blockSlaves.add(blankLinesBlock);
301
302 new OrController(blockMasters, blockSlaves);
303
304
305 ArrayList<CheckboxPreference> lineWidthMasters= new ArrayList<CheckboxPreference>();
306 lineWidthMasters.add(javadoc);
307 lineWidthMasters.add(this);
308 return lineWidthMasters;
309 }
310
311 public boolean generated_3939874930691785911(final StringPreference disableTagPref, final StringPreference enableTagPref) {
312 getControl().addListener(SWT.Selection, new Listener() {
313 public void handleEvent(Event event) {
314 boolean enabled= getChecked();
315 enableTagPref.setEnabled(enabled);
316 disableTagPref.setEnabled(enabled);
317 }
318 });
319
320 boolean enabled= getChecked();
321 return enabled;
322 }
323
324 public void generated_3724076951776666983(final ControlStatementsTabPage controlstatementstabpage, int numColumns, final Group ifElseGroup, Label l, GridData gd) {
325 l.setLayoutData(gd);
326
327 controlstatementstabpage.fSimpleIfPref= controlstatementstabpage.createOption(ifElseGroup, numColumns - 1, FormatterMessages.ControlStatementsTabPage_if_else_group_keep_simple_if_on_one_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_SIMPLE_IF_ON_ONE_LINE, FormatterTabPage.FALSE_TRUE);
328
329 addObserver( new Observer() {
330 public void update(Observable o, Object arg) {
331 controlstatementstabpage.fSimpleIfPref.setEnabled(!getChecked());
332 }
333
334 });
335
336 controlstatementstabpage.fSimpleIfPref.setEnabled(!getChecked());
337
338 controlstatementstabpage.createOption(ifElseGroup, numColumns, FormatterMessages.ControlStatementsTabPage_if_else_group_keep_else_on_same_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_ELSE_STATEMENT_ON_SAME_LINE, FormatterTabPage.FALSE_TRUE);
339 }
340
341 public void generated_2792348246262311845(final ComboPreference tabPolicy, final IndentationTabPage indentationtabpage, final NumberPreference indentSize, final NumberPreference tabSize, String tabchar) {
342 indentationtabpage.updateTabPreferences(tabchar, tabSize, indentSize, this);
343 tabPolicy.addObserver(new Observer() {
344 public void update(Observable o, Object arg) {
345 indentationtabpage.updateTabPreferences((String) arg, tabSize, indentSize, CheckboxPreference.this);
346 }
347 });
348 }
349
350 public CheckboxPreference generated_729494425690224476(BracesTabPage bracestabpage) {
351 GridData data= (GridData) getControl().getLayoutData();
352 data.horizontalIndent= bracestabpage.fPixelConverter.convertWidthInCharsToPixels(1);
353 return this;
354 }
355 }
356
357 protected final class RadioPreference extends ButtonPreference {
358 public RadioPreference(Composite composite, int numColumns, Map<String, String> preferences, String key, String[] values, String text) {
359 super(composite, numColumns, preferences, key, values, text, SWT.RADIO);
360 }
361
362 public RadioPreference generated_2286414519167355793(ModifyDialogTabPage modifydialogtabpage) {
363 modifydialogtabpage.fDefaultFocusManager.add(this);
364 addObserver(modifydialogtabpage.fUpdater);
365 return this;
366 }
367 }
368
369 /**
370 * Wrapper around a Combo box.
371 */
372 protected final class ComboPreference extends Preference {
373 private final String [] fItems;
374 private final String[] fValues;
375 private final Combo fCombo;
376
377 /**
378 * Create a new ComboPreference.
379 * @param composite The composite on which the SWT widgets are added.
380 * @param numColumns The number of columns in the composite's GridLayout.
381 * @param preferences The map to store the values.
382 * @param key The key to store the values.
383 * @param values An array of n elements indicating the values to store for each selection.
384 * @param text The label text for this Preference.
385 * @param items An array of n elements indicating the text to be written in the combo box.
386 */
387 public ComboPreference(Composite composite, int numColumns,
388 Map<String, String> preferences, String key,
389 String [] values, String text, String [] items) {
390 super(preferences, key);
391 if (values == null || items == null || text == null)
392 throw new IllegalArgumentException(FormatterMessages.ModifyDialogTabPage_error_msg_values_items_text_unassigned);
393 fValues= values;
394 fItems= items;
395 createLabel(numColumns - 1, composite, text);
396 fCombo= new Combo(composite, SWT.SINGLE | SWT.READ_ONLY);
397 fCombo.setFont(composite.getFont());
398 SWTUtil.setDefaultVisibleItemCount(fCombo);
399 fCombo.setItems(items);
400
401 int max= 0;
402 for (int i= 0; i < items.length; i++)
403 if (items[i].length() > max) max= items[i].length();
404
405 fCombo.setLayoutData(createGridData(1, GridData.HORIZONTAL_ALIGN_FILL, fCombo.computeSize(SWT.DEFAULT, SWT.DEFAULT).x));
406
407 updateWidget();
408
409 fCombo.addSelectionListener(new SelectionAdapter() {
410 @Override
411 public void widgetSelected(SelectionEvent e) {
412 comboSelected(((Combo)e.widget).getSelectionIndex());
413 }
414 });
415 }
416
417 protected void comboSelected(int index) {
418 getPreferences().put(getKey(), fValues[index]);
419 setChanged();
420 notifyObservers(fValues[index]);
421 }
422
423 @Override
424 protected void updateWidget() {
425 if (getKey() != null) {
426 fCombo.setEnabled(getEnabled());
427 fCombo.setText(getSelectedItem());
428 } else {
429 fCombo.setText(""); //$NON-NLS-1$
430 fCombo.setEnabled(false);
431 }
432 }
433
434 public String getSelectedItem() {
435 final String selected= getPreferences().get(getKey());
436 for (int i= 0; i < fValues.length; i++) {
437 if (fValues[i].equals(selected)) {
438 return fItems[i];
439 }
440 }
441 return ""; //$NON-NLS-1$
442 }
443
444 public boolean hasValue(String value) {
445 return value.equals(getPreferences().get(getKey()));
446 }
447
448 @Override
449 public Control getControl() {
450 return fCombo;
451 }
452
453 public ComboPreference generated_5306086071415273487(ModifyDialogTabPage modifydialogtabpage) {
454 modifydialogtabpage.fDefaultFocusManager.add(this);
455 addObserver(modifydialogtabpage.fUpdater);
456 return this;
457 }
458
459 public void generated_7782093339050417989(int numColumns, final Group group, final BracesTabPage bracestabpage) {
460 final CheckboxPreference arrayInitCheckBox= bracestabpage.createIndentedCheckboxPref(group, numColumns, FormatterMessages.BracesTabPage_option_keep_empty_array_initializer_on_one_line, DefaultCodeFormatterConstants.FORMATTER_KEEP_EMPTY_ARRAY_INITIALIZER_ON_ONE_LINE, FormatterTabPage.FALSE_TRUE);
461
462 addObserver(new Observer() {
463 public void update(Observable o, Object arg) {
464 bracestabpage.updateOptionEnablement((ComboPreference) o, arrayInitCheckBox);
465 }
466 });
467 bracestabpage.updateOptionEnablement(this, arrayInitCheckBox);
468 }
469 }
470
471 /**
472 * Wrapper around a textfied which requests an integer input of a given range.
473 */
474 protected final class NumberPreference extends Preference {
475
476 private final int fMinValue, fMaxValue;
477 private final Label fNumberLabel;
478 private final Text fNumberText;
479
480 protected int fSelected;
481 protected int fOldSelected;
482
483
484 /**
485 * Create a new NumberPreference.
486 * @param composite The composite on which the SWT widgets are added.
487 * @param numColumns The number of columns in the composite's GridLayout.
488 * @param preferences The map to store the values.
489 * @param key The key to store the values.
490 * @param minValue The minimum value which is valid input.
491 * @param maxValue The maximum value which is valid input.
492 * @param text The label text for this Preference.
493 */
494 public NumberPreference(Composite composite, int numColumns,
495 Map<String, String> preferences, String key,
496 int minValue, int maxValue, String text) {
497 super(preferences, key);
498
499 fNumberLabel= createLabel(numColumns - 1, composite, text, GridData.FILL_HORIZONTAL);
500 fNumberText= new Text(composite, SWT.SINGLE | SWT.BORDER | SWT.RIGHT);
501 fNumberText.setFont(composite.getFont());
502
503 final int length= Integer.toString(maxValue).length() + 3;
504 fNumberText.setLayoutData(createGridData(1, GridData.HORIZONTAL_ALIGN_END, fPixelConverter.convertWidthInCharsToPixels(length)));
505
506 fMinValue= minValue;
507 fMaxValue= maxValue;
508
509 updateWidget();
510
511 fNumberText.addFocusListener(new FocusListener() {
512 public void focusGained(FocusEvent e) {
513 NumberPreference.this.focusGained();
514 }
515 public void focusLost(FocusEvent e) {
516 NumberPreference.this.focusLost();
517 }
518 });
519
520 fNumberText.addModifyListener(new ModifyListener() {
521 public void modifyText(ModifyEvent e) {
522 fieldModified();
523 }
524 });
525 }
526
527 private IStatus createErrorStatus() {
528 return new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(FormatterMessages.ModifyDialogTabPage_NumberPreference_error_invalid_value, new String [] {Integer.toString(fMinValue), Integer.toString(fMaxValue)}), null);
529
530 }
531
532 protected void focusGained() {
533 fOldSelected= fSelected;
534 fNumberText.setSelection(0, fNumberText.getCharCount());
535 }
536
537 protected void focusLost() {
538 updateStatus(null);
539 final String input= fNumberText.getText();
540 if (!validInput(input))
541 fSelected= fOldSelected;
542 else
543 fSelected= Integer.parseInt(input);
544 if (fSelected != fOldSelected) {
545 saveSelected();
546 fNumberText.setText(Integer.toString(fSelected));
547 }
548 }
549
550
551 protected void fieldModified() {
552 final String trimInput= fNumberText.getText().trim();
553 final boolean valid= validInput(trimInput);
554
555 updateStatus(valid ? null : createErrorStatus());
556
557 if (valid) {
558 final int number= Integer.parseInt(trimInput);
559 if (fSelected != number) {
560 fSelected= number;
561 saveSelected();
562 }
563 }
564 }
565
566 private boolean validInput(String trimInput) {
567 int number;
568
569 try {
570 number= Integer.parseInt(trimInput);
571 } catch (NumberFormatException x) {
572 return false;
573 }
574
575 if (number < fMinValue) return false;
576 if (number > fMaxValue) return false;
577 return true;
578 }
579
580 private void saveSelected() {
581 getPreferences().put(getKey(), Integer.toString(fSelected));
582 setChanged();
583 notifyObservers();
584 }
585
586 @Override
587 protected void updateWidget() {
588 final boolean hasKey= getKey() != null;
589
590 fNumberLabel.setEnabled(hasKey && getEnabled());
591 fNumberText.setEnabled(hasKey && getEnabled());
592
593 if (hasKey) {
594 String s= getPreferences().get(getKey());
595 try {
596 fSelected= Integer.parseInt(s);
597 } catch (NumberFormatException e) {
598 final String message= Messages.format(FormatterMessages.ModifyDialogTabPage_NumberPreference_error_invalid_key, getKey());
599 JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, message, e));
600 s= ""; //$NON-NLS-1$
601 }
602 fNumberText.setText(s);
603 } else {
604 fNumberText.setText(""); //$NON-NLS-1$
605 }
606 }
607
608 @Override
609 public Control getControl() {
610 return fNumberText;
611 }
612
613 public NumberPreference generated_7537063215368924867(ModifyDialogTabPage modifydialogtabpage) {
614 modifydialogtabpage.fDefaultFocusManager.add(this);
615 addObserver(modifydialogtabpage.fUpdater);
616 return this;
617 }
618
619 public void generated_1702283291770472824(final LineWrappingTabPage linewrappingtabpage) {
620 linewrappingtabpage.fDefaultFocusManager.add(this);
621 addObserver(linewrappingtabpage.fUpdater);
622 addObserver(new Observer() {
623 public void update(Observable o, Object arg) {
624 linewrappingtabpage.fDialogSettings.put(LineWrappingTabPage.PREF_PREVIEW_LINE_WIDTH, linewrappingtabpage.fPreviewPreferences.get(linewrappingtabpage.LINE_SPLIT));
625 }
626 });
627 }
628
629 public void generated_857278696475165910() {
630 setEnabled(true);
631 setKey(DefaultCodeFormatterConstants.FORMATTER_INDENTATION_SIZE);
632 }
633 }
634
635
636 /**
637 * Wrapper around a text field which requests a string input.
638 *
639 * @since 3.6
640 */
641 protected final class StringPreference extends Preference {
642
643 /**
644 * Validates the input.
645 * <p>
646 * The default implementation declares all non-<code>null</code> values as valid.
647 * </p>
648 *
649 * @since 3.6
650 */
651 protected class Validator {
652 boolean isValid(String input) {
653 return input != null;
654 }
655 }
656
657 private final Label fLabel;
658
659 private final Text fText;
660
661 private IInputValidator fInputValidator;
662
663 protected String fSelected;
664
665 protected String fOldSelected;
666
667 /**
668 * Creates a new <code>StringPreference</code>.
669 *
670 * @param composite the composite on which the SWT widgets are added.
671 * @param numColumns the number of columns in the composite's {@link GridLayout}
672 * @param preferences the map to store the values.
673 * @param key the key to store the values.
674 * @param text the label text for this Preference.
675 * @param inputValidator the input validator or <code>null</code> if none
676 */
677 public StringPreference(Composite composite, int numColumns, Map<String, String> preferences, String key, String text, IInputValidator inputValidator) {
678 super(preferences, key);
679
680 fInputValidator= inputValidator;
681
682 fLabel= new Label(composite, SWT.NONE);
683 fLabel.setFont(composite.getFont());
684 fLabel.setText(text);
685
686 fLabel.setLayoutData(createGridData(numColumns - 1, GridData.HORIZONTAL_ALIGN_BEGINNING, SWT.DEFAULT));
687
688 fText= new Text(composite, SWT.SINGLE | SWT.BORDER);
689 fText.setFont(composite.getFont());
690
691 final int length= 30;
692 fText.setLayoutData(createGridData(1, GridData.HORIZONTAL_ALIGN_BEGINNING, fPixelConverter.convertWidthInCharsToPixels(length)));
693
694 updateWidget();
695
696 fText.addFocusListener(new FocusListener() {
697 public void focusGained(FocusEvent e) {
698 StringPreference.this.focusGained();
699 }
700
701 public void focusLost(FocusEvent e) {
702 StringPreference.this.focusLost();
703 }
704 });
705
706 fText.addModifyListener(new ModifyListener() {
707 public void modifyText(ModifyEvent e) {
708 fieldModified();
709 }
710 });
711 }
712
713 private IStatus createErrorStatus(String errorText) {
714 return new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, errorText, null);
715
716 }
717
718 protected void focusGained() {
719 fOldSelected= fSelected;
720 fText.setSelection(0, fText.getCharCount());
721 }
722
723 protected void focusLost() {
724 updateStatus(null);
725 final String input= fText.getText();
726 if (fInputValidator != null && fInputValidator.isValid(input) != null)
727 fSelected= fOldSelected;
728 else
729 fSelected= input;
730 if (fSelected != fOldSelected) {
731 saveSelected();
732 fText.setText(fSelected);
733 }
734 }
735
736
737 protected void fieldModified() {
738 final String text= fText.getText();
739 final String errorText= fInputValidator != null ? fInputValidator.isValid(text) : null;
740 if (errorText == null) {
741 updateStatus(null);
742 if (fSelected != text) {
743 fSelected= text;
744 saveSelected();
745 }
746 } else
747 updateStatus(createErrorStatus(errorText));
748 }
749
750 private void saveSelected() {
751 getPreferences().put(getKey(), fSelected);
752 setChanged();
753 notifyObservers();
754 }
755
756 @Override
757 protected void updateWidget() {
758 final boolean hasKey= getKey() != null;
759
760 fLabel.setEnabled(hasKey && getEnabled());
761 fText.setEnabled(hasKey && getEnabled());
762
763 if (hasKey) {
764 fSelected= getPreferences().get(getKey());
765 fText.setText(fSelected);
766 } else {
767 fText.setText(""); //$NON-NLS-1$
768 }
769 }
770
771 @Override
772 public Control getControl() {
773 return fText;
774 }
775
776 public StringPreference generated_5807262205297883895(ModifyDialogTabPage modifydialogtabpage) {
777 modifydialogtabpage.fDefaultFocusManager.add(this);
778 addObserver(modifydialogtabpage.fUpdater);
779 return this;
780 }
781 }
782
783
784 /**
785 * This class provides the default way to preserve and re-establish the focus
786 * over multiple modify sessions. Each ModifyDialogTabPage has its own instance,
787 * and it should add all relevant controls upon creation, always in the same sequence.
788 * This established a mapping of controls to indexes, which allows to restore the focus
789 * in a later session.
790 * The index is saved in the dialog settings, and there is only one common preference for
791 * all tab pages. It is always the currently active tab page which stores its focus
792 * index.
793 */
794 protected final static class DefaultFocusManager extends FocusAdapter {
795
796 private final static String PREF_LAST_FOCUS_INDEX= JavaUI.ID_PLUGIN + "formatter_page.modify_dialog_tab_page.last_focus_index"; //$NON-NLS-1$
797
798 private final IDialogSettings fDialogSettings;
799
800 private final Map<Control, Integer> fItemMap;
801 private final List<Control> fItemList;
802
803 private int fIndex;
804
805 public DefaultFocusManager() {
806 fDialogSettings= JavaPlugin.getDefault().getDialogSettings();
807 fItemMap= new HashMap<Control, Integer>();
808 fItemList= new ArrayList<Control>();
809 fIndex= 0;
810 }
811
812 @Override
813 public void focusGained(FocusEvent e) {
814 fDialogSettings.put(PREF_LAST_FOCUS_INDEX, fItemMap.get(e.widget).intValue());
815 }
816
817 public void add(Control control) {
818 control.addFocusListener(this);
819 fItemList.add(fIndex, control);
820 fItemMap.put(control, new Integer(fIndex++));
821 }
822
823 public void add(Preference preference) {
824 final Control control= preference.getControl();
825 if (control != null)
826 add(control);
827 }
828
829 public boolean isUsed() {
830 return fIndex != 0;
831 }
832
833 public void restoreFocus() {
834 int index= 0;
835 try {
836 index= fDialogSettings.getInt(PREF_LAST_FOCUS_INDEX);
837 // make sure the value is within the range
838 if ((index >= 0) && (index <= fItemList.size() - 1)) {
839 fItemList.get(index).setFocus();
840 }
841 } catch (NumberFormatException ex) {
842 // this is the first time
843 }
844 }
845
846 public void resetFocus() {
847 fDialogSettings.put(PREF_LAST_FOCUS_INDEX, -1);
848 }
849
850 public void generated_1888790499004421333() {
851 if (isUsed()) {
852 restoreFocus();
853 }
854 }
855
856 public void generated_4449091096061266128(LineWrappingTabPage linewrappingtabpage) {
857 add(linewrappingtabpage.fCategoriesViewer.getControl());
858 add(linewrappingtabpage.fWrappingStyleCombo);
859 add(linewrappingtabpage.fIndentStyleCombo);
860 add(linewrappingtabpage.fForceSplit);
861 }
862
863 public void generated_1381852057704543217(JavaElementComponent javaelementcomponent, final GridData optionsGd) {
864 optionsGd.heightHint= fPixelConverter.convertHeightInCharsToPixels(3);
865 javaelementcomponent.fOptionsViewer.getControl().setLayoutData(optionsGd);
866
867 add(javaelementcomponent.fInnerViewer.getControl());
868 add(javaelementcomponent.fOptionsViewer.getControl());
869 }
870 }
871
872 /**
873 * Layout used for the settings part. Makes sure to show scrollbars
874 * if necessary. The settings part needs to be layouted on resize.
875 */
876 private static class PageLayout extends Layout {
877
878 private final ScrolledComposite fContainer;
879 private final int fMinimalWidth;
880 private final int fMinimalHight;
881
882 private PageLayout(ScrolledComposite container, int minimalWidth, int minimalHight) {
883 fContainer= container;
884 fMinimalWidth= minimalWidth;
885 fMinimalHight= minimalHight;
886 }
887
888 @Override
889 public Point computeSize(Composite composite, int wHint, int hHint, boolean force) {
890 if (wHint != SWT.DEFAULT && hHint != SWT.DEFAULT) {
891 return new Point(wHint, hHint);
892 }
893
894 int x = fMinimalWidth;
895 int y = fMinimalHight;
896 Control[] children = composite.getChildren();
897 for (int i = 0; i < children.length; i++) {
898 Point size = children[i].computeSize(SWT.DEFAULT, SWT.DEFAULT, force);
899 x = Math.max(x, size.x);
900 y = Math.max(y, size.y);
901 }
902
903 Rectangle area= fContainer.getClientArea();
904 if (area.width > x) {
905 fContainer.setExpandHorizontal(true);
906 } else {
907 fContainer.setExpandHorizontal(false);
908 }
909
910 if (area.height > y) {
911 fContainer.setExpandVertical(true);
912 } else {
913 fContainer.setExpandVertical(false);
914 }
915
916 if (wHint != SWT.DEFAULT) {
917 x = wHint;
918 }
919 if (hHint != SWT.DEFAULT) {
920 y = hHint;
921 }
922
923 return new Point(x, y);
924 }
925
926 @Override
927 public void layout(Composite composite, boolean force) {
928 Rectangle rect = composite.getClientArea();
929 Control[] children = composite.getChildren();
930 for (int i = 0; i < children.length; i++) {
931 children[i].setSize(rect.width, rect.height);
932 }
933 }
934 }
935
936 /**
937 * The default focus manager. This widget knows all widgets which can have the focus
938 * and listens for focusGained events, on which it stores the index of the current
939 * focus holder. When the dialog is restarted, <code>restoreFocus()</code> sets the
940 * focus to the last control which had it.
941 *
942 * The standard Preference object are managed by this focus manager if they are created
943 * using the respective factory methods. Other SWT widgets can be added in subclasses
944 * when they are created.
945 */
946 protected final DefaultFocusManager fDefaultFocusManager;
947
948 /**
949 * A pixel converter for layout calculations
950 */
951 protected PixelConverter fPixelConverter;
952
953
954 /**
955 * The map where the current settings are stored.
956 */
957 protected Map<String, String> fWorkingValues;
958
959 /**
960 * The modify dialog where we can display status messages.
961 */
962 private IModifyDialogTabPage.IModificationListener fModifyListener;
963
964
965 /*
966 * Create a new <code>ModifyDialogTabPage</code>
967 */
968 public ModifyDialogTabPage(IModifyDialogTabPage.IModificationListener modifyListener, Map<String, String> workingValues) {
969 fWorkingValues= workingValues;
970 fModifyListener= modifyListener;
971 fDefaultFocusManager= new DefaultFocusManager();
972 }
973
974 public ModifyDialogTabPage() {
975 fDefaultFocusManager= new DefaultFocusManager();
976 }
977
978 /**
979 * {@inheritDoc}
980 */
981 public void setWorkingValues(Map<String, String> workingValues) {
982 fWorkingValues= workingValues;
983 }
984
985 /**
986 * {@inheritDoc}
987 */
988 public void setModifyListener(IModifyDialogTabPage.IModificationListener modifyListener) {
989 fModifyListener= modifyListener;
990 }
991
992 /**
993 * Create the contents of this tab page.
994 * <p>
995 * Subclasses should implement <code>doCreatePreferences</code> and <code>doCreatePreview</code>
996 * may also be overridden as necessary.
997 * </p>
998 *
999 * @param parent The parent composite
1000 * @return Created content control
1001 */
1002 public Composite createContents(Composite parent) {
1003 final int numColumns= 4;
1004
1005 if (fPixelConverter == null) {
1006 fPixelConverter= new PixelConverter(parent);
1007 }
1008
1009 final SashForm sashForm = new SashForm(parent, SWT.HORIZONTAL);
1010 sashForm.setFont(parent.getFont());
1011
1012 Composite scrollContainer = new Composite(sashForm, SWT.NONE);
1013
1014 GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
1015 scrollContainer.setLayoutData(gridData);
1016
1017 GridLayout layout= new GridLayout(2, false);
1018 layout.marginHeight= 0;
1019 layout.marginWidth= 0;
1020 layout.horizontalSpacing= 0;
1021 layout.verticalSpacing= 0;
1022 scrollContainer.setLayout(layout);
1023
1024 ScrolledComposite scroll= new ScrolledComposite(scrollContainer, SWT.V_SCROLL | SWT.H_SCROLL);
1025 scroll.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
1026 scroll.setExpandHorizontal(true);
1027 scroll.setExpandVertical(true);
1028
1029 final Composite settingsContainer= new Composite(scroll, SWT.NONE);
1030 settingsContainer.setFont(sashForm.getFont());
1031
1032 scroll.setContent(settingsContainer);
1033
1034 settingsContainer.setLayout(new PageLayout(scroll, 400, 400));
1035 settingsContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
1036
1037 Composite settingsPane= new Composite(settingsContainer, SWT.NONE);
1038 settingsPane.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
1039
1040 layout= new GridLayout(numColumns, false);
1041 layout.verticalSpacing= (int)(1.5 * fPixelConverter.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING));
1042 layout.horizontalSpacing= fPixelConverter.convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
1043 layout.marginHeight= fPixelConverter.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
1044 layout.marginWidth= fPixelConverter.convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
1045 settingsPane.setLayout(layout);
1046 doCreatePreferences(settingsPane, numColumns);
1047
1048 settingsContainer.setSize(settingsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT));
1049
1050 scroll.addControlListener(new ControlListener() {
1051
1052 public void controlMoved(ControlEvent e) {
1053 }
1054
1055 public void controlResized(ControlEvent e) {
1056 settingsContainer.setSize(settingsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT));
1057 }
1058 });
1059
1060 Label sashHandle = new Label(scrollContainer, SWT.SEPARATOR | SWT.VERTICAL);
1061 gridData= new GridData(SWT.RIGHT, SWT.FILL, false, true);
1062 sashHandle.setLayoutData(gridData);
1063
1064 final Composite previewPane= new Composite(sashForm, SWT.NONE);
1065 previewPane.setLayout(createGridLayout(numColumns, true));
1066 previewPane.setFont(sashForm.getFont());
1067 doCreatePreviewPane(previewPane, numColumns);
1068
1069 initializePage();
1070
1071 sashForm.setWeights(new int [] {3, 3});
1072 return sashForm;
1073 }
1074
1075 /**
1076 * This method is called after all controls have been allocated, including the preview.
1077 * It can be used to set the preview text and to create listeners.
1078 *
1079 */
1080 protected abstract void initializePage();
1081
1082
1083 /**
1084 * Create the left side of the modify dialog. This is meant to be implemented by subclasses.
1085 * @param composite Composite to create in
1086 * @param numColumns Number of columns to use
1087 */
1088 protected abstract void doCreatePreferences(Composite composite, int numColumns);
1089
1090
1091 /**
1092 * Create the right side of the modify dialog. By default, the preview is displayed there.
1093 * Subclasses can override this method in order to customize the right-hand side of the
1094 * dialog.
1095 * @param composite Composite to create in
1096 * @param numColumns Number of columns to use
1097 * @return Created composite
1098 */
1099 protected Composite doCreatePreviewPane(Composite composite, int numColumns) {
1100
1101 createLabel(numColumns, composite, FormatterMessages.ModifyDialogTabPage_preview_label_text);
1102
1103 final JavaPreview preview= doCreateJavaPreview(composite);
1104 fDefaultFocusManager.add(preview.getControl());
1105
1106 final GridData gd= createGridData(numColumns, GridData.FILL_BOTH, 0);
1107 gd.widthHint= 0;
1108 gd.heightHint=0;
1109 preview.getControl().setLayoutData(gd);
1110
1111 return composite;
1112 }
1113
1114
1115 /**
1116 * To be implemented by subclasses. This method should return an instance of JavaPreview.
1117 * Currently, the choice is between CompilationUnitPreview which contains a valid compilation
1118 * unit, or a SnippetPreview which formats several independent code snippets and displays them
1119 * in the same window.
1120 * @param parent Parent composite
1121 * @return Created preview
1122 */
1123 protected abstract JavaPreview doCreateJavaPreview(Composite parent);
1124
1125
1126 /**
1127 * {@inheritDoc}
1128 */
1129 public final void makeVisible() {
1130 fDefaultFocusManager.resetFocus();
1131 doUpdatePreview();
1132 }
1133
1134 /**
1135 * Update the preview. To be implemented by subclasses.
1136 */
1137 protected abstract void doUpdatePreview();
1138
1139 protected void notifyValuesModified() {
1140 fModifyListener.valuesModified();
1141 }
1142 /**
1143 * {@inheritDoc}
1144 */
1145 public void setInitialFocus() {
1146 fDefaultFocusManager.generated_1888790499004421333();
1147 }
1148
1149 /**
1150 * Set the status field on the dialog. This can be used by tab pages to report
1151 * inconsistent input. The OK button is disabled if the kind is IStatus.ERROR.
1152 * @param status Status describing the current page error state
1153 */
1154 protected void updateStatus(IStatus status) {
1155 fModifyListener.updateStatus(status);
1156 }
1157
1158 /*
1159 * Factory methods to make GUI construction easier
1160 */
1161
1162 /*
1163 * Create a GridLayout with the default margin and spacing settings, as
1164 * well as the specified number of columns.
1165 */
1166 protected GridLayout createGridLayout(int numColumns, boolean margins) {
1167 final GridLayout layout= new GridLayout(numColumns, false);
1168 layout.verticalSpacing= fPixelConverter.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
1169 layout.horizontalSpacing= fPixelConverter.convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
1170 if (margins) {
1171 layout.marginHeight= fPixelConverter.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
1172 layout.marginWidth= fPixelConverter.convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
1173 } else {
1174 layout.marginHeight= 0;
1175 layout.marginWidth= 0;
1176 }
1177 return layout;
1178 }
1179
1180 /*
1181 * Convenience method to create a GridData.
1182 */
1183 protected static GridData createGridData(int numColumns, int style, int widthHint) {
1184 final GridData gd= new GridData(style);
1185 gd.horizontalSpan= numColumns;
1186 gd.widthHint= widthHint;
1187 return gd;
1188 }
1189
1190
1191 /*
1192 * Convenience method to create a label.
1193 */
1194 protected static Label createLabel(int numColumns, Composite parent, String text) {
1195 return createLabel(numColumns, parent, text, GridData.FILL_HORIZONTAL);
1196 }
1197
1198 /*
1199 * Convenience method to create a label
1200 */
1201 protected static Label createLabel(int numColumns, Composite parent, String text, int gridDataStyle) {
1202 final Label label= new Label(parent, SWT.WRAP);
1203 label.setFont(parent.getFont());
1204 label.setText(text);
1205
1206 PixelConverter pixelConverter= new PixelConverter(parent);
1207 label.setLayoutData(createGridData(numColumns, gridDataStyle, pixelConverter.convertHorizontalDLUsToPixels(150)));
1208 return label;
1209 }
1210
1211 /*
1212 * Convenience method to create a group
1213 */
1214 protected Group createGroup(int numColumns, Composite parent, String text ) {
1215 final Group group= new Group(parent, SWT.NONE);
1216 group.setFont(parent.getFont());
1217 group.setLayoutData(createGridData(numColumns, GridData.FILL_HORIZONTAL, SWT.DEFAULT));
1218
1219 final GridLayout layout= new GridLayout(numColumns, false);
1220 layout.verticalSpacing= fPixelConverter.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
1221 layout.horizontalSpacing= fPixelConverter.convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
1222 layout.marginHeight= fPixelConverter.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
1223
1224 //layout.marginHeight= fPixelConverter.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
1225 //layout.marginWidth= fPixelConverter.convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
1226
1227 group.setLayout(layout);//createGridLayout(numColumns, true));
1228 group.setText(text);
1229 return group;
1230 }
1231
1232
1233 /*
1234 * Convenience method to create a NumberPreference. The widget is registered as
1235 * a potential focus holder, and the default updater is added.
1236 */
1237 protected NumberPreference createNumberPref(Composite composite, int numColumns, String name, String key,
1238 int minValue, int maxValue) {
1239 final NumberPreference pref= new NumberPreference(composite, numColumns, fWorkingValues,
1240 key, minValue, maxValue, name);
1241 return pref.generated_7537063215368924867(this);
1242 }
1243
1244 /*
1245 * Convenience method to create a StringPreference. The widget is registered as
1246 * a potential focus holder, and the default updater is added.
1247 * @since 3.6
1248 */
1249 protected StringPreference createStringPref(Composite composite, int numColumns, String name, String key, IInputValidator inputValidator) {
1250 StringPreference pref= new StringPreference(composite, numColumns, fWorkingValues, key, name, inputValidator);
1251 return pref.generated_5807262205297883895(this);
1252 }
1253
1254 /*
1255 * Convenience method to create a ComboPreference. The widget is registered as
1256 * a potential focus holder, and the default updater is added.
1257 */
1258 protected ComboPreference createComboPref(Composite composite, int numColumns, String name,
1259 String key, String [] values, String [] items) {
1260 final ComboPreference pref= new ComboPreference(composite, numColumns,
1261 fWorkingValues, key, values, name, items);
1262 return pref.generated_5306086071415273487(this);
1263 }
1264
1265 /*
1266 * Convenience method to create a CheckboxPreference. The widget is registered as
1267 * a potential focus holder, and the default updater is added.
1268 */
1269 protected CheckboxPreference createCheckboxPref(Composite composite, int numColumns, String name, String key,
1270 String [] values) {
1271 final CheckboxPreference pref= new CheckboxPreference(composite, numColumns,
1272 fWorkingValues, key, values, name);
1273 return pref.generated_5729534999395002544(this);
1274 }
1275
1276 protected RadioPreference createRadioPref(Composite composite, int numColumns, String name, String key,
1277 String [] values) {
1278 final RadioPreference pref= new RadioPreference(composite, numColumns,
1279 fWorkingValues, key, values, name);
1280 return pref.generated_2286414519167355793(this);
1281 }
1282
1283 /*
1284 * Create a nice javadoc comment for some string.
1285 */
1286 protected static String createPreviewHeader(String title) {
1287 return "/**\n* " + title + "\n*/\n"; //$NON-NLS-1$ //$NON-NLS-2$
1288 }
1289}