OSDN Git Service

リファクタリング
authorAkiyoshi Kamide <kamide@yk.rim.or.jp>
Thu, 23 Jun 2016 17:06:26 +0000 (02:06 +0900)
committerAkiyoshi Kamide <kamide@yk.rim.or.jp>
Thu, 23 Jun 2016 17:06:26 +0000 (02:06 +0900)
src/camidion/chordhelper/ChordHelperApplet.java
src/camidion/chordhelper/mididevice/ReceiverListModel.java
src/camidion/chordhelper/mididevice/ReceiverListView.java
src/camidion/chordhelper/mididevice/TransceiverListCellRenderer.java [new file with mode: 0644]
src/camidion/chordhelper/mididevice/TransmitterListView.java

index 42fd448..edd9b93 100644 (file)
@@ -285,7 +285,7 @@ public class ChordHelperApplet extends JApplet {
         */
        public static class VersionInfo {
                public static final String      NAME = "MIDI Chord Helper";
-               public static final String      VERSION = "Ver.20160622.1";
+               public static final String      VERSION = "Ver.20160623.1";
                public static final String      COPYRIGHT = "Copyright (C) 2004-2016";
                public static final String      AUTHER = "@きよし - Akiyoshi Kamide";
                public static final String      URL = "http://www.yk.rim.or.jp/~kamide/music/chordhelper/";
index 4af7c69..32ed91f 100644 (file)
@@ -13,9 +13,7 @@ import javax.swing.AbstractListModel;
  */
 public class ReceiverListModel extends AbstractListModel<Receiver> {
        protected MidiDeviceModel deviceModel;
-       public ReceiverListModel(MidiDeviceModel deviceModel) {
-               this.deviceModel = deviceModel;
-       }
+       public ReceiverListModel(MidiDeviceModel deviceModel) { this.deviceModel = deviceModel; }
        @Override
        public Receiver getElementAt(int index) {
                return deviceModel.getMidiDevice().getReceivers().get(index);
index bc1905d..670f4bf 100644 (file)
@@ -15,9 +15,7 @@ import java.awt.dnd.DropTargetListener;
 
 import javax.sound.midi.Receiver;
 import javax.sound.midi.Transmitter;
-import javax.swing.JLabel;
 import javax.swing.JList;
-import javax.swing.ListCellRenderer;
 import javax.swing.ListSelectionModel;
 
 /**
@@ -27,27 +25,11 @@ import javax.swing.ListSelectionModel;
  */
 public class ReceiverListView extends JList<Receiver> {
        /**
-        * レシーバを描画するクラス
+        * このリストによって表示される{@link Receiver}のリストを保持するデータモデルを返します。
+        * @return 表示される{@link Receiver}のリストを提供するデータモデル
         */
-       private static class CellRenderer extends JLabel implements ListCellRenderer<Receiver> {
-               public Component getListCellRendererComponent(JList<? extends Receiver> list,
-                               Receiver value, int index, boolean isSelected, boolean cellHasFocus)
-               {
-                       setEnabled(list.isEnabled());
-                       setFont(list.getFont());
-                       setOpaque(true);
-                       if (isSelected) {
-                               setBackground(list.getSelectionBackground());
-                               setForeground(list.getSelectionForeground());
-                       } else {
-                               setBackground(list.getBackground());
-                               setForeground(list.getForeground());
-                       }
-                       setIcon(MidiDeviceDialog.MIDI_CONNECTER_ICON);
-                       setToolTipText("受信端子(Rx):ドラッグ&ドロップしてTxに接続できます。");
-                       return this;
-               }
-       }
+       @Override
+       public ReceiverListModel getModel() { return (ReceiverListModel) super.getModel(); }
        /**
         * 引数で指定された{@link Receiver}のセル範囲を示す、
         * リストの座標系内の境界の矩形を返します。対応するセルがない場合はnullを返します。
@@ -58,35 +40,34 @@ public class ReceiverListView extends JList<Receiver> {
                return getCellBounds(index,index);
        }
        /**
-        * このリストによって表示される{@link Receiver}のリストを保持するデータモデルを返します。
-        * @return 表示される{@link Receiver}のリストを提供するデータモデル
-        */
-       @Override
-       public ReceiverListModel getModel() {
-               return (ReceiverListModel) super.getModel();
-       }
-       /**
         * 仮想MIDI端子リストビューを生成します。
         * @param model このビューから参照されるデータモデル
         * @param cablePane MIDIケーブル描画面
         */
        public ReceiverListView(ReceiverListModel model, MidiCablePane cablePane) {
                super(model);
-               setCellRenderer(new CellRenderer());
                setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                setLayoutOrientation(JList.HORIZONTAL_WRAP);
                setVisibleRowCount(0);
-               //
-               // レシーバのドラッグを受け付ける
+               setCellRenderer(new TransceiverListCellRenderer<Receiver>() {
+                       public Component getListCellRendererComponent(JList<? extends Receiver> list,
+                                       Receiver value, int index, boolean isSelected, boolean cellHasFocus)
+                       {
+                               super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
+                               setToolTipText("受信端子(Rx):ドラッグ&ドロップしてTxに接続できます。");
+                               return this;
+                       }
+               });
+               DragSource dragSource = new DragSource();
                DragGestureListener dgl = new DragGestureListener() {
                        @Override
                        public void dragGestureRecognized(DragGestureEvent event) {
                                if( (event.getDragAction() & DnDConstants.ACTION_COPY_OR_MOVE) == 0 ) return;
-                               MidiCablePane.dragging.setData(getModel().getElementAt(locationToIndex(event.getDragOrigin())));
+                               Receiver rx = getModel().getElementAt(locationToIndex(event.getDragOrigin()));
+                               MidiCablePane.dragging.setData(rx);
                                event.startDrag(DragSource.DefaultLinkDrop, MidiCablePane.dragging, cablePane.dragSourceListener);
                        }
                };
-               DragSource dragSource = new DragSource();
                dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, dgl);
                dragSource.addDragSourceMotionListener(cablePane.dragSourceMotionListener);
                //
@@ -101,8 +82,7 @@ public class ReceiverListView extends JList<Receiver> {
                        @Override
                        public void drop(DropTargetDropEvent event) {
                                event.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
-                               int maskedBits = event.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE;
-                               if( maskedBits == 0 ) {
+                               if( (event.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) == 0 ) {
                                        event.dropComplete(false);
                                        return;
                                }
diff --git a/src/camidion/chordhelper/mididevice/TransceiverListCellRenderer.java b/src/camidion/chordhelper/mididevice/TransceiverListCellRenderer.java
new file mode 100644 (file)
index 0000000..caa55a3
--- /dev/null
@@ -0,0 +1,26 @@
+package camidion.chordhelper.mididevice;
+
+import java.awt.Component;
+
+import javax.swing.JLabel;
+import javax.swing.JList;
+import javax.swing.ListCellRenderer;
+
+public class TransceiverListCellRenderer<E> extends JLabel implements ListCellRenderer<E> {
+       public Component getListCellRendererComponent(JList<? extends E> list,
+                       E value, int index, boolean isSelected, boolean cellHasFocus)
+       {
+               setEnabled(list.isEnabled());
+               setFont(list.getFont());
+               setOpaque(true);
+               if (isSelected) {
+                       setBackground(list.getSelectionBackground());
+                       setForeground(list.getSelectionForeground());
+               } else {
+                       setBackground(list.getBackground());
+                       setForeground(list.getForeground());
+               }
+               setIcon(MidiDeviceDialog.MIDI_CONNECTER_ICON);
+               return this;
+       }
+}
index e15c52b..2d35e49 100644 (file)
@@ -17,9 +17,7 @@ import java.awt.dnd.DropTargetListener;
 
 import javax.sound.midi.Receiver;
 import javax.sound.midi.Transmitter;
-import javax.swing.JLabel;
 import javax.swing.JList;
-import javax.swing.ListCellRenderer;
 import javax.swing.ListSelectionModel;
 
 /**
@@ -29,31 +27,11 @@ import javax.swing.ListSelectionModel;
  */
 public class TransmitterListView extends JList<Transmitter> {
        /**
-        * トランスミッタを描画するクラス
+        * このリストによって表示される{@link Transmitter}のリストを保持するデータモデルを返します。
+        * @return 表示される{@link Transmitter}のリストを提供するデータモデル
         */
-       private static class CellRenderer extends JLabel implements ListCellRenderer<Transmitter> {
-               public Component getListCellRendererComponent(JList<? extends Transmitter> list,
-                               Transmitter value, int index, boolean isSelected, boolean cellHasFocus)
-               {
-                       setEnabled(list.isEnabled());
-                       setFont(list.getFont());
-                       setOpaque(true);
-                       if (isSelected) {
-                               setBackground(list.getSelectionBackground());
-                               setForeground(list.getSelectionForeground());
-                       } else {
-                               setBackground(list.getBackground());
-                               setForeground(list.getForeground());
-                       }
-                       setIcon(MidiDeviceDialog.MIDI_CONNECTER_ICON);
-                       if( value instanceof DummyTransmitter ) {
-                               setToolTipText("未接続の送信端子(Tx):ドラッグ&ドロップしてRxに接続できます。");
-                       } else {
-                               setToolTipText("接続済の送信端子(Tx):ドラッグ&ドロップして接続先Rxを変更、または切断できます。");
-                       }
-                       return this;
-               }
-       }
+       @Override
+       public TransmitterListModel getModel() { return (TransmitterListModel) super.getModel(); }
        /**
         * 引数で指定された{@link Transmitter}のセル範囲を示す、
         * リストの座標系内の境界の矩形を返します。対応するセルがない場合はnullを返します。
@@ -64,26 +42,30 @@ public class TransmitterListView extends JList<Transmitter> {
                return getCellBounds(index,index);
        }
        /**
-        * このリストによって表示される{@link Transmitter}のリストを保持するデータモデルを返します。
-        * @return 表示される{@link Transmitter}のリストを提供するデータモデル
-        */
-       @Override
-       public TransmitterListModel getModel() {
-               return (TransmitterListModel) super.getModel();
-       }
-       /**
         * 仮想MIDI端子リストビューを生成します。
         * @param model このビューから参照されるデータモデル
         * @param cablePane MIDIケーブル描画面
         */
        public TransmitterListView(TransmitterListModel model, MidiCablePane cablePane) {
                super(model);
-               setCellRenderer(new CellRenderer());
                setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                setLayoutOrientation(JList.HORIZONTAL_WRAP);
                setVisibleRowCount(0);
-               //
-               // トランスミッタのドラッグを受け付ける
+               setCellRenderer(new TransceiverListCellRenderer<Transmitter>() {
+                       @Override
+                       public Component getListCellRendererComponent(JList<? extends Transmitter> list,
+                                       Transmitter value, int index, boolean isSelected, boolean cellHasFocus)
+                       {
+                               super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
+                               if( value instanceof DummyTransmitter ) {
+                                       setToolTipText("未接続の送信端子(Tx):ドラッグ&ドロップしてRxに接続できます。");
+                               } else {
+                                       setToolTipText("接続済の送信端子(Tx):ドラッグ&ドロップして接続先Rxを変更、または切断できます。");
+                               }
+                               return this;
+                       }
+               });
+               DragSource dragSource = new DragSource();
                DragGestureListener dgl = new DragGestureListener() {
                        @Override
                        public void dragGestureRecognized(DragGestureEvent event) {
@@ -110,7 +92,6 @@ public class TransmitterListView extends JList<Transmitter> {
                                });
                        }
                };
-               DragSource dragSource = new DragSource();
                dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, dgl);
                dragSource.addDragSourceMotionListener(cablePane.dragSourceMotionListener);
                //
@@ -125,8 +106,7 @@ public class TransmitterListView extends JList<Transmitter> {
                        @Override
                        public void drop(DropTargetDropEvent event) {
                                event.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
-                               int maskedBits = event.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE;
-                               if( maskedBits == 0 ) {
+                               if( (event.getDropAction() & DnDConstants.ACTION_COPY_OR_MOVE) == 0 ) {
                                        event.dropComplete(false);
                                        return;
                                }