-
-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: GlossaryTextArea: action with lambda #1046
Open
miurahr
wants to merge
9
commits into
master
Choose a base branch
from
topic/miurahr/gui/glossary/use-actions-for-menuitems
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
78ba76c
refactor: GlossaryTextArea to use Actions classes
miurahr 8ebce45
refactor: GlossaryTextArea to use Actions classes(wip)
miurahr 945d8cb
style: GlossaryTextArea to use Actions classes(wip)
miurahr b352883
style: make action class final
miurahr 16b6a99
fix: Detection of ALT_MASK modifier
miurahr 5238581
docs: javadoc of ProjectUICommands#openFile
miurahr 7661316
style: apply spotless to GlossaryTextArea.java
miurahr 58705ba
fix: remove shortcut for popup menu action
miurahr b81e1e3
refactor: use lambda instead of Action class
miurahr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -72,6 +72,7 @@ | |
import org.omegat.gui.editor.EditorUtils; | ||
import org.omegat.gui.main.DockableScrollPane; | ||
import org.omegat.gui.main.IMainWindow; | ||
import org.omegat.gui.main.ProjectUICommands; | ||
import org.omegat.gui.shortcuts.PropertiesShortcuts; | ||
import org.omegat.util.HttpConnectionUtils; | ||
import org.omegat.util.Log; | ||
|
@@ -87,6 +88,8 @@ | |
import org.omegat.util.gui.TooltipAttribute; | ||
import org.omegat.util.gui.UIThreadsUtil; | ||
|
||
import static org.openide.awt.Mnemonics.setLocalizedText; | ||
|
||
/** | ||
* This is a Glossary pane that displays glossary entries. | ||
* | ||
|
@@ -294,20 +297,13 @@ private void doPopup(Point p) { | |
|
||
private void populateContextMenu(JPopupMenu popup) { | ||
boolean projectLoaded = Core.getProject().isProjectLoaded(); | ||
|
||
final String selection = getSelectedText(); | ||
JMenuItem item = popup.add(OStrings.getString("GUI_GLOSSARYWINDOW_insertselection")); | ||
JMenuItem item = popup.add(createMenuItem("GUI_GLOSSARYWINDOW_insertselection", | ||
"glossaryWindowInsertSelectionPopupMenuItem", () -> Core.getEditor().insertText(selection))); | ||
item.setEnabled(projectLoaded && !StringUtil.isEmpty(selection)); | ||
item.addActionListener(new ActionListener() { | ||
@Override | ||
public void actionPerformed(ActionEvent e) { | ||
Core.getEditor().insertText(selection); | ||
} | ||
}); | ||
item = popup.add(OStrings.getString("GUI_GLOSSARYWINDOW_addentry")); | ||
item = popup.add(createMenuItem("GUI_GLOSSARYWINDOW_addentry", "glossaryWindowAddEntryPopupMenuItem", | ||
() -> showCreateGlossaryEntryDialog(Core.getMainWindow().getApplicationFrame()))); | ||
item.setEnabled(projectLoaded); | ||
item.addActionListener( | ||
e -> showCreateGlossaryEntryDialog(Core.getMainWindow().getApplicationFrame())); | ||
} | ||
|
||
@Override | ||
|
@@ -410,30 +406,29 @@ public void windowClosed(WindowEvent e) { | |
public void populatePaneMenu(JPopupMenu menu) { | ||
populateContextMenu(menu); | ||
menu.addSeparator(); | ||
final JMenuItem openFile = new JMenuItem(OStrings.getString("GUI_GLOSSARYWINDOW_SETTINGS_OPEN_FILE")); | ||
openFile.addActionListener(new ActionListener() { | ||
@Override | ||
public void actionPerformed(ActionEvent e) { | ||
Core.getMainWindow().getMainMenu().invokeAction("projectAccessWritableGlossaryMenuItem", | ||
e.getModifiers()); | ||
} | ||
}); | ||
JMenuItem openFile = createMenuItem("GUI_GLOSSARYWINDOW_SETTINGS_OPEN_FILE", | ||
"glossaryWindowSettingsOpenWriteableFileMenuItem", evt -> { | ||
Log.logInfoRB("LOG_MENU_CLICK", evt.getActionCommand()); | ||
final int modifier = evt.getModifiers(); | ||
ProjectUICommands.openWritableGlossaryFile((modifier & ActionEvent.ALT_MASK) != 0); | ||
}); | ||
openFile.setEnabled(false); | ||
if (Core.getProject().isProjectLoaded()) { | ||
String glossaryPath = Core.getProject().getProjectProperties().getWriteableGlossary(); | ||
openFile.setEnabled(!StringUtil.isEmpty(glossaryPath) && new File(glossaryPath).isFile()); | ||
} | ||
menu.add(openFile); | ||
menu.addSeparator(); | ||
final JMenuItem notify = new JCheckBoxMenuItem( | ||
OStrings.getString("GUI_GLOSSARYWINDOW_SETTINGS_NOTIFICATIONS")); | ||
final JMenuItem notify = createCheckBoxMenuItem("GUI_GLOSSARYWINDOW_SETTINGS_NOTIFICATIONS", | ||
"glossaryWindowSettingsNotificationMenuItem", evt -> { | ||
Log.logInfoRB("LOG_MENU_CLICK", evt.getActionCommand()); | ||
Object o = evt.getSource(); | ||
if (o instanceof JCheckBoxMenuItem) { | ||
Preferences.setPreference(Preferences.NOTIFY_GLOSSARY_HITS, | ||
((JCheckBoxMenuItem) o).isSelected()); | ||
} | ||
}); | ||
notify.setSelected(Preferences.isPreference(Preferences.NOTIFY_GLOSSARY_HITS)); | ||
notify.addActionListener(new ActionListener() { | ||
@Override | ||
public void actionPerformed(ActionEvent e) { | ||
Preferences.setPreference(Preferences.NOTIFY_GLOSSARY_HITS, notify.isSelected()); | ||
} | ||
}); | ||
menu.add(notify); | ||
menu.addSeparator(); | ||
final JMenuItem sortOrderSrcLength = new JCheckBoxMenuItem( | ||
|
@@ -451,4 +446,29 @@ public void actionPerformed(ActionEvent e) { | |
.setPreference(Preferences.GLOSSARY_SORT_BY_LENGTH, sortOrderLocLength.isSelected())); | ||
menu.add(sortOrderLocLength); | ||
} | ||
|
||
private static JMenuItem createMenuItem(String titleKey, String command, Runnable action) { | ||
return createMenuItem(titleKey, command, ev -> { | ||
Log.logInfoRB("LOG_MENU_CLICK", ev.getActionCommand()); | ||
action.run(); | ||
}); | ||
} | ||
|
||
private static JMenuItem createMenuItem(String titleKey, String command, ActionListener actionListener) { | ||
JMenuItem result = new JMenuItem(); | ||
setLocalizedText(result, OStrings.getString(titleKey)); | ||
result.setActionCommand(command); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think also action command is not necessary. In the past it was used when same action listener was shared by multiple menus, which is the case in MainWindowMenu actually. |
||
result.addActionListener(actionListener); | ||
return result; | ||
} | ||
|
||
private static JCheckBoxMenuItem createCheckBoxMenuItem(String titleKey, String command, | ||
ActionListener actionListener) { | ||
JCheckBoxMenuItem result = new JCheckBoxMenuItem(); | ||
result.setActionCommand(command); | ||
setLocalizedText(result, OStrings.getString(titleKey)); | ||
result.setSelected(Preferences.isPreference(Preferences.NOTIFY_GLOSSARY_HITS)); | ||
result.addActionListener(actionListener); | ||
return result; | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you plan to use the same methodology for main menu, you should move these methods to a common class, for example org.omegat.gui.common.MenuFactory