OSDN Git Service

remove WebIPC & use java.awt.Desktop
authorOlyutorskii <olyutorskii@users.osdn.me>
Sun, 22 Apr 2018 16:34:07 +0000 (01:34 +0900)
committerOlyutorskii <olyutorskii@users.osdn.me>
Sun, 22 Apr 2018 16:34:07 +0000 (01:34 +0900)
src/main/java/jp/sfjp/jindolf/dxchg/WebIPC.java [deleted file]
src/main/java/jp/sfjp/jindolf/dxchg/WebIPCDialog.java

diff --git a/src/main/java/jp/sfjp/jindolf/dxchg/WebIPC.java b/src/main/java/jp/sfjp/jindolf/dxchg/WebIPC.java
deleted file mode 100644 (file)
index e1b7e29..0000000
+++ /dev/null
@@ -1,347 +0,0 @@
-/*
- * Inter Process Communication with Web browser
- *
- * License : The MIT License
- * Copyright(c) 2009 olyutorskii
- */
-
-package jp.sfjp.jindolf.dxchg;
-
-import java.awt.GraphicsEnvironment;
-import java.awt.HeadlessException;
-import java.io.IOException;
-import java.lang.reflect.Field;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.net.URI;
-
-/**
- * Webブラウザとのプロセス間通信。
- * java.awt.Desktopの代用品。
- * JRE1.6でしか使えない。がしかし、JRE1.5でもコンパイル&ロード可能。
- * ※参照: java.awt.Desktop
- */
-public final class WebIPC{
-
-    private static final Class<?> DESKTOP_KLASS;
-    private static final Method   METHOD_ISDESKTOPSUPPORTED;
-    private static final Method   METHOD_GETDESKTOP;
-    private static final Method   METHOD_ISSUPPORTED;
-    private static final Method   METHOD_BROWSE;
-
-    private static final Class<?> DESKTOP_ACTION_KLASS;
-    private static final Enum<?>  BROWSE_ENUM;
-
-    static{
-        DESKTOP_KLASS       = getClass("java.awt.Desktop");
-        DESKTOP_ACTION_KLASS = getInnerClass(DESKTOP_KLASS,
-                                           "java.awt.Desktop.Action");
-
-        METHOD_ISDESKTOPSUPPORTED = getMethod("isDesktopSupported");
-        METHOD_GETDESKTOP         = getMethod("getDesktop");
-        METHOD_ISSUPPORTED        = getMethod("isSupported",
-                                             DESKTOP_ACTION_KLASS);
-        METHOD_BROWSE             = getMethod("browse",
-                                             URI.class);
-
-        BROWSE_ENUM = getEnumMember(DESKTOP_ACTION_KLASS, "BROWSE");
-    }
-
-
-    private final Object desktop;
-
-
-    /**
-     * 見えないコンストラクタ。
-     * @throws HeadlessException GUI環境が未接続
-     * @throws UnsupportedOperationException 未サポート
-     */
-    private WebIPC()
-            throws HeadlessException,
-                   UnsupportedOperationException {
-        super();
-
-        try{
-            this.desktop = METHOD_GETDESKTOP.invoke(null, (Object[]) null);
-        }catch(InvocationTargetException e){
-            Throwable targetException = e.getTargetException();
-
-            if(targetException instanceof RuntimeException){
-                throw (RuntimeException) targetException;
-            }
-            if(targetException instanceof Error){
-                throw (Error) targetException;
-            }
-
-            AssertionError thw = new AssertionError();
-            thw.initCause(e);
-            throw thw;
-        }catch(IllegalAccessException e){
-            AssertionError thw = new AssertionError();
-            thw.initCause(e);
-            throw thw;
-        }
-
-        return;
-    }
-
-
-    /**
-     * クラス名からClassインスタンスを探す。
-     * @param klassName クラス名
-     * @return Classインスタンス。クラスが見つからなければnull。
-     */
-    private static Class<?> getClass(String klassName){
-        Class<?> result;
-        try{
-            result = Class.forName(klassName);
-        }catch(ClassNotFoundException e){
-            result = null;
-        }
-        return result;
-    }
-
-    /**
-     * 内部クラス名からClassインスタンスを探す。
-     * @param parent 囲む親クラス。
-     * @param canonical 内部クラスのカノニカル名
-     * @return Classインスタンス。クラスが見つからなければnull。
-     */
-    private static Class<?> getInnerClass(Class<?> parent,
-                                            String canonical){
-        if(parent == null) return null;
-
-        Class<?> result = null;
-
-        Class<?>[] innerKlasses = parent.getClasses();
-        for(Class<?> klass : innerKlasses){
-            if(klass.getCanonicalName().equals(canonical)){
-                result = klass;
-                break;
-            }
-        }
-
-        return result;
-    }
-
-    /**
-     * Desktopクラスのメソッド名からMethodインスタンスを探す。
-     * @param methodName メソッド名
-     * @param paramTypes 引数型並び
-     * @return Methodインスタンス。見つからなければnull。
-     */
-    private static Method getMethod(String methodName,
-                                     Class<?>... paramTypes){
-        if(DESKTOP_KLASS == null) return null;
-
-        Method result;
-
-        try{
-            result = DESKTOP_KLASS.getMethod(methodName, paramTypes);
-        }catch(NoSuchMethodException e){
-            result = null;
-        }
-
-        return result;
-    }
-
-    /**
-     * Enumのメンバを探す。
-     * @param parent Enumの型
-     * @param memberName メンバ名
-     * @return Enumインスタンス。見つからなければnull。
-     */
-    private static Enum<?> getEnumMember(Class<?> parent,
-                                           String memberName){
-        if(parent == null) return null;
-
-        Field field;
-        try{
-            field = parent.getField(memberName);
-        }catch(NoSuchFieldException e){
-            return null;
-        }
-
-        Object value;
-        try{
-            value = field.get(null);
-        }catch(IllegalAccessException e){
-            return null;
-        }catch(IllegalArgumentException e){
-            return null;
-        }
-
-        if( ! (value instanceof Enum) ) return null;
-
-        return (Enum<?>) value;
-    }
-
-    /**
-     * JRE1.6より提供されたjava.awt.Desktopが利用可能か判定する。
-     * ※参照: java.awt.DesktopのisDesktopSupported()
-     * @return Desktopが利用可能ならtrue。JRE1.5以前だとたぶんfalse。
-     */
-    public static boolean isDesktopSupported(){
-        if(METHOD_ISDESKTOPSUPPORTED == null) return false;
-
-        Object invokeResult;
-        try{
-            invokeResult = METHOD_ISDESKTOPSUPPORTED.invoke(null,
-                                                            (Object[]) null);
-        }catch(InvocationTargetException e){
-            Throwable targetException = e.getTargetException();
-
-            if(targetException instanceof RuntimeException){
-                throw (RuntimeException) targetException;
-            }else if(targetException instanceof Error){
-                throw (Error) targetException;
-            }
-
-            AssertionError thw = new AssertionError();
-            thw.initCause(e);
-            throw thw;
-        }catch(IllegalAccessException e){
-            AssertionError thw = new AssertionError();
-            thw.initCause(e);
-            throw thw;
-        }
-
-        if( ! (invokeResult instanceof Boolean) ){
-            assert false;
-            return false;
-        }
-
-        boolean result = (Boolean) invokeResult;
-
-        return result;
-    }
-
-    /**
-     * WebIPCインスタンスを得る。
-     * ※参照: java.awt.DesktopのgetDesktop()
-     * @return インスタンス
-     * @throws java.awt.HeadlessException スクリーンデバイスが見つからない
-     * @throws java.lang.UnsupportedOperationException 未サポートの機能
-     */
-    public static WebIPC getWebIPC()
-            throws HeadlessException,
-                   UnsupportedOperationException {
-        if(GraphicsEnvironment.isHeadless()) throw new HeadlessException();
-        if( ! isDesktopSupported() ){
-            throw new UnsupportedOperationException();
-        }
-
-        WebIPC webIPC = new WebIPC();
-
-        return webIPC;
-    }
-
-    /**
-     * Webブラウザに任意のURIを表示させる。
-     * ※参照: java.awt.Desktopのbrowse(java.net.URI)
-     * @param uri URI
-     * @throws NullPointerException 引数がnull
-     * @throws UnsupportedOperationException 未サポートの機能
-     * @throws IOException ブラウザが見つからない
-     * @throws SecurityException セキュリティ違反
-     * @throws IllegalArgumentException URI形式が変
-     */
-    public void browse(URI uri)
-            throws NullPointerException,
-                   UnsupportedOperationException,
-                   IOException,
-                   SecurityException,
-                   IllegalArgumentException {
-        if(uri == null) throw new NullPointerException();
-        if( ! isSupported(Action.BROWSE) ){
-            throw new UnsupportedOperationException();
-        }
-
-        try{
-            METHOD_BROWSE.invoke(this.desktop, uri);
-        }catch(InvocationTargetException e){
-            Throwable targetException = e.getTargetException();
-
-            if(targetException instanceof IOException){
-                throw (IOException) targetException;
-            }
-            if(targetException instanceof RuntimeException){
-                throw (RuntimeException) targetException;
-            }
-            if(targetException instanceof Error){
-                throw (Error) targetException;
-            }
-
-            AssertionError thw = new AssertionError();
-            thw.initCause(e);
-            throw thw;
-        }catch(IllegalAccessException e){
-            AssertionError thw = new AssertionError();
-            thw.initCause(e);
-            throw thw;
-        }
-
-        return;
-    }
-
-    /**
-     * 指定した機能(アクション)がサポートされているか否か判定する。
-     * ※参照: java.awt.Desktop#isSupported(java.awt.Desktop.Action)
-     * @param action アクション
-     * @return アクションがサポートされていればtrue。
-     */
-    public boolean isSupported(Action action){
-        switch(action){
-        case BROWSE:
-            break;
-        default:
-            return false;
-        }
-
-        Object invokeResult;
-        try{
-            invokeResult = METHOD_ISSUPPORTED.invoke(this.desktop,
-                                                     BROWSE_ENUM);
-        }catch(InvocationTargetException e){
-            Throwable targetException = e.getTargetException();
-
-            if(targetException instanceof RuntimeException){
-                throw (RuntimeException) targetException;
-            }
-            if(targetException instanceof Error){
-                throw (Error) targetException;
-            }
-
-            AssertionError thw = new AssertionError();
-            thw.initCause(e);
-            throw thw;
-        }catch(IllegalAccessException e){
-            AssertionError thw = new AssertionError();
-            thw.initCause(e);
-            throw thw;
-        }
-
-        if( ! (invokeResult instanceof Boolean) ){
-            assert false;
-            return false;
-        }
-
-        boolean result = (Boolean) invokeResult;
-
-        return result;
-    }
-
-    /**
-     * 各種デスクトップアクション。
-     * ※参照 java.awt.Desktop.Action
-     */
-    public static enum Action{
-        /** Webブラウザでのブラウズ。 */
-        BROWSE,
-    //  EDIT,
-    //  MAIL,
-    //  OPEN,
-    //  PRINT,
-    }
-
-}
index ff65200..845e28b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Dialog for WebIPC
+ * Dialog for Desktop
  *
  * License : The MIT License
  * Copyright(c) 2009 olyutorskii
@@ -8,6 +8,7 @@
 package jp.sfjp.jindolf.dxchg;
 
 import java.awt.Container;
+import java.awt.Desktop;
 import java.awt.Frame;
 import java.awt.GridBagConstraints;
 import java.awt.GridBagLayout;
@@ -36,7 +37,6 @@ import javax.swing.SwingConstants;
 import javax.swing.TransferHandler;
 import javax.swing.border.Border;
 import javax.swing.border.EtchedBorder;
-import jp.sfjp.jindolf.JreChecker;
 import jp.sfjp.jindolf.VerInfo;
 import jp.sfjp.jindolf.util.GUIUtils;
 import jp.sfjp.jindolf.util.Monodizer;
@@ -45,15 +45,16 @@ import jp.sfjp.jindolf.util.Monodizer;
  * Webブラウザ起動用の専用ダイアログ。
  */
 @SuppressWarnings("serial")
-public class WebIPCDialog
-        extends JDialog
-        implements ActionListener {
+public class WebIPCDialog extends JDialog {
 
-    private static final String TITLE_WWW =
-            VerInfo.getFrameTitle("URLへのアクセス確認");
+    private static final Logger LOGGER = Logger.getAnonymousLogger();
 
+    private static final String CMD_BROWSE   = "browse";
+    private static final String CMD_CLIPCOPY = "clipcopy";
+    private static final String CMD_CANCEL   = "cancel";
 
-    private static final Logger LOGGER = Logger.getAnonymousLogger();
+    private static final String TITLE_WWW =
+            VerInfo.getFrameTitle("URLへのアクセス確認");
 
 
     private final String warnMessage;
@@ -71,7 +72,7 @@ public class WebIPCDialog
     private final JButton cancel =
             new JButton("閉じる");
 
-    private final WebIPC ipc;
+    private final Desktop desktop;
 
     private URI uri;
 
@@ -86,55 +87,30 @@ public class WebIPCDialog
 
         GUIUtils.modifyWindowAttributes(this, true, false, true);
 
-        WebIPC webipc = null;
-        if(WebIPC.isDesktopSupported()){
-            webipc = WebIPC.getWebIPC();
-            if( ! webipc.isSupported(WebIPC.Action.BROWSE) ){
+        Desktop webipc = null;
+        if(Desktop.isDesktopSupported()){
+            webipc = Desktop.getDesktop();
+            if( ! webipc.isSupported(Desktop.Action.BROWSE) ){
                 webipc = null;
             }
         }
-        this.ipc = webipc;
-
-        if(this.ipc == null){
-            if( ! JreChecker.has16Runtime() ){
-                this.warnMessage =
-                        "この機能を利用するには、JRE1.6以上が必要です";
-            }else{
-                this.warnMessage =
-                        "何らかの理由でこの機能は利用不可になっています";
-            }
+        this.desktop = webipc;
+
+        if(this.desktop == null){
+            this.warnMessage =
+                    "何らかの理由でこの機能は利用不可になっています";
         }else{
             this.warnMessage = "";
         }
 
-        Border inside =
-                BorderFactory.createEmptyBorder(1, 4, 1, 4);
-        Border outside =
-                BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
-        Border border =
-                BorderFactory.createCompoundBorder(outside, inside);
-        this.urltext.setBorder(border);
-        this.urltext.setEditable(false);
-        this.urltext.setLineWrap(true);
-        this.urltext.setComponentPopupMenu(new TextPopup());
-        Monodizer.monodize(this.urltext);
-
-        this.dndLabel.setIcon(GUIUtils.getWWWIcon());
-        this.dndLabel.setHorizontalTextPosition(SwingConstants.LEFT);
-        this.dndLabel.setTransferHandler(new DnDHandler());
-        this.dndLabel.addMouseListener(new DragIgniter());
+        buildDnDIcon();
+        buildUrlLabel();
+        buildButton();
 
         Container container = getContentPane();
         design(container);
 
-        this.browse  .addActionListener(this);
-        this.clipcopy.addActionListener(this);
-        this.cancel  .addActionListener(this);
-
-        getRootPane().setDefaultButton(this.browse);
-        this.browse.requestFocusInWindow();
-
-        if(this.ipc == null){
+        if(this.desktop == null){
             this.browse.setToolTipText(this.warnMessage);
         }
 
@@ -150,6 +126,7 @@ public class WebIPCDialog
         return;
     }
 
+
     /**
      * Webブラウザ起動用のモーダルダイアログを表示する。
      * @param owner オーナーフレーム
@@ -188,6 +165,59 @@ public class WebIPCDialog
         return true;
     }
 
+
+    /**
+     * DragAndDropアイコンを構成する。
+     */
+    private void buildDnDIcon(){
+        this.dndLabel.setIcon(GUIUtils.getWWWIcon());
+        this.dndLabel.setHorizontalTextPosition(SwingConstants.LEFT);
+        this.dndLabel.setTransferHandler(new DnDHandler());
+        this.dndLabel.addMouseListener(new DragIgniter());
+        return;
+    }
+
+    /**
+     * URL表示部を構成する。
+     */
+    private void buildUrlLabel(){
+        Border inside =
+                BorderFactory.createEmptyBorder(1, 4, 1, 4);
+        Border outside =
+                BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
+        Border border =
+                BorderFactory.createCompoundBorder(outside, inside);
+
+        this.urltext.setBorder(border);
+
+        this.urltext.setEditable(false);
+        this.urltext.setLineWrap(true);
+        this.urltext.setComponentPopupMenu(new TextPopup());
+
+        Monodizer.monodize(this.urltext);
+
+        return;
+    }
+
+    /**
+     * ボタンを構成する。
+     */
+    private void buildButton(){
+        this.browse  .setActionCommand(CMD_BROWSE);
+        this.clipcopy.setActionCommand(CMD_CLIPCOPY);
+        this.cancel  .setActionCommand(CMD_CANCEL);
+
+        ActionListener btnListsner = new ButtonListener();
+        this.browse  .addActionListener(btnListsner);
+        this.clipcopy.addActionListener(btnListsner);
+        this.cancel  .addActionListener(btnListsner);
+
+        getRootPane().setDefaultButton(this.browse);
+        this.browse.requestFocusInWindow();
+
+        return;
+    }
+
     /**
      * レイアウトを行う。
      * @param container レイアウトコンテナ
@@ -268,23 +298,6 @@ public class WebIPCDialog
     }
 
     /**
-     * ボタン押下リスナ。
-     * @param event ボタン押下イベント
-     */
-    @Override
-    public void actionPerformed(ActionEvent event){
-        Object source = event.getSource();
-        if(source == this.browse){
-            actionBrowse();
-        }else if(source == this.clipcopy){
-            actionClipboardCopy();
-        }else if(source == this.cancel){
-            actionCancel();
-        }
-        return;
-    }
-
-    /**
      * WebブラウザでURLを表示。
      */
     private void actionBrowse(){
@@ -293,13 +306,8 @@ public class WebIPCDialog
             return;
         }
 
-        if(this.ipc == null){
-            String title;
-            if( ! JreChecker.has16Runtime() ){
-                title = "新しいJavaを入手しましょう";
-            }else{
-                title = "報告";
-            }
+        if(this.desktop == null){
+            String title = "報告";
             JOptionPane.showMessageDialog(
                     this,
                     this.warnMessage, title,
@@ -309,16 +317,14 @@ public class WebIPCDialog
 
         try{
             try{
-                this.ipc.browse(this.uri);
+                this.desktop.browse(this.uri);
             }catch(NullPointerException e){
                 assert false;
-            }catch(UnsupportedOperationException e){
-                // NOTHING
-            }catch(IOException e){
-                // NOTHING
-            }catch(SecurityException e){
-                // NOTHING
-            }catch(IllegalArgumentException e){
+            }catch(   UnsupportedOperationException
+                    | IOException
+                    | SecurityException
+                    | IllegalArgumentException e
+                    ){
                 // NOTHING
             }
             String logmsg =   "URL "
@@ -372,6 +378,32 @@ public class WebIPCDialog
         return;
     }
 
+
+    /**
+     * ボタンリスナ。
+     */
+    private class ButtonListener implements ActionListener{
+
+        /**
+         * ボタン押下リスナ。
+         * @param event ボタン押下イベント
+         */
+        @Override
+        public void actionPerformed(ActionEvent event){
+            String cmd = event.getActionCommand();
+            if(CMD_BROWSE.equals(cmd)){
+                actionBrowse();
+            }else if(CMD_CLIPCOPY.equals(cmd)){
+                actionClipboardCopy();
+            }else if(CMD_CANCEL.equals(cmd)){
+                actionCancel();
+            }
+            return;
+        }
+
+    }
+
+
     /**
      * Drag&amp;Dropの転送処理を管理。
      */
@@ -445,6 +477,7 @@ public class WebIPCDialog
 
     }
 
+
     /**
      * ドラッグ開始イベント処理。
      */