Author: mareshkau
Date: 2009-03-04 13:50:54 -0500 (Wed, 04 Mar 2009)
New Revision: 13982
Added:
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/outline/cssdialog/common/CSSValidator.java
Modified:
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/messages/JstUIMessages.java
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/messages/messages.properties
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/outline/cssdialog/CSSClassDialog.java
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/outline/cssdialog/StyleComposite.java
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/outline/cssdialog/events/StyleAttributes.java
Log:
https://jira.jboss.org/jira/browse/JBIDE-3909
Modified:
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/messages/JstUIMessages.java
===================================================================
---
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/messages/JstUIMessages.java 2009-03-04
18:44:37 UTC (rev 13981)
+++
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/messages/JstUIMessages.java 2009-03-04
18:50:54 UTC (rev 13982)
@@ -104,4 +104,5 @@
public static String BUTTON_ADD_NEW_STYLE_CLASS;
public static String ENTER_CSS_CLASS_NAME;
public static String CSS_CLASS_NAME_NOT_VALID;
+ public static String CSS_INVALID_STYLE_PROPERTY;
}
Modified:
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/messages/messages.properties
===================================================================
---
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/messages/messages.properties 2009-03-04
18:44:37 UTC (rev 13981)
+++
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/messages/messages.properties 2009-03-04
18:50:54 UTC (rev 13982)
@@ -8,6 +8,7 @@
CSS_STYLE_EDITOR_TITLE=CSS Style
CSS_EMPTY_FILE_PATH_MESSAGE=Choose any CSS file
CSS_EMPTY_STYLE_CLASS_MESSAGE=Style class is empty
+CSS_INVALID_STYLE_PROPERTY=Invalid Style class property has been entered.Preview Tab will
be not updated.
CSS_SAVE_DIALOG_TITLE=Save CSS file
CSS_SAVE_DIALOG_MESSAGE=CSS file has been changed.\nWould you like to save changes?
Modified:
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/outline/cssdialog/CSSClassDialog.java
===================================================================
---
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/outline/cssdialog/CSSClassDialog.java 2009-03-04
18:44:37 UTC (rev 13981)
+++
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/outline/cssdialog/CSSClassDialog.java 2009-03-04
18:50:54 UTC (rev 13982)
@@ -50,6 +50,7 @@
import org.jboss.tools.common.model.ui.widgets.Split;
import org.jboss.tools.jst.jsp.messages.JstUIMessages;
import org.jboss.tools.jst.jsp.outline.cssdialog.common.CSSModel;
+import org.jboss.tools.jst.jsp.outline.cssdialog.common.CSSValidator;
import org.jboss.tools.jst.jsp.outline.cssdialog.common.Constants;
import org.jboss.tools.jst.jsp.outline.cssdialog.common.Util;
import org.jboss.tools.jst.jsp.outline.cssdialog.events.ChangeStyleEvent;
@@ -64,7 +65,7 @@
*
* @author Igor Zhukov (izhukov(a)exadel.com)
*/
-public class CSSClassDialog extends TitleAreaDialog {
+public class CSSClassDialog extends TitleAreaDialog implements ChangeStyleListener {
public static final String ID =
"org.jboss.tools.jst.jsp.outline.cssdialog.CSSClassDialog"; //$NON-NLS-1$
@@ -108,6 +109,8 @@
private IStatus filePathStatus = null;
// 2. holds an error if the destination class style is empty
private IStatus classNameStatus = null;
+ // 3. holds an error if inccorrect property was specified
+ private IStatus cssValueStatus =null;
// an array of subscribed message dialog listener
private ArrayList<MessageDialogListener> errorListeners = new
ArrayList<MessageDialogListener>();
@@ -115,12 +118,6 @@
// parameter indicates if dialog was opened from Wizard
private final boolean callFromWizard;
- // this job is used to correctly process change style class combo text with delay
-// private UIJob uiJob = null;
- // the job name
-// private String jobName = "Update CSS Composite"; //$NON-NLS-1$
-// // delay for job in milliseconds
-// private int delay = 1000;
/**
* Constructor.
@@ -138,6 +135,7 @@
classNameStatus = new Status(IStatus.ERROR, notUsed, 0,
JstUIMessages.CSS_EMPTY_STYLE_CLASS_MESSAGE, null);
styleAttributes = new StyleAttributes();
+ styleAttributes.addChangeStyleListener(this);
this.callFromWizard = callFromWizard;
this.selection = selection;
init();
@@ -164,7 +162,6 @@
if (currentFile == null) {
currentFile = Util.getActiveCssFile();
}
-// currentClassStyle = Util.getActivePageCSSSelectorIfAny();
}
/**
@@ -253,7 +250,7 @@
styleChanged = true;
if (currentClassStyle != null &&
!currentClassStyle.equals(Constants.EMPTY)
&& currentFile != null &&
!currentFile.equals(Constants.EMPTY)) {
- applyButton.setEnabled(true);
+ updateApplyButton(true);
}
}
});
@@ -281,16 +278,6 @@
// add content assist to style COMBO component
SimpleContentProposalProvider proposalProvider = new
SimpleContentProposalProvider(classCombo.getItems());
proposalProvider.setFiltering(true);
-// contentAssistAdapter = new ContentAssistCommandAdapter(
-// classCombo, new ComboContentAdapter(), proposalProvider, null, null, true);
-// contentAssistAdapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
-// contentAssistAdapter.addContentProposalListener(new IContentProposalListener() {
-// public void proposalAccepted(IContentProposal proposal) {
-// cssStyleClassChanged();
-// applyButton.setEnabled(false);
-// keyInputSelector = false;
-// }
-// });
split.setWeights(new int[]{15, 85});
split.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true,
true));
@@ -324,80 +311,6 @@
applyToStatusLine(status);
}
});
-
-// Button button = new Button(parent, SWT.PUSH);
-// button.setLayoutData(new GridData(GridData.END, GridData.CENTER, false,
false));
-//
-// ImageDescriptor imageDesc =
JspEditorPlugin.getImageDescriptor(Constants.IMAGE_FOLDERLARGE_FILE_LOCATION);
-// Image image = imageDesc.createImage();
-// button.setImage(image);
-// button.setToolTipText(JstUIMessages.CSS_BROWSE_BUTTON_TOOLTIP);
-// button.addDisposeListener(new DisposeListener() {
-// public void widgetDisposed(DisposeEvent e) {
-// Button button = (Button) e.getSource();
-// button.getImage().dispose();
-// }
-// });
-// button.addSelectionListener(new SelectionAdapter() {
-// public void widgetSelected(SelectionEvent event) {
-//// IResource project = Util.getCurrentProject();
-// IResource project = ResourcesPlugin.getWorkspace().getRoot();
-//
-// ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(getShell(),
-// new WorkbenchLabelProvider(), new BaseWorkbenchContentProvider());
-// dialog.addFilter(new FileExtensionFilter(fileExtensions));
-// dialog.setTitle(JstUIMessages.CSS_FILE_SELECT_DIALOG_TITLE);
-// dialog.setMessage(JstUIMessages.CSS_FILE_SELECT_DIALOG_LABEL);
-// dialog.setInput(project);
-// dialog.setAllowMultiple(false);
-// dialog.setDoubleClickSelects(false);
-// if (currentFile != null) {
-// dialog.setInitialSelection(currentFile);
-// }
-// dialog.setEmptyListMessage(JstUIMessages.CSS_FILE_SELECT_DIALOG_EMPTY_MESSAGE);
-//
-// if (dialog.open() == Window.OK) {
-// IResource res = (IResource) dialog.getFirstResult();
-// // make some important saving actions
-// if (res instanceof IFile) {
-// if (styleChanged && currentFile != null) {
-// MessageBox messageBox = new MessageBox(getParentShell(), SWT.YES | SWT.NO |
SWT.ICON_QUESTION);
-// messageBox.setText(JstUIMessages.CSS_SAVE_DIALOG_TITLE);
-// messageBox.setMessage(CSSClassDialog.getMessageForSaveDialog(currentFile));
-// int result = messageBox.open();
-// if (result == SWT.YES) {
-// saveChanges(true);
-// } else {
-// updateStyleComposite();
-// }
-// }
-// styleComposite.revertPreview();
-// releaseResources();
-//
-// // open new CSS file and initialize dialog
-// boolean useRelativePath = true;
-// if (project instanceof IWorkspaceRoot) {
-// useRelativePath = false;
-// }
-// boolean updateCSSModel = false;
-// if (currentFile != null && !currentFile.equals(Constants.EMPTY)) {
-// currentClassStyle = null;
-// updateCSSModel = true;
-// }
-// currentFile = (IFile)res;
-// initCSSModel(currentFile, useRelativePath, updateCSSModel);
-// updateOKButtonState();
-// applyButton.setEnabled(false);
-// styleChanged = false;
-//
-// // update content assist proposals
-// SimpleContentProposalProvider proposalProvider =
-//
(SimpleContentProposalProvider)contentAssistAdapter.getContentProposalProvider();
-// proposalProvider.setProposals(classCombo.getItems());
-// }
-// }
-// }
-// });
}
/**
@@ -415,24 +328,6 @@
classCombo = new Combo(parent, SWT.BORDER|SWT.READ_ONLY);
classCombo.setLayoutData(gridData);
- // add selection listener
-// classCombo.addSelectionListener(new SelectionAdapter() {
-// public void widgetSelected(SelectionEvent e) {
-// if (keyInputSelector) {
-// keyInputSelector = false;
-// if (currentClassStyle != null &&
classCombo.indexOf(currentClassStyle) != -1) {
-// return;
-// }
-// }
-// keyInputSelector = false;
-// }
-// });
- // add key modified listener
-// classCombo.addKeyListener(new KeyAdapter() {
-// public void keyReleased(KeyEvent e) {
-// keyInputSelector = true;
-// }
-// });
// this listener is responsible for processing dialog header message events
classCombo.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
@@ -452,7 +347,7 @@
return;
}
cssStyleClassChanged();
- applyButton.setEnabled(false);
+ updateApplyButton(false);
}
});
//creates a button for add new class
@@ -485,7 +380,7 @@
* @param styleClassName - name of new style class
*/
public void addNewStyleClass(String styleClassName) {
- applyButton.setEnabled(true);
+ updateApplyButton(true);
styleChanged = true;
currentClassStyle = styleClassName;
updateStyleComposite();
@@ -546,7 +441,7 @@
buttonComposite.setLayout(new GridLayout());
// add APPLY button
applyButton = createCustomButton(buttonComposite, JstUIMessages.BUTTON_APPLY);
- applyButton.setEnabled(false);
+ updateApplyButton(false);
applyButton.setToolTipText(JstUIMessages.CSS_APPLY_CHANGES);
applyButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
@@ -556,7 +451,7 @@
classCombo.add(currentClassStyle);
}
saveChanges(false);
- applyButton.setEnabled(false);
+ updateApplyButton(false);
styleChanged = false;
}
}
@@ -569,7 +464,7 @@
styleComposite.clearStyleComposite(currentClassStyle);
styleComposite.updatePreview(currentClassStyle);
styleComposite.updateStyle();
- applyButton.setEnabled(true);
+ updateApplyButton(true);
styleChanged = true;
}
});
@@ -626,7 +521,7 @@
// } else {
// styleChanged = false;
// }
- applyButton.setEnabled(true);
+ updateApplyButton(true);
styleChanged = true;
updateStyleComposite();
@@ -701,7 +596,7 @@
private void updateOKButtonState() {
Button okButton = getButton(IDialogConstants.OK_ID);
if (okButton != null) {
- if (currentClassStyle == null || currentClassStyle.equals(Constants.EMPTY) ||
currentFile == null) {
+ if (findMostSevere()!=null&&findMostSevere().getSeverity()==IStatus.ERROR)
{
okButton.setEnabled(false);
} else {
okButton.setEnabled(true);
@@ -890,10 +785,15 @@
if (classNameStatus.matches(IStatus.ERROR)) {
return classNameStatus;
}
- if (filePathStatus.getSeverity() > classNameStatus.getSeverity()) {
- return filePathStatus;
+ if(cssValueStatus!=null && cssValueStatus.matches(IStatus.ERROR)){
+ return cssValueStatus;
}
- else return classNameStatus;
+ if(cssValueStatus!=null && cssValueStatus.matches(IStatus.ERROR)){
+ return cssValueStatus;
+ } else {
+ return classNameStatus;
+ }
+
}
/**
@@ -934,4 +834,38 @@
public void setCurrentFile(IFile currentFile) {
this.currentFile = currentFile;
}
+
+ /* (non-Javadoc)
+ * @see
org.jboss.tools.jst.jsp.outline.cssdialog.events.ChangeStyleListener#styleChanged(org.jboss.tools.jst.jsp.outline.cssdialog.events.ChangeStyleEvent)
+ */
+ public void styleChanged(ChangeStyleEvent event) {
+ if (!this.styleAttributes.isValid()) {
+ cssValueStatus = new Status(IStatus.ERROR, notUsed, 0,
+ JstUIMessages.CSS_INVALID_STYLE_PROPERTY, null);
+ } else {
+ cssValueStatus = null;
+ }
+ if (cssValueStatus != null && classCombo != null) {
+ classCombo.setEnabled(false);
+ } else {
+ classCombo.setEnabled(true);
+
+ }
+ updateApplyButton(true);
+ updateOKButtonState();
+ applyToStatusLine(findMostSevere());
+ }
+ /**
+ * Update upplyButtonState
+ * @param available
+ */
+ private void updateApplyButton(boolean enabled){
+ if(cssValueStatus!=null
+ &&cssValueStatus.matches(IStatus.ERROR)) {
+ applyButton.setEnabled(false);
+ }else {
+ applyButton.setEnabled(enabled);
+ }
+
+ }
}
Modified:
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/outline/cssdialog/StyleComposite.java
===================================================================
---
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/outline/cssdialog/StyleComposite.java 2009-03-04
18:44:37 UTC (rev 13981)
+++
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/outline/cssdialog/StyleComposite.java 2009-03-04
18:50:54 UTC (rev 13982)
@@ -237,6 +237,11 @@
tabBoxesControl.updateData(false);
lastSelectedTab = tabBoxes;
} else if (tabFolder.getSelection()[FIRST_SELECTION] == tabPreview)
{
+ if(!styleAttributes.isValid()){
+ //mareshkau, if styles attributes not valid we cann't create
+ //correct preview
+ return;
+ }
cssModel.setCSS(styleAttributes.getCssSelector(), styleAttributes);
tabPreviewControl.selectEditorArea(styleAttributes.getCssSelector(),
0);
lastSelectedTab = tabPreview;
Added:
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/outline/cssdialog/common/CSSValidator.java
===================================================================
---
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/outline/cssdialog/common/CSSValidator.java
(rev 0)
+++
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/outline/cssdialog/common/CSSValidator.java 2009-03-04
18:50:54 UTC (rev 13982)
@@ -0,0 +1,135 @@
+/*******************************************************************************
+ * Copyright (c) 2007-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.jst.jsp.outline.cssdialog.common;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+
+import org.eclipse.wst.css.core.internal.provisional.document.ICSSModel;
+import org.eclipse.wst.sse.core.StructuredModelManager;
+import org.eclipse.wst.sse.core.internal.provisional.IModelManager;
+import org.jboss.tools.jst.jsp.JspEditorPlugin;
+import org.w3c.dom.css.CSSStyleDeclaration;
+import org.w3c.dom.css.CSSStyleRule;
+import org.w3c.dom.css.CSSStyleSheet;
+
+/**
+ * CSS Validator
+ *
+ * @author yradtsevich
+ *
+ */
+public class CSSValidator {
+ // FIXME: this hard-coded string have to be replaced by reference
+ private static final String CSS_CONTENT_TYPE_IDENTIFIER =
"org.eclipse.wst.css.core.csssource"; //$NON-NLS-1$
+ private final CSSStyleSheet validatingCSS;
+ private final LogHacker logHacker = new LogHacker();
+
+ @SuppressWarnings("restriction")
+ public CSSValidator() {
+ IModelManager modelManager = StructuredModelManager.getModelManager();
+ ICSSModel validatingModel = (ICSSModel)
modelManager.createUnManagedStructuredModelFor(CSS_CONTENT_TYPE_IDENTIFIER);
+
+ validatingCSS = (CSSStyleSheet) validatingModel.getDocument();
+ }
+
+ private void cleanValidatingCSS() {
+ int ruleNumber;
+
+ while ((ruleNumber = validatingCSS.getCssRules().getLength()) > 0) {
+ validatingCSS.deleteRule(ruleNumber - 1);
+ }
+ }
+
+ /**
+ * Validates value of a CSS attribute.
+ *
+ * @param value the
+ * @return {@code true} if the attribute is valid, {@code false} otherwise
+ */
+ public boolean isValidValue(String value) {
+ logHacker.disableLogging();
+ boolean valid = true;
+ try {
+ validatingCSS.insertRule(".testSelector {}", 0); //$NON-NLS-1$
+ CSSStyleRule cssRule = ((CSSStyleRule) validatingCSS.getCssRules().item(0));
+ CSSStyleDeclaration declaration = cssRule.getStyle();
+ declaration.setProperty("background", value, Constants.EMPTY);
//$NON-NLS-1$
+ } catch (Throwable e) {
+ valid = false;
+ } finally {
+ logHacker.enableLogging();
+ cleanValidatingCSS();
+ }
+
+ return valid;
+ }
+
+ /**
+ * Validates CSS selector value.
+ *
+ * @param selector the selector value
+ * @return {@code true} if the selector is valid, {@code false} otherwise
+ */
+ public boolean isValidSelector(String selector) {
+ boolean valid = true;
+ try {
+ validatingCSS.insertRule(selector + " {}", 0); //$NON-NLS-1$
+ } catch (Throwable e) {
+ valid = false;
+ } finally {
+ cleanValidatingCSS();
+ }
+
+ return valid;
+ }
+
+ /**
+ * Gives opportunity to delete all loggers from {@link
org.eclipse.core.internal.runtime.RuntimeLog}
+ *
+ * @author yradtsevich
+ *
+ */
+ private class LogHacker {
+ private Field logListeners = null;
+ private Object oldValue = null;
+
+ public LogHacker() {
+ try {
+ logListeners =
org.eclipse.core.internal.runtime.RuntimeLog.class.getDeclaredField("logListeners");
+ logListeners.setAccessible(true);
+ } catch (Throwable e) {
+ JspEditorPlugin.getPluginLog().logError(e);
+ }
+ }
+
+ public void disableLogging() {
+ try {
+ oldValue = logListeners.get(null);
+ logListeners.set(null, new ArrayList(0));
+ } catch (Throwable e) {
+ JspEditorPlugin.getPluginLog().logError(e);
+ }
+ }
+
+ public void enableLogging() {
+ try {
+ if (oldValue != null) {
+ logListeners.set(null, oldValue);
+ }
+ } catch (Throwable e) {
+ JspEditorPlugin.getPluginLog().logError(e);
+ } finally {
+ oldValue = null;
+ }
+ }
+ }
+}
Modified:
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/outline/cssdialog/events/StyleAttributes.java
===================================================================
---
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/outline/cssdialog/events/StyleAttributes.java 2009-03-04
18:44:37 UTC (rev 13981)
+++
trunk/jst/plugins/org.jboss.tools.jst.jsp/src/org/jboss/tools/jst/jsp/outline/cssdialog/events/StyleAttributes.java 2009-03-04
18:50:54 UTC (rev 13982)
@@ -16,6 +16,7 @@
import java.util.Set;
import java.util.Map.Entry;
+import org.jboss.tools.jst.jsp.outline.cssdialog.common.CSSValidator;
import org.jboss.tools.jst.jsp.outline.cssdialog.common.Constants;
/**
@@ -26,12 +27,13 @@
private String cssSelector = null;
private HashMap<String, String> attributeMap = null;
private ArrayList<ChangeStyleListener> listeners = new
ArrayList<ChangeStyleListener>();
-
+ private CSSValidator cssValidator;
/**
* Default constructor.
*/
public StyleAttributes() {
this.attributeMap = new HashMap<String, String>();
+ cssValidator = new CSSValidator();
}
/**
@@ -158,7 +160,7 @@
public String getCssSelector() {
return cssSelector;
}
-
+
/**
* String representation of style attributes.
*
@@ -173,4 +175,16 @@
}
return buf.toString();
}
+ /**
+ * Checks if attributes that has been entered to style valid
+ * @return
+ */
+ public boolean isValid() {
+ for(String value : getAttributeMap().values()) {
+ if(!cssValidator.isValidValue(value)) {
+ return false;
+ }
+ }
+ return true;
+ }
}