• R/O
  • SSH
  • HTTPS

コミット

よく使われているワード(クリックで追加)

javaandroidc++linuxc#objective-ccocoa誰得qtrubypythonbathyscaphephpgamewindowsguic翻訳omegattwitterframeworktestbtronarduinovb.net計画中(planning stage)directxpreviewerゲームエンジンdom

OmegaT のメニューバーにフォルダーツリー参照用のメニューを追加します。


コミットメタ情報

リビジョン61 (tree)
日時2014-05-01 17:41:37
作者yu-tang

ログメッセージ

refactoring

変更サマリ

差分

--- trunk/src/jp/sourceforge/users/yutang/omegat/plugin/foldermenu/MenuManager.java (revision 60)
+++ trunk/src/jp/sourceforge/users/yutang/omegat/plugin/foldermenu/MenuManager.java (revision 61)
@@ -1,7 +1,7 @@
11 /**************************************************************************
22 FolderMenu - easy access to project folders from menu.
33
4- Copyright (C) 2013 Yu Tang
4+ Copyright (C) 2013-2014 Yu Tang
55 Home page: http://sourceforge.jp/users/yu-tang/
66 Support center: http://sourceforge.jp/users/yu-tang/
77
@@ -64,11 +64,11 @@
6464 root.add(new JSeparator());
6565
6666 // Current Source File
67- currentSourceFile = new ShellLinkMenuItem(L10n.getCurrentSourceFileNoneMenuLabel());
67+ currentSourceFile = new ShellLinkMenuItem(L10n.getSourceDocumentMenuLabel());
6868 root.add(currentSourceFile.getMenuItem());
6969
7070 // Current Target File
71- currentTargetFile = new ShellLinkMenuItem(L10n.getCurrentTargetFileNoneMenuLabel());
71+ currentTargetFile = new ShellLinkMenuItem(L10n.getTargetDocumentMenuLabel());
7272 root.add(currentTargetFile.getMenuItem());
7373
7474 // insert Files menu before the last menu (Help menu.)
@@ -92,23 +92,21 @@
9292 public void linkCurrentSourceFile(File file) throws IOException {
9393 String sourceRoot = Core.getProject().getProjectProperties().getSourceRoot();
9494 String midName = FileUtil.computeRelativePath(new File(sourceRoot), file);
95- String label = StaticUtils.format(L10n.getCurrentSourceFileMenuLabel(), midName);
96- currentSourceFile.link(file, label);
95+ currentSourceFile.link(file, midName);
9796 }
9897
9998 public void unlinkCurrentSourceFile() {
100- currentSourceFile.unlink(L10n.getCurrentSourceFileNoneMenuLabel());
99+ currentSourceFile.unlink();
101100 }
102101
103102 public void linkCurrentTargetFile(File file) throws IOException {
104103 String targetRoot = Core.getProject().getProjectProperties().getTargetRoot();
105104 String midName = FileUtil.computeRelativePath(new File(targetRoot), file);
106- String label = StaticUtils.format(L10n.getCurrentTargetFileMenuLabel(), midName);
107- currentTargetFile.link(file, label);
105+ currentTargetFile.link(file, midName);
108106 }
109107
110108 public void unlinkCurrentTargetFile() {
111- currentTargetFile.unlink(L10n.getCurrentTargetFileNoneMenuLabel());
109+ currentTargetFile.unlink();
112110 }
113111
114112 private JMenu createLocalizedMenu(String labelString) {
--- trunk/src/jp/sourceforge/users/yutang/omegat/plugin/foldermenu/MenuHelper.java (revision 60)
+++ trunk/src/jp/sourceforge/users/yutang/omegat/plugin/foldermenu/MenuHelper.java (revision 61)
@@ -1,7 +1,7 @@
11 /**************************************************************************
22 FolderMenu - easy access to project folders from menu.
33
4- Copyright (C) 2013 Yu Tang
4+ Copyright (C) 2013-2014 Yu Tang
55 Home page: http://sourceforge.jp/users/yu-tang/
66 Support center: http://sourceforge.jp/users/yu-tang/pf/
77
@@ -17,6 +17,7 @@
1717 package jp.sourceforge.users.yutang.omegat.plugin.foldermenu;
1818
1919 import java.awt.Component;
20+import java.awt.Desktop;
2021 import java.awt.event.ActionEvent;
2122 import java.awt.event.ActionListener;
2223 import java.awt.event.KeyEvent;
@@ -36,9 +37,12 @@
3637 import javax.swing.event.MenuKeyListener;
3738 import javax.swing.event.MenuListener;
3839 import javax.swing.filechooser.FileSystemView;
40+import jp.sourceforge.users.yutang.omegat.plugin.foldermenu.filepreview.FilePreview;
3941 import org.omegat.core.Core;
4042 import org.omegat.core.data.IProject;
4143 import org.omegat.util.Log;
44+import static org.omegat.util.Platform.getOsType;
45+import org.omegat.util.StaticUtils;
4246
4347 /**
4448 *
@@ -61,7 +65,7 @@
6165 al = new ActionListener() {
6266
6367 @Override
64- public void actionPerformed(ActionEvent e) { $(e).open(); }
68+ public void actionPerformed(ActionEvent e) { open( (JMenuItem) e.getSource()); }
6569
6670 };
6771
@@ -80,7 +84,7 @@
8084 JMenu menu = (JMenu) selection;
8185 if (menu.isEnabled()) {
8286 manager.clearSelectedPath();
83- $(menu).open();
87+ open(menu);
8488 }
8589 }
8690 }
@@ -94,7 +98,7 @@
9498 mol = new MouseListener() {
9599
96100 @Override
97- public void mouseClicked(MouseEvent e) { $(e).open(); }
101+ public void mouseClicked(MouseEvent e) { open((JMenuItem) e.getSource()); }
98102
99103 @Override
100104 public void mousePressed(MouseEvent e) { /* do nothing */ }
@@ -115,7 +119,7 @@
115119 @Override
116120 public void menuSelected(MenuEvent e) {
117121 // Lazy create submenus
118- $(e).createChildren();
122+ ((ShellLinkMenu) e.getSource()).createChildren();
119123 }
120124
121125 @Override
@@ -148,29 +152,45 @@
148152 }
149153
150154 if (sourceFile == null || !sourceFile.isFile()) {
151- menuManager.unlinkCurrentSourceFile();
152- menuManager.unlinkCurrentTargetFile();
155+ if (prevSourceFile != null) {
156+ menuManager.unlinkCurrentSourceFile();
157+ prevSourceFile = null;
158+ }
159+ if (prevTranslatedFile != null) {
160+ menuManager.unlinkCurrentTargetFile();
161+ prevTranslatedFile = null;
162+ }
153163 return;
154164 }
155165
156166 try {
157- menuManager.linkCurrentSourceFile(sourceFile);
167+ if (!sourceFile.equals(prevSourceFile)) {
168+ menuManager.linkCurrentSourceFile(sourceFile);
169+ prevSourceFile = sourceFile;
170+ }
158171 } catch (IOException ex) {
159172 Log.log(ex.getMessage());
160173 menuManager.unlinkCurrentSourceFile();
174+ prevSourceFile = null;
161175 }
162176
163177 if (translatedFile == null || !translatedFile.isFile()) {
164- menuManager.unlinkCurrentTargetFile();
178+ if (prevTranslatedFile != null) {
179+ menuManager.unlinkCurrentTargetFile();
180+ prevTranslatedFile = null;
181+ }
165182 } else {
166183 try {
167- menuManager.linkCurrentTargetFile(translatedFile);
184+ if (!translatedFile.equals(prevTranslatedFile)) {
185+ menuManager.linkCurrentTargetFile(translatedFile);
186+ prevTranslatedFile = translatedFile;
187+ }
168188 } catch (IOException ex) {
169189 Log.log(ex.getMessage());
170190 menuManager.unlinkCurrentTargetFile();
191+ prevTranslatedFile = null;
171192 }
172193 }
173-
174194 }
175195
176196 @Override
@@ -240,26 +260,36 @@
240260 return comp;
241261 }
242262
243- private static ShellLinkMenuItem $(JMenuItem mi) {
244- return new ShellLinkMenuItem(mi);
245- }
263+ private static void open(JMenuItem item) {
264+ if (! item.isEnabled()) {
265+ return;
266+ }
246267
247- private static ShellLinkMenu $(JMenu m) {
248- return new ShellLinkMenu(m);
268+ String path = item.getActionCommand();
269+ try {
270+ switch (getOsType()) {
271+ case WIN64:
272+ case WIN32:
273+ File file = new File(path);
274+ if (! FilePreview.open(file)) {
275+ Desktop.getDesktop().open(file);
276+ }
277+ break;
278+ case MAC64:
279+ case MAC32:
280+ new ProcessBuilder("open", path).start();
281+ break;
282+ default: // Linux and others
283+ new ProcessBuilder("xdg-open", path).start();
284+ break;
285+ }
286+ } catch (IOException ex) {
287+ Log.log(ex);
288+ Core.getMainWindow().showMessageDialog(StaticUtils.format(
289+ L10n.getErrMsgFileHasNoAssoc(), path));
290+ }
249291 }
250292
251- private static ShellLinkMenuItem $(ActionEvent e) {
252- return new ShellLinkMenuItem((JMenuItem) e.getSource());
253- }
254-
255- private static ShellLinkMenu $(MouseEvent e) {
256- return new ShellLinkMenu((JMenu) e.getSource());
257- }
258-
259- private static ShellLinkMenu $(MenuEvent e) {
260- return new ShellLinkMenu((JMenu) e.getSource());
261- }
262-
263293 public static void setMenuManager(MenuManager manager) {
264294 menuManager = manager;
265295 }
@@ -273,5 +303,7 @@
273303 private static final MenuListener melRoot;
274304 private static final Comparator<File> comp;
275305 private static MenuManager menuManager;
306+ private static File prevSourceFile;
307+ private static File prevTranslatedFile;
276308
277309 }
--- trunk/src/jp/sourceforge/users/yutang/omegat/plugin/foldermenu/ShellLinkMenuItem.java (revision 60)
+++ trunk/src/jp/sourceforge/users/yutang/omegat/plugin/foldermenu/ShellLinkMenuItem.java (revision 61)
@@ -1,7 +1,7 @@
11 /**************************************************************************
22 FolderMenu - easy access to project folders from menu.
33
4- Copyright (C) 2013 Yu Tang
4+ Copyright (C) 2013-2014 Yu Tang
55 Home page: http://sourceforge.jp/users/yu-tang/
66 Support center: http://sourceforge.jp/users/yu-tang/pf/
77
@@ -16,97 +16,51 @@
1616
1717 package jp.sourceforge.users.yutang.omegat.plugin.foldermenu;
1818
19-import java.awt.Desktop;
2019 import java.io.File;
2120 import java.io.IOException;
2221 import javax.swing.JMenuItem;
23-import org.omegat.core.Core;
24-import jp.sourceforge.users.yutang.omegat.plugin.foldermenu.filepreview.FilePreview;
25-import org.omegat.util.Log;
26-import static org.omegat.util.Platform.OsType.*;
27-import static org.omegat.util.Platform.getOsType;
28-import org.omegat.util.StaticUtils;
2922
3023 /**
3124 *
3225 * @author Yu Tang
3326 */
34-public class ShellLinkMenuItem {
27+public class ShellLinkMenuItem extends JMenuItem {
3528
36- protected JMenuItem menuItem;
37-
3829 public ShellLinkMenuItem() {
39- menuItem = new JMenuItem();
30+ super();
4031 }
4132
42- public ShellLinkMenuItem(JMenuItem mi) {
43- menuItem = mi;
44- }
45-
4633 public ShellLinkMenuItem(File file) throws IOException {
47- JMenuItem m = new JMenuItem(file.getName(), MenuHelper.getIcon(file));
48- m.setActionCommand(file.getCanonicalPath());
49- m.addActionListener(MenuHelper.getActionListener());
50- menuItem = m;
34+ super(file.getName(), MenuHelper.getIcon(file));
35+ setActionCommand(file.getCanonicalPath());
36+ addActionListener(MenuHelper.getActionListener());
5137 }
5238
5339 public ShellLinkMenuItem(String label) {
54- menuItem = new JMenuItem(label);
55- menuItem.setEnabled(false);
40+ super(label);
41+ setEnabled(false);
5642 }
57-
58- public boolean isEnabled() {
59- return menuItem.isEnabled();
60- }
61-
43+
6244 public JMenuItem getMenuItem() {
63- return menuItem;
45+ return (JMenuItem) this;
6446 }
6547
66- public void link(File file, String label) throws IOException {
67- menuItem.setActionCommand(file.getCanonicalPath());
68- menuItem.setIcon(MenuHelper.getIcon(file));
69- menuItem.setEnabled(true);
70- menuItem.setText(label);
71- if (menuItem.getActionListeners().length == 0) {
72- menuItem.addActionListener(MenuHelper.getActionListener());
48+ public void link(File file, String toolTipText) throws IOException {
49+ setToolTipText(toolTipText);
50+ setActionCommand(file.getCanonicalPath());
51+ setIcon(MenuHelper.getIcon(file));
52+ setEnabled(true);
53+
54+ if (getActionListeners().length == 0) {
55+ addActionListener(MenuHelper.getActionListener());
7356 }
7457 }
7558
76- public void unlink(String label) {
77- menuItem.setEnabled(false);
78- menuItem.setActionCommand("");
79- menuItem.setIcon(null);
80- menuItem.setText(label);
59+ public void unlink() {
60+ setToolTipText(null);
61+ setEnabled(false);
62+ setActionCommand("");
63+ setIcon(null);
8164 }
8265
83- public void open() {
84- if (! menuItem.isEnabled()) {
85- return;
86- }
87-
88- String path = menuItem.getActionCommand();
89- try {
90- switch (getOsType()) {
91- case WIN64:
92- case WIN32:
93- File file = new File(path);
94- if (! FilePreview.open(file)) {
95- Desktop.getDesktop().open(file);
96- }
97- break;
98- case MAC64:
99- case MAC32:
100- new ProcessBuilder("open", path).start();
101- break;
102- default: // Linux and others
103- new ProcessBuilder("xdg-open", path).start();
104- break;
105- }
106- } catch (IOException ex) {
107- Log.log(ex);
108- Core.getMainWindow().showMessageDialog(StaticUtils.format(
109- L10n.getErrMsgFileHasNoAssoc(), path));
110- }
111- }
11266 }
--- trunk/src/jp/sourceforge/users/yutang/omegat/plugin/foldermenu/L10n.java (revision 60)
+++ trunk/src/jp/sourceforge/users/yutang/omegat/plugin/foldermenu/L10n.java (revision 61)
@@ -1,7 +1,7 @@
11 /**************************************************************************
22 FolderMenu - easy access to project folders from menu.
33
4- Copyright (C) 2013 Yu Tang
4+ Copyright (C) 2013-2014 Yu Tang
55 Home page: http://sourceforge.jp/users/yu-tang/
66 Support center: http://sourceforge.jp/users/yu-tang/
77
@@ -43,22 +43,14 @@
4343 public static String getUserConfigMenuLabel() {
4444 return bundle.getString("USER_CONFIG_MENU_LABEL");
4545 }
46-
47- public static String getCurrentSourceFileNoneMenuLabel() {
48- return bundle.getString("CURRENT_SOURCE_FILE_NONE_MENU_LABEL");
46+
47+ public static String getSourceDocumentMenuLabel() {
48+ return bundle.getString("SOURCE_DOC_MENU_LABEL");
4949 }
5050
51- public static String getCurrentTargetFileNoneMenuLabel() {
52- return bundle.getString("CURRENT_TARGET_FILE_NONE_MENU_LABEL");
51+ public static String getTargetDocumentMenuLabel() {
52+ return bundle.getString("TARGET_DOC_MENU_LABEL");
5353 }
54-
55- public static String getCurrentSourceFileMenuLabel() {
56- return bundle.getString("CURRENT_SOURCE_FILE_MENU_LABEL");
57- }
58-
59- public static String getCurrentTargetFileMenuLabel() {
60- return bundle.getString("CURRENT_TARGET_FILE_MENU_LABEL");
61- }
6254
6355 // for Word
6456
--- trunk/src/jp/sourceforge/users/yutang/omegat/plugin/foldermenu/ShellLinkMenu.java (revision 60)
+++ trunk/src/jp/sourceforge/users/yutang/omegat/plugin/foldermenu/ShellLinkMenu.java (revision 61)
@@ -1,7 +1,7 @@
11 /**************************************************************************
22 FolderMenu - easy access to project folders from menu.
33
4- Copyright (C) 2013 Yu Tang
4+ Copyright (C) 2013-2014 Yu Tang
55 Home page: http://sourceforge.jp/users/yu-tang/
66 Support center: http://sourceforge.jp/users/yu-tang/pf/
77
@@ -22,7 +22,6 @@
2222 import javax.swing.JMenu;
2323 import static jp.sourceforge.users.yutang.omegat.plugin.foldermenu.MenuHelper.getComparator;
2424 import static jp.sourceforge.users.yutang.omegat.plugin.foldermenu.MenuHelper.getFilteredListFiles;
25-import static jp.sourceforge.users.yutang.omegat.plugin.foldermenu.MenuHelper.getIcon;
2625 import static jp.sourceforge.users.yutang.omegat.plugin.foldermenu.MenuHelper.getMenuListener;
2726 import static jp.sourceforge.users.yutang.omegat.plugin.foldermenu.MenuHelper.getMouseListener;
2827 import org.omegat.util.Log;
@@ -32,46 +31,38 @@
3231 *
3332 * @author Yu Tang
3433 */
35-public class ShellLinkMenu extends ShellLinkMenuItem {
34+public class ShellLinkMenu extends JMenu {
3635
37- public ShellLinkMenu(JMenu m) {
38- super(m);
36+ @SuppressWarnings("LeakingThisInConstructor")
37+ public ShellLinkMenu(File folder, String label) throws IOException {
38+ this(folder);
39+ Mnemonics.setLocalizedText(this, label);
3940 }
4041
4142 public ShellLinkMenu(File folder) throws IOException {
42- this(folder, null);
43+ super(folder.getName());
44+
45+ addMenuListener(getMenuListener());
46+ addMouseListener(getMouseListener());
47+
48+ setIcon(MenuHelper.getIcon(folder));
49+ setActionCommand(folder.getCanonicalPath());
4350 }
4451
52+ @SuppressWarnings("LeakingThisInConstructor")
4553 public ShellLinkMenu(String label) throws IOException {
46- this(null, label);
47- }
54+ super();
55+ Mnemonics.setLocalizedText(this, label);
4856
49- public ShellLinkMenu(File folder, String label) throws IOException {
50- JMenu m;
51- if (label == null) {
52- m = new JMenu(folder.getName());
53- } else {
54- m = new JMenu();
55- Mnemonics.setLocalizedText(m, label);
56- }
57-
58- m.addMenuListener(getMenuListener());
59- m.addMouseListener(getMouseListener());
60-
61- if (folder != null) {
62- m.setIcon(getIcon(folder));
63- m.setActionCommand(folder.getCanonicalPath());
64- } else {
65- m.setEnabled(false);
66- }
67-
68- menuItem = m;
57+ addMenuListener(getMenuListener());
58+ addMouseListener(getMouseListener());
59+
60+ setEnabled(false);
6961 }
7062
7163 public void createChildren() {
72- JMenu menu = (JMenu) menuItem;
73- if (menu.isEnabled() && menu.getItemCount() == 0) {
74- File folder = new File(menu.getActionCommand());
64+ if (isEnabled() && getItemCount() == 0) {
65+ File folder = new File(getActionCommand());
7566 File[] filteredListFiles = getFilteredListFiles(folder);
7667 Arrays.sort(filteredListFiles, getComparator());
7768
@@ -78,9 +69,9 @@
7869 for (File file : filteredListFiles) {
7970 try {
8071 if (file.isDirectory() && hasChildren(file)) {
81- menu.add(new ShellLinkMenu(file).getMenuItem());
72+ add(new ShellLinkMenu(file));
8273 } else {
83- menu.add(new ShellLinkMenuItem(file).getMenuItem());
74+ add(new ShellLinkMenuItem(file));
8475 }
8576 } catch (IOException ex) {
8677 Log.log(ex);
@@ -90,25 +81,24 @@
9081 }
9182
9283 public JMenu getMenu() {
93- return (JMenu) menuItem;
84+ return (JMenu) this;
9485 }
9586
9687 public void link(File folder) throws IOException {
97- JMenu menu = (JMenu) menuItem;
98- menu.setActionCommand(folder.getCanonicalPath());
99- menu.setIcon(getIcon(folder));
100- menu.setEnabled(true);
88+ setActionCommand(folder.getCanonicalPath());
89+ setIcon(MenuHelper.getIcon(folder));
90+ setEnabled(true);
10191 }
10292
10393 public void unlink() {
104- JMenu menu = (JMenu) menuItem;
105- menu.setEnabled(false);
106- menu.removeAll();
107- menu.setActionCommand("");
108- menu.setIcon(null);
94+ setEnabled(false);
95+ removeAll();
96+ setActionCommand("");
97+ setIcon(null);
10998 }
11099
111100 private boolean hasChildren(File folder) {
112101 return getFilteredListFiles(folder).length > 0;
113102 }
103+
114104 }