2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.ui.preferences;
7 import java.util.ArrayList;
8 import java.util.HashMap;
9 import java.util.Iterator;
10 import java.util.List;
12 import java.util.Vector;
14 import net.sourceforge.phpdt.internal.corext.template.ContextTypeRegistry;
15 import net.sourceforge.phpdt.internal.corext.template.Template;
16 import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
17 import net.sourceforge.phpdt.internal.corext.template.TemplateTranslator;
18 import net.sourceforge.phpdt.internal.ui.dialog.StatusDialog;
19 import net.sourceforge.phpdt.internal.ui.dialog.StatusInfo;
20 import net.sourceforge.phpdt.internal.ui.text.template.TemplateVariableProcessor;
21 import net.sourceforge.phpdt.internal.ui.util.SWTUtil;
22 import net.sourceforge.phpeclipse.IPreferenceConstants;
23 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
24 import net.sourceforge.phpeclipse.phpeditor.PHPSourceViewerConfiguration;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.jface.action.Action;
27 import org.eclipse.jface.action.GroupMarker;
28 import org.eclipse.jface.action.IAction;
29 import org.eclipse.jface.action.IMenuListener;
30 import org.eclipse.jface.action.IMenuManager;
31 import org.eclipse.jface.action.MenuManager;
32 import org.eclipse.jface.action.Separator;
33 import org.eclipse.jface.preference.IPreferenceStore;
34 import org.eclipse.jface.preference.PreferenceConverter;
35 import org.eclipse.jface.resource.JFaceResources;
36 import org.eclipse.jface.text.Document;
37 import org.eclipse.jface.text.IDocument;
38 import org.eclipse.jface.text.ITextListener;
39 import org.eclipse.jface.text.ITextOperationTarget;
40 import org.eclipse.jface.text.ITextViewer;
41 import org.eclipse.jface.text.ITextViewerExtension;
42 import org.eclipse.jface.text.TextEvent;
43 import org.eclipse.jface.text.contentassist.ContentAssistant;
44 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
45 import org.eclipse.jface.text.contentassist.IContentAssistant;
46 import org.eclipse.jface.text.source.ISourceViewer;
47 import org.eclipse.jface.text.source.SourceViewer;
48 import org.eclipse.jface.viewers.ISelectionChangedListener;
49 import org.eclipse.jface.viewers.SelectionChangedEvent;
50 import org.eclipse.swt.SWT;
51 import org.eclipse.swt.custom.StyledText;
52 import org.eclipse.swt.custom.VerifyKeyListener;
53 import org.eclipse.swt.events.KeyEvent;
54 import org.eclipse.swt.events.KeyListener;
55 import org.eclipse.swt.events.ModifyEvent;
56 import org.eclipse.swt.events.ModifyListener;
57 import org.eclipse.swt.events.SelectionEvent;
58 import org.eclipse.swt.events.SelectionListener;
59 import org.eclipse.swt.events.VerifyEvent;
60 import org.eclipse.swt.graphics.Color;
61 import org.eclipse.swt.graphics.Font;
62 import org.eclipse.swt.graphics.RGB;
63 import org.eclipse.swt.layout.GridData;
64 import org.eclipse.swt.layout.GridLayout;
65 import org.eclipse.swt.widgets.Button;
66 import org.eclipse.swt.widgets.Combo;
67 import org.eclipse.swt.widgets.Composite;
68 import org.eclipse.swt.widgets.Control;
69 import org.eclipse.swt.widgets.Display;
70 import org.eclipse.swt.widgets.Label;
71 import org.eclipse.swt.widgets.Menu;
72 import org.eclipse.swt.widgets.Shell;
73 import org.eclipse.swt.widgets.Text;
74 import org.eclipse.ui.texteditor.ITextEditorActionConstants;
75 import org.eclipse.ui.texteditor.IUpdate;
78 * Dialog to edit a template.
80 public class EditTemplateDialog extends StatusDialog {
82 private static class SimpleJavaSourceViewerConfiguration extends PHPSourceViewerConfiguration {
84 private final IContentAssistProcessor fProcessor;
86 // SimpleJavaSourceViewerConfiguration(JavaTextTools tools, ITextEditor editor, IContentAssistProcessor processor) {
87 SimpleJavaSourceViewerConfiguration(IContentAssistProcessor processor) {
89 fProcessor= processor;
93 * @see SourceViewerConfiguration#getContentAssistant(ISourceViewer)
95 public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
97 IPreferenceStore store= PHPeclipsePlugin.getDefault().getPreferenceStore();
99 ContentAssistant assistant= new ContentAssistant();
100 assistant.setContentAssistProcessor(fProcessor, IDocument.DEFAULT_CONTENT_TYPE);
101 // Register the same processor for strings and single line comments to get code completion at the start of those partitions.
102 // assistant.setContentAssistProcessor(fProcessor, JavaPartitionScanner.JAVA_STRING);
103 // assistant.setContentAssistProcessor(fProcessor, JavaPartitionScanner.JAVA_SINGLE_LINE_COMMENT);
105 // assistant.setContentAssistProcessor(fProcessor, JavaPartitionScanner.JAVA_DOC);
107 // assistant.enableAutoInsert(store.getBoolean(ContentAssistPreference.AUTOINSERT));
108 // assistant.enableAutoActivation(store.getBoolean(ContentAssistPreference.AUTOACTIVATION));
109 // assistant.setAutoActivationDelay(store.getInt(ContentAssistPreference.AUTOACTIVATION_DELAY));
110 assistant.setProposalPopupOrientation(assistant.PROPOSAL_OVERLAY);
111 assistant.setContextInformationPopupOrientation(assistant.CONTEXT_INFO_ABOVE);
112 // assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
114 Display display= sourceViewer.getTextWidget().getDisplay();
116 Color background= createColor(store, IPreferenceConstants.PROPOSALS_BACKGROUND, display);
117 assistant.setContextInformationPopupBackground(background);
118 assistant.setContextSelectorBackground(background);
119 assistant.setProposalSelectorBackground(background);
121 Color foreground= createColor(store, IPreferenceConstants.PROPOSALS_FOREGROUND, display);
122 assistant.setContextInformationPopupForeground(foreground);
123 assistant.setContextSelectorForeground(foreground);
124 assistant.setProposalSelectorForeground(foreground);
130 * Creates a color from the information stored in the given preference store.
131 * Returns <code>null</code> if there is no such information available.
133 private Color createColor(IPreferenceStore store, String key, Display display) {
137 if (store.contains(key)) {
139 if (store.isDefault(key))
140 rgb= PreferenceConverter.getDefaultColor(store, key);
142 rgb= PreferenceConverter.getColor(store, key);
145 return new Color(display, rgb);
152 private static class TextViewerAction extends Action implements IUpdate {
154 private int fOperationCode= -1;
155 private ITextOperationTarget fOperationTarget;
157 public TextViewerAction(ITextViewer viewer, int operationCode) {
158 fOperationCode= operationCode;
159 fOperationTarget= viewer.getTextOperationTarget();
164 * Updates the enabled state of the action.
165 * Fires a property change if the enabled state changes.
167 * @see Action#firePropertyChange(String, Object, Object)
169 public void update() {
171 boolean wasEnabled= isEnabled();
172 boolean isEnabled= (fOperationTarget != null && fOperationTarget.canDoOperation(fOperationCode));
173 setEnabled(isEnabled);
175 if (wasEnabled != isEnabled) {
176 firePropertyChange(ENABLED, wasEnabled ? Boolean.TRUE : Boolean.FALSE, isEnabled ? Boolean.TRUE : Boolean.FALSE);
184 if (fOperationCode != -1 && fOperationTarget != null) {
185 fOperationTarget.doOperation(fOperationCode);
190 private final Template fTemplate;
192 private Text fNameText;
193 private Text fDescriptionText;
194 private Combo fContextCombo;
195 private SourceViewer fPatternEditor;
196 private Button fInsertVariableButton;
198 private TemplateTranslator fTranslator= new TemplateTranslator();
199 private boolean fSuppressError= true; // #4354
200 private Map fGlobalActions= new HashMap(10);
201 private List fSelectionActions = new ArrayList(3);
202 private Vector fContextTypes= new Vector();
204 private final TemplateVariableProcessor fProcessor= new TemplateVariableProcessor();
206 public EditTemplateDialog(Shell parent, Template template, boolean edit) {
209 setShellStyle(getShellStyle() | SWT.MAX | SWT.RESIZE);
212 ? TemplateMessages.getString("EditTemplateDialog.title.edit") //$NON-NLS-1$
213 : TemplateMessages.getString("EditTemplateDialog.title.new"); //$NON-NLS-1$
218 ContextTypeRegistry registry= ContextTypeRegistry.getInstance();
219 for (Iterator iterator= registry.iterator(); iterator.hasNext(); )
220 fContextTypes.add(iterator.next());
222 if (fContextTypes.size() > 0)
223 fProcessor.setContextType(ContextTypeRegistry.getInstance().getContextType((String) fContextTypes.get(0)));
227 * @see Dialog#createDialogArea(Composite)
229 protected Control createDialogArea(Composite ancestor) {
230 Composite parent= new Composite(ancestor, SWT.NONE);
231 GridLayout layout= new GridLayout();
232 layout.numColumns= 2;
233 parent.setLayout(layout);
234 parent.setLayoutData(new GridData(GridData.FILL_BOTH));
236 createLabel(parent, TemplateMessages.getString("EditTemplateDialog.name")); //$NON-NLS-1$
238 Composite composite= new Composite(parent, SWT.NONE);
239 composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
240 layout= new GridLayout();
241 layout.numColumns= 3;
242 layout.marginWidth= 0;
243 layout.marginHeight= 0;
244 composite.setLayout(layout);
246 fNameText= createText(composite);
247 fNameText.addModifyListener(new ModifyListener() {
248 public void modifyText(ModifyEvent e) {
249 if (fSuppressError && (fNameText.getText().trim().length() != 0))
250 fSuppressError= false;
256 createLabel(composite, TemplateMessages.getString("EditTemplateDialog.context")); //$NON-NLS-1$
257 fContextCombo= new Combo(composite, SWT.READ_ONLY);
259 for (Iterator iterator= fContextTypes.iterator(); iterator.hasNext(); )
260 fContextCombo.add((String) iterator.next());
262 fContextCombo.addModifyListener(new ModifyListener() {
263 public void modifyText(ModifyEvent e) {
264 String name= fContextCombo.getText();
265 fProcessor.setContextType(ContextTypeRegistry.getInstance().getContextType(name));
269 createLabel(parent, TemplateMessages.getString("EditTemplateDialog.description")); //$NON-NLS-1$
270 fDescriptionText= createText(parent);
272 Label patternLabel= createLabel(parent, TemplateMessages.getString("EditTemplateDialog.pattern")); //$NON-NLS-1$
273 patternLabel.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
274 fPatternEditor= createEditor(parent);
276 Label filler= new Label(parent, SWT.NONE);
277 filler.setLayoutData(new GridData());
279 composite= new Composite(parent, SWT.NONE);
280 layout= new GridLayout();
281 layout.marginWidth= 0;
282 layout.marginHeight= 0;
283 composite.setLayout(layout);
284 composite.setLayoutData(new GridData());
286 fInsertVariableButton= new Button(composite, SWT.NONE);
287 fInsertVariableButton.setLayoutData(getButtonGridData(fInsertVariableButton));
288 fInsertVariableButton.setText(TemplateMessages.getString("EditTemplateDialog.insert.variable")); //$NON-NLS-1$
289 fInsertVariableButton.addSelectionListener(new SelectionListener() {
290 public void widgetSelected(SelectionEvent e) {
291 fPatternEditor.getTextWidget().setFocus();
292 fPatternEditor.doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
295 public void widgetDefaultSelected(SelectionEvent e) {}
298 fNameText.setText(fTemplate.getName());
299 fDescriptionText.setText(fTemplate.getDescription());
300 fContextCombo.select(getIndex(fTemplate.getContextTypeName()));
307 private static GridData getButtonGridData(Button button) {
308 GridData data= new GridData(GridData.FILL_HORIZONTAL);
309 data.heightHint= SWTUtil.getButtonHeigthHint(button);
314 private static Label createLabel(Composite parent, String name) {
315 Label label= new Label(parent, SWT.NULL);
317 label.setLayoutData(new GridData());
322 private static Text createText(Composite parent) {
323 Text text= new Text(parent, SWT.BORDER);
324 text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
329 private SourceViewer createEditor(Composite parent) {
330 SourceViewer viewer= new SourceViewer(parent, null, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
331 // JavaTextTools tools= JavaPlugin.getDefault().getJavaTextTools();
332 IDocument document= new Document(fTemplate.getPattern());
333 // IDocumentPartitioner partitioner= tools.createDocumentPartitioner();
334 // document.setDocumentPartitioner(partitioner);
335 // partitioner.connect(document);
336 // viewer.configure(new SimpleJavaSourceViewerConfiguration(tools, null, fProcessor));
337 viewer.configure(new SimpleJavaSourceViewerConfiguration(fProcessor));
338 viewer.setEditable(true);
339 viewer.setDocument(document);
341 Font font= JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT);
342 viewer.getTextWidget().setFont(font);
344 Control control= viewer.getControl();
345 GridData data= new GridData(GridData.FILL_BOTH);
346 data.widthHint= convertWidthInCharsToPixels(60);
347 data.heightHint= convertHeightInCharsToPixels(5);
348 control.setLayoutData(data);
350 viewer.addTextListener(new ITextListener() {
351 public void textChanged(TextEvent event) {
353 fTranslator.translate(event.getDocumentEvent().getDocument().get());
354 } catch (CoreException e) {
355 PHPeclipsePlugin.log(e);
363 viewer.addSelectionChangedListener(new ISelectionChangedListener() {
364 public void selectionChanged(SelectionChangedEvent event) {
365 updateSelectionDependentActions();
369 if (viewer instanceof ITextViewerExtension) {
370 ((ITextViewerExtension) viewer).prependVerifyKeyListener(new VerifyKeyListener() {
371 public void verifyKey(VerifyEvent event) {
372 handleVerifyKeyPressed(event);
376 viewer.getTextWidget().addKeyListener(new KeyListener() {
377 public void keyPressed(KeyEvent e) {
381 public void keyReleased(KeyEvent e) {}
388 private void handleKeyPressed(KeyEvent event) {
389 if (event.stateMask != SWT.CTRL)
392 switch (event.character) {
394 fPatternEditor.doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
398 case (int) 'z' - (int) 'a' + 1:
399 fPatternEditor.doOperation(ITextOperationTarget.UNDO);
404 private void handleVerifyKeyPressed(VerifyEvent event) {
408 if (event.stateMask != SWT.CTRL)
411 switch (event.character) {
413 fPatternEditor.doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
418 case (int) 'z' - (int) 'a' + 1:
419 fPatternEditor.doOperation(ITextOperationTarget.UNDO);
425 private void initializeActions() {
426 TextViewerAction action= new TextViewerAction(fPatternEditor, fPatternEditor.UNDO);
427 action.setText(TemplateMessages.getString("EditTemplateDialog.undo")); //$NON-NLS-1$
428 fGlobalActions.put(ITextEditorActionConstants.UNDO, action);
430 action= new TextViewerAction(fPatternEditor, fPatternEditor.CUT);
431 action.setText(TemplateMessages.getString("EditTemplateDialog.cut")); //$NON-NLS-1$
432 fGlobalActions.put(ITextEditorActionConstants.CUT, action);
434 action= new TextViewerAction(fPatternEditor, fPatternEditor.COPY);
435 action.setText(TemplateMessages.getString("EditTemplateDialog.copy")); //$NON-NLS-1$
436 fGlobalActions.put(ITextEditorActionConstants.COPY, action);
438 action= new TextViewerAction(fPatternEditor, fPatternEditor.PASTE);
439 action.setText(TemplateMessages.getString("EditTemplateDialog.paste")); //$NON-NLS-1$
440 fGlobalActions.put(ITextEditorActionConstants.PASTE, action);
442 action= new TextViewerAction(fPatternEditor, fPatternEditor.SELECT_ALL);
443 action.setText(TemplateMessages.getString("EditTemplateDialog.select.all")); //$NON-NLS-1$
444 fGlobalActions.put(ITextEditorActionConstants.SELECT_ALL, action);
446 action= new TextViewerAction(fPatternEditor, fPatternEditor.CONTENTASSIST_PROPOSALS);
447 action.setText(TemplateMessages.getString("EditTemplateDialog.content.assist")); //$NON-NLS-1$
448 fGlobalActions.put("ContentAssistProposal", action); //$NON-NLS-1$
450 fSelectionActions.add(ITextEditorActionConstants.CUT);
451 fSelectionActions.add(ITextEditorActionConstants.COPY);
452 fSelectionActions.add(ITextEditorActionConstants.PASTE);
454 // create context menu
455 MenuManager manager= new MenuManager(null, null);
456 manager.setRemoveAllWhenShown(true);
457 manager.addMenuListener(new IMenuListener() {
458 public void menuAboutToShow(IMenuManager mgr) {
459 fillContextMenu(mgr);
463 StyledText text= fPatternEditor.getTextWidget();
464 Menu menu= manager.createContextMenu(text);
468 private void fillContextMenu(IMenuManager menu) {
469 menu.add(new GroupMarker(ITextEditorActionConstants.GROUP_UNDO));
470 menu.appendToGroup(ITextEditorActionConstants.GROUP_UNDO, (IAction) fGlobalActions.get(ITextEditorActionConstants.UNDO));
472 menu.add(new Separator(ITextEditorActionConstants.GROUP_EDIT));
473 menu.appendToGroup(ITextEditorActionConstants.GROUP_EDIT, (IAction) fGlobalActions.get(ITextEditorActionConstants.CUT));
474 menu.appendToGroup(ITextEditorActionConstants.GROUP_EDIT, (IAction) fGlobalActions.get(ITextEditorActionConstants.COPY));
475 menu.appendToGroup(ITextEditorActionConstants.GROUP_EDIT, (IAction) fGlobalActions.get(ITextEditorActionConstants.PASTE));
476 menu.appendToGroup(ITextEditorActionConstants.GROUP_EDIT, (IAction) fGlobalActions.get(ITextEditorActionConstants.SELECT_ALL));
478 // menu.add(new Separator(IContextMenuConstants.GROUP_GENERATE));
479 // menu.appendToGroup(IContextMenuConstants.GROUP_GENERATE, (IAction) fGlobalActions.get("ContentAssistProposal")); //$NON-NLS-1$
482 protected void updateSelectionDependentActions() {
483 Iterator iterator= fSelectionActions.iterator();
484 while (iterator.hasNext())
485 updateAction((String)iterator.next());
488 protected void updateUndoAction() {
489 IAction action= (IAction) fGlobalActions.get(ITextEditorActionConstants.UNDO);
490 if (action instanceof IUpdate)
491 ((IUpdate) action).update();
494 protected void updateAction(String actionId) {
495 IAction action= (IAction) fGlobalActions.get(actionId);
496 if (action instanceof IUpdate)
497 ((IUpdate) action).update();
500 private int getIndex(String context) {
501 ContextTypeRegistry registry= ContextTypeRegistry.getInstance();
502 registry.getContextType(context);
507 return fContextTypes.indexOf(context);
510 protected void okPressed() {
511 fTemplate.setName(fNameText.getText());
512 fTemplate.setDescription(fDescriptionText.getText());
513 fTemplate.setContext(fContextCombo.getText());
514 fTemplate.setPattern(fPatternEditor.getTextWidget().getText());
519 private void updateButtons() {
520 boolean valid= fNameText.getText().trim().length() != 0;
522 StatusInfo status= new StatusInfo();
526 status.setError(""); //$NON-NLS-1$
528 status.setError(TemplateMessages.getString("EditTemplateDialog.error.noname")); //$NON-NLS-1$
529 } else if (fTranslator.getErrorMessage() != null) {
530 status.setError(fTranslator.getErrorMessage());
533 updateStatus(status);
537 * @see org.eclipse.jface.window.Window#configureShell(Shell)
539 protected void configureShell(Shell newShell) {
540 super.configureShell(newShell);
541 // WorkbenchHelp.setHelp(newShell, IJavaHelpContextIds.EDIT_TEMPLATE_DIALOG);