Author: yradtsevich
Date: 2009-03-19 10:55:25 -0400 (Thu, 19 Mar 2009)
New Revision: 14264
Added:
branches/jbosstools-3.0.x/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/Message.java
Modified:
branches/jbosstools-3.0.x/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/VpeEditAnyDialog.java
branches/jbosstools-3.0.x/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/messages/VpeUIMessages.java
branches/jbosstools-3.0.x/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/messages/messages.properties
Log:
RESOLVED - issue JBIDE-2521: Allow to select html tag wich should be used to render
unknown tag in VPE Template dialog
https://jira.jboss.org/jira/browse/JBIDE-2521
- Validation of VPE Template dialog has been added.
Added:
branches/jbosstools-3.0.x/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/Message.java
===================================================================
---
branches/jbosstools-3.0.x/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/Message.java
(rev 0)
+++
branches/jbosstools-3.0.x/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/Message.java 2009-03-19
14:55:25 UTC (rev 14264)
@@ -0,0 +1,35 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Exadel, Inc. and Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Exadel, Inc. and Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.vpe.editor;
+
+import org.eclipse.jface.dialogs.IMessageProvider;
+
+/**
+ * Simple implementation of {@link IMessageProvider}.
+ *
+ * @author yradtsevich
+ *
+ */
+public class Message implements IMessageProvider {
+ private String message;
+ private int messageType;
+
+ public Message(String text, int type) {
+ this.message = text;
+ this.messageType = type;
+ }
+ public String getMessage() {
+ return message;
+ }
+ public int getMessageType() {
+ return messageType;
+ }
+}
Modified:
branches/jbosstools-3.0.x/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/VpeEditAnyDialog.java
===================================================================
---
branches/jbosstools-3.0.x/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/VpeEditAnyDialog.java 2009-03-19
13:36:13 UTC (rev 14263)
+++
branches/jbosstools-3.0.x/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/editor/template/VpeEditAnyDialog.java 2009-03-19
14:55:25 UTC (rev 14264)
@@ -10,12 +10,18 @@
******************************************************************************/
package org.jboss.tools.vpe.editor.template;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import org.eclipse.jface.dialogs.IDialogConstants;
+import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
@@ -30,7 +36,13 @@
import org.jboss.tools.jst.jsp.JspEditorPlugin;
import org.jboss.tools.jst.jsp.outline.cssdialog.CSSStyleDialog;
import org.jboss.tools.jst.jsp.outline.cssdialog.common.Constants;
+import org.jboss.tools.vpe.VpePlugin;
+import org.jboss.tools.vpe.editor.template.expression.VpeExpressionBuilder;
+import org.jboss.tools.vpe.editor.Message;
+import org.jboss.tools.vpe.editor.template.expression.VpeExpressionBuilderException;
import org.jboss.tools.vpe.messages.VpeUIMessages;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Document;
/**
* Class which presents dialog for any template
@@ -41,9 +53,10 @@
private VpeAnyData data;
private CheckControl ctlChildren;
- private Text txtTagForDisplay;
+ private Text txtTagForDisplay;
private Text txtValue;
private Text txtStyle;
+ private VpeEditAnyDialogValidator templateVerifier;
public VpeEditAnyDialog(Shell shell, VpeAnyData data) {
super(shell);
@@ -52,10 +65,12 @@
@Override
protected Control createDialogArea(Composite parent) {
+ templateVerifier = new VpeEditAnyDialogValidator();
getShell().setText(VpeUIMessages.TEMPLATE);
setTitle(VpeUIMessages.TAG_ATTRIBUTES);
- setMessage((data.getUri() != null ? ("URI: " + data.getUri() +
"\n") : "") + VpeUIMessages.TAG_NAME + data.getName()); //$NON-NLS-1$
//$NON-NLS-2$ //$NON-NLS-3$
+ setMessage(getDefaultMessage());
+
Composite topComposite = (Composite)super.createDialogArea(parent);
((GridData)topComposite.getLayoutData()).widthHint = 300;
@@ -80,6 +95,7 @@
gd.horizontalSpan=2;
txtTagForDisplay.setLayoutData(gd);
txtTagForDisplay.setText(data.getTagForDisplay() != null ?
data.getTagForDisplay() : ""); //$NON-NLS-1$
+ txtTagForDisplay.addModifyListener(templateVerifier);
// txtTagForDisplay.select(tagNameItemIndex);
@@ -95,6 +111,7 @@
gd.horizontalSpan = 2;
txtValue.setLayoutData(gd);
txtValue.setText(data.getValue() != null ? data.getValue() : "");
//$NON-NLS-1$
+ txtValue.addModifyListener(templateVerifier);
//style control
Label lbStyle = makeLabel(composite, VpeUIMessages.STYLE);
@@ -132,7 +149,20 @@
return composite;
}
+
+
+ @Override
+ public void create() {
+ super.create();
+ templateVerifier.validateAll(false);
+ }
+
+ private IMessageProvider getDefaultMessage() {
+ final String message = (data.getUri() != null ? ("URI: " +
data.getUri() + "\n") : "") + VpeUIMessages.TAG_NAME + data.getName();
//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ return new Message(message, IMessageProvider.NONE);
+ }
+
private Label makeLabel(Composite parent, String text) {
Label lbl = new Label(parent, SWT.NONE);
lbl.setText(text);
@@ -174,6 +204,20 @@
return data.isChanged() || (oldValue != newValue);
}
+ /**
+ * Sets the message for this dialog with an indication of what type of
+ * message it is.
+ * <p>
+ * @param message the message, or <code>null</code> to clear the message
+ */
+ public void setMessage(IMessageProvider message) {
+ if (message == null) {
+ setMessage(null, IMessageProvider.NONE);
+ } else {
+ setMessage(message.getMessage(), message.getMessageType());
+ }
+ }
+
private class CheckControl {
private Label label;
private Button button;
@@ -198,4 +242,97 @@
return button.getSelection();
}
}
+
+ /**
+ * Validator of {@link VpeEditAnyDialog}.
+ *
+ * @author yradtsevich
+ */
+ private class VpeEditAnyDialogValidator implements ModifyListener {
+ /**
+ * Used to validate tag-names.
+ */
+ private Document xmlDocument = null;
+ public VpeEditAnyDialogValidator() {
+ try {
+ xmlDocument =
DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
+ } catch (ParserConfigurationException e) {
+ VpePlugin.getPluginLog().logError(e);
+ }
+ }
+
+ /**
+ * Validates {@link VpeEditAnyDialog#txtTagForDisplay} field.
+ *
+ * @return {@code null} if it is valid or an instance of {@code IMessageProvider} if
+ * it contains any error.
+ */
+ private IMessageProvider validateTagForDisplay() {
+ if (xmlDocument != null) {
+ try {
+ xmlDocument.createElement(txtTagForDisplay.getText());
+ } catch (DOMException e) {
+ return new Message(VpeUIMessages.TAG_FOR_DISPLAY +
VpeUIMessages.ERROR_MESSAGE_POSTFIX + " (" + //$NON-NLS-1$
+ e.getMessage() + ")." , IMessageProvider.ERROR); //$NON-NLS-1$
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Validates {@link VpeEditAnyDialog#txtValue} field.
+ *
+ * @return {@code null} if it is valid or an instance of {@code IMessageProvider} if
+ * it contains any error.
+ */
+ private IMessageProvider validateValue() {
+
+ try {
+ VpeExpressionBuilder.buildCompletedExpression(txtValue.getText(), true);
+ } catch (VpeExpressionBuilderException e) {
+ return new Message(VpeUIMessages.VALUE + VpeUIMessages.ERROR_MESSAGE_POSTFIX + "
(" + //$NON-NLS-1$
+ e.getMessage() + ")." , IMessageProvider.ERROR); //$NON-NLS-1$
+ }
+
+ return null;
+ }
+
+ /**
+ * Validates all fields of {@link VpeEditAnyDialog} and changes the view of
+ * dialog according to validation results.
+ *
+ * @param updateMessage if it is {@code true}, the dialog's message will be
updated.
+ */
+ void validateAll(boolean updateMessage) {
+ IMessageProvider message = VpeEditAnyDialog.this.getDefaultMessage();
+
+ IMessageProvider tagForDisplayMessage = validateTagForDisplay();
+ IMessageProvider valueMessage = validateValue();
+
+ if (tagForDisplayMessage != null) {
+ message = tagForDisplayMessage;
+ } else if (valueMessage != null) {
+ message = valueMessage;
+ }
+
+ Button okButton = getButton(IDialogConstants.OK_ID);
+ if (message.getMessageType() <= IMessageProvider.INFORMATION) {
+ okButton.setEnabled(true);
+ } else {
+ okButton.setEnabled(false);
+ }
+
+ if (updateMessage) {
+ VpeEditAnyDialog.this.setMessage(message);
+ }
+ }
+
+ /**
+ * Fired when a field is modified.
+ */
+ public void modifyText(ModifyEvent e) {
+ validateAll(true);
+ }
+ }
}
Modified:
branches/jbosstools-3.0.x/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/messages/VpeUIMessages.java
===================================================================
---
branches/jbosstools-3.0.x/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/messages/VpeUIMessages.java 2009-03-19
13:36:13 UTC (rev 14263)
+++
branches/jbosstools-3.0.x/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/messages/VpeUIMessages.java 2009-03-19
14:55:25 UTC (rev 14264)
@@ -81,6 +81,7 @@
public static String SHOW;
public static String HIDE;
public static String TAG_FOR_DISPLAY;
+ public static String ERROR_MESSAGE_POSTFIX;
public static String TemplatesPreferencePage_Edit;
public static String TemplatesPreferencePage_Remove;
public static String TemplatesTableProvider_Children;
Modified:
branches/jbosstools-3.0.x/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/messages/messages.properties
===================================================================
---
branches/jbosstools-3.0.x/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/messages/messages.properties 2009-03-19
13:36:13 UTC (rev 14263)
+++
branches/jbosstools-3.0.x/vpe/plugins/org.jboss.tools.vpe/src/org/jboss/tools/vpe/messages/messages.properties 2009-03-19
14:55:25 UTC (rev 14264)
@@ -59,6 +59,7 @@
SHOW=Show
HIDE=Hide
TAG_FOR_DISPLAY=Tag for Display
+ERROR_MESSAGE_POSTFIX=\ is not valid
TemplatesPreferencePage_Edit=Edit
TemplatesPreferencePage_Remove=Remove
TemplatesTableProvider_Children=Children