Scala #034: ListView.selection

中級篇

Java.use(better, src=Scala) # ソースコードの歩き方《中級篇》
ソースコードの歩き方《Scala2.8.0》

《著》小粒ちゃん@湘南組、小粒ちゃん@博多組《監修》小泉ひよ子とタマゴ倶楽部
第1版♪2010/07/14

Scala #034: ListView.selection

》作業中です《

■ リスト項目を選択する


 36:   val control = new ListView(items) {
 37:     Command.listenTo(selection)
 38:   }

リスト項目を選択すると、Command が ListView.selection に呼応します。

 61:   object Command extends Publisher {
 62:     reactions += {
 63:       case ListSelectionChanged(source, range, live) => 
 64:         val value = source.selection.items.mkString
 65:         println(":: "+value)
 66:         _update(value)
 67:     }
 68:   }

選択した項目が変化すると、イベント ListSelectionChanged が発生するので、それに反応する処理を記述します。

@
180	  object selection extends Publisher {
181	    protected abstract class Indices[A](a: =>Seq[A]) extends scala.collection.mutable.Set[A] { 
182	      def -=(n: A): this.type 
183	      def +=(n: A): this.type
184	      def contains(n: A) = a.contains(n)
185	      override def size = a.length
186	      def iterator = a.iterator
187	    }
188	   
189	    def leadIndex: Int = peer.getSelectionModel.getLeadSelectionIndex
190	    def anchorIndex: Int = peer.getSelectionModel.getAnchorSelectionIndex
191	 
192	    /**
193	     * The indices of the currently selected items.
194	     */
195	    object indices extends Indices(peer.getSelectedIndices) {
196	      def -=(n: Int): this.type = { peer.removeSelectionInterval(n,n); this }
197	      def +=(n: Int): this.type = { peer.addSelectionInterval(n,n); this }
198	      @deprecated("Use ListView.selection.leadIndex")
199	      def leadIndex: Int = peer.getSelectionModel.getLeadSelectionIndex
200	      @deprecated("Use ListView.selection.anchorIndex")
201	      def anchorIndex: Int = peer.getSelectionModel.getAnchorSelectionIndex
202	    }
203	   
204	    @deprecated("Use ListView.selectIndices") 
205	    def selectIndices(ind: Int*) = peer.setSelectedIndices(ind.toArray)
206	   
207	    /**
208	     * The currently selected items.
209	     */
210	    object items extends scala.collection.SeqProxy[A] {
211	      def self = peer.getSelectedValues.map(_.asInstanceOf[A])
212	      @deprecated("Use ListView.selection.leadIndex")
213	      def leadIndex: Int = peer.getSelectionModel.getLeadSelectionIndex
214	      @deprecated("Use ListView.selection.anchorIndex")
215	      def anchorIndex: Int = peer.getSelectionModel.getAnchorSelectionIndex
216	    }
217	   
218	    def intervalMode: IntervalMode.Value = IntervalMode(peer.getSelectionModel.getSelectionMode)
219	    def intervalMode_=(m: IntervalMode.Value) { peer.getSelectionModel.setSelectionMode(m.id) }
220	
221	    peer.getSelectionModel.addListSelectionListener(new ListSelectionListener {
222	      def valueChanged(e: javax.swing.event.ListSelectionEvent) {
223	        publish(new ListSelectionChanged(ListView.this, e.getFirstIndex to e.getLastIndex, e.getValueIsAdjusting))
224	      }
225	    })
226	
227	    def adjusting = peer.getSelectionModel.getValueIsAdjusting
228	  }

》作業中です《


関連記事

TOP

Last updated♪2010/10/03