OmegaT の背景に画像を表示します。
新しい MoeConfig が機能するよう各クラスを調整
| @@ -14,7 +14,7 @@ | ||
| 14 | 14 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | 15 | **************************************************************************/ |
| 16 | 16 | |
| 17 | -package jp.sourceforge.users.yutang.omegat.plugin.moenizer.config; | |
| 17 | +package jp.sourceforge.users.yutang.omegat.plugin.moenizer; | |
| 18 | 18 | |
| 19 | 19 | import groovy.util.ConfigObject; |
| 20 | 20 | import groovy.util.ConfigSlurper; |
| @@ -32,9 +32,6 @@ | ||
| 32 | 32 | import java.util.Map; |
| 33 | 33 | import java.util.Set; |
| 34 | 34 | import java.util.TreeSet; |
| 35 | -import java.util.logging.Level; | |
| 36 | -import java.util.logging.Logger; | |
| 37 | -import jp.sourceforge.users.yutang.omegat.plugin.moenizer.MoeUtil; | |
| 38 | 35 | import org.omegat.util.Log; |
| 39 | 36 | |
| 40 | 37 | /** |
| @@ -42,7 +39,7 @@ | ||
| 42 | 39 | * |
| 43 | 40 | * @author Yu-Tang |
| 44 | 41 | */ |
| 45 | -public class ConfigSet { | |
| 42 | +public class MoeConfig { | |
| 46 | 43 | |
| 47 | 44 | public enum ProviderType { |
| 48 | 45 | Color, SimpleImage |
| @@ -65,7 +62,7 @@ | ||
| 65 | 62 | }); |
| 66 | 63 | ; |
| 67 | 64 | |
| 68 | - public ConfigSet(File file) throws IOException { | |
| 65 | + public MoeConfig(File file) throws IOException { | |
| 69 | 66 | this.file = file; |
| 70 | 67 | if (file.isFile()) { |
| 71 | 68 | config = new ConfigSlurper().parse(file.toURI().toURL()); |
| @@ -20,13 +20,12 @@ | ||
| 20 | 20 | import java.util.concurrent.TimeUnit; |
| 21 | 21 | import javax.swing.SwingUtilities; |
| 22 | 22 | import javax.swing.SwingWorker; |
| 23 | - | |
| 23 | +import jp.sourceforge.users.yutang.omegat.plugin.moenizer.imageloader.IImageLoader; | |
| 24 | +import jp.sourceforge.users.yutang.omegat.plugin.moenizer.imageloader.ImageLoader; | |
| 25 | +import org.omegat.core.Core; | |
| 24 | 26 | import org.omegat.core.CoreEvents; |
| 25 | 27 | import org.omegat.core.events.IApplicationEventListener; |
| 26 | 28 | import org.omegat.core.events.IProjectEventListener; |
| 27 | -import jp.sourceforge.users.yutang.omegat.plugin.moenizer.imageloader.IImageLoader; | |
| 28 | -import jp.sourceforge.users.yutang.omegat.plugin.moenizer.imageloader.ImageLoader; | |
| 29 | -import org.omegat.core.Core; | |
| 30 | 29 | import org.omegat.util.Log; |
| 31 | 30 | |
| 32 | 31 | /** |
| @@ -40,7 +39,8 @@ | ||
| 40 | 39 | |
| 41 | 40 | private IImageLoader imageLoader; |
| 42 | 41 | private MoeUI ui; |
| 43 | - | |
| 42 | + private MoeConfig config; | |
| 43 | + | |
| 44 | 44 | public static void loadPlugins() { |
| 45 | 45 | try { |
| 46 | 46 | // Not initialize in console mode |
| @@ -67,13 +67,14 @@ | ||
| 67 | 67 | CoreEvents.registerProjectChangeListener(this); |
| 68 | 68 | |
| 69 | 69 | ui = null; |
| 70 | - imageLoader = ImageLoader.getInstance(); | |
| 70 | + config = new MoeConfig(MoeUtil.getMoeConfigFile()); | |
| 71 | + imageLoader = ImageLoader.getInstance(config.getCurrentConfig()); | |
| 71 | 72 | // この時点ではまだウィンドウがインスタンス化されていないので、 |
| 72 | 73 | // ウィンドウに対する処理は少し待つ。 |
| 73 | 74 | |
| 74 | 75 | (new SlideShow()).execute(); |
| 75 | 76 | |
| 76 | - ui = MoeUI.getMoeUI(); | |
| 77 | + ui = MoeUI.getInstance(); | |
| 77 | 78 | |
| 78 | 79 | // この時点でコンポーネントの透過設定をしても反映されない(タイミング?)。 |
| 79 | 80 | // Workaround として、invokeLater でキューに突っ込んで、後で処理する。 |
| @@ -87,6 +88,7 @@ | ||
| 87 | 88 | //CoreEvents.unregisterApplicationEventListener(this); // ここで発行すると、スレッドエラーになるので注意 |
| 88 | 89 | } catch(Exception e) { |
| 89 | 90 | Log.log("Error raised at " + this.getClass().getCanonicalName() + ".onApplicationStartup()\n" + e); |
| 91 | + e.printStackTrace(); | |
| 90 | 92 | } |
| 91 | 93 | } |
| 92 | 94 |
| @@ -16,9 +16,9 @@ | ||
| 16 | 16 | |
| 17 | 17 | package jp.sourceforge.users.yutang.omegat.plugin.moenizer.imageloader; |
| 18 | 18 | |
| 19 | +import groovy.util.ConfigObject; | |
| 19 | 20 | import java.lang.reflect.Method; |
| 20 | -import java.net.URL; | |
| 21 | -import static jp.sourceforge.users.yutang.omegat.plugin.moenizer.MoeConfig.*; | |
| 21 | +import java.net.MalformedURLException; | |
| 22 | 22 | import org.omegat.util.Log; |
| 23 | 23 | |
| 24 | 24 | /** |
| @@ -27,15 +27,15 @@ | ||
| 27 | 27 | */ |
| 28 | 28 | public class ImageLoader { |
| 29 | 29 | |
| 30 | - public static IImageLoader getInstance() { | |
| 31 | - Object[] sources = getSources(); | |
| 30 | + public static IImageLoader getInstance(ConfigObject config) throws MalformedURLException { | |
| 31 | + /*Object[] sources = getSources(); | |
| 32 | 32 | |
| 33 | 33 | if (sources.length == 0) { |
| 34 | 34 | return null; // no source, no need loader |
| 35 | - } | |
| 35 | + }*/ | |
| 36 | 36 | |
| 37 | 37 | // default |
| 38 | - return new DefaultImageLoader(); | |
| 38 | + return new DefaultImageLoader(config); | |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | private static Object getAcceptableSource(Class<?> c, Object[] sources) { |
| @@ -16,12 +16,13 @@ | ||
| 16 | 16 | |
| 17 | 17 | package jp.sourceforge.users.yutang.omegat.plugin.moenizer.imageloader; |
| 18 | 18 | |
| 19 | +import groovy.util.ConfigObject; | |
| 19 | 20 | import java.awt.image.BufferedImage; |
| 20 | 21 | import java.io.File; |
| 22 | +import java.net.MalformedURLException; | |
| 21 | 23 | import java.net.URL; |
| 22 | 24 | import java.util.regex.Pattern; |
| 23 | 25 | import javax.imageio.ImageIO; |
| 24 | -import static jp.sourceforge.users.yutang.omegat.plugin.moenizer.MoeConfig.*; | |
| 25 | 26 | import org.omegat.util.Log; |
| 26 | 27 | |
| 27 | 28 | /** |
| @@ -39,7 +40,13 @@ | ||
| 39 | 40 | public class DefaultImageLoader implements IImageLoader { |
| 40 | 41 | |
| 41 | 42 | private static final Pattern RE_HAS_IMAGE_EXTENSION = Pattern.compile(".+\\.(?:bmp|png|jpg|jpeg|gif)"); |
| 42 | - private long interval; | |
| 43 | + private final String CONF_KEY_INTERVAL = "interval"; | |
| 44 | + private final String CONF_KEY_ENABLED = "enabled"; | |
| 45 | + private final String CONF_KEY_IMAGE = "image"; | |
| 46 | + | |
| 47 | + private final int DEFAULT_INTERVAL = 0; | |
| 48 | + | |
| 49 | + private final long interval; | |
| 43 | 50 | private Object[] sources; |
| 44 | 51 | private Object currentSource; |
| 45 | 52 | private int sourceIndex; |
| @@ -46,9 +53,9 @@ | ||
| 46 | 53 | |
| 47 | 54 | protected BufferedImage image; |
| 48 | 55 | |
| 49 | - public DefaultImageLoader() { | |
| 50 | - interval = getInterval(); | |
| 51 | - sources = getSources(); | |
| 56 | + public DefaultImageLoader(ConfigObject config) throws MalformedURLException { | |
| 57 | + interval = getInterval(config); | |
| 58 | + sources = getSources(config); | |
| 52 | 59 | currentSource = null; |
| 53 | 60 | image = null; |
| 54 | 61 | sourceIndex = -1; |
| @@ -93,6 +100,7 @@ | ||
| 93 | 100 | */ |
| 94 | 101 | @Override |
| 95 | 102 | public BufferedImage getNextImage() { |
| 103 | + Log.log("DefaultImageLoader.getNextImage() called."); | |
| 96 | 104 | return image; |
| 97 | 105 | } |
| 98 | 106 |
| @@ -159,4 +167,32 @@ | ||
| 159 | 167 | } |
| 160 | 168 | } |
| 161 | 169 | |
| 170 | + private long getInterval(ConfigObject config) { | |
| 171 | + if (config.containsKey(CONF_KEY_INTERVAL)) { | |
| 172 | + return (Integer) config.get(CONF_KEY_INTERVAL); | |
| 173 | + } else { | |
| 174 | + return DEFAULT_INTERVAL; | |
| 175 | + } | |
| 176 | + } | |
| 177 | + | |
| 178 | + private Object[] getSources(ConfigObject config) throws MalformedURLException { | |
| 179 | + boolean enabled = false; | |
| 180 | + if (config.containsKey(CONF_KEY_ENABLED)) { | |
| 181 | + enabled = (Boolean) config.get(CONF_KEY_ENABLED); | |
| 182 | + } | |
| 183 | + | |
| 184 | + if (enabled) { | |
| 185 | + if (config.containsKey(CONF_KEY_IMAGE)) { | |
| 186 | + String s = (String) config.get(CONF_KEY_IMAGE); | |
| 187 | + if (s.startsWith("http://") || s.startsWith("https://")) { | |
| 188 | + return new Object[]{new URL(s)}; | |
| 189 | + } else { | |
| 190 | + return new Object[]{new File(s)}; | |
| 191 | + } | |
| 192 | + } | |
| 193 | + } | |
| 194 | + | |
| 195 | + return new Object[]{}; | |
| 196 | + } | |
| 197 | + | |
| 162 | 198 | } |
| @@ -42,23 +42,14 @@ | ||
| 42 | 42 | */ |
| 43 | 43 | public class MoeLayeredPane extends JLayeredPane { |
| 44 | 44 | |
| 45 | - private BufferedImage image; | |
| 46 | - private TexturePaint paint; | |
| 47 | - private float alpha; | |
| 45 | + private BufferedImage image = null; | |
| 46 | + private TexturePaint paint = null; | |
| 47 | + private float alpha = 0.5f; //@@TODO replace literal to constant | |
| 48 | 48 | |
| 49 | 49 | public MoeLayeredPane() { |
| 50 | 50 | super(); |
| 51 | - this.image = null; | |
| 52 | - this.paint = null; | |
| 53 | - this.alpha = MoeConfig.getOpacity(); | |
| 54 | 51 | } |
| 55 | 52 | |
| 56 | - public MoeLayeredPane(BufferedImage image) { | |
| 57 | - super(); | |
| 58 | - setPicture(image); | |
| 59 | - this.alpha = MoeConfig.getOpacity(); | |
| 60 | - } | |
| 61 | - | |
| 62 | 53 | public void setBackground(BufferedImage image) { |
| 63 | 54 | setPicture(image); |
| 64 | 55 | this.repaint(); |
| @@ -35,7 +35,6 @@ | ||
| 35 | 35 | import javax.swing.JMenuBar; |
| 36 | 36 | import javax.swing.JPanel; |
| 37 | 37 | import javax.swing.JScrollPane; |
| 38 | -import javax.swing.JTextPane; | |
| 39 | 38 | import org.omegat.core.Core; |
| 40 | 39 | import org.omegat.util.gui.UIThreadsUtil; |
| 41 | 40 |
| @@ -57,33 +56,20 @@ | ||
| 57 | 56 | moeUI = null; |
| 58 | 57 | } |
| 59 | 58 | |
| 60 | - // Singleton. Not allow to Instanciate from outside. Use getMoeUI() to get instance. | |
| 61 | - private MoeUI(BufferedImage image) { | |
| 59 | + // Singleton. Not allow to Instanciate from outside. Use getInstance() to get instance. | |
| 60 | + private MoeUI() { | |
| 62 | 61 | UIThreadsUtil.mustBeSwingThread(); |
| 63 | 62 | |
| 64 | - initUI(image); | |
| 63 | + initUI(); | |
| 65 | 64 | } |
| 66 | 65 | |
| 67 | - private MoeUI() { | |
| 68 | - UIThreadsUtil.mustBeSwingThread(); | |
| 69 | - | |
| 70 | - initUI(null); | |
| 71 | - } | |
| 72 | - | |
| 73 | - public static MoeUI getMoeUI(BufferedImage image) { | |
| 66 | + public static MoeUI getInstance() { | |
| 74 | 67 | if (moeUI == null) { |
| 75 | - moeUI = new MoeUI(image); | |
| 76 | - } | |
| 77 | - return moeUI; | |
| 78 | - } | |
| 79 | - | |
| 80 | - public static MoeUI getMoeUI() { | |
| 81 | - if (moeUI == null) { | |
| 82 | 68 | moeUI = new MoeUI(); |
| 83 | 69 | } |
| 84 | 70 | return moeUI; |
| 85 | 71 | } |
| 86 | - | |
| 72 | + | |
| 87 | 73 | public void transparent() { |
| 88 | 74 | UIThreadsUtil.mustBeSwingThread(); |
| 89 | 75 |
| @@ -134,13 +120,9 @@ | ||
| 134 | 120 | }); |
| 135 | 121 | } |
| 136 | 122 | |
| 137 | - private void initUI(BufferedImage image) { | |
| 123 | + private void initUI() { | |
| 138 | 124 | frame = Core.getMainWindow().getApplicationFrame(); |
| 139 | - if (image == null) { | |
| 140 | - layeredPane = new MoeLayeredPane(); | |
| 141 | - } else { | |
| 142 | - layeredPane = new MoeLayeredPane(image); | |
| 143 | - } | |
| 125 | + layeredPane = new MoeLayeredPane(); | |
| 144 | 126 | contentPane = frame.getContentPane(); |
| 145 | 127 | menuBar = frame.getJMenuBar(); |
| 146 | 128 | desktop = getDockingDesktop(contentPane); |