GlazedlistsSwingBuilderCustomizer.groovy
001 /*
002  * Copyright 2014-2016 the original author or authors.
003  *
004  * Licensed under the Apache License, Version 2.0 (the "License");
005  * you may not use this file except in compliance with the License.
006  * You may obtain a copy of the License at
007  *
008  *     http://www.apache.org/licenses/LICENSE-2.0
009  *
010  * Unless required by applicable law or agreed to in writing, software
011  * distributed under the License is distributed on an "AS IS" BASIS,
012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  * See the License for the specific language governing permissions and
014  * limitations under the License.
015  */
016 package griffon.builder.swing
017 
018 import ca.odell.glazedlists.EventList
019 import ca.odell.glazedlists.SortedList
020 import ca.odell.glazedlists.TextFilterator
021 import ca.odell.glazedlists.TreeList
022 import ca.odell.glazedlists.gui.AbstractTableComparatorChooser
023 import ca.odell.glazedlists.swing.AutoCompleteSupport
024 import ca.odell.glazedlists.swing.EventListJXTableSorting
025 import ca.odell.glazedlists.swing.EventSelectionModel
026 import ca.odell.glazedlists.swing.TableComparatorChooser
027 import ca.odell.glazedlists.swing.TreeTableSupport
028 import griffon.builder.swing.factory.EventComboBoxModelFactory
029 import griffon.builder.swing.factory.EventJXTableModelFactory
030 import griffon.builder.swing.factory.EventListModelFactory
031 import griffon.builder.swing.factory.EventTableModelFactory
032 import griffon.builder.swing.factory.EventTreeModelFactory
033 import griffon.builder.swing.factory.EventTreeModelUpdateFactory
034 import griffon.inject.DependsOn
035 import org.codehaus.griffon.runtime.groovy.view.AbstractBuilderCustomizer
036 
037 import javax.inject.Named
038 import javax.swing.JComboBox
039 import javax.swing.JTable
040 import javax.swing.ListSelectionModel
041 import java.text.Format
042 
043 /**
044  @author Andres Almiray
045  */
046 @DependsOn('glazedlists-core')
047 @Named('glazedlists-swing')
048 class GlazedlistsSwingBuilderCustomizer extends AbstractBuilderCustomizer {
049     GlazedlistsSwingBuilderCustomizer() {
050         setFactories([
051             eventComboBoxModel: new EventComboBoxModelFactory(),
052             eventListModel    : new EventListModelFactory(),
053             eventTreeModel    : new EventTreeModelFactory(),
054             afterEdit         : new EventTreeModelUpdateFactory(),
055             eventTableModel   : new EventTableModelFactory(),
056             eventJXTableModel : new EventJXTableModelFactory()
057         ])
058 
059         setMethods([
060             installTableComparatorChooser     : Map args ->
061                 def params = [target: current, strategy: AbstractTableComparatorChooser.SINGLE_COLUMN+ args
062                 if (!(params.target instanceof JTable)) {
063                     throw new IllegalArgumentException('target: must be a JTable!')
064                 }
065                 if (!(params.source instanceof EventList)) {
066                     throw new IllegalArgumentException("source: must be an EventList!")
067                 }
068                 TableComparatorChooser.install(params.target, params.source, params.strategy)
069             },
070 
071             installTreeTableSupport           : Map args ->
072                 def params = [target: current, index: 1i+ args
073                 if (!(params.target instanceof JTable)) {
074                     throw new IllegalArgumentException('target: must be a JTable!')
075                 }
076                 if (!(params.source instanceof TreeList)) {
077                     throw new IllegalArgumentException('source: must be an TreeList!')
078                 }
079                 TreeTableSupport.install(params.target, params.source, params.index as int)
080             },
081 
082             installComboBoxAutoCompleteSupport: Map args ->
083                 def params = [target: current+ args
084                 if (!(params.target instanceof JComboBox)) {
085                     throw new IllegalArgumentException('target: must be a JComboBox!')
086                 }
087                 if (!(params.items instanceof EventList)) {
088                     throw new IllegalArgumentException('items: must be an EventList!')
089                 }
090                 if (args.textFilterator) {
091                     if (!(params.textFilterator instanceof TextFilterator)) {
092                         throw new IllegalArgumentException("textFilterator: must be an ${TextFilterator.class.name}!")
093                     }
094                     if (args.format) {
095                         if (!(params.format instanceof Format)) {
096                             throw new IllegalArgumentException("format: must be an ${Format.class.name}!")
097                         }
098                         AutoCompleteSupport.install(params.target, params.items, params.textFilterator, params.format)
099                     else {
100                         AutoCompleteSupport.install(params.target, params.items, params.textFilterator)
101                     }
102                 else {
103                     AutoCompleteSupport.install(params.target, params.items)
104                 }
105             },
106 
107             installEventSelectionModel        : Map args ->
108                 def params = [target: current, mode: AbstractTableComparatorChooser.SINGLE_COLUMN+ args
109                 if (!(params.target instanceof JTable)) {
110                     throw new IllegalArgumentException('target: must be a JTable!')
111                 }
112                 if (!(params.source instanceof EventList)) {
113                     throw new IllegalArgumentException('source: must be an EventList!')
114                 }
115                 if (!params.containsKey('selectionMode')) {
116                     params.selectionMode = ListSelectionModel.SINGLE_SELECTION
117                 }
118                 if (!(params.selectionMode instanceof Integer|| !(0..2).contains(params.selectionMode)) {
119                     throw new IllegalArgumentException('source: must be a one of ListSelectionModel.SINGLE_SELECTION, ListSelectionModel.MULTIPLE_INTERVAL_SELECTION or ListSelectionModel.SINGLE_INTERVAL_SELECTION!')
120                 }
121                 def selectionModel = new EventSelectionModel(params.source)
122                 selectionModel.selectionMode = params.selectionMode
123                 params.target.selectionModel = selectionModel
124                 return selectionModel
125             },
126 
127             installJXTableSorting             : Map args ->
128                 def params = [target: current+ args
129                 Class jxtableClass
130                 try {
131                     jxtableClass = Class.forName('org.jdesktop.swingx.JXTable')
132                 catch (e) {
133                 }
134                 if (!(jxtableClass && jxtable.isAssignableFrom(params.target))) {
135                     throw new IllegalArgumentException('target: must be a JXTable!')
136                 }
137                 if (!(params.source instanceof SortedList)) {
138                     throw new IllegalArgumentException('source: must be an SortedList!')
139                 }
140                 if (!params.containsKey('multiple')) {
141                     params.multiple = false
142                 }
143                 def jxts = EventListJXTableSorting.install(params.target, params.source)
144                 jxts.multipleColumnSort = params.multiple
145                 jxts
146             }
147         ])
148     }
149 }