mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-10 22:17:13 -06:00
Theme Editor: increment/decrement numbers at caret using Ctrl+UP/Ctrl+DOWN
This commit is contained in:
@@ -34,6 +34,7 @@ import org.fife.ui.rtextarea.RTextArea;
|
||||
import org.fife.ui.rtextarea.RTextAreaUI;
|
||||
import com.formdev.flatlaf.UIDefaultsLoaderAccessor;
|
||||
import com.formdev.flatlaf.themeeditor.FlatSyntaxTextAreaActions.DuplicateLinesAction;
|
||||
import com.formdev.flatlaf.themeeditor.FlatSyntaxTextAreaActions.IncrementNumberAction;
|
||||
|
||||
/**
|
||||
* A text area that supports editing FlatLaf themes.
|
||||
@@ -60,6 +61,8 @@ class FlatSyntaxTextArea
|
||||
ActionMap actionMap = getActionMap();
|
||||
actionMap.put( FlatSyntaxTextAreaActions.duplicateLinesUpAction, new DuplicateLinesAction( FlatSyntaxTextAreaActions.duplicateLinesUpAction, true ) );
|
||||
actionMap.put( FlatSyntaxTextAreaActions.duplicateLinesDownAction, new DuplicateLinesAction( FlatSyntaxTextAreaActions.duplicateLinesDownAction, false ) );
|
||||
actionMap.put( FlatSyntaxTextAreaActions.incrementNumberAction, new IncrementNumberAction( FlatSyntaxTextAreaActions.incrementNumberAction, true ) );
|
||||
actionMap.put( FlatSyntaxTextAreaActions.decrementNumberAction, new IncrementNumberAction( FlatSyntaxTextAreaActions.decrementNumberAction, false ) );
|
||||
|
||||
// add editor key strokes
|
||||
InputMap inputMap = getInputMap();
|
||||
@@ -67,6 +70,8 @@ class FlatSyntaxTextArea
|
||||
int alt = InputEvent.ALT_DOWN_MASK;
|
||||
inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_UP, defaultModifier|alt), FlatSyntaxTextAreaActions.duplicateLinesUpAction );
|
||||
inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_DOWN, defaultModifier|alt), FlatSyntaxTextAreaActions.duplicateLinesDownAction );
|
||||
inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_UP, defaultModifier), FlatSyntaxTextAreaActions.incrementNumberAction );
|
||||
inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_DOWN, defaultModifier), FlatSyntaxTextAreaActions.decrementNumberAction );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -28,6 +28,10 @@ class FlatSyntaxTextAreaActions
|
||||
{
|
||||
static final String duplicateLinesUpAction = "FlatLaf.DuplicateLinesUpAction";
|
||||
static final String duplicateLinesDownAction = "FlatLaf.DuplicateLinesDownAction";
|
||||
static final String incrementNumberAction = "FlatLaf.IncrementNumberAction";
|
||||
static final String decrementNumberAction = "FlatLaf.DecrementNumberAction";
|
||||
|
||||
//---- class DuplicateLinesAction -----------------------------------------
|
||||
|
||||
static class DuplicateLinesAction
|
||||
extends RecordableTextAction
|
||||
@@ -73,4 +77,59 @@ class FlatSyntaxTextAreaActions
|
||||
return getName();
|
||||
}
|
||||
}
|
||||
|
||||
//---- class IncrementNumberAction ----------------------------------------
|
||||
|
||||
static class IncrementNumberAction
|
||||
extends RecordableTextAction
|
||||
{
|
||||
private final boolean increment;
|
||||
|
||||
IncrementNumberAction( String name, boolean increment ) {
|
||||
super( name );
|
||||
this.increment = increment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformedImpl( ActionEvent e, RTextArea textArea ) {
|
||||
try {
|
||||
int caretPosition = textArea.getCaretPosition();
|
||||
int start = caretPosition;
|
||||
int end = caretPosition;
|
||||
for( int i = caretPosition - 1; i >= 0; i-- ) {
|
||||
if( !Character.isDigit( textArea.getText( i, 1 ).charAt( 0 ) ) )
|
||||
break;
|
||||
start = i;
|
||||
}
|
||||
int length = textArea.getDocument().getLength();
|
||||
for( int i = caretPosition; i < length; i++ ) {
|
||||
if( !Character.isDigit( textArea.getText( i, 1 ).charAt( 0 ) ) )
|
||||
break;
|
||||
end = i + 1;
|
||||
}
|
||||
|
||||
if( start == end )
|
||||
return;
|
||||
|
||||
String str = textArea.getText( start, end - start );
|
||||
long number = Long.parseLong( str );
|
||||
if( increment )
|
||||
number++;
|
||||
else
|
||||
number--;
|
||||
|
||||
if( number < 0 )
|
||||
return;
|
||||
|
||||
textArea.replaceRange( Long.toString( number ), start, end );
|
||||
} catch( BadLocationException | IndexOutOfBoundsException | NumberFormatException ex ) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMacroID() {
|
||||
return getName();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user