Theme Editor: added basic auto-complete for keys

This commit is contained in:
Karl Tauber
2020-07-08 10:43:24 +02:00
parent eb5a3168b9
commit eafad942e7
6 changed files with 1086 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
/*
* 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 java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashSet;
import org.fife.ui.autocomplete.BasicCompletion;
import org.fife.ui.autocomplete.DefaultCompletionProvider;
/**
* @author Karl Tauber
*/
class FlatCompletionProvider
extends DefaultCompletionProvider
{
FlatCompletionProvider() {
// load all keys
HashSet<String> keys = new HashSet<>();
try {
try( InputStream in = getClass().getResourceAsStream( "/com/formdev/flatlaf/themeeditor/FlatLafUIKeys.txt" ) ) {
if( in != null ) {
try( BufferedReader reader = new BufferedReader( new InputStreamReader( in, "UTF-8" ) ) ) {
String key;
while( (key = reader.readLine()) != null ) {
keys.add( key );
}
}
}
}
} catch( IOException ex ) {
ex.printStackTrace(); // TODO
}
// collect key parts
HashSet<String> keyParts = new HashSet<>();
for( String key : keys ) {
int delimIndex = key.length() + 1;
while( (delimIndex = key.lastIndexOf( '.', delimIndex - 1 )) >= 0 )
keyParts.add( key.substring( 0, delimIndex + 1 ) );
}
// add key parts
for( String keyPart : keyParts )
addCompletion( new BasicCompletion( this, keyPart ) );
// add all keys
for( String key : keys )
addCompletion( new BasicCompletion( this, key ) );
}
@Override
protected boolean isValidChar( char ch ) {
return super.isValidChar( ch ) || ch == '.';
}
}

View File

@@ -25,6 +25,8 @@ import java.nio.charset.StandardCharsets;
import java.util.List;
import javax.swing.JLayer;
import javax.swing.JPanel;
import org.fife.ui.autocomplete.AutoCompletion;
import org.fife.ui.autocomplete.CompletionProvider;
import org.fife.ui.rsyntaxtextarea.AbstractTokenMakerFactory;
import org.fife.ui.rsyntaxtextarea.FileLocation;
import org.fife.ui.rsyntaxtextarea.SyntaxScheme;
@@ -74,6 +76,13 @@ class FlatThemeEditorPane
scheme.getStyle( FlatThemeTokenMaker.TOKEN_COLOR ).background = new Color( 0x0a000000, true );
scheme.getStyle( FlatThemeTokenMaker.TOKEN_VARIABLE ).background = new Color( 0x1800cc00, true );
// autocomplete
CompletionProvider provider = new FlatCompletionProvider();
AutoCompletion ac = new AutoCompletion( provider );
ac.setChoicesWindowSize( UIScale.scale( 300 ), UIScale.scale( 400 ) );
ac.setDescriptionWindowSize( UIScale.scale( 300 ), UIScale.scale( 400 ) );
ac.install( textArea );
// create overlay layer
JLayer<FlatSyntaxTextArea> overlay = new JLayer<>( textArea, new FlatThemeEditorOverlay() );