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;
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.source.SourceViewer;
30 import org.eclipse.jface.viewers.CheckStateChangedEvent;
31 import org.eclipse.jface.viewers.CheckboxTableViewer;
32 import org.eclipse.jface.viewers.DoubleClickEvent;
33 import org.eclipse.jface.viewers.ICheckStateListener;
34 import org.eclipse.jface.viewers.IDoubleClickListener;
35 import org.eclipse.jface.viewers.ISelectionChangedListener;
36 import org.eclipse.jface.viewers.IStructuredSelection;
37 import org.eclipse.jface.viewers.SelectionChangedEvent;
38 import org.eclipse.jface.viewers.StructuredSelection;
39 import org.eclipse.jface.viewers.TableLayout;
40 import org.eclipse.jface.viewers.Viewer;
41 import org.eclipse.jface.viewers.ViewerSorter;
42 import org.eclipse.swt.SWT;
43 import org.eclipse.swt.events.ControlAdapter;
44 import org.eclipse.swt.events.ControlEvent;
45 import org.eclipse.swt.graphics.Font;
46 import org.eclipse.swt.graphics.Point;
47 import org.eclipse.swt.graphics.Rectangle;
48 import org.eclipse.swt.layout.GridData;
49 import org.eclipse.swt.layout.GridLayout;
50 import org.eclipse.swt.widgets.Button;
51 import org.eclipse.swt.widgets.Composite;
52 import org.eclipse.swt.widgets.Control;
53 import org.eclipse.swt.widgets.Event;
54 import org.eclipse.swt.widgets.FileDialog;
55 import org.eclipse.swt.widgets.Label;
56 import org.eclipse.swt.widgets.Listener;
57 import org.eclipse.swt.widgets.Table;
58 import org.eclipse.swt.widgets.TableColumn;
59 import org.eclipse.ui.IWorkbench;
60 import org.eclipse.ui.IWorkbenchPreferencePage;
62 public class TemplatePreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
64 // preference store keys
65 private static final String PREF_FORMAT_TEMPLATES = PHPeclipsePlugin.PLUGIN_ID + ".template.format"; //$NON-NLS-1$
67 private Templates fTemplates;
69 private CheckboxTableViewer fTableViewer;
70 private Button fAddButton;
71 private Button fEditButton;
72 private Button fImportButton;
73 private Button fExportButton;
74 private Button fExportAllButton;
75 private Button fRemoveButton;
76 private Button fEnableAllButton;
77 private Button fDisableAllButton;
79 private SourceViewer fPatternViewer;
80 // private Button fFormatButton;
82 public TemplatePreferencePage() {
85 setPreferenceStore(PHPeclipsePlugin.getDefault().getPreferenceStore());
86 setDescription(TemplateMessages.getString("TemplatePreferencePage.message")); //$NON-NLS-1$
88 fTemplates = Templates.getInstance();
92 * @see PreferencePage#createContents(Composite)
94 protected Control createContents(Composite ancestor) {
95 PHPEditorEnvironment.connect(this);
96 Composite parent = new Composite(ancestor, SWT.NONE);
97 GridLayout layout = new GridLayout();
98 layout.numColumns = 2;
99 layout.marginHeight = 0;
100 layout.marginWidth = 0;
101 parent.setLayout(layout);
103 Composite innerParent = new Composite(parent, SWT.NONE);
104 GridLayout innerLayout = new GridLayout();
105 innerLayout.numColumns = 2;
106 innerLayout.marginHeight = 0;
107 innerLayout.marginWidth = 0;
108 innerParent.setLayout(innerLayout);
109 GridData gd = new GridData(GridData.FILL_BOTH);
110 gd.horizontalSpan = 2;
111 innerParent.setLayoutData(gd);
113 Table table = new Table(innerParent, SWT.CHECK | SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
115 GridData data = new GridData(GridData.FILL_BOTH);
116 data.widthHint = convertWidthInCharsToPixels(3);
117 data.heightHint = convertHeightInCharsToPixels(10);
118 table.setLayoutData(data);
120 table.setHeaderVisible(true);
121 table.setLinesVisible(true);
123 TableLayout tableLayout = new TableLayout();
124 table.setLayout(tableLayout);
126 TableColumn column1 = new TableColumn(table, SWT.NONE);
127 column1.setText(TemplateMessages.getString("TemplatePreferencePage.column.name")); //$NON-NLS-1$
129 TableColumn column2 = new TableColumn(table, SWT.NONE);
130 column2.setText(TemplateMessages.getString("TemplatePreferencePage.column.context")); //$NON-NLS-1$
132 TableColumn column3 = new TableColumn(table, SWT.NONE);
133 column3.setText(TemplateMessages.getString("TemplatePreferencePage.column.description")); //$NON-NLS-1$
135 fTableViewer = new CheckboxTableViewer(table);
136 fTableViewer.setLabelProvider(new TemplateLabelProvider());
137 fTableViewer.setContentProvider(new TemplateContentProvider());
139 fTableViewer.setSorter(new ViewerSorter() {
140 public int compare(Viewer viewer, Object object1, Object object2) {
141 if ((object1 instanceof Template) && (object2 instanceof Template)) {
142 Template left = (Template) object1;
143 Template right = (Template) object2;
144 int result = left.getName().compareToIgnoreCase(right.getName());
147 return left.getDescription().compareToIgnoreCase(right.getDescription());
149 return super.compare(viewer, object1, object2);
152 public boolean isSorterProperty(Object element, String property) {
157 fTableViewer.addDoubleClickListener(new IDoubleClickListener() {
158 public void doubleClick(DoubleClickEvent e) {
163 fTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
164 public void selectionChanged(SelectionChangedEvent e) {
169 fTableViewer.addCheckStateListener(new ICheckStateListener() {
170 public void checkStateChanged(CheckStateChangedEvent event) {
171 Template template = (Template) event.getElement();
172 template.setEnabled(event.getChecked());
176 Composite buttons = new Composite(innerParent, SWT.NONE);
177 buttons.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
178 layout = new GridLayout();
179 layout.marginHeight = 0;
180 layout.marginWidth = 0;
181 buttons.setLayout(layout);
183 fAddButton = new Button(buttons, SWT.PUSH);
184 fAddButton.setText(TemplateMessages.getString("TemplatePreferencePage.new")); //$NON-NLS-1$
185 fAddButton.setLayoutData(getButtonGridData(fAddButton));
186 fAddButton.addListener(SWT.Selection, new Listener() {
187 public void handleEvent(Event e) {
192 fEditButton = new Button(buttons, SWT.PUSH);
193 fEditButton.setText(TemplateMessages.getString("TemplatePreferencePage.edit")); //$NON-NLS-1$
194 fEditButton.setLayoutData(getButtonGridData(fEditButton));
195 fEditButton.addListener(SWT.Selection, new Listener() {
196 public void handleEvent(Event e) {
201 fRemoveButton = new Button(buttons, SWT.PUSH);
202 fRemoveButton.setText(TemplateMessages.getString("TemplatePreferencePage.remove")); //$NON-NLS-1$
203 fRemoveButton.setLayoutData(getButtonGridData(fRemoveButton));
204 fRemoveButton.addListener(SWT.Selection, new Listener() {
205 public void handleEvent(Event e) {
210 fImportButton = new Button(buttons, SWT.PUSH);
211 fImportButton.setText(TemplateMessages.getString("TemplatePreferencePage.import")); //$NON-NLS-1$
212 fImportButton.setLayoutData(getButtonGridData(fImportButton));
213 fImportButton.addListener(SWT.Selection, new Listener() {
214 public void handleEvent(Event e) {
219 fExportButton = new Button(buttons, SWT.PUSH);
220 fExportButton.setText(TemplateMessages.getString("TemplatePreferencePage.export")); //$NON-NLS-1$
221 fExportButton.setLayoutData(getButtonGridData(fExportButton));
222 fExportButton.addListener(SWT.Selection, new Listener() {
223 public void handleEvent(Event e) {
228 fExportAllButton = new Button(buttons, SWT.PUSH);
229 fExportAllButton.setText(TemplateMessages.getString("TemplatePreferencePage.export.all")); //$NON-NLS-1$
230 fExportAllButton.setLayoutData(getButtonGridData(fExportAllButton));
231 fExportAllButton.addListener(SWT.Selection, new Listener() {
232 public void handleEvent(Event e) {
237 fEnableAllButton = new Button(buttons, SWT.PUSH);
238 fEnableAllButton.setText(TemplateMessages.getString("TemplatePreferencePage.enable.all")); //$NON-NLS-1$
239 fEnableAllButton.setLayoutData(getButtonGridData(fEnableAllButton));
240 fEnableAllButton.addListener(SWT.Selection, new Listener() {
241 public void handleEvent(Event e) {
246 fDisableAllButton = new Button(buttons, SWT.PUSH);
247 fDisableAllButton.setText(TemplateMessages.getString("TemplatePreferencePage.disable.all")); //$NON-NLS-1$
248 fDisableAllButton.setLayoutData(getButtonGridData(fDisableAllButton));
249 fDisableAllButton.addListener(SWT.Selection, new Listener() {
250 public void handleEvent(Event e) {
255 fPatternViewer = createViewer(parent);
257 // fFormatButton= new Button(parent, SWT.CHECK);
258 // fFormatButton.setText(TemplateMessages.getString("TemplatePreferencePage.use.code.formatter")); //$NON-NLS-1$
259 // GridData gd1= new GridData();
260 // gd1.horizontalSpan= 2;
261 // fFormatButton.setLayoutData(gd1);
263 fTableViewer.setInput(fTemplates);
264 fTableViewer.setAllChecked(false);
265 fTableViewer.setCheckedElements(getEnabledTemplates());
267 IPreferenceStore prefs = PHPeclipsePlugin.getDefault().getPreferenceStore();
268 // fFormatButton.setSelection(prefs.getBoolean(PREF_FORMAT_TEMPLATES));
271 configureTableResizing(innerParent, buttons, table, column1, column2, column3);
273 // WorkbenchHelp.setHelp(parent, IJavaHelpContextIds.TEMPLATE_PREFERENCE_PAGE);
279 * Correctly resizes the table so no phantom columns appear
281 private static void configureTableResizing(
282 final Composite parent,
283 final Composite buttons,
285 final TableColumn column1,
286 final TableColumn column2,
287 final TableColumn column3) {
288 parent.addControlListener(new ControlAdapter() {
289 public void controlResized(ControlEvent e) {
290 Rectangle area = parent.getClientArea();
291 Point preferredSize = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
292 int width = area.width - 2 * table.getBorderWidth();
293 if (preferredSize.y > area.height) {
294 // Subtract the scrollbar width from the total column width
295 // if a vertical scrollbar will be required
296 Point vBarSize = table.getVerticalBar().getSize();
299 width -= buttons.getSize().x;
300 Point oldSize = table.getSize();
301 if (oldSize.x > width) {
302 // table is getting smaller so make the columns
303 // smaller first and then resize the table to
304 // match the client area width
305 column1.setWidth(width / 4);
306 column2.setWidth(width / 4);
307 column3.setWidth(width - (column1.getWidth() + column2.getWidth()));
308 table.setSize(width, area.height);
310 // table is getting bigger so make the table
311 // bigger first and then make the columns wider
312 // to match the client area width
313 table.setSize(width, area.height);
314 column1.setWidth(width / 4);
315 column2.setWidth(width / 4);
316 column3.setWidth(width - (column1.getWidth() + column2.getWidth()));
322 private Template[] getEnabledTemplates() {
323 Template[] templates = fTemplates.getTemplates();
325 List list = new ArrayList(templates.length);
327 for (int i = 0; i != templates.length; i++)
328 if (templates[i].isEnabled())
329 list.add(templates[i]);
331 return (Template[]) list.toArray(new Template[list.size()]);
334 private SourceViewer createViewer(Composite parent) {
335 Label label = new Label(parent, SWT.NONE);
336 label.setText(TemplateMessages.getString("TemplatePreferencePage.preview")); //$NON-NLS-1$
337 GridData data = new GridData();
338 data.horizontalSpan = 2;
339 label.setLayoutData(data);
341 SourceViewer viewer = new SourceViewer(parent, null, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
342 // JavaTextTools tools= PHPeclipsePlugin.getDefault().getJavaTextTools();
343 IDocument document = new Document();
344 // IDocumentPartitioner partitioner= new PHPPartitionScanner(); //tools.createDocumentPartitioner();
345 // document.setDocumentPartitioner(partitioner);
346 // partitioner.connect(document);
348 viewer.configure(new PHPSourceViewerConfiguration()); // (tools, null));
349 viewer.setEditable(false);
350 viewer.setDocument(document);
351 viewer.getTextWidget().setBackground(getShell().getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
353 Font font = JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT);
354 viewer.getTextWidget().setFont(font);
356 Control control = viewer.getControl();
357 data = new GridData(GridData.FILL_BOTH);
358 data.horizontalSpan = 2;
359 data.heightHint = convertHeightInCharsToPixels(5);
360 control.setLayoutData(data);
365 private static GridData getButtonGridData(Button button) {
366 GridData data = new GridData(GridData.FILL_HORIZONTAL);
367 data.widthHint = SWTUtil.getButtonWidthHint(button);
368 data.heightHint = SWTUtil.getButtonHeigthHint(button);
373 private void selectionChanged1() {
374 IStructuredSelection selection = (IStructuredSelection) fTableViewer.getSelection();
376 if (selection.size() == 1) {
377 Template template = (Template) selection.getFirstElement();
378 fPatternViewer.getTextWidget().setText(template.getPattern());
380 fPatternViewer.getTextWidget().setText(""); //$NON-NLS-1$
386 private void updateButtons() {
387 int selectionCount = ((IStructuredSelection) fTableViewer.getSelection()).size();
388 int itemCount = fTableViewer.getTable().getItemCount();
390 fEditButton.setEnabled(selectionCount == 1);
391 fExportButton.setEnabled(selectionCount > 0);
392 fRemoveButton.setEnabled(selectionCount > 0 && selectionCount <= itemCount);
393 fEnableAllButton.setEnabled(itemCount > 0);
394 fDisableAllButton.setEnabled(itemCount > 0);
399 Template template = new Template();
401 ContextTypeRegistry registry = ContextTypeRegistry.getInstance();
402 ContextType type = registry.getContextType("php"); //$NON-NLS-1$
404 String contextTypeName;
406 contextTypeName = type.getName();
408 Iterator iterator = registry.iterator();
409 contextTypeName = (String) iterator.next();
411 template.setContext(contextTypeName); //$NON-NLS-1$
413 EditTemplateDialog dialog = new EditTemplateDialog(getShell(), template, false);
414 if (dialog.open() == EditTemplateDialog.OK) {
415 fTemplates.add(template);
416 fTableViewer.refresh();
417 fTableViewer.setChecked(template, template.isEnabled());
418 fTableViewer.setSelection(new StructuredSelection(template));
422 private void edit() {
423 IStructuredSelection selection = (IStructuredSelection) fTableViewer.getSelection();
425 Object[] objects = selection.toArray();
426 if ((objects == null) || (objects.length != 1))
429 Template template = (Template) selection.getFirstElement();
433 private void edit(Template template) {
434 Template newTemplate = new Template(template);
435 EditTemplateDialog dialog = new EditTemplateDialog(getShell(), newTemplate, true);
436 if (dialog.open() == EditTemplateDialog.OK) {
438 if (!newTemplate.getName().equals(template.getName()) && MessageDialog.openQuestion(getShell(), TemplateMessages.getString("TemplatePreferencePage.question.create.new.title"), //$NON-NLS-1$
439 TemplateMessages.getString("TemplatePreferencePage.question.create.new.message"))) //$NON-NLS-1$
441 template = newTemplate;
442 fTemplates.add(template);
443 fTableViewer.refresh();
445 template.setName(newTemplate.getName());
446 template.setDescription(newTemplate.getDescription());
447 template.setContext(newTemplate.getContextTypeName());
448 template.setPattern(newTemplate.getPattern());
449 fTableViewer.refresh(template);
451 fTableViewer.setChecked(template, template.isEnabled());
452 fTableViewer.setSelection(new StructuredSelection(template));
456 private void import_() {
457 FileDialog dialog = new FileDialog(getShell());
458 dialog.setText(TemplateMessages.getString("TemplatePreferencePage.import.title")); //$NON-NLS-1$
459 dialog.setFilterExtensions(new String[] { TemplateMessages.getString("TemplatePreferencePage.import.extension")}); //$NON-NLS-1$
460 String path = dialog.open();
466 fTemplates.addFromFile(new File(path));
468 fTableViewer.refresh();
469 fTableViewer.setAllChecked(false);
470 fTableViewer.setCheckedElements(getEnabledTemplates());
472 } catch (CoreException e) {
473 openReadErrorDialog(e);
477 private void exportAll() {
481 private void export() {
482 IStructuredSelection selection = (IStructuredSelection) fTableViewer.getSelection();
483 Object[] templates = selection.toArray();
485 TemplateSet templateSet = new TemplateSet();
486 for (int i = 0; i != templates.length; i++)
487 templateSet.add((Template) templates[i]);
492 private void export(TemplateSet templateSet) {
493 FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
494 dialog.setText(TemplateMessages.getFormattedString("TemplatePreferencePage.export.title", new Integer(templateSet.getTemplates().length))); //$NON-NLS-1$
495 dialog.setFilterExtensions(new String[] { TemplateMessages.getString("TemplatePreferencePage.export.extension")}); //$NON-NLS-1$
496 dialog.setFileName(TemplateMessages.getString("TemplatePreferencePage.export.filename")); //$NON-NLS-1$
497 String path = dialog.open();
502 File file = new File(path);
504 if (!file.exists() || confirmOverwrite(file)) {
506 templateSet.saveToFile(file);
507 } catch (CoreException e) {
508 PHPeclipsePlugin.log(e);
509 openWriteErrorDialog(e);
514 private boolean confirmOverwrite(File file) {
515 return MessageDialog.openQuestion(getShell(), TemplateMessages.getString("TemplatePreferencePage.export.exists.title"), //$NON-NLS-1$
516 TemplateMessages.getFormattedString("TemplatePreferencePage.export.exists.message", file.getAbsolutePath())); //$NON-NLS-1$
519 private void remove() {
520 IStructuredSelection selection = (IStructuredSelection) fTableViewer.getSelection();
522 Iterator elements = selection.iterator();
523 while (elements.hasNext()) {
524 Template template = (Template) elements.next();
525 fTemplates.remove(template);
528 fTableViewer.refresh();
531 private void enableAll(boolean enable) {
532 Template[] templates = fTemplates.getTemplates();
533 for (int i = 0; i != templates.length; i++)
534 templates[i].setEnabled(enable);
536 fTableViewer.setAllChecked(enable);
540 * @see IWorkbenchPreferencePage#init(IWorkbench)
542 public void init(IWorkbench workbench) {
546 * @see Control#setVisible(boolean)
548 public void setVisible(boolean visible) {
549 super.setVisible(visible);
551 setTitle(TemplateMessages.getString("TemplatePreferencePage.title")); //$NON-NLS-1$
555 * @see PreferencePage#performDefaults()
557 protected void performDefaults() {
558 IPreferenceStore prefs = PHPeclipsePlugin.getDefault().getPreferenceStore();
559 // fFormatButton.setSelection(prefs.getDefaultBoolean(PREF_FORMAT_TEMPLATES));
562 fTemplates.restoreDefaults();
563 } catch (CoreException e) {
564 PHPeclipsePlugin.log(e);
565 openReadErrorDialog(e);
569 fTableViewer.refresh();
570 fTableViewer.setAllChecked(false);
571 fTableViewer.setCheckedElements(getEnabledTemplates());
575 * @see PreferencePage#performOk()
577 public boolean performOk() {
578 IPreferenceStore prefs = PHPeclipsePlugin.getDefault().getPreferenceStore();
579 // prefs.setValue(PREF_FORMAT_TEMPLATES, fFormatButton.getSelection());
583 } catch (CoreException e) {
584 PHPeclipsePlugin.log(e);
585 openWriteErrorDialog(e);
588 PHPeclipsePlugin.getDefault().savePluginPreferences();
589 PHPEditorEnvironment.disconnect(this);
590 return super.performOk();
594 * @see PreferencePage#performCancel()
596 public boolean performCancel() {
599 } catch (CoreException e) {
600 PHPeclipsePlugin.log(e);
601 openReadErrorDialog(e);
604 PHPEditorEnvironment.disconnect(this);
605 return super.performCancel();
609 * Initializes the default values of this page in the preference bundle.
610 * Will be called on startup of the PHPeclipsePlugin
612 public static void initDefaults(IPreferenceStore prefs) {
613 prefs.setDefault(PREF_FORMAT_TEMPLATES, true);
616 // public static boolean useCodeFormatter() {
617 // IPreferenceStore prefs= PHPeclipsePlugin.getDefault().getPreferenceStore();
618 // return prefs.getBoolean(PREF_FORMAT_TEMPLATES);
621 private void openReadErrorDialog(CoreException e) {
622 ErrorDialog.openError(getShell(), TemplateMessages.getString("TemplatePreferencePage.error.read.title"), //$NON-NLS-1$
623 null, e.getStatus());
626 private void openWriteErrorDialog(CoreException e) {
627 ErrorDialog.openError(getShell(), TemplateMessages.getString("TemplatePreferencePage.error.write.title"), //$NON-NLS-1$
628 null, e.getStatus());