1 package net.sourceforge.phpdt.internal.ui.preferences;
4 import java.util.ArrayList;
5 import java.util.Iterator;
8 import net.sourceforge.phpdt.internal.corext.template.ContextType;
9 import net.sourceforge.phpdt.internal.corext.template.ContextTypeRegistry;
10 import net.sourceforge.phpdt.internal.corext.template.Template;
11 import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
12 import net.sourceforge.phpdt.internal.corext.template.TemplateSet;
13 import net.sourceforge.phpdt.internal.corext.template.Templates;
14 import net.sourceforge.phpdt.internal.ui.text.template.TemplateContentProvider;
15 import net.sourceforge.phpdt.internal.ui.text.template.TemplateLabelProvider;
16 import net.sourceforge.phpdt.internal.ui.util.SWTUtil;
17 import net.sourceforge.phpdt.ui.text.JavaTextTools;
18 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import net.sourceforge.phpeclipse.phpeditor.PHPSourceViewerConfiguration;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.jface.dialogs.ErrorDialog;
23 import org.eclipse.jface.dialogs.MessageDialog;
24 import org.eclipse.jface.preference.IPreferenceStore;
25 import org.eclipse.jface.preference.PreferencePage;
26 import org.eclipse.jface.resource.JFaceResources;
27 import org.eclipse.jface.text.Document;
28 import org.eclipse.jface.text.IDocument;
29 import org.eclipse.jface.text.IDocumentPartitioner;
30 import org.eclipse.jface.text.source.SourceViewer;
31 import org.eclipse.jface.viewers.CheckStateChangedEvent;
32 import org.eclipse.jface.viewers.CheckboxTableViewer;
33 import org.eclipse.jface.viewers.DoubleClickEvent;
34 import org.eclipse.jface.viewers.ICheckStateListener;
35 import org.eclipse.jface.viewers.IDoubleClickListener;
36 import org.eclipse.jface.viewers.ISelectionChangedListener;
37 import org.eclipse.jface.viewers.IStructuredSelection;
38 import org.eclipse.jface.viewers.SelectionChangedEvent;
39 import org.eclipse.jface.viewers.StructuredSelection;
40 import org.eclipse.jface.viewers.TableLayout;
41 import org.eclipse.jface.viewers.Viewer;
42 import org.eclipse.jface.viewers.ViewerSorter;
43 import org.eclipse.swt.SWT;
44 import org.eclipse.swt.events.ControlAdapter;
45 import org.eclipse.swt.events.ControlEvent;
46 import org.eclipse.swt.graphics.Font;
47 import org.eclipse.swt.graphics.Point;
48 import org.eclipse.swt.graphics.Rectangle;
49 import org.eclipse.swt.layout.GridData;
50 import org.eclipse.swt.layout.GridLayout;
51 import org.eclipse.swt.widgets.Button;
52 import org.eclipse.swt.widgets.Composite;
53 import org.eclipse.swt.widgets.Control;
54 import org.eclipse.swt.widgets.Event;
55 import org.eclipse.swt.widgets.FileDialog;
56 import org.eclipse.swt.widgets.Label;
57 import org.eclipse.swt.widgets.Listener;
58 import org.eclipse.swt.widgets.Table;
59 import org.eclipse.swt.widgets.TableColumn;
60 import org.eclipse.ui.IWorkbench;
61 import org.eclipse.ui.IWorkbenchPreferencePage;
63 public class TemplatePreferencePage
64 extends PreferencePage
65 implements IWorkbenchPreferencePage {
67 // preference store keys
68 private static final String PREF_FORMAT_TEMPLATES = PHPeclipsePlugin.PLUGIN_ID + ".template.format"; //$NON-NLS-1$
70 private Templates fTemplates;
72 private CheckboxTableViewer fTableViewer;
73 private Button fAddButton;
74 private Button fEditButton;
75 private Button fImportButton;
76 private Button fExportButton;
77 private Button fExportAllButton;
78 private Button fRemoveButton;
79 private Button fEnableAllButton;
80 private Button fDisableAllButton;
82 private SourceViewer fPatternViewer;
83 // private Button fFormatButton;
85 public TemplatePreferencePage() {
88 setPreferenceStore(PHPeclipsePlugin.getDefault().getPreferenceStore());
89 setDescription(TemplateMessages.getString("TemplatePreferencePage.message")); //$NON-NLS-1$
91 fTemplates = Templates.getInstance();
95 * @see PreferencePage#createContents(Composite)
97 protected Control createContents(Composite ancestor) {
98 // PHPEditorEnvironment.connect(this);
99 Composite parent = new Composite(ancestor, SWT.NONE);
100 GridLayout layout = new GridLayout();
101 layout.numColumns = 2;
102 layout.marginHeight = 0;
103 layout.marginWidth = 0;
104 parent.setLayout(layout);
106 Composite innerParent = new Composite(parent, SWT.NONE);
107 GridLayout innerLayout = new GridLayout();
108 innerLayout.numColumns = 2;
109 innerLayout.marginHeight = 0;
110 innerLayout.marginWidth = 0;
111 innerParent.setLayout(innerLayout);
112 GridData gd = new GridData(GridData.FILL_BOTH);
113 gd.horizontalSpan = 2;
114 innerParent.setLayoutData(gd);
119 SWT.CHECK | SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
121 GridData data = new GridData(GridData.FILL_BOTH);
122 data.widthHint = convertWidthInCharsToPixels(3);
123 data.heightHint = convertHeightInCharsToPixels(10);
124 table.setLayoutData(data);
126 table.setHeaderVisible(true);
127 table.setLinesVisible(true);
129 TableLayout tableLayout = new TableLayout();
130 table.setLayout(tableLayout);
132 TableColumn column1 = new TableColumn(table, SWT.NONE);
133 column1.setText(TemplateMessages.getString("TemplatePreferencePage.column.name")); //$NON-NLS-1$
135 TableColumn column2 = new TableColumn(table, SWT.NONE);
136 column2.setText(TemplateMessages.getString("TemplatePreferencePage.column.context")); //$NON-NLS-1$
138 TableColumn column3 = new TableColumn(table, SWT.NONE);
139 column3.setText(TemplateMessages.getString("TemplatePreferencePage.column.description")); //$NON-NLS-1$
141 fTableViewer = new CheckboxTableViewer(table);
142 fTableViewer.setLabelProvider(new TemplateLabelProvider());
143 fTableViewer.setContentProvider(new TemplateContentProvider());
145 fTableViewer.setSorter(new ViewerSorter() {
146 public int compare(Viewer viewer, Object object1, Object object2) {
147 if ((object1 instanceof Template) && (object2 instanceof Template)) {
148 Template left = (Template) object1;
149 Template right = (Template) object2;
150 int result = left.getName().compareToIgnoreCase(right.getName());
153 return left.getDescription().compareToIgnoreCase(
154 right.getDescription());
156 return super.compare(viewer, object1, object2);
159 public boolean isSorterProperty(Object element, String property) {
164 fTableViewer.addDoubleClickListener(new IDoubleClickListener() {
165 public void doubleClick(DoubleClickEvent e) {
170 fTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
171 public void selectionChanged(SelectionChangedEvent e) {
176 fTableViewer.addCheckStateListener(new ICheckStateListener() {
177 public void checkStateChanged(CheckStateChangedEvent event) {
178 Template template = (Template) event.getElement();
179 template.setEnabled(event.getChecked());
183 Composite buttons = new Composite(innerParent, SWT.NONE);
184 buttons.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
185 layout = new GridLayout();
186 layout.marginHeight = 0;
187 layout.marginWidth = 0;
188 buttons.setLayout(layout);
190 fAddButton = new Button(buttons, SWT.PUSH);
191 fAddButton.setText(TemplateMessages.getString("TemplatePreferencePage.new")); //$NON-NLS-1$
192 fAddButton.setLayoutData(getButtonGridData(fAddButton));
193 fAddButton.addListener(SWT.Selection, new Listener() {
194 public void handleEvent(Event e) {
199 fEditButton = new Button(buttons, SWT.PUSH);
200 fEditButton.setText(TemplateMessages.getString("TemplatePreferencePage.edit")); //$NON-NLS-1$
201 fEditButton.setLayoutData(getButtonGridData(fEditButton));
202 fEditButton.addListener(SWT.Selection, new Listener() {
203 public void handleEvent(Event e) {
208 fRemoveButton = new Button(buttons, SWT.PUSH);
209 fRemoveButton.setText(TemplateMessages.getString("TemplatePreferencePage.remove")); //$NON-NLS-1$
210 fRemoveButton.setLayoutData(getButtonGridData(fRemoveButton));
211 fRemoveButton.addListener(SWT.Selection, new Listener() {
212 public void handleEvent(Event e) {
217 fImportButton = new Button(buttons, SWT.PUSH);
218 fImportButton.setText(TemplateMessages.getString("TemplatePreferencePage.import")); //$NON-NLS-1$
219 fImportButton.setLayoutData(getButtonGridData(fImportButton));
220 fImportButton.addListener(SWT.Selection, new Listener() {
221 public void handleEvent(Event e) {
226 fExportButton = new Button(buttons, SWT.PUSH);
227 fExportButton.setText(TemplateMessages.getString("TemplatePreferencePage.export")); //$NON-NLS-1$
228 fExportButton.setLayoutData(getButtonGridData(fExportButton));
229 fExportButton.addListener(SWT.Selection, new Listener() {
230 public void handleEvent(Event e) {
235 fExportAllButton = new Button(buttons, SWT.PUSH);
236 fExportAllButton.setText(TemplateMessages.getString("TemplatePreferencePage.export.all")); //$NON-NLS-1$
237 fExportAllButton.setLayoutData(getButtonGridData(fExportAllButton));
238 fExportAllButton.addListener(SWT.Selection, new Listener() {
239 public void handleEvent(Event e) {
244 fEnableAllButton = new Button(buttons, SWT.PUSH);
245 fEnableAllButton.setText(TemplateMessages.getString("TemplatePreferencePage.enable.all")); //$NON-NLS-1$
246 fEnableAllButton.setLayoutData(getButtonGridData(fEnableAllButton));
247 fEnableAllButton.addListener(SWT.Selection, new Listener() {
248 public void handleEvent(Event e) {
253 fDisableAllButton = new Button(buttons, SWT.PUSH);
254 fDisableAllButton.setText(TemplateMessages.getString("TemplatePreferencePage.disable.all")); //$NON-NLS-1$
255 fDisableAllButton.setLayoutData(getButtonGridData(fDisableAllButton));
256 fDisableAllButton.addListener(SWT.Selection, new Listener() {
257 public void handleEvent(Event e) {
262 fPatternViewer = createViewer(parent);
264 // fFormatButton= new Button(parent, SWT.CHECK);
265 // fFormatButton.setText(ObfuscatorMessages.getString("TemplatePreferencePage.use.code.formatter")); //$NON-NLS-1$
266 // GridData gd1= new GridData();
267 // gd1.horizontalSpan= 2;
268 // fFormatButton.setLayoutData(gd1);
270 fTableViewer.setInput(fTemplates);
271 fTableViewer.setAllChecked(false);
272 fTableViewer.setCheckedElements(getEnabledTemplates());
274 IPreferenceStore prefs = PHPeclipsePlugin.getDefault().getPreferenceStore();
275 // fFormatButton.setSelection(prefs.getBoolean(PREF_FORMAT_TEMPLATES));
278 configureTableResizing(
286 // WorkbenchHelp.setHelp(parent, IJavaHelpContextIds.TEMPLATE_PREFERENCE_PAGE);
292 * Correctly resizes the table so no phantom columns appear
294 private static void configureTableResizing(
295 final Composite parent,
296 final Composite buttons,
298 final TableColumn column1,
299 final TableColumn column2,
300 final TableColumn column3) {
301 parent.addControlListener(new ControlAdapter() {
302 public void controlResized(ControlEvent e) {
303 Rectangle area = parent.getClientArea();
304 Point preferredSize = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
305 int width = area.width - 2 * table.getBorderWidth();
306 if (preferredSize.y > area.height) {
307 // Subtract the scrollbar width from the total column width
308 // if a vertical scrollbar will be required
309 Point vBarSize = table.getVerticalBar().getSize();
312 width -= buttons.getSize().x;
313 Point oldSize = table.getSize();
314 if (oldSize.x > width) {
315 // table is getting smaller so make the columns
316 // smaller first and then resize the table to
317 // match the client area width
318 column1.setWidth(width / 4);
319 column2.setWidth(width / 4);
320 column3.setWidth(width - (column1.getWidth() + column2.getWidth()));
321 table.setSize(width, area.height);
323 // table is getting bigger so make the table
324 // bigger first and then make the columns wider
325 // to match the client area width
326 table.setSize(width, area.height);
327 column1.setWidth(width / 4);
328 column2.setWidth(width / 4);
329 column3.setWidth(width - (column1.getWidth() + column2.getWidth()));
335 private Template[] getEnabledTemplates() {
336 Template[] templates = fTemplates.getTemplates();
338 List list = new ArrayList(templates.length);
340 for (int i = 0; i != templates.length; i++)
341 if (templates[i].isEnabled())
342 list.add(templates[i]);
344 return (Template[]) list.toArray(new Template[list.size()]);
347 private SourceViewer createViewer(Composite parent) {
348 Label label = new Label(parent, SWT.NONE);
349 label.setText(TemplateMessages.getString("TemplatePreferencePage.preview")); //$NON-NLS-1$
350 GridData data = new GridData();
351 data.horizontalSpan = 2;
352 label.setLayoutData(data);
354 SourceViewer viewer =
355 new SourceViewer(parent, null, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
356 JavaTextTools tools = PHPeclipsePlugin.getDefault().getJavaTextTools();
357 IDocument document = new Document();
358 IDocumentPartitioner partitioner = tools.createDocumentPartitioner();
359 document.setDocumentPartitioner(partitioner);
360 partitioner.connect(document);
362 viewer.configure(new PHPSourceViewerConfiguration(tools, null));
364 viewer.setEditable(false);
365 viewer.setDocument(document);
366 viewer.getTextWidget().setBackground(
367 getShell().getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
369 Font font = JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT);
370 viewer.getTextWidget().setFont(font);
372 Control control = viewer.getControl();
373 data = new GridData(GridData.FILL_BOTH);
374 data.horizontalSpan = 2;
375 data.heightHint = convertHeightInCharsToPixels(5);
376 control.setLayoutData(data);
381 private static GridData getButtonGridData(Button button) {
382 GridData data = new GridData(GridData.FILL_HORIZONTAL);
383 data.widthHint = SWTUtil.getButtonWidthHint(button);
384 data.heightHint = SWTUtil.getButtonHeigthHint(button);
389 private void selectionChanged1() {
390 IStructuredSelection selection =
391 (IStructuredSelection) fTableViewer.getSelection();
393 if (selection.size() == 1) {
394 Template template = (Template) selection.getFirstElement();
395 fPatternViewer.getTextWidget().setText(template.getPattern());
397 fPatternViewer.getTextWidget().setText(""); //$NON-NLS-1$
403 private void updateButtons() {
405 ((IStructuredSelection) fTableViewer.getSelection()).size();
406 int itemCount = fTableViewer.getTable().getItemCount();
408 fEditButton.setEnabled(selectionCount == 1);
409 fExportButton.setEnabled(selectionCount > 0);
410 fRemoveButton.setEnabled(selectionCount > 0 && selectionCount <= itemCount);
411 fEnableAllButton.setEnabled(itemCount > 0);
412 fDisableAllButton.setEnabled(itemCount > 0);
417 Template template = new Template();
419 ContextTypeRegistry registry = ContextTypeRegistry.getInstance();
420 ContextType type = registry.getContextType("php"); //$NON-NLS-1$
422 String contextTypeName;
424 contextTypeName = type.getName();
426 Iterator iterator = registry.iterator();
427 contextTypeName = (String) iterator.next();
429 template.setContext(contextTypeName); //$NON-NLS-1$
431 EditTemplateDialog dialog =
432 new EditTemplateDialog(getShell(), template, false);
433 if (dialog.open() == EditTemplateDialog.OK) {
434 fTemplates.add(template);
435 fTableViewer.refresh();
436 fTableViewer.setChecked(template, template.isEnabled());
437 fTableViewer.setSelection(new StructuredSelection(template));
441 private void edit() {
442 IStructuredSelection selection =
443 (IStructuredSelection) fTableViewer.getSelection();
445 Object[] objects = selection.toArray();
446 if ((objects == null) || (objects.length != 1))
449 Template template = (Template) selection.getFirstElement();
453 private void edit(Template template) {
454 Template newTemplate = new Template(template);
455 EditTemplateDialog dialog =
456 new EditTemplateDialog(getShell(), newTemplate, true);
457 if (dialog.open() == EditTemplateDialog.OK) {
459 if (!newTemplate.getName().equals(template.getName()) && MessageDialog.openQuestion(getShell(), TemplateMessages.getString("TemplatePreferencePage.question.create.new.title"), //$NON-NLS-1$
460 TemplateMessages.getString("TemplatePreferencePage.question.create.new.message"))) //$NON-NLS-1$
462 template = newTemplate;
463 fTemplates.add(template);
464 fTableViewer.refresh();
466 template.setName(newTemplate.getName());
467 template.setDescription(newTemplate.getDescription());
468 template.setContext(newTemplate.getContextTypeName());
469 template.setPattern(newTemplate.getPattern());
470 fTableViewer.refresh(template);
472 fTableViewer.setChecked(template, template.isEnabled());
473 fTableViewer.setSelection(new StructuredSelection(template));
477 private void import_() {
478 FileDialog dialog = new FileDialog(getShell());
479 dialog.setText(TemplateMessages.getString("TemplatePreferencePage.import.title")); //$NON-NLS-1$
480 dialog.setFilterExtensions(new String[] { TemplateMessages.getString("TemplatePreferencePage.import.extension")}); //$NON-NLS-1$
481 String path = dialog.open();
487 fTemplates.addFromFile(new File(path));
489 fTableViewer.refresh();
490 fTableViewer.setAllChecked(false);
491 fTableViewer.setCheckedElements(getEnabledTemplates());
493 } catch (CoreException e) {
494 openReadErrorDialog(e);
498 private void exportAll() {
502 private void export() {
503 IStructuredSelection selection =
504 (IStructuredSelection) fTableViewer.getSelection();
505 Object[] templates = selection.toArray();
507 TemplateSet templateSet = new TemplateSet();
508 for (int i = 0; i != templates.length; i++)
509 templateSet.add((Template) templates[i]);
514 private void export(TemplateSet templateSet) {
515 FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
516 dialog.setText(TemplateMessages.getFormattedString("TemplatePreferencePage.export.title", new Integer(templateSet.getTemplates().length))); //$NON-NLS-1$
517 dialog.setFilterExtensions(new String[] { TemplateMessages.getString("TemplatePreferencePage.export.extension")}); //$NON-NLS-1$
518 dialog.setFileName(TemplateMessages.getString("TemplatePreferencePage.export.filename")); //$NON-NLS-1$
519 String path = dialog.open();
524 File file = new File(path);
526 if (!file.exists() || confirmOverwrite(file)) {
528 templateSet.saveToFile(file);
529 } catch (CoreException e) {
530 PHPeclipsePlugin.log(e);
531 openWriteErrorDialog(e);
536 private boolean confirmOverwrite(File file) {
537 return MessageDialog.openQuestion(getShell(), TemplateMessages.getString("TemplatePreferencePage.export.exists.title"), //$NON-NLS-1$
538 TemplateMessages.getFormattedString("TemplatePreferencePage.export.exists.message", file.getAbsolutePath())); //$NON-NLS-1$
541 private void remove() {
542 IStructuredSelection selection =
543 (IStructuredSelection) fTableViewer.getSelection();
545 Iterator elements = selection.iterator();
546 while (elements.hasNext()) {
547 Template template = (Template) elements.next();
548 fTemplates.remove(template);
551 fTableViewer.refresh();
554 private void enableAll(boolean enable) {
555 Template[] templates = fTemplates.getTemplates();
556 for (int i = 0; i != templates.length; i++)
557 templates[i].setEnabled(enable);
559 fTableViewer.setAllChecked(enable);
563 * @see IWorkbenchPreferencePage#init(IWorkbench)
565 public void init(IWorkbench workbench) {
569 * @see Control#setVisible(boolean)
571 public void setVisible(boolean visible) {
572 super.setVisible(visible);
574 setTitle(TemplateMessages.getString("TemplatePreferencePage.title")); //$NON-NLS-1$
578 * @see PreferencePage#performDefaults()
580 protected void performDefaults() {
581 IPreferenceStore prefs = PHPeclipsePlugin.getDefault().getPreferenceStore();
582 // fFormatButton.setSelection(prefs.getDefaultBoolean(PREF_FORMAT_TEMPLATES));
585 fTemplates.restoreDefaults();
586 } catch (CoreException e) {
587 PHPeclipsePlugin.log(e);
588 openReadErrorDialog(e);
592 fTableViewer.refresh();
593 fTableViewer.setAllChecked(false);
594 fTableViewer.setCheckedElements(getEnabledTemplates());
598 * @see PreferencePage#performOk()
600 public boolean performOk() {
601 IPreferenceStore prefs = PHPeclipsePlugin.getDefault().getPreferenceStore();
602 // prefs.setValue(PREF_FORMAT_TEMPLATES, fFormatButton.getSelection());
606 } catch (CoreException e) {
607 PHPeclipsePlugin.log(e);
608 openWriteErrorDialog(e);
611 PHPeclipsePlugin.getDefault().savePluginPreferences();
612 // PHPEditorEnvironment.disconnect(this);
613 return super.performOk();
617 * @see PreferencePage#performCancel()
619 public boolean performCancel() {
622 } catch (CoreException e) {
623 PHPeclipsePlugin.log(e);
624 openReadErrorDialog(e);
627 // PHPEditorEnvironment.disconnect(this);
628 return super.performCancel();
632 * Initializes the default values of this page in the preference bundle.
633 * Will be called on startup of the PHPeclipsePlugin
635 public static void initDefaults(IPreferenceStore prefs) {
636 prefs.setDefault(PREF_FORMAT_TEMPLATES, true);
639 // public static boolean useCodeFormatter() {
640 // IPreferenceStore prefs= PHPeclipsePlugin.getDefault().getPreferenceStore();
641 // return prefs.getBoolean(PREF_FORMAT_TEMPLATES);
644 private void openReadErrorDialog(CoreException e) {
645 ErrorDialog.openError(getShell(), TemplateMessages.getString("TemplatePreferencePage.error.read.title"), //$NON-NLS-1$
646 null, e.getStatus());
649 private void openWriteErrorDialog(CoreException e) {
650 ErrorDialog.openError(getShell(), TemplateMessages.getString("TemplatePreferencePage.error.write.title"), //$NON-NLS-1$
651 null, e.getStatus());