diff --git a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/UIDefaultsLoaderAccessor.java b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/UIDefaultsLoaderAccessor.java new file mode 100644 index 00000000..3c5c8b5b --- /dev/null +++ b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/UIDefaultsLoaderAccessor.java @@ -0,0 +1,29 @@ +/* + * Copyright 2020 FormDev Software GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.formdev.flatlaf; + +/** + * Enable accessing package private methods of {@link UIDefaultsLoader}. + * + * @author Karl Tauber + */ +public class UIDefaultsLoaderAccessor +{ + public static int parseColorRGBA( String value ) { + return UIDefaultsLoader.parseColorRGBA( value ); + } +} diff --git a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeEditorPane.java b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeEditorPane.java index 4500c18e..27de8f8d 100644 --- a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeEditorPane.java +++ b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeEditorPane.java @@ -54,6 +54,7 @@ class FlatThemeEditorPane textArea = new FlatSyntaxTextArea(); textArea.setSyntaxEditingStyle( FLATLAF_STYLE ); textArea.setMarkOccurrences( true ); + textArea.addParser( new FlatThemeParser() ); // theme try { diff --git a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeParser.java b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeParser.java new file mode 100644 index 00000000..96f80d95 --- /dev/null +++ b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeParser.java @@ -0,0 +1,64 @@ +/* + * Copyright 2020 FormDev Software GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.formdev.flatlaf.themeeditor; + +import javax.swing.text.Element; +import org.fife.ui.rsyntaxtextarea.RSyntaxDocument; +import org.fife.ui.rsyntaxtextarea.Token; +import org.fife.ui.rsyntaxtextarea.parser.AbstractParser; +import org.fife.ui.rsyntaxtextarea.parser.DefaultParseResult; +import org.fife.ui.rsyntaxtextarea.parser.DefaultParserNotice; +import org.fife.ui.rsyntaxtextarea.parser.ParseResult; +import com.formdev.flatlaf.UIDefaultsLoaderAccessor; + +/** + * Parser for FlatLaf properties files that checks for invalid values. + * + * @author Karl Tauber + */ +class FlatThemeParser + extends AbstractParser +{ + private final DefaultParseResult result; + + FlatThemeParser() { + result = new DefaultParseResult( this ); + } + + @Override + public ParseResult parse( RSyntaxDocument doc, String style ) { + Element root = doc.getDefaultRootElement(); + + result.clearNotices(); + result.setParsedLines( 0, root.getElementCount() - 1 ); + + for( Token token : doc ) { + if( token.getType() == FlatThemeTokenMaker.TOKEN_COLOR ) { + try { + UIDefaultsLoaderAccessor.parseColorRGBA( token.getLexeme() ); + } catch( IllegalArgumentException ex ) { + result.addNotice( new DefaultParserNotice( this, + "Invalid color.\n\nUse #RRGGBB, #RRGGBBAA, #RGB or #RGBA", + root.getElementIndex( token.getOffset() ), + token.getOffset(), token.length() ) ); + } + } + } + + return result; + } +} diff --git a/flatlaf-theme-editor/theme-editor-test.properties b/flatlaf-theme-editor/theme-editor-test.properties index 18bf541b..4fe494bb 100644 --- a/flatlaf-theme-editor/theme-editor-test.properties +++ b/flatlaf-theme-editor/theme-editor-test.properties @@ -15,6 +15,8 @@ Prop.color1=#f80 Prop.color2=#ff8800 Prop.color3=#f804 Prop.color4=#ff880044 +Prop.color5=#12 +Prop.color6=#12345 Prop.border=1,2,3,4 Prop.scaledInteger={scaledInteger}123