Theme Editor: mark invalid color values with a curly underline

This commit is contained in:
Karl Tauber
2020-04-05 18:04:28 +02:00
parent 78d06d82d6
commit acb62e347a
4 changed files with 96 additions and 0 deletions

View File

@@ -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 );
}
}

View File

@@ -54,6 +54,7 @@ class FlatThemeEditorPane
textArea = new FlatSyntaxTextArea(); textArea = new FlatSyntaxTextArea();
textArea.setSyntaxEditingStyle( FLATLAF_STYLE ); textArea.setSyntaxEditingStyle( FLATLAF_STYLE );
textArea.setMarkOccurrences( true ); textArea.setMarkOccurrences( true );
textArea.addParser( new FlatThemeParser() );
// theme // theme
try { try {

View File

@@ -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;
}
}

View File

@@ -15,6 +15,8 @@ Prop.color1=#f80
Prop.color2=#ff8800 Prop.color2=#ff8800
Prop.color3=#f804 Prop.color3=#f804
Prop.color4=#ff880044 Prop.color4=#ff880044
Prop.color5=#12
Prop.color6=#12345
Prop.border=1,2,3,4 Prop.border=1,2,3,4
Prop.scaledInteger={scaledInteger}123 Prop.scaledInteger={scaledInteger}123