From 07ff02c0a5be18e773ed689f053ce352b46d17ce Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Mon, 10 Aug 2009 11:05:15 +0000 Subject: [PATCH] * forgot to add some files --- .../filebot/resources/action.properties.png | Bin 0 -> 561 bytes .../rename/ExpressionFormatDocument.java | 95 ++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 source/net/sourceforge/filebot/resources/action.properties.png create mode 100644 source/net/sourceforge/filebot/ui/panel/rename/ExpressionFormatDocument.java diff --git a/source/net/sourceforge/filebot/resources/action.properties.png b/source/net/sourceforge/filebot/resources/action.properties.png new file mode 100644 index 0000000000000000000000000000000000000000..315829a5f551173f5fbf5f3915be7c371b85b12d GIT binary patch literal 561 zcmV-10?z%3P)|V zFEBa&=h-L#0019!MObuGZ)S9NVRB^vO<`klZ*65{X<;BnX>w(EZ*psMAUL&X(s%#> z0ftFLK~y-6rIXKU6hRQie_d611jV~Z5S2uT!5~3FguH>5yu!YLPY~b0yWl}{^KT5q z4aUV?NEQ)UWDx=ZAHhs_PkESeW^x%1Tc_@(>-T+MH67fYMalrS2RjZRBB%t^6jQ}a zF*8gBTQW=)H9Hj_O%{>jGeYrWIoAC6l)_4aQOCh~WdF-|I+5$WgNN!obhZwEilQ4K@^Iru zL*M)SjYs!x^*VU<`k)gbCf*%R+mOKf;k25#x9Sk;S8$3Vwty59TbnBl{q3g{w(hU? zI(V`Bu>;7N?ZM843(sGE=mq?%u;_1;g}Q;ige=sb6ifA(00000NkvXXu0mjfeVghs literal 0 HcmV?d00001 diff --git a/source/net/sourceforge/filebot/ui/panel/rename/ExpressionFormatDocument.java b/source/net/sourceforge/filebot/ui/panel/rename/ExpressionFormatDocument.java new file mode 100644 index 00000000..57a3a292 --- /dev/null +++ b/source/net/sourceforge/filebot/ui/panel/rename/ExpressionFormatDocument.java @@ -0,0 +1,95 @@ + +package net.sourceforge.filebot.ui.panel.rename; + + +import javax.swing.event.DocumentEvent; +import javax.swing.text.AttributeSet; +import javax.swing.text.BadLocationException; +import javax.swing.text.Document; +import javax.swing.text.PlainDocument; + + +class ExpressionFormatDocument extends PlainDocument { + + private Completion lastCompletion; + + + @Override + public void insertString(int offset, String text, AttributeSet attributes) throws BadLocationException { + if (text == null || text.isEmpty()) { + return; + } + + // ignore user input that matches the last auto-completion + if (lastCompletion != null && lastCompletion.didComplete(this, offset, text)) { + lastCompletion = null; + + // behave as if something was inserted (e.g. update caret position) + fireInsertUpdate(new DefaultDocumentEvent(offset, text.length(), DocumentEvent.EventType.INSERT)); + return; + } + + // try to auto-complete input + lastCompletion = Completion.getCompletion(this, offset, text); + + if (lastCompletion != null) { + text = lastCompletion.complete(this, offset, text); + } + + super.insertString(offset, text, attributes); + } + + + public Completion getLastCompletion() { + return lastCompletion; + } + + + public enum Completion { + RoundBrackets("()"), + SquareBrackets("[]"), + CurlyBrackets("{}"), + RegexLiteral("//"), + SingleQuoteStringLiteral("''"), + DoubleQuoteStringLiteral("\"\""); + + public final String pattern; + + + private Completion(String pattern) { + this.pattern = pattern; + } + + + public boolean canComplete(Document document, int offset, String input) { + return pattern.startsWith(input); + } + + + public boolean didComplete(Document document, int offset, String input) { + try { + return document.getText(0, offset).concat(input).endsWith(pattern); + } catch (BadLocationException e) { + return false; + } + } + + + public String complete(Document document, int offset, String input) { + return pattern; + } + + + public static Completion getCompletion(Document document, int offset, String input) { + for (Completion completion : values()) { + if (completion.canComplete(document, offset, input)) { + return completion; + } + } + + return null; + } + + } + +}