Skip to content

Commit

Permalink
Added partial mnemonic support #3
Browse files Browse the repository at this point in the history
  • Loading branch information
hiohiohio committed Sep 17, 2014
1 parent f9ed0fa commit 4fadfd1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Note: User's config directory quoted from the [Manual](http://sourceforge.net/p/

* **items**: This element has an *optional* attribute *priority*.
* **item**: This element has an *optional* attribute *nopopup*.
* **name**: A string to show as a menu item.
* **name**: A string to show as a menu item. For a mnemonic, you can use '_'.
* **url**: A URL with a **{target}** placeholder with *optional* attributes: *target* and *encoding*.
* **command**: A command string with a **{taget}** placeholder with *optional* attributes: *target*, *encoding*, and *delimiter*.
* **keystroke**: A definition of the keyboard shortcut (same as OmegaT's custom keyboard shortcut). Ref. [KeyStroke (Java Platform SE 8 )](http://docs.oracle.com/javase/8/docs/api/javax/swing/KeyStroke.html#getKeyStroke-java.lang.String-)
Expand Down Expand Up @@ -79,6 +79,14 @@ Note: User's config directory quoted from the [Manual](http://sourceforge.net/p/
<url target="ascii_only">http://search.yahoo.com/search?p={target}</url>
<url target="non_ascii_only">http://search.yahoo.co.jp/search?p={target}</url>
</item>
<item>
<name>Google _difinition</name>
<url target="ascii_only">https://www.google.com/search?q=Define+{target}</url>
</item>
<item>
<name>Google _patent</name>
<url target="ascii_only">https://www.google.com/search?q=Patent+{target}</url>
</item>
</items>
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,31 @@ public enum ENCODING {
DEFAULT, ESCAPE, NONE
}

private static final int UNDEFINED_KEYCODE = -2;

private String name;
private List<ExternalFinderItemURL> URLs;
private List<ExternalFinderItemCommand> commands;
private KeyStroke keystroke;
private boolean nopopup = false;
private Boolean asciiOnly = null;
private Boolean nonAsciiOnly = null;
private int keycode = UNDEFINED_KEYCODE;

public ExternalFinderItem() {
this.URLs = new ArrayList<ExternalFinderItemURL>();
this.commands = new ArrayList<ExternalFinderItemCommand>();
}

public ExternalFinderItem(String name, List<ExternalFinderItemURL> URLs, List<ExternalFinderItemCommand> commands, KeyStroke keystroke, boolean nopopup) {
this.name = name;
int mnemonic = mnemonicPosition(name);
if (mnemonic != -1) {
this.name = name.substring(0, mnemonic) + name.substring(mnemonic + 1);
this.keycode = (int) Character.toUpperCase(name.charAt(mnemonic + 1));
} else {
this.name = name;
this.keycode = -1;
}
this.URLs = URLs;
this.commands = commands;
this.keystroke = keystroke;
Expand All @@ -63,7 +73,14 @@ public String getName() {
}

public void setName(String name) {
this.name = name;
int mnemonic = mnemonicPosition(name);
if (mnemonic != -1) {
this.name = name.substring(0, mnemonic) + name.substring(mnemonic + 1);
this.keycode = (int) Character.toUpperCase(name.charAt(mnemonic + 1));
} else {
this.name = name;
this.keycode = -1;
}
}

public List<ExternalFinderItemURL> getURLs() {
Expand Down Expand Up @@ -134,12 +151,31 @@ private boolean isTargetOnly(final TARGET target) {
return true;
}

public int getKeycode() {
return keycode;
}

private static int mnemonicPosition(String name) {
int ret = name.indexOf("_");
if (ret != -1 && (ret + 1) != name.length()) {
char ch = name.charAt(ret + 1);
if (ch == ' ') {
ret = -1;
}
} else {
ret = -1;
}

return ret;
}

public ExternalFinderItem replaceRefs(final ExternalFinderItem item) {
this.name = item.name;
this.URLs = item.URLs;
this.commands = item.commands;
this.keystroke = item.keystroke;
this.nopopup = item.nopopup;
this.keycode = item.keycode;

this.asciiOnly = null; // item.isAsciiOnly();
this.nonAsciiOnly = null; // item.isNonAsciiOnly();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,17 @@ public List<Component> generate() {
&& finderItem.isAsciiOnly()) {
continue;
}

JMenuItem item = new JMenuItem(finderItem.getName());
// adding mnemonic
item.setMnemonic(finderItem.getKeycode());

// set keyboard shortcut
if (!popup) {
item.setAccelerator(finderItem.getKeystroke());
}
item.addActionListener(new ExternalFinderItemActionListener(finderItem));

menuItems.add(item);
}
if (popup) {
Expand Down

0 comments on commit 4fadfd1

Please sign in to comment.