mirror of https://github.com/weisJ/darklaf.git
weisj
5 years ago
8 changed files with 329 additions and 13 deletions
@ -0,0 +1,51 @@ |
|||||||
|
/* |
||||||
|
* MIT License |
||||||
|
* |
||||||
|
* Copyright (c) 2020 Jannis Weis |
||||||
|
* |
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
||||||
|
* of this software and associated documentation files (the "Software"), to deal |
||||||
|
* in the Software without restriction, including without limitation the rights |
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||||
|
* copies of the Software, and to permit persons to whom the Software is |
||||||
|
* furnished to do so, subject to the following conditions: |
||||||
|
* |
||||||
|
* The above copyright notice and this permission notice shall be included in all |
||||||
|
* copies or substantial portions of the Software. |
||||||
|
* |
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||||||
|
* SOFTWARE. |
||||||
|
* |
||||||
|
*/ |
||||||
|
package com.github.weisj.darklaf.ui.text.action; |
||||||
|
|
||||||
|
import javax.swing.text.Caret; |
||||||
|
import javax.swing.text.JTextComponent; |
||||||
|
import javax.swing.text.TextAction; |
||||||
|
|
||||||
|
import com.github.weisj.darklaf.ui.text.DarkCaret; |
||||||
|
|
||||||
|
public abstract class DarkTextAction extends TextAction { |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a new JTextAction object. |
||||||
|
* |
||||||
|
* @param name the name of the action |
||||||
|
*/ |
||||||
|
public DarkTextAction(final String name) { |
||||||
|
super(name); |
||||||
|
} |
||||||
|
|
||||||
|
protected void setupDeleteMode(final JTextComponent textComponent, final boolean isDeleteMode) { |
||||||
|
if (textComponent == null) return; |
||||||
|
Caret c = textComponent.getCaret(); |
||||||
|
if (c instanceof DarkCaret) { |
||||||
|
((DarkCaret) c).setDeleteCharMode(isDeleteMode); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,76 @@ |
|||||||
|
/* |
||||||
|
* MIT License |
||||||
|
* |
||||||
|
* Copyright (c) 2020 Jannis Weis |
||||||
|
* |
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
||||||
|
* of this software and associated documentation files (the "Software"), to deal |
||||||
|
* in the Software without restriction, including without limitation the rights |
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||||
|
* copies of the Software, and to permit persons to whom the Software is |
||||||
|
* furnished to do so, subject to the following conditions: |
||||||
|
* |
||||||
|
* The above copyright notice and this permission notice shall be included in all |
||||||
|
* copies or substantial portions of the Software. |
||||||
|
* |
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||||||
|
* SOFTWARE. |
||||||
|
* |
||||||
|
*/ |
||||||
|
package com.github.weisj.darklaf.ui.text.action; |
||||||
|
|
||||||
|
import java.awt.event.ActionEvent; |
||||||
|
|
||||||
|
import javax.swing.*; |
||||||
|
import javax.swing.text.*; |
||||||
|
|
||||||
|
public class DeleteNextCharAction extends DarkTextAction { |
||||||
|
|
||||||
|
public DeleteNextCharAction() { |
||||||
|
super(DefaultEditorKit.deleteNextCharAction); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void actionPerformed(final ActionEvent e) { |
||||||
|
JTextComponent target = getTextComponent(e); |
||||||
|
setupDeleteMode(target, true); |
||||||
|
boolean beep = true; |
||||||
|
if ((target != null) && (target.isEditable())) { |
||||||
|
try { |
||||||
|
Document doc = target.getDocument(); |
||||||
|
Caret caret = target.getCaret(); |
||||||
|
int dot = caret.getDot(); |
||||||
|
int mark = caret.getMark(); |
||||||
|
if (dot != mark) { |
||||||
|
doc.remove(Math.min(dot, mark), Math.abs(dot - mark)); |
||||||
|
beep = false; |
||||||
|
} else if (dot < doc.getLength()) { |
||||||
|
int delChars = 1; |
||||||
|
|
||||||
|
if (dot < doc.getLength() - 1) { |
||||||
|
String dotChars = doc.getText(dot, 2); |
||||||
|
char c0 = dotChars.charAt(0); |
||||||
|
char c1 = dotChars.charAt(1); |
||||||
|
|
||||||
|
if (c0 >= '\uD800' && c0 <= '\uDBFF' && |
||||||
|
c1 >= '\uDC00' && c1 <= '\uDFFF') { |
||||||
|
delChars = 2; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
doc.remove(dot, delChars); |
||||||
|
beep = false; |
||||||
|
} |
||||||
|
} catch (BadLocationException ignored) {} |
||||||
|
} |
||||||
|
if (beep) { |
||||||
|
UIManager.getLookAndFeel().provideErrorFeedback(target); |
||||||
|
} |
||||||
|
setupDeleteMode(target, false); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,76 @@ |
|||||||
|
/* |
||||||
|
* MIT License |
||||||
|
* |
||||||
|
* Copyright (c) 2020 Jannis Weis |
||||||
|
* |
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
||||||
|
* of this software and associated documentation files (the "Software"), to deal |
||||||
|
* in the Software without restriction, including without limitation the rights |
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||||
|
* copies of the Software, and to permit persons to whom the Software is |
||||||
|
* furnished to do so, subject to the following conditions: |
||||||
|
* |
||||||
|
* The above copyright notice and this permission notice shall be included in all |
||||||
|
* copies or substantial portions of the Software. |
||||||
|
* |
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||||||
|
* SOFTWARE. |
||||||
|
* |
||||||
|
*/ |
||||||
|
package com.github.weisj.darklaf.ui.text.action; |
||||||
|
|
||||||
|
import java.awt.event.ActionEvent; |
||||||
|
|
||||||
|
import javax.swing.*; |
||||||
|
import javax.swing.text.*; |
||||||
|
|
||||||
|
public class DeletePreviousCharAction extends DarkTextAction { |
||||||
|
|
||||||
|
public DeletePreviousCharAction() { |
||||||
|
super(DefaultEditorKit.deletePrevCharAction); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void actionPerformed(final ActionEvent e) { |
||||||
|
JTextComponent target = getTextComponent(e); |
||||||
|
setupDeleteMode(target, true); |
||||||
|
boolean beep = true; |
||||||
|
if ((target != null) && (target.isEditable())) { |
||||||
|
try { |
||||||
|
Document doc = target.getDocument(); |
||||||
|
Caret caret = target.getCaret(); |
||||||
|
int dot = caret.getDot(); |
||||||
|
int mark = caret.getMark(); |
||||||
|
if (dot != mark) { |
||||||
|
doc.remove(Math.min(dot, mark), Math.abs(dot - mark)); |
||||||
|
beep = false; |
||||||
|
} else if (dot > 0) { |
||||||
|
int delChars = 1; |
||||||
|
|
||||||
|
if (dot > 1) { |
||||||
|
String dotChars = doc.getText(dot - 2, 2); |
||||||
|
char c0 = dotChars.charAt(0); |
||||||
|
char c1 = dotChars.charAt(1); |
||||||
|
|
||||||
|
if (c0 >= '\uD800' && c0 <= '\uDBFF' && |
||||||
|
c1 >= '\uDC00' && c1 <= '\uDFFF') { |
||||||
|
delChars = 2; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
doc.remove(dot - delChars, delChars); |
||||||
|
beep = false; |
||||||
|
} |
||||||
|
} catch (BadLocationException ignored) {} |
||||||
|
} |
||||||
|
if (beep) { |
||||||
|
UIManager.getLookAndFeel().provideErrorFeedback(target); |
||||||
|
} |
||||||
|
setupDeleteMode(target, false); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,51 @@ |
|||||||
|
/* |
||||||
|
* MIT License |
||||||
|
* |
||||||
|
* Copyright (c) 2020 Jannis Weis |
||||||
|
* |
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
||||||
|
* of this software and associated documentation files (the "Software"), to deal |
||||||
|
* in the Software without restriction, including without limitation the rights |
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||||
|
* copies of the Software, and to permit persons to whom the Software is |
||||||
|
* furnished to do so, subject to the following conditions: |
||||||
|
* |
||||||
|
* The above copyright notice and this permission notice shall be included in all |
||||||
|
* copies or substantial portions of the Software. |
||||||
|
* |
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||||||
|
* SOFTWARE. |
||||||
|
* |
||||||
|
*/ |
||||||
|
package com.github.weisj.darklaf.ui.text.action; |
||||||
|
|
||||||
|
import java.awt.event.ActionEvent; |
||||||
|
|
||||||
|
import javax.swing.text.Caret; |
||||||
|
import javax.swing.text.JTextComponent; |
||||||
|
import javax.swing.text.TextAction; |
||||||
|
|
||||||
|
import com.github.weisj.darklaf.ui.text.DarkCaret; |
||||||
|
|
||||||
|
public class ToggleInsertAction extends TextAction { |
||||||
|
|
||||||
|
public ToggleInsertAction() { |
||||||
|
super("toggle insert mode"); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void actionPerformed(final ActionEvent e) { |
||||||
|
JTextComponent target = getTextComponent(e); |
||||||
|
if (target != null) { |
||||||
|
Caret c = target.getCaret(); |
||||||
|
if (c instanceof DarkCaret) { |
||||||
|
((DarkCaret) c).setInsertMode(!((DarkCaret) c).isInsertMode()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue