Skip to content

Commit

Permalink
refactor: GlossaryTextArea to use Actions classes(wip)
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Miura <[email protected]>
  • Loading branch information
miurahr committed May 31, 2024
1 parent 78ba76c commit 8ebce45
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 203 deletions.
72 changes: 68 additions & 4 deletions src/org/omegat/gui/glossary/GlossaryTextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
Expand All @@ -50,6 +51,7 @@
import java.util.List;
import java.util.Locale;

import javax.swing.Action;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
Expand All @@ -61,19 +63,18 @@
import javax.swing.text.JTextComponent;
import javax.swing.text.StyledDocument;

import org.openide.awt.AbstractMnemonicsAction;

import org.omegat.core.Core;
import org.omegat.core.data.ProjectProperties;
import org.omegat.core.data.SourceTextEntry;
import org.omegat.core.data.StringEntry;
import org.omegat.gui.common.EntryInfoThreadPane;
import org.omegat.gui.dialogs.CreateGlossaryEntry;
import org.omegat.gui.editor.EditorUtils;
import org.omegat.gui.glossary.actions.AddEntryAction;
import org.omegat.gui.glossary.actions.InsertSectionAction;
import org.omegat.gui.glossary.actions.SettingsNotifications;
import org.omegat.gui.glossary.actions.SettingsOpenFileAction;
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;
Expand Down Expand Up @@ -431,4 +432,67 @@ public void populatePaneMenu(JPopupMenu menu) {
.setPreference(Preferences.GLOSSARY_SORT_BY_LENGTH, sortOrderLocLength.isSelected()));
menu.add(sortOrderLocLength);
}

public static class AddEntryAction extends AbstractMnemonicsAction {
GlossaryTextArea glossaryTextArea;

public AddEntryAction(GlossaryTextArea glossaryTextArea) {
super(OStrings.getString("GUI_GLOSSARYWINDOW_addentry"), OStrings.getLocale());
this.glossaryTextArea = glossaryTextArea;
}

@Override
public void actionPerformed(final ActionEvent e) {
glossaryTextArea.showCreateGlossaryEntryDialog(Core.getMainWindow().getApplicationFrame());
}
}

public static class InsertSectionAction extends AbstractMnemonicsAction {
private final String selection;

public InsertSectionAction(String selection) {
super(OStrings.getString("GUI_GLOSSARYWINDOW_insertselection"), OStrings.getLocale());
this.selection = selection;
}

@Override
public void actionPerformed(final ActionEvent e) {
Core.getEditor().insertText(selection);
}
}

public static class SettingsNotifications extends AbstractMnemonicsAction {
public SettingsNotifications() {
super(OStrings.getString("GUI_GLOSSARYWINDOW_SETTINGS_NOTIFICATIONS"), OStrings.getLocale());
}

@Override
public void actionPerformed(final ActionEvent e) {
Object notify = e.getSource();
if (notify instanceof JCheckBoxMenuItem) {
Preferences.setPreference(Preferences.NOTIFY_GLOSSARY_HITS, ((JCheckBoxMenuItem) notify).isSelected());
}
}
}

@SuppressWarnings("serial")
public static class SettingsOpenFileAction extends AbstractMnemonicsAction {
public SettingsOpenFileAction() {
super(OStrings.getString("GUI_GLOSSARYWINDOW_SETTINGS_OPEN_FILE"), OStrings.getLocale());
final String key = "projectAccessWriteableGlossaryMenuItem";
putValue(Action.ACTION_COMMAND_KEY, key);
try {
KeyStroke keyStoroke = PropertiesShortcuts.getMainMenuShortcuts().getKeyStroke(key);
putValue(Action.ACCELERATOR_KEY, keyStoroke);
} catch (Exception ignored) {
}
}

@Override
public void actionPerformed(final ActionEvent e) {
Log.logInfoRB("LOG_MENU_CLICK", e.getActionCommand());
int modifier = e.getModifiers();
ProjectUICommands.openWritableGlossaryFile((modifier & ActionEvent.ALT_MASK) != modifier);
}
}
}
48 changes: 0 additions & 48 deletions src/org/omegat/gui/glossary/actions/AddEntryAction.java

This file was deleted.

47 changes: 0 additions & 47 deletions src/org/omegat/gui/glossary/actions/InsertSectionAction.java

This file was deleted.

49 changes: 0 additions & 49 deletions src/org/omegat/gui/glossary/actions/SettingsNotifications.java

This file was deleted.

54 changes: 0 additions & 54 deletions src/org/omegat/gui/glossary/actions/SettingsOpenFileAction.java

This file was deleted.

35 changes: 35 additions & 0 deletions src/org/omegat/gui/main/ProjectUICommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
import org.omegat.util.StaticUtils;
import org.omegat.util.StringUtil;
import org.omegat.util.WikiGet;
import org.omegat.util.gui.DesktopWrapper;
import org.omegat.util.gui.OmegaTFileChooser;
import org.omegat.util.gui.OpenProjectFileChooser;
import org.omegat.util.gui.UIThreadsUtil;
Expand Down Expand Up @@ -1315,6 +1316,40 @@ public static void doWikiImport() {
}
}

public static void openWritableGlossaryFile(boolean parent) {
if (!Core.getProject().isProjectLoaded()) {
return;
}
String path = Core.getProject().getProjectProperties().getWriteableGlossary();
if (StringUtil.isEmpty(path)) {
return;
}
File toOpen = new File(path);
if (parent) {
toOpen = toOpen.getParentFile();
}
openFile(toOpen);
}

public static void openFile(File path) {
try {
path = path.getCanonicalFile(); // Normalize file name in case it is
// displayed
} catch (Exception ex) {
// Ignore
}
if (!path.exists()) {
Core.getMainWindow().showStatusMessageRB("LFC_ERROR_FILE_DOESNT_EXIST", path);
return;
}
try {
DesktopWrapper.open(path);
} catch (Exception ex) {
Log.logErrorRB(ex, "RPF_ERROR");
Core.getMainWindow().displayErrorRB(ex, "RPF_ERROR");
}
}

private static boolean ensureProjectDir(File dir) {
if (!dir.isDirectory() && !dir.mkdirs()) {
Log.logErrorRB("CT_ERROR_CREATING_PROJECT_DIR", dir);
Expand Down
2 changes: 1 addition & 1 deletion src/org/omegat/util/OStrings.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static ResourceBundle getResourceBundle() {
return bundle;
}

public Locale getLocale() {
public static Locale getLocale() {
return bundle.getLocale();
}

Expand Down

0 comments on commit 8ebce45

Please sign in to comment.