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.phpeclipse.PHPeclipsePlugin;
18 import net.sourceforge.phpeclipse.phpeditor.PHPEditorEnvironment;
19 import net.sourceforge.phpeclipse.phpeditor.PHPSourceViewerConfiguration;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.jface.dialogs.ErrorDialog;
22 import org.eclipse.jface.dialogs.MessageDialog;
23 import org.eclipse.jface.preference.IPreferenceStore;
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.jface.viewers.CheckStateChangedEvent;
30 import org.eclipse.jface.viewers.CheckboxTableViewer;
31 import org.eclipse.jface.viewers.DoubleClickEvent;
32 import org.eclipse.jface.viewers.ICheckStateListener;
33 import org.eclipse.jface.viewers.IDoubleClickListener;
34 import org.eclipse.jface.viewers.ISelectionChangedListener;
35 import org.eclipse.jface.viewers.IStructuredSelection;
36 import org.eclipse.jface.viewers.SelectionChangedEvent;
37 import org.eclipse.jface.viewers.StructuredSelection;
38 import org.eclipse.jface.viewers.TableLayout;
39 import org.eclipse.jface.viewers.Viewer;
40 import org.eclipse.jface.viewers.ViewerSorter;
41 import org.eclipse.swt.SWT;
42 import org.eclipse.swt.events.ControlAdapter;
43 import org.eclipse.swt.events.ControlEvent;
44 import org.eclipse.swt.graphics.Font;
45 import org.eclipse.swt.graphics.Point;
46 import org.eclipse.swt.graphics.Rectangle;
47 import org.eclipse.swt.layout.GridData;
48 import org.eclipse.swt.layout.GridLayout;
49 import org.eclipse.swt.widgets.Button;
50 import org.eclipse.swt.widgets.Composite;
51 import org.eclipse.swt.widgets.Control;
52 import org.eclipse.swt.widgets.Event;
53 import org.eclipse.swt.widgets.FileDialog;
54 import org.eclipse.swt.widgets.Label;
55 import org.eclipse.swt.widgets.Listener;
56 import org.eclipse.swt.widgets.Table;
57 import org.eclipse.swt.widgets.TableColumn;
58 import org.eclipse.ui.IWorkbench;
59 import org.eclipse.ui.IWorkbenchPreferencePage;
61 public class TemplatePreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
63 // preference store keys
64 private static final String PREF_FORMAT_TEMPLATES = PHPeclipsePlugin.PLUGIN_ID + ".template.format"; //$NON-NLS-1$
66 private Templates fTemplates;
68 private CheckboxTableViewer fTableViewer;
69 private Button fAddButton;
70 private Button fEditButton;
71 private Button fImportButton;
72 private Button fExportButton;
73 private Button fExportAllButton;
74 private Button fRemoveButton;
75 private Button fEnableAllButton;
76 private Button fDisableAllButton;
78 private SourceViewer fPatternViewer;
79 // private Button fFormatButton;
81 public TemplatePreferencePage() {
84 setPreferenceStore(PHPeclipsePlugin.getDefault().getPreferenceStore());
85 setDescription(TemplateMessages.getString("TemplatePreferencePage.message")); //$NON-NLS-1$
87 fTemplates = Templates.getInstance();
91 * @see PreferencePage#createContents(Composite)
93 protected Control createContents(Composite ancestor) {
94 PHPEditorEnvironment.connect(this);
95 Composite parent = new Composite(ancestor, SWT.NONE);
96 GridLayout layout = new GridLayout();
97 layout.numColumns = 2;
98 layout.marginHeight = 0;
99 layout.marginWidth = 0;
100 parent.setLayout(layout);
102 Composite innerParent = new Composite(parent, SWT.NONE);
103 GridLayout innerLayout = new GridLayout();
104 innerLayout.numColumns = 2;
105 innerLayout.marginHeight = 0;
106 innerLayout.marginWidth = 0;
107 innerParent.setLayout(innerLayout);
108 GridData gd = new GridData(GridData.FILL_BOTH);
109 gd.horizontalSpan = 2;
110 innerParent.setLayoutData(gd);
112 Table table = new Table(innerParent, SWT.CHECK | SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
114 GridData data = new GridData(GridData.FILL_BOTH);
115 data.widthHint = convertWidthInCharsToPixels(3);
116 data.heightHint = convertHeightInCharsToPixels(10);
117 table.setLayoutData(data);
119 table.setHeaderVisible(true);
120 table.setLinesVisible(true);
122 TableLayout tableLayout = new TableLayout();
123 table.setLayout(tableLayout);
125 TableColumn column1 = new TableColumn(table, SWT.NONE);
126 column1.setText(TemplateMessages.getString("TemplatePreferencePage.column.name")); //$NON-NLS-1$
128 TableColumn column2 = new TableColumn(table, SWT.NONE);
129 column2.setText(TemplateMessages.getString("TemplatePreferencePage.column.context")); //$NON-NLS-1$
131 TableColumn column3 = new TableColumn(table, SWT.NONE);
132 column3.setText(TemplateMessages.getString("TemplatePreferencePage.column.description")); //$NON-NLS-1$
134 fTableViewer = new CheckboxTableViewer(table);
135 fTableViewer.setLabelProvider(new TemplateLabelProvider());
136 fTableViewer.setContentProvider(new TemplateContentProvider());
138 fTableViewer.setSorter(new ViewerSorter() {
139 public int compare(Viewer viewer, Object object1, Object object2) {
140 if ((object1 instanceof Template) && (object2 instanceof Template)) {
141 Template left = (Template) object1;
142 Template right = (Template) object2;
143 int result = left.getName().compareToIgnoreCase(right.getName());
146 return left.getDescription().compareToIgnoreCase(right.getDescription());
148 return super.compare(viewer, object1, object2);
151 public boolean isSorterProperty(Object element, String property) {
156 fTableViewer.addDoubleClickListener(new IDoubleClickListener() {
157 public void doubleClick(DoubleClickEvent e) {
162 fTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
163 public void selectionChanged(SelectionChangedEvent e) {
168 fTableViewer.addCheckStateListener(new ICheckStateListener() {
169 public void checkStateChanged(CheckStateChangedEvent event) {
170 Template template = (Template) event.getElement();
171 template.setEnabled(event.getChecked());
175 Composite buttons = new Composite(innerParent, SWT.NONE);
176 buttons.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
177 layout = new GridLayout();
178 layout.marginHeight = 0;
179 layout.marginWidth = 0;
180 buttons.setLayout(layout);
182 fAddButton = new Button(buttons, SWT.PUSH);
183 fAddButton.setText(TemplateMessages.getString("TemplatePreferencePage.new")); //$NON-NLS-1$
184 fAddButton.setLayoutData(getButtonGridData(fAddButton));
185 fAddButton.addListener(SWT.Selection, new Listener() {
186 public void handleEvent(Event e) {
191 fEditButton = new Button(buttons, SWT.PUSH);
192 fEditButton.setText(TemplateMessages.getString("TemplatePreferencePage.edit")); //$NON-NLS-1$
193 fEditButton.setLayoutData(getButtonGridData(fEditButton));
194 fEditButton.addListener(SWT.Selection, new Listener() {
195 public void handleEvent(Event e) {
200 fRemoveButton = new Button(buttons, SWT.PUSH);
201 fRemoveButton.setText(TemplateMessages.getString("TemplatePreferencePage.remove")); //$NON-NLS-1$
202 fRemoveButton.setLayoutData(getButtonGridData(fRemoveButton));
203 fRemoveButton.addListener(SWT.Selection, new Listener() {
204 public void handleEvent(Event e) {
209 fImportButton = new Button(buttons, SWT.PUSH);
210 fImportButton.setText(TemplateMessages.getString("TemplatePreferencePage.import")); //$NON-NLS-1$
211 fImportButton.setLayoutData(getButtonGridData(fImportButton));
212 fImportButton.addListener(SWT.Selection, new Listener() {
213 public void handleEvent(Event e) {
218 fExportButton = new Button(buttons, SWT.PUSH);
219 fExportButton.setText(TemplateMessages.getString("TemplatePreferencePage.export")); //$NON-NLS-1$
220 fExportButton.setLayoutData(getButtonGridData(fExportButton));
221 fExportButton.addListener(SWT.Selection, new Listener() {
222 public void handleEvent(Event e) {
227 fExportAllButton = new Button(buttons, SWT.PUSH);
228 fExportAllButton.setText(TemplateMessages.getString("TemplatePreferencePage.export.all")); //$NON-NLS-1$
229 fExportAllButton.setLayoutData(getButtonGridData(fExportAllButton));
230 fExportAllButton.addListener(SWT.Selection, new Listener() {
231 public void handleEvent(Event e) {
236 fEnableAllButton = new Button(buttons, SWT.PUSH);
237 fEnableAllButton.setText(TemplateMessages.getString("TemplatePreferencePage.enable.all")); //$NON-NLS-1$
238 fEnableAllButton.setLayoutData(getButtonGridData(fEnableAllButton));
239 fEnableAllButton.addListener(SWT.Selection, new Listener() {
240 public void handleEvent(Event e) {
245 fDisableAllButton = new Button(buttons, SWT.PUSH);
246 fDisableAllButton.setText(TemplateMessages.getString("TemplatePreferencePage.disable.all")); //$NON-NLS-1$
247 fDisableAllButton.setLayoutData(getButtonGridData(fDisableAllButton));
248 fDisableAllButton.addListener(SWT.Selection, new Listener() {
249 public void handleEvent(Event e) {
254 fPatternViewer = createViewer(parent);
256 // fFormatButton= new Button(parent, SWT.CHECK);
257 // fFormatButton.setText(TemplateMessages.getString("TemplatePreferencePage.use.code.formatter")); //$NON-NLS-1$
258 // GridData gd1= new GridData();
259 // gd1.horizontalSpan= 2;
260 // fFormatButton.setLayoutData(gd1);
262 fTableViewer.setInput(fTemplates);
263 fTableViewer.setAllChecked(false);
264 fTableViewer.setCheckedElements(getEnabledTemplates());
266 IPreferenceStore prefs = PHPeclipsePlugin.getDefault().getPreferenceStore();
267 // fFormatButton.setSelection(prefs.getBoolean(PREF_FORMAT_TEMPLATES));
270 configureTableResizing(innerParent, buttons, table, column1, column2, column3);
272 // WorkbenchHelp.setHelp(parent, IJavaHelpContextIds.TEMPLATE_PREFERENCE_PAGE);
278 * Correctly resizes the table so no phantom columns appear
280 private static void configureTableResizing(
281 final Composite parent,
282 final Composite buttons,
284 final TableColumn column1,
285 final TableColumn column2,
286 final TableColumn column3) {
287 parent.addControlListener(new ControlAdapter() {
288 public void controlResized(ControlEvent e) {
289 Rectangle area = parent.getClientArea();
290 Point preferredSize = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
291 int width = area.width - 2 * table.getBorderWidth();
292 if (preferredSize.y > area.height) {
293 // Subtract the scrollbar width from the total column width
294 // if a vertical scrollbar will be required
295 Point vBarSize = table.getVerticalBar().getSize();
298 width -= buttons.getSize().x;
299 Point oldSize = table.getSize();
300 if (oldSize.x > width) {
301 // table is getting smaller so make the columns
302 // smaller first and then resize the table to
303 // match the client area width
304 column1.setWidth(width / 4);
305 column2.setWidth(width / 4);
306 column3.setWidth(width - (column1.getWidth() + column2.getWidth()));
307 table.setSize(width, area.height);
309 // table is getting bigger so make the table
310 // bigger first and then make the columns wider
311 // to match the client area width
312 table.setSize(width, area.height);
313 column1.setWidth(width / 4);
314 column2.setWidth(width / 4);
315 column3.setWidth(width - (column1.getWidth() + column2.getWidth()));
321 private Template[] getEnabledTemplates() {
322 Template[] templates = fTemplates.getTemplates();
324 List list = new ArrayList(templates.length);
326 for (int i = 0; i != templates.length; i++)
327 if (templates[i].isEnabled())
328 list.add(templates[i]);
330 return (Template[]) list.toArray(new Template[list.size()]);
333 private SourceViewer createViewer(Composite parent) {
334 Label label = new Label(parent, SWT.NONE);
335 label.setText(TemplateMessages.getString("TemplatePreferencePage.preview")); //$NON-NLS-1$
336 GridData data = new GridData();
337 data.horizontalSpan = 2;
338 label.setLayoutData(data);
340 SourceViewer viewer = new SourceViewer(parent, null, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
341 // JavaTextTools tools= PHPeclipsePlugin.getDefault().getJavaTextTools();
342 IDocument document = new Document();
343 // IDocumentPartitioner partitioner= new PHPPartitionScanner(); //tools.createDocumentPartitioner();
344 // document.setDocumentPartitioner(partitioner);
345 // partitioner.connect(document);
347 viewer.configure(new PHPSourceViewerConfiguration()); // (tools, null));
348 viewer.setEditable(false);
349 viewer.setDocument(document);
350 viewer.getTextWidget().setBackground(getShell().getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
352 Font font = JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT);
353 viewer.getTextWidget().setFont(font);
355 Control control = viewer.getControl();
356 data = new GridData(GridData.FILL_BOTH);
357 data.horizontalSpan = 2;
358 data.heightHint = convertHeightInCharsToPixels(5);
359 control.setLayoutData(data);
364 private static GridData getButtonGridData(Button button) {
365 GridData data = new GridData(GridData.FILL_HORIZONTAL);
366 data.widthHint = SWTUtil.getButtonWidthHint(button);
367 data.heightHint = SWTUtil.getButtonHeigthHint(button);
372 private void selectionChanged1() {
373 IStructuredSelection selection = (IStructuredSelection) fTableViewer.getSelection();
375 if (selection.size() == 1) {
376 Template template = (Template) selection.getFirstElement();
377 fPatternViewer.getTextWidget().setText(template.getPattern());
379 fPatternViewer.getTextWidget().setText(""); //$NON-NLS-1$
385 private void updateButtons() {
386 int selectionCount = ((IStructuredSelection) fTableViewer.getSelection()).size();
387 int itemCount = fTableViewer.getTable().getItemCount();
389 fEditButton.setEnabled(selectionCount == 1);
390 fExportButton.setEnabled(selectionCount > 0);
391 fRemoveButton.setEnabled(selectionCount > 0 && selectionCount <= itemCount);
392 fEnableAllButton.setEnabled(itemCount > 0);
393 fDisableAllButton.setEnabled(itemCount > 0);
398 Template template = new Template();
400 ContextTypeRegistry registry = ContextTypeRegistry.getInstance();
401 ContextType type = registry.getContextType("php"); //$NON-NLS-1$
403 String contextTypeName;
405 contextTypeName = type.getName();
407 Iterator iterator = registry.iterator();
408 contextTypeName = (String) iterator.next();
410 template.setContext(contextTypeName); //$NON-NLS-1$
412 EditTemplateDialog dialog = new EditTemplateDialog(getShell(), template, false);
413 if (dialog.open() == dialog.OK) {
414 fTemplates.add(template);
415 fTableViewer.refresh();
416 fTableViewer.setChecked(template, template.isEnabled());
417 fTableViewer.setSelection(new StructuredSelection(template));
421 private void edit() {
422 IStructuredSelection selection = (IStructuredSelection) fTableViewer.getSelection();
424 Object[] objects = selection.toArray();
425 if ((objects == null) || (objects.length != 1))
428 Template template = (Template) selection.getFirstElement();
432 private void edit(Template template) {
433 Template newTemplate = new Template(template);
434 EditTemplateDialog dialog = new EditTemplateDialog(getShell(), newTemplate, true);
435 if (dialog.open() == dialog.OK) {
437 if (!newTemplate.getName().equals(template.getName()) && MessageDialog.openQuestion(getShell(), TemplateMessages.getString("TemplatePreferencePage.question.create.new.title"), //$NON-NLS-1$
438 TemplateMessages.getString("TemplatePreferencePage.question.create.new.message"))) //$NON-NLS-1$
440 template = newTemplate;
441 fTemplates.add(template);
442 fTableViewer.refresh();
444 template.setName(newTemplate.getName());
445 template.setDescription(newTemplate.getDescription());
446 template.setContext(newTemplate.getContextTypeName());
447 template.setPattern(newTemplate.getPattern());
448 fTableViewer.refresh(template);
450 fTableViewer.setChecked(template, template.isEnabled());
451 fTableViewer.setSelection(new StructuredSelection(template));
455 private void import_() {
456 FileDialog dialog = new FileDialog(getShell());
457 dialog.setText(TemplateMessages.getString("TemplatePreferencePage.import.title")); //$NON-NLS-1$
458 dialog.setFilterExtensions(new String[] { TemplateMessages.getString("TemplatePreferencePage.import.extension")}); //$NON-NLS-1$
459 String path = dialog.open();
465 fTemplates.addFromFile(new File(path));
467 fTableViewer.refresh();
468 fTableViewer.setAllChecked(false);
469 fTableViewer.setCheckedElements(getEnabledTemplates());
471 } catch (CoreException e) {
472 openReadErrorDialog(e);
476 private void exportAll() {
480 private void export() {
481 IStructuredSelection selection = (IStructuredSelection) fTableViewer.getSelection();
482 Object[] templates = selection.toArray();
484 TemplateSet templateSet = new TemplateSet();
485 for (int i = 0; i != templates.length; i++)
486 templateSet.add((Template) templates[i]);
491 private void export(TemplateSet templateSet) {
492 FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
493 dialog.setText(TemplateMessages.getFormattedString("TemplatePreferencePage.export.title", new Integer(templateSet.getTemplates().length))); //$NON-NLS-1$
494 dialog.setFilterExtensions(new String[] { TemplateMessages.getString("TemplatePreferencePage.export.extension")}); //$NON-NLS-1$
495 dialog.setFileName(TemplateMessages.getString("TemplatePreferencePage.export.filename")); //$NON-NLS-1$
496 String path = dialog.open();
501 File file = new File(path);
503 if (!file.exists() || confirmOverwrite(file)) {
505 templateSet.saveToFile(file);
506 } catch (CoreException e) {
507 PHPeclipsePlugin.log(e);
508 openWriteErrorDialog(e);
513 private boolean confirmOverwrite(File file) {
514 return MessageDialog.openQuestion(getShell(), TemplateMessages.getString("TemplatePreferencePage.export.exists.title"), //$NON-NLS-1$
515 TemplateMessages.getFormattedString("TemplatePreferencePage.export.exists.message", file.getAbsolutePath())); //$NON-NLS-1$
518 private void remove() {
519 IStructuredSelection selection = (IStructuredSelection) fTableViewer.getSelection();
521 Iterator elements = selection.iterator();
522 while (elements.hasNext()) {
523 Template template = (Template) elements.next();
524 fTemplates.remove(template);
527 fTableViewer.refresh();
530 private void enableAll(boolean enable) {
531 Template[] templates = fTemplates.getTemplates();
532 for (int i = 0; i != templates.length; i++)
533 templates[i].setEnabled(enable);
535 fTableViewer.setAllChecked(enable);
539 * @see IWorkbenchPreferencePage#init(IWorkbench)
541 public void init(IWorkbench workbench) {
545 * @see Control#setVisible(boolean)
547 public void setVisible(boolean visible) {
548 super.setVisible(visible);
550 setTitle(TemplateMessages.getString("TemplatePreferencePage.title")); //$NON-NLS-1$
554 * @see PreferencePage#performDefaults()
556 protected void performDefaults() {
557 IPreferenceStore prefs = PHPeclipsePlugin.getDefault().getPreferenceStore();
558 // fFormatButton.setSelection(prefs.getDefaultBoolean(PREF_FORMAT_TEMPLATES));
561 fTemplates.restoreDefaults();
562 } catch (CoreException e) {
563 PHPeclipsePlugin.log(e);
564 openReadErrorDialog(e);
568 fTableViewer.refresh();
569 fTableViewer.setAllChecked(false);
570 fTableViewer.setCheckedElements(getEnabledTemplates());
574 * @see PreferencePage#performOk()
576 public boolean performOk() {
577 IPreferenceStore prefs = PHPeclipsePlugin.getDefault().getPreferenceStore();
578 // prefs.setValue(PREF_FORMAT_TEMPLATES, fFormatButton.getSelection());
582 } catch (CoreException e) {
583 PHPeclipsePlugin.log(e);
584 openWriteErrorDialog(e);
587 PHPeclipsePlugin.getDefault().savePluginPreferences();
588 PHPEditorEnvironment.disconnect(this);
589 return super.performOk();
593 * @see PreferencePage#performCancel()
595 public boolean performCancel() {
598 } catch (CoreException e) {
599 PHPeclipsePlugin.log(e);
600 openReadErrorDialog(e);
603 PHPEditorEnvironment.disconnect(this);
604 return super.performCancel();
608 * Initializes the default values of this page in the preference bundle.
609 * Will be called on startup of the PHPeclipsePlugin
611 public static void initDefaults(IPreferenceStore prefs) {
612 prefs.setDefault(PREF_FORMAT_TEMPLATES, true);
615 // public static boolean useCodeFormatter() {
616 // IPreferenceStore prefs= PHPeclipsePlugin.getDefault().getPreferenceStore();
617 // return prefs.getBoolean(PREF_FORMAT_TEMPLATES);
620 private void openReadErrorDialog(CoreException e) {
621 ErrorDialog.openError(getShell(), TemplateMessages.getString("TemplatePreferencePage.error.read.title"), //$NON-NLS-1$
622 null, e.getStatus());
625 private void openWriteErrorDialog(CoreException e) {
626 ErrorDialog.openError(getShell(), TemplateMessages.getString("TemplatePreferencePage.error.write.title"), //$NON-NLS-1$
627 null, e.getStatus());