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.IPHPPartitions;
15 import net.sourceforge.phpdt.internal.ui.text.template.TemplateContentProvider;
16 import net.sourceforge.phpdt.internal.ui.text.template.TemplateLabelProvider;
17 import net.sourceforge.phpdt.internal.ui.util.SWTUtil;
18 import net.sourceforge.phpdt.ui.text.JavaTextTools;
19 import net.sourceforge.phpdt.ui.text.PHPSourceViewerConfiguration;
20 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.jface.dialogs.ErrorDialog;
24 import org.eclipse.jface.dialogs.MessageDialog;
25 import org.eclipse.jface.preference.IPreferenceStore;
26 import org.eclipse.jface.preference.PreferencePage;
27 import org.eclipse.jface.resource.JFaceResources;
28 import org.eclipse.jface.text.Document;
29 import org.eclipse.jface.text.IDocument;
30 import org.eclipse.jface.text.IDocumentPartitioner;
31 import org.eclipse.jface.text.source.SourceViewer;
32 import org.eclipse.jface.viewers.CheckStateChangedEvent;
33 import org.eclipse.jface.viewers.CheckboxTableViewer;
34 import org.eclipse.jface.viewers.DoubleClickEvent;
35 import org.eclipse.jface.viewers.ICheckStateListener;
36 import org.eclipse.jface.viewers.IDoubleClickListener;
37 import org.eclipse.jface.viewers.ISelectionChangedListener;
38 import org.eclipse.jface.viewers.IStructuredSelection;
39 import org.eclipse.jface.viewers.SelectionChangedEvent;
40 import org.eclipse.jface.viewers.StructuredSelection;
41 import org.eclipse.jface.viewers.TableLayout;
42 import org.eclipse.jface.viewers.Viewer;
43 import org.eclipse.jface.viewers.ViewerSorter;
44 import org.eclipse.swt.SWT;
45 import org.eclipse.swt.events.ControlAdapter;
46 import org.eclipse.swt.events.ControlEvent;
47 import org.eclipse.swt.graphics.Font;
48 import org.eclipse.swt.graphics.Point;
49 import org.eclipse.swt.graphics.Rectangle;
50 import org.eclipse.swt.layout.GridData;
51 import org.eclipse.swt.layout.GridLayout;
52 import org.eclipse.swt.widgets.Button;
53 import org.eclipse.swt.widgets.Composite;
54 import org.eclipse.swt.widgets.Control;
55 import org.eclipse.swt.widgets.Event;
56 import org.eclipse.swt.widgets.FileDialog;
57 import org.eclipse.swt.widgets.Label;
58 import org.eclipse.swt.widgets.Listener;
59 import org.eclipse.swt.widgets.Table;
60 import org.eclipse.swt.widgets.TableColumn;
61 import org.eclipse.ui.IWorkbench;
62 import org.eclipse.ui.IWorkbenchPreferencePage;
64 public class TemplatePreferencePage
65 extends PreferencePage
66 implements IWorkbenchPreferencePage {
68 // preference store keys
69 private static final String PREF_FORMAT_TEMPLATES = PHPeclipsePlugin.PLUGIN_ID + ".template.format"; //$NON-NLS-1$
71 private Templates fTemplates;
73 private CheckboxTableViewer fTableViewer;
74 private Button fAddButton;
75 private Button fEditButton;
76 private Button fImportButton;
77 private Button fExportButton;
78 private Button fExportAllButton;
79 private Button fRemoveButton;
80 private Button fEnableAllButton;
81 private Button fDisableAllButton;
83 private SourceViewer fPatternViewer;
84 // private Button fFormatButton;
86 public TemplatePreferencePage() {
89 setPreferenceStore(PHPeclipsePlugin.getDefault().getPreferenceStore());
90 setDescription(TemplateMessages.getString("TemplatePreferencePage.message")); //$NON-NLS-1$
92 fTemplates = Templates.getInstance();
96 * @see PreferencePage#createContents(Composite)
98 protected Control createContents(Composite ancestor) {
99 // PHPEditorEnvironment.connect(this);
100 Composite parent = new Composite(ancestor, SWT.NONE);
101 GridLayout layout = new GridLayout();
102 layout.numColumns = 2;
103 layout.marginHeight = 0;
104 layout.marginWidth = 0;
105 parent.setLayout(layout);
107 Composite innerParent = new Composite(parent, SWT.NONE);
108 GridLayout innerLayout = new GridLayout();
109 innerLayout.numColumns = 2;
110 innerLayout.marginHeight = 0;
111 innerLayout.marginWidth = 0;
112 innerParent.setLayout(innerLayout);
113 GridData gd = new GridData(GridData.FILL_BOTH);
114 gd.horizontalSpan = 2;
115 innerParent.setLayoutData(gd);
120 SWT.CHECK | SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
122 GridData data = new GridData(GridData.FILL_BOTH);
123 data.widthHint = convertWidthInCharsToPixels(3);
124 data.heightHint = convertHeightInCharsToPixels(10);
125 table.setLayoutData(data);
127 table.setHeaderVisible(true);
128 table.setLinesVisible(true);
130 TableLayout tableLayout = new TableLayout();
131 table.setLayout(tableLayout);
133 TableColumn column1 = new TableColumn(table, SWT.NONE);
134 column1.setText(TemplateMessages.getString("TemplatePreferencePage.column.name")); //$NON-NLS-1$
136 TableColumn column2 = new TableColumn(table, SWT.NONE);
137 column2.setText(TemplateMessages.getString("TemplatePreferencePage.column.context")); //$NON-NLS-1$
139 TableColumn column3 = new TableColumn(table, SWT.NONE);
140 column3.setText(TemplateMessages.getString("TemplatePreferencePage.column.description")); //$NON-NLS-1$
142 fTableViewer = new CheckboxTableViewer(table);
143 fTableViewer.setLabelProvider(new TemplateLabelProvider());
144 fTableViewer.setContentProvider(new TemplateContentProvider());
146 fTableViewer.setSorter(new ViewerSorter() {
147 public int compare(Viewer viewer, Object object1, Object object2) {
148 if ((object1 instanceof Template) && (object2 instanceof Template)) {
149 Template left = (Template) object1;
150 Template right = (Template) object2;
151 int result = left.getName().compareToIgnoreCase(right.getName());
154 return left.getDescription().compareToIgnoreCase(
155 right.getDescription());
157 return super.compare(viewer, object1, object2);
160 public boolean isSorterProperty(Object element, String property) {
165 fTableViewer.addDoubleClickListener(new IDoubleClickListener() {
166 public void doubleClick(DoubleClickEvent e) {
171 fTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
172 public void selectionChanged(SelectionChangedEvent e) {
177 fTableViewer.addCheckStateListener(new ICheckStateListener() {
178 public void checkStateChanged(CheckStateChangedEvent event) {
179 Template template = (Template) event.getElement();
180 template.setEnabled(event.getChecked());
184 Composite buttons = new Composite(innerParent, SWT.NONE);
185 buttons.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
186 layout = new GridLayout();
187 layout.marginHeight = 0;
188 layout.marginWidth = 0;
189 buttons.setLayout(layout);
191 fAddButton = new Button(buttons, SWT.PUSH);
192 fAddButton.setText(TemplateMessages.getString("TemplatePreferencePage.new")); //$NON-NLS-1$
193 fAddButton.setLayoutData(getButtonGridData(fAddButton));
194 fAddButton.addListener(SWT.Selection, new Listener() {
195 public void handleEvent(Event e) {
200 fEditButton = new Button(buttons, SWT.PUSH);
201 fEditButton.setText(TemplateMessages.getString("TemplatePreferencePage.edit")); //$NON-NLS-1$
202 fEditButton.setLayoutData(getButtonGridData(fEditButton));
203 fEditButton.addListener(SWT.Selection, new Listener() {
204 public void handleEvent(Event e) {
209 fRemoveButton = new Button(buttons, SWT.PUSH);
210 fRemoveButton.setText(TemplateMessages.getString("TemplatePreferencePage.remove")); //$NON-NLS-1$
211 fRemoveButton.setLayoutData(getButtonGridData(fRemoveButton));
212 fRemoveButton.addListener(SWT.Selection, new Listener() {
213 public void handleEvent(Event e) {
218 fImportButton = new Button(buttons, SWT.PUSH);
219 fImportButton.setText(TemplateMessages.getString("TemplatePreferencePage.import")); //$NON-NLS-1$
220 fImportButton.setLayoutData(getButtonGridData(fImportButton));
221 fImportButton.addListener(SWT.Selection, new Listener() {
222 public void handleEvent(Event e) {
227 fExportButton = new Button(buttons, SWT.PUSH);
228 fExportButton.setText(TemplateMessages.getString("TemplatePreferencePage.export")); //$NON-NLS-1$
229 fExportButton.setLayoutData(getButtonGridData(fExportButton));
230 fExportButton.addListener(SWT.Selection, new Listener() {
231 public void handleEvent(Event e) {
236 fExportAllButton = new Button(buttons, SWT.PUSH);
237 fExportAllButton.setText(TemplateMessages.getString("TemplatePreferencePage.export.all")); //$NON-NLS-1$
238 fExportAllButton.setLayoutData(getButtonGridData(fExportAllButton));
239 fExportAllButton.addListener(SWT.Selection, new Listener() {
240 public void handleEvent(Event e) {
245 fEnableAllButton = new Button(buttons, SWT.PUSH);
246 fEnableAllButton.setText(TemplateMessages.getString("TemplatePreferencePage.enable.all")); //$NON-NLS-1$
247 fEnableAllButton.setLayoutData(getButtonGridData(fEnableAllButton));
248 fEnableAllButton.addListener(SWT.Selection, new Listener() {
249 public void handleEvent(Event e) {
254 fDisableAllButton = new Button(buttons, SWT.PUSH);
255 fDisableAllButton.setText(TemplateMessages.getString("TemplatePreferencePage.disable.all")); //$NON-NLS-1$
256 fDisableAllButton.setLayoutData(getButtonGridData(fDisableAllButton));
257 fDisableAllButton.addListener(SWT.Selection, new Listener() {
258 public void handleEvent(Event e) {
263 fPatternViewer = createViewer(parent);
265 // fFormatButton= new Button(parent, SWT.CHECK);
266 // fFormatButton.setText(ObfuscatorMessages.getString("TemplatePreferencePage.use.code.formatter")); //$NON-NLS-1$
267 // GridData gd1= new GridData();
268 // gd1.horizontalSpan= 2;
269 // fFormatButton.setLayoutData(gd1);
271 fTableViewer.setInput(fTemplates);
272 fTableViewer.setAllChecked(false);
273 fTableViewer.setCheckedElements(getEnabledTemplates());
275 IPreferenceStore prefs = PHPeclipsePlugin.getDefault().getPreferenceStore();
276 // fFormatButton.setSelection(prefs.getBoolean(PREF_FORMAT_TEMPLATES));
279 configureTableResizing(
287 // WorkbenchHelp.setHelp(parent, IJavaHelpContextIds.TEMPLATE_PREFERENCE_PAGE);
293 * Correctly resizes the table so no phantom columns appear
295 private static void configureTableResizing(
296 final Composite parent,
297 final Composite buttons,
299 final TableColumn column1,
300 final TableColumn column2,
301 final TableColumn column3) {
302 parent.addControlListener(new ControlAdapter() {
303 public void controlResized(ControlEvent e) {
304 Rectangle area = parent.getClientArea();
305 Point preferredSize = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
306 int width = area.width - 2 * table.getBorderWidth();
307 if (preferredSize.y > area.height) {
308 // Subtract the scrollbar width from the total column width
309 // if a vertical scrollbar will be required
310 Point vBarSize = table.getVerticalBar().getSize();
313 width -= buttons.getSize().x;
314 Point oldSize = table.getSize();
315 if (oldSize.x > width) {
316 // table is getting smaller so make the columns
317 // smaller first and then resize the table to
318 // match the client area width
319 column1.setWidth(width / 4);
320 column2.setWidth(width / 4);
321 column3.setWidth(width - (column1.getWidth() + column2.getWidth()));
322 table.setSize(width, area.height);
324 // table is getting bigger so make the table
325 // bigger first and then make the columns wider
326 // to match the client area width
327 table.setSize(width, area.height);
328 column1.setWidth(width / 4);
329 column2.setWidth(width / 4);
330 column3.setWidth(width - (column1.getWidth() + column2.getWidth()));
336 private Template[] getEnabledTemplates() {
337 Template[] templates = fTemplates.getTemplates();
339 List list = new ArrayList(templates.length);
341 for (int i = 0; i != templates.length; i++)
342 if (templates[i].isEnabled())
343 list.add(templates[i]);
345 return (Template[]) list.toArray(new Template[list.size()]);
348 private SourceViewer createViewer(Composite parent) {
349 Label label = new Label(parent, SWT.NONE);
350 label.setText(TemplateMessages.getString("TemplatePreferencePage.preview")); //$NON-NLS-1$
351 GridData data = new GridData();
352 data.horizontalSpan = 2;
353 label.setLayoutData(data);
355 SourceViewer viewer =
356 new SourceViewer(parent, null, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
357 JavaTextTools tools = PHPeclipsePlugin.getDefault().getJavaTextTools();
358 IDocument document = new Document();
359 IDocumentPartitioner partitioner = tools.createDocumentPartitioner();
360 document.setDocumentPartitioner(partitioner);
361 partitioner.connect(document);
363 viewer.configure(new PHPSourceViewerConfiguration(tools, null, IPHPPartitions.PHP_PARTITIONING));
365 viewer.setEditable(false);
366 viewer.setDocument(document);
367 viewer.getTextWidget().setBackground(
368 getShell().getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
370 Font font = JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT);
371 viewer.getTextWidget().setFont(font);
373 Control control = viewer.getControl();
374 data = new GridData(GridData.FILL_BOTH);
375 data.horizontalSpan = 2;
376 data.heightHint = convertHeightInCharsToPixels(5);
377 control.setLayoutData(data);
382 private static GridData getButtonGridData(Button button) {
383 GridData data = new GridData(GridData.FILL_HORIZONTAL);
384 data.widthHint = SWTUtil.getButtonWidthHint(button);
385 data.heightHint = SWTUtil.getButtonHeigthHint(button);
390 private void selectionChanged1() {
391 IStructuredSelection selection =
392 (IStructuredSelection) fTableViewer.getSelection();
394 if (selection.size() == 1) {
395 Template template = (Template) selection.getFirstElement();
396 fPatternViewer.getTextWidget().setText(template.getPattern());
398 fPatternViewer.getTextWidget().setText(""); //$NON-NLS-1$
404 private void updateButtons() {
406 ((IStructuredSelection) fTableViewer.getSelection()).size();
407 int itemCount = fTableViewer.getTable().getItemCount();
409 fEditButton.setEnabled(selectionCount == 1);
410 fExportButton.setEnabled(selectionCount > 0);
411 fRemoveButton.setEnabled(selectionCount > 0 && selectionCount <= itemCount);
412 fEnableAllButton.setEnabled(itemCount > 0);
413 fDisableAllButton.setEnabled(itemCount > 0);
418 Template template = new Template();
420 ContextTypeRegistry registry = ContextTypeRegistry.getInstance();
421 ContextType type = registry.getContextType("php"); //$NON-NLS-1$
423 String contextTypeName;
425 contextTypeName = type.getName();
427 Iterator iterator = registry.iterator();
428 contextTypeName = (String) iterator.next();
430 template.setContext(contextTypeName); //$NON-NLS-1$
432 EditTemplateDialog dialog =
433 new EditTemplateDialog(getShell(), template, false);
434 if (dialog.open() == EditTemplateDialog.OK) {
435 fTemplates.add(template);
436 fTableViewer.refresh();
437 fTableViewer.setChecked(template, template.isEnabled());
438 fTableViewer.setSelection(new StructuredSelection(template));
442 private void edit() {
443 IStructuredSelection selection =
444 (IStructuredSelection) fTableViewer.getSelection();
446 Object[] objects = selection.toArray();
447 if ((objects == null) || (objects.length != 1))
450 Template template = (Template) selection.getFirstElement();
454 private void edit(Template template) {
455 Template newTemplate = new Template(template);
456 EditTemplateDialog dialog =
457 new EditTemplateDialog(getShell(), newTemplate, true);
458 if (dialog.open() == EditTemplateDialog.OK) {
460 if (!newTemplate.getName().equals(template.getName()) && MessageDialog.openQuestion(getShell(), TemplateMessages.getString("TemplatePreferencePage.question.create.new.title"), //$NON-NLS-1$
461 TemplateMessages.getString("TemplatePreferencePage.question.create.new.message"))) //$NON-NLS-1$
463 template = newTemplate;
464 fTemplates.add(template);
465 fTableViewer.refresh();
467 template.setName(newTemplate.getName());
468 template.setDescription(newTemplate.getDescription());
469 template.setContext(newTemplate.getContextTypeName());
470 template.setPattern(newTemplate.getPattern());
471 fTableViewer.refresh(template);
473 fTableViewer.setChecked(template, template.isEnabled());
474 fTableViewer.setSelection(new StructuredSelection(template));
478 private void import_() {
479 FileDialog dialog = new FileDialog(getShell());
480 dialog.setText(TemplateMessages.getString("TemplatePreferencePage.import.title")); //$NON-NLS-1$
481 dialog.setFilterExtensions(new String[] { TemplateMessages.getString("TemplatePreferencePage.import.extension")}); //$NON-NLS-1$
482 String path = dialog.open();
488 fTemplates.addFromFile(new File(path));
490 fTableViewer.refresh();
491 fTableViewer.setAllChecked(false);
492 fTableViewer.setCheckedElements(getEnabledTemplates());
494 } catch (CoreException e) {
495 openReadErrorDialog(e);
499 private void exportAll() {
503 private void export() {
504 IStructuredSelection selection =
505 (IStructuredSelection) fTableViewer.getSelection();
506 Object[] templates = selection.toArray();
508 TemplateSet templateSet = new TemplateSet();
509 for (int i = 0; i != templates.length; i++)
510 templateSet.add((Template) templates[i]);
515 private void export(TemplateSet templateSet) {
516 FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
517 dialog.setText(TemplateMessages.getFormattedString("TemplatePreferencePage.export.title", new Integer(templateSet.getTemplates().length))); //$NON-NLS-1$
518 dialog.setFilterExtensions(new String[] { TemplateMessages.getString("TemplatePreferencePage.export.extension")}); //$NON-NLS-1$
519 dialog.setFileName(TemplateMessages.getString("TemplatePreferencePage.export.filename")); //$NON-NLS-1$
520 String path = dialog.open();
525 File file = new File(path);
527 if (!file.exists() || confirmOverwrite(file)) {
529 templateSet.saveToFile(file);
530 } catch (CoreException e) {
531 PHPeclipsePlugin.log(e);
532 openWriteErrorDialog(e);
537 private boolean confirmOverwrite(File file) {
538 return MessageDialog.openQuestion(getShell(), TemplateMessages.getString("TemplatePreferencePage.export.exists.title"), //$NON-NLS-1$
539 TemplateMessages.getFormattedString("TemplatePreferencePage.export.exists.message", file.getAbsolutePath())); //$NON-NLS-1$
542 private void remove() {
543 IStructuredSelection selection =
544 (IStructuredSelection) fTableViewer.getSelection();
546 Iterator elements = selection.iterator();
547 while (elements.hasNext()) {
548 Template template = (Template) elements.next();
549 fTemplates.remove(template);
552 fTableViewer.refresh();
555 private void enableAll(boolean enable) {
556 Template[] templates = fTemplates.getTemplates();
557 for (int i = 0; i != templates.length; i++)
558 templates[i].setEnabled(enable);
560 fTableViewer.setAllChecked(enable);
564 * @see IWorkbenchPreferencePage#init(IWorkbench)
566 public void init(IWorkbench workbench) {
570 * @see Control#setVisible(boolean)
572 public void setVisible(boolean visible) {
573 super.setVisible(visible);
575 setTitle(TemplateMessages.getString("TemplatePreferencePage.title")); //$NON-NLS-1$
579 * @see PreferencePage#performDefaults()
581 protected void performDefaults() {
582 IPreferenceStore prefs = PHPeclipsePlugin.getDefault().getPreferenceStore();
583 // fFormatButton.setSelection(prefs.getDefaultBoolean(PREF_FORMAT_TEMPLATES));
586 fTemplates.restoreDefaults();
587 } catch (CoreException e) {
588 PHPeclipsePlugin.log(e);
589 openReadErrorDialog(e);
593 fTableViewer.refresh();
594 fTableViewer.setAllChecked(false);
595 fTableViewer.setCheckedElements(getEnabledTemplates());
599 * @see PreferencePage#performOk()
601 public boolean performOk() {
602 IPreferenceStore prefs = PHPeclipsePlugin.getDefault().getPreferenceStore();
603 // prefs.setValue(PREF_FORMAT_TEMPLATES, fFormatButton.getSelection());
607 } catch (CoreException e) {
608 PHPeclipsePlugin.log(e);
609 openWriteErrorDialog(e);
612 PHPeclipsePlugin.getDefault().savePluginPreferences();
613 // PHPEditorEnvironment.disconnect(this);
614 return super.performOk();
618 * @see PreferencePage#performCancel()
620 public boolean performCancel() {
623 } catch (CoreException e) {
624 PHPeclipsePlugin.log(e);
625 openReadErrorDialog(e);
628 // PHPEditorEnvironment.disconnect(this);
629 return super.performCancel();
633 * Initializes the default values of this page in the preference bundle.
634 * Will be called on startup of the PHPeclipsePlugin
636 public static void initDefaults(IPreferenceStore prefs) {
637 prefs.setDefault(PREF_FORMAT_TEMPLATES, true);
640 // public static boolean useCodeFormatter() {
641 // IPreferenceStore prefs= PHPeclipsePlugin.getDefault().getPreferenceStore();
642 // return prefs.getBoolean(PREF_FORMAT_TEMPLATES);
645 private void openReadErrorDialog(CoreException e) {
646 ErrorDialog.openError(getShell(), TemplateMessages.getString("TemplatePreferencePage.error.read.title"), //$NON-NLS-1$
647 null, e.getStatus());
650 private void openWriteErrorDialog(CoreException e) {
651 ErrorDialog.openError(getShell(), TemplateMessages.getString("TemplatePreferencePage.error.write.title"), //$NON-NLS-1$
652 null, e.getStatus());