-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: AbstractMnemonicsAction class (#8)
* feat: AbstractMnemonicsAction class - Introduce MnemonicsAction interface - Give Action with mnemonics processing Signed-off-by: Hiroshi Miura <[email protected]> * feat: add constructor and method that take a bundle or omitting locale * style: imports * refactor: weaken visibility --------- Signed-off-by: Hiroshi Miura <[email protected]>
- Loading branch information
Showing
4 changed files
with
118 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.openide.awt; | ||
|
||
import javax.swing.*; | ||
import java.util.Locale; | ||
import java.util.ResourceBundle; | ||
|
||
/** | ||
* @author Hiroshi Miura | ||
*/ | ||
public abstract class AbstractMnemonicsAction extends AbstractAction { | ||
public AbstractMnemonicsAction() { | ||
super(); | ||
} | ||
|
||
public AbstractMnemonicsAction(String key, ResourceBundle bundle) { | ||
super(); | ||
setText(key, bundle); | ||
} | ||
|
||
public AbstractMnemonicsAction(String text, Locale locale) { | ||
super(); | ||
setText(text, locale); | ||
} | ||
|
||
public AbstractMnemonicsAction(String text) { | ||
super(); | ||
setText(text); | ||
} | ||
|
||
public void setText(String key, ResourceBundle bundle) { | ||
setText(bundle.getString(key), bundle.getLocale()); | ||
} | ||
|
||
public void setText(String text) { | ||
setText(text, new Locale("en")); | ||
} | ||
|
||
public void setText(String text, Locale locale) { | ||
int i = Mnemonics.findMnemonicAmpersand(text); | ||
if (i < 0) { | ||
putValue(Action.NAME, text); | ||
putValue(Action.DISPLAYED_MNEMONIC_INDEX_KEY, -1); | ||
} else { | ||
putValue(Action.NAME, text.substring(0, i) + text.substring(i + 1)); | ||
Mnemonics.setMnemonicAndIndex(this, text.charAt(i + 1), i, locale); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.openide.awt; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import javax.swing.Action; | ||
import java.awt.event.ActionEvent; | ||
import java.util.Locale; | ||
|
||
import static org.openide.awt.Mnemonics.isMacOS; | ||
|
||
public class MnemonicsActionTest { | ||
|
||
@Test | ||
public void testSetLocalizedTextAction() { | ||
AbstractMnemonicsAction item = new AbstractMnemonicsAction() { | ||
@Override | ||
public void actionPerformed(ActionEvent actionEvent) { | ||
} | ||
}; | ||
item.setText("&Simple Text"); | ||
Assert.assertEquals("Simple Text", item.getValue(Action.NAME)); | ||
Assert.assertEquals(isMacOS()? -1: 0, item.getValue(Action.DISPLAYED_MNEMONIC_INDEX_KEY)); | ||
// | ||
item.setText("Rock & Roll"); | ||
Assert.assertEquals("Rock & Roll", item.getValue(Action.NAME)); | ||
Assert.assertEquals(-1, item.getValue(Action.DISPLAYED_MNEMONIC_INDEX_KEY)); | ||
// | ||
item.setText("&\u041F\u043E\u0438\u0441\u043A", new Locale("ru")); | ||
Assert.assertEquals("\u041F\u043E\u0438\u0441\u043A", item.getValue(Action.NAME)); | ||
Assert.assertEquals(isMacOS()? -1: 0, item.getValue(Action.DISPLAYED_MNEMONIC_INDEX_KEY)); | ||
Assert.assertEquals(isMacOS()? 0: 71, item.getValue(Action.MNEMONIC_KEY)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters