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.PHPSourceViewerConfiguration;
19 import net.sourceforge.phpeclipse.phpeditor.php.PHPPartitionScanner;
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.IDocumentPartitioner;
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 Composite parent= new Composite(ancestor, SWT.NONE);
96 GridLayout layout= new GridLayout();
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(final Composite parent, final Composite buttons, final Table table, final TableColumn column1, final TableColumn column2, final TableColumn column3) {
281 parent.addControlListener(new ControlAdapter() {
282 public void controlResized(ControlEvent e) {
283 Rectangle area = parent.getClientArea();
284 Point preferredSize = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
285 int width = area.width - 2 * table.getBorderWidth();
286 if (preferredSize.y > area.height) {
287 // Subtract the scrollbar width from the total column width
288 // if a vertical scrollbar will be required
289 Point vBarSize = table.getVerticalBar().getSize();
292 width-= buttons.getSize().x;
293 Point oldSize = table.getSize();
294 if (oldSize.x > width) {
295 // table is getting smaller so make the columns
296 // smaller first and then resize the table to
297 // match the client area width
298 column1.setWidth(width/4);
299 column2.setWidth(width/4);
300 column3.setWidth(width - (column1.getWidth() + column2.getWidth()));
301 table.setSize(width, area.height);
303 // table is getting bigger so make the table
304 // bigger first and then make the columns wider
305 // to match the client area width
306 table.setSize(width, area.height);
307 column1.setWidth(width/4);
308 column2.setWidth(width/4);
309 column3.setWidth(width - (column1.getWidth() + column2.getWidth()));
316 private Template[] getEnabledTemplates() {
317 Template[] templates= fTemplates.getTemplates();
319 List list= new ArrayList(templates.length);
321 for (int i= 0; i != templates.length; i++)
322 if (templates[i].isEnabled())
323 list.add(templates[i]);
325 return (Template[]) list.toArray(new Template[list.size()]);
328 private SourceViewer createViewer(Composite parent) {
329 Label label= new Label(parent, SWT.NONE);
330 label.setText(TemplateMessages.getString("TemplatePreferencePage.preview")); //$NON-NLS-1$
331 GridData data= new GridData();
332 data.horizontalSpan= 2;
333 label.setLayoutData(data);
335 SourceViewer viewer= new SourceViewer(parent, null, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
336 // JavaTextTools tools= PHPeclipsePlugin.getDefault().getJavaTextTools();
337 IDocument document= new Document();
338 // IDocumentPartitioner partitioner= new PHPPartitionScanner(); //tools.createDocumentPartitioner();
339 // document.setDocumentPartitioner(partitioner);
340 // partitioner.connect(document);
341 viewer.configure(new PHPSourceViewerConfiguration()); // (tools, null));
342 viewer.setEditable(false);
343 viewer.setDocument(document);
344 viewer.getTextWidget().setBackground(getShell().getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
346 Font font= JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT);
347 viewer.getTextWidget().setFont(font);
349 Control control= viewer.getControl();
350 data= new GridData(GridData.FILL_BOTH);
351 data.horizontalSpan= 2;
352 data.heightHint= convertHeightInCharsToPixels(5);
353 control.setLayoutData(data);
358 private static GridData getButtonGridData(Button button) {
359 GridData data= new GridData(GridData.FILL_HORIZONTAL);
360 data.widthHint= SWTUtil.getButtonWidthHint(button);
361 data.heightHint= SWTUtil.getButtonHeigthHint(button);
366 private void selectionChanged1() {
367 IStructuredSelection selection= (IStructuredSelection) fTableViewer.getSelection();
369 if (selection.size() == 1) {
370 Template template= (Template) selection.getFirstElement();
371 fPatternViewer.getTextWidget().setText(template.getPattern());
373 fPatternViewer.getTextWidget().setText(""); //$NON-NLS-1$
379 private void updateButtons() {
380 int selectionCount= ((IStructuredSelection) fTableViewer.getSelection()).size();
381 int itemCount= fTableViewer.getTable().getItemCount();
383 fEditButton.setEnabled(selectionCount == 1);
384 fExportButton.setEnabled(selectionCount > 0);
385 fRemoveButton.setEnabled(selectionCount > 0 && selectionCount <= itemCount);
386 fEnableAllButton.setEnabled(itemCount > 0);
387 fDisableAllButton.setEnabled(itemCount > 0);
392 Template template= new Template();
394 ContextTypeRegistry registry=ContextTypeRegistry.getInstance();
395 ContextType type= registry.getContextType("php"); //$NON-NLS-1$
397 String contextTypeName;
399 contextTypeName= type.getName();
401 Iterator iterator= registry.iterator();
402 contextTypeName= (String) iterator.next();
404 template.setContext(contextTypeName); //$NON-NLS-1$
406 EditTemplateDialog dialog= new EditTemplateDialog(getShell(), template, false);
407 if (dialog.open() == dialog.OK) {
408 fTemplates.add(template);
409 fTableViewer.refresh();
410 fTableViewer.setChecked(template, template.isEnabled());
411 fTableViewer.setSelection(new StructuredSelection(template));
415 private void edit() {
416 IStructuredSelection selection= (IStructuredSelection) fTableViewer.getSelection();
418 Object[] objects= selection.toArray();
419 if ((objects == null) || (objects.length != 1))
422 Template template= (Template) selection.getFirstElement();
426 private void edit(Template template) {
427 Template newTemplate= new Template(template);
428 EditTemplateDialog dialog= new EditTemplateDialog(getShell(), newTemplate, true);
429 if (dialog.open() == dialog.OK) {
431 if (!newTemplate.getName().equals(template.getName()) &&
432 MessageDialog.openQuestion(getShell(),
433 TemplateMessages.getString("TemplatePreferencePage.question.create.new.title"), //$NON-NLS-1$
434 TemplateMessages.getString("TemplatePreferencePage.question.create.new.message"))) //$NON-NLS-1$
436 template= newTemplate;
437 fTemplates.add(template);
438 fTableViewer.refresh();
440 template.setName(newTemplate.getName());
441 template.setDescription(newTemplate.getDescription());
442 template.setContext(newTemplate.getContextTypeName());
443 template.setPattern(newTemplate.getPattern());
444 fTableViewer.refresh(template);
446 fTableViewer.setChecked(template, template.isEnabled());
447 fTableViewer.setSelection(new StructuredSelection(template));
451 private void import_() {
452 FileDialog dialog= new FileDialog(getShell());
453 dialog.setText(TemplateMessages.getString("TemplatePreferencePage.import.title")); //$NON-NLS-1$
454 dialog.setFilterExtensions(new String[] {TemplateMessages.getString("TemplatePreferencePage.import.extension")}); //$NON-NLS-1$
455 String path= dialog.open();
461 fTemplates.addFromFile(new File(path));
463 fTableViewer.refresh();
464 fTableViewer.setAllChecked(false);
465 fTableViewer.setCheckedElements(getEnabledTemplates());
467 } catch (CoreException e) {
468 openReadErrorDialog(e);
472 private void exportAll() {
476 private void export() {
477 IStructuredSelection selection= (IStructuredSelection) fTableViewer.getSelection();
478 Object[] templates= selection.toArray();
480 TemplateSet templateSet= new TemplateSet();
481 for (int i= 0; i != templates.length; i++)
482 templateSet.add((Template) templates[i]);
487 private void export(TemplateSet templateSet) {
488 FileDialog dialog= new FileDialog(getShell(), SWT.SAVE);
489 dialog.setText(TemplateMessages.getFormattedString("TemplatePreferencePage.export.title", new Integer(templateSet.getTemplates().length))); //$NON-NLS-1$
490 dialog.setFilterExtensions(new String[] {TemplateMessages.getString("TemplatePreferencePage.export.extension")}); //$NON-NLS-1$
491 dialog.setFileName(TemplateMessages.getString("TemplatePreferencePage.export.filename")); //$NON-NLS-1$
492 String path= dialog.open();
497 File file= new File(path);
499 if (!file.exists() || confirmOverwrite(file)) {
501 templateSet.saveToFile(file);
502 } catch (CoreException e) {
503 PHPeclipsePlugin.log(e);
504 openWriteErrorDialog(e);
509 private boolean confirmOverwrite(File file) {
510 return MessageDialog.openQuestion(getShell(),
511 TemplateMessages.getString("TemplatePreferencePage.export.exists.title"), //$NON-NLS-1$
512 TemplateMessages.getFormattedString("TemplatePreferencePage.export.exists.message", file.getAbsolutePath())); //$NON-NLS-1$
515 private void remove() {
516 IStructuredSelection selection= (IStructuredSelection) fTableViewer.getSelection();
518 Iterator elements= selection.iterator();
519 while (elements.hasNext()) {
520 Template template= (Template) elements.next();
521 fTemplates.remove(template);
524 fTableViewer.refresh();
527 private void enableAll(boolean enable) {
528 Template[] templates= fTemplates.getTemplates();
529 for (int i= 0; i != templates.length; i++)
530 templates[i].setEnabled(enable);
532 fTableViewer.setAllChecked(enable);
536 * @see IWorkbenchPreferencePage#init(IWorkbench)
538 public void init(IWorkbench workbench) {}
541 * @see Control#setVisible(boolean)
543 public void setVisible(boolean visible) {
544 super.setVisible(visible);
546 setTitle(TemplateMessages.getString("TemplatePreferencePage.title")); //$NON-NLS-1$
550 * @see PreferencePage#performDefaults()
552 protected void performDefaults() {
553 IPreferenceStore prefs= PHPeclipsePlugin.getDefault().getPreferenceStore();
554 // fFormatButton.setSelection(prefs.getDefaultBoolean(PREF_FORMAT_TEMPLATES));
557 fTemplates.restoreDefaults();
558 } catch (CoreException e) {
559 PHPeclipsePlugin.log(e);
560 openReadErrorDialog(e);
564 fTableViewer.refresh();
565 fTableViewer.setAllChecked(false);
566 fTableViewer.setCheckedElements(getEnabledTemplates());
570 * @see PreferencePage#performOk()
572 public boolean performOk() {
573 IPreferenceStore prefs= PHPeclipsePlugin.getDefault().getPreferenceStore();
574 // prefs.setValue(PREF_FORMAT_TEMPLATES, fFormatButton.getSelection());
578 } catch (CoreException e) {
579 PHPeclipsePlugin.log(e);
580 openWriteErrorDialog(e);
583 PHPeclipsePlugin.getDefault().savePluginPreferences();
584 return super.performOk();
588 * @see PreferencePage#performCancel()
590 public boolean performCancel() {
593 } catch (CoreException e) {
594 PHPeclipsePlugin.log(e);
595 openReadErrorDialog(e);
598 return super.performCancel();
602 * Initializes the default values of this page in the preference bundle.
603 * Will be called on startup of the PHPeclipsePlugin
605 public static void initDefaults(IPreferenceStore prefs) {
606 prefs.setDefault(PREF_FORMAT_TEMPLATES, true);
609 // public static boolean useCodeFormatter() {
610 // IPreferenceStore prefs= PHPeclipsePlugin.getDefault().getPreferenceStore();
611 // return prefs.getBoolean(PREF_FORMAT_TEMPLATES);
614 private void openReadErrorDialog(CoreException e) {
615 ErrorDialog.openError(getShell(),
616 TemplateMessages.getString("TemplatePreferencePage.error.read.title"), //$NON-NLS-1$
617 null, e.getStatus());
620 private void openWriteErrorDialog(CoreException e) {
621 ErrorDialog.openError(getShell(),
622 TemplateMessages.getString("TemplatePreferencePage.error.write.title"), //$NON-NLS-1$
623 null, e.getStatus());