|
@@ -15,128 +15,142 @@ |
15 |
15 |
|
*/ |
16 |
16 |
|
package com.google.gwt.gen2.table.client; |
17 |
17 |
|
|
18 |
|
- |
import com.google.gwt.core.client.GWT; |
19 |
|
- |
import com.google.gwt.user.client.ui.RadioButton; |
20 |
|
- |
import com.google.gwt.user.client.ui.VerticalPanel; |
21 |
|
- |
import com.google.gwt.user.client.ui.Widget; |
22 |
|
- |
|
23 |
|
- |
import java.util.HashMap; |
24 |
|
- |
import java.util.Iterator; |
25 |
|
- |
import java.util.Map; |
|
18 |
+ |
import java.util.*; |
|
19 |
+ |
|
|
20 |
+ |
import com.google.gwt.core.client.*; |
|
21 |
+ |
import com.google.gwt.user.client.ui.*; |
26 |
22 |
|
|
27 |
23 |
|
/** |
28 |
24 |
|
* An {@link InlineCellEditor} that allows the user to select a {@link String} |
29 |
25 |
|
* from a list of {@link RadioButton RadioButtons}. |
30 |
|
- |
* |
|
26 |
+ |
* |
31 |
27 |
|
* @param <ColType> the data type of the column |
32 |
28 |
|
*/ |
33 |
|
- |
public class RadioCellEditor<ColType> extends InlineCellEditor<ColType> { |
34 |
|
- |
/** |
35 |
|
- |
* A mapping of {@link RadioButton RadioButtons} to the value for the buttons. |
36 |
|
- |
*/ |
37 |
|
- |
private Map<RadioButton, ColType> radioMap = new HashMap<RadioButton, ColType>(); |
38 |
|
- |
|
39 |
|
- |
/** |
40 |
|
- |
* The vertical panel used to layout the contents. |
41 |
|
- |
*/ |
42 |
|
- |
private VerticalPanel vpanel = new VerticalPanel(); |
43 |
|
- |
|
44 |
|
- |
/** |
45 |
|
- |
* Construct a new {@link RadioCellEditor}. |
46 |
|
- |
*/ |
47 |
|
- |
public RadioCellEditor() { |
48 |
|
- |
this(GWT.<InlineCellEditorImages> create(InlineCellEditorImages.class)); |
49 |
|
- |
} |
50 |
|
- |
|
51 |
|
- |
/** |
52 |
|
- |
* Construct a new {@link RadioCellEditor} with the specified images. |
53 |
|
- |
* |
54 |
|
- |
* @param images the images to use for the accept/cancel buttons |
55 |
|
- |
*/ |
56 |
|
- |
public RadioCellEditor(InlineCellEditorImages images) { |
57 |
|
- |
this(new VerticalPanel(), images); |
58 |
|
- |
} |
59 |
|
- |
|
60 |
|
- |
/** |
61 |
|
- |
* Constructor. |
62 |
|
- |
* |
63 |
|
- |
* @param images the images to use for the accept/cancel buttons |
64 |
|
- |
*/ |
65 |
|
- |
private RadioCellEditor(VerticalPanel vPanel, InlineCellEditorImages images) { |
66 |
|
- |
super(vPanel, images); |
67 |
|
- |
this.vpanel = vPanel; |
68 |
|
- |
} |
69 |
|
- |
|
70 |
|
- |
/** |
71 |
|
- |
* Add a {@link RadioButton} to the editor. |
72 |
|
- |
* |
73 |
|
- |
* @param radio the radio button to add |
74 |
|
- |
* @param value the value associated with the {@link RadioButton} |
75 |
|
- |
*/ |
76 |
|
- |
public void addRadioButton(RadioButton radio, ColType value) { |
77 |
|
- |
vpanel.add(radio); |
78 |
|
- |
radioMap.put(radio, value); |
79 |
|
- |
} |
80 |
|
- |
|
81 |
|
- |
@Override |
82 |
|
- |
public void editCell(CellEditInfo cellEditInfo, ColType cellValue, |
83 |
|
- |
Callback<ColType> callback) { |
84 |
|
- |
super.editCell(cellEditInfo, cellValue, callback); |
85 |
|
- |
Iterator<Widget> it = vpanel.iterator(); |
86 |
|
- |
while (it.hasNext()) { |
87 |
|
- |
RadioButton radio = (RadioButton) it.next(); |
88 |
|
- |
if (radio.getValue()) { |
89 |
|
- |
radio.setFocus(true); |
90 |
|
- |
return; |
91 |
|
- |
} |
92 |
|
- |
} |
93 |
|
- |
} |
94 |
|
- |
|
95 |
|
- |
/** |
96 |
|
- |
* Insert a {@link RadioButton} at the specified index. |
97 |
|
- |
* |
98 |
|
- |
* @param radio the radio button to add |
99 |
|
- |
* @param value the value associated with the {@link RadioButton} |
100 |
|
- |
* @param beforeIndex the index before which it will be inserted |
101 |
|
- |
* @throws IndexOutOfBoundsException if <code>beforeIndex</code> is out of |
102 |
|
- |
* range |
103 |
|
- |
*/ |
104 |
|
- |
public void insertRadioButton(RadioButton radio, ColType value, |
105 |
|
- |
int beforeIndex) throws IndexOutOfBoundsException { |
106 |
|
- |
vpanel.insert(radio, beforeIndex); |
107 |
|
- |
radioMap.put(radio, value); |
108 |
|
- |
} |
109 |
|
- |
|
110 |
|
- |
/** |
111 |
|
- |
* Remove a {@link RadioButton} from the editor. |
112 |
|
- |
* |
113 |
|
- |
* @param radio the radio button to remove |
114 |
|
- |
*/ |
115 |
|
- |
public void removeRadioButton(RadioButton radio) { |
116 |
|
- |
vpanel.remove(radio); |
117 |
|
- |
radioMap.remove(radio); |
118 |
|
- |
} |
119 |
|
- |
|
120 |
|
- |
@Override |
121 |
|
- |
protected ColType getValue() { |
122 |
|
- |
Iterator<Widget> it = vpanel.iterator(); |
123 |
|
- |
while (it.hasNext()) { |
124 |
|
- |
RadioButton radio = (RadioButton) it.next(); |
125 |
|
- |
if (radio.getValue()) { |
126 |
|
- |
return radioMap.get(radio); |
127 |
|
- |
} |
128 |
|
- |
} |
129 |
|
- |
return null; |
130 |
|
- |
} |
131 |
|
- |
|
132 |
|
- |
@Override |
133 |
|
- |
protected void setValue(ColType cellValue) { |
134 |
|
- |
for (Map.Entry<RadioButton, ColType> entry : radioMap.entrySet()) { |
135 |
|
- |
if (entry.getValue().equals(cellValue)) { |
136 |
|
- |
entry.getKey().setValue(true); |
137 |
|
- |
} else { |
138 |
|
- |
entry.getKey().setValue(false); |
139 |
|
- |
} |
|
29 |
+ |
public class RadioCellEditor<ColType> extends InlineCellEditor<ColType> |
|
30 |
+ |
{ |
|
31 |
+ |
/** |
|
32 |
+ |
* A mapping of {@link RadioButton RadioButtons} to the value for the buttons. |
|
33 |
+ |
*/ |
|
34 |
+ |
private Map<RadioButton, ColType> radioMap = new HashMap<RadioButton, ColType>(); |
|
35 |
+ |
|
|
36 |
+ |
/** |
|
37 |
+ |
* The vertical panel used to layout the contents. |
|
38 |
+ |
*/ |
|
39 |
+ |
private VerticalPanel vpanel = new VerticalPanel(); |
|
40 |
+ |
|
|
41 |
+ |
/** |
|
42 |
+ |
* Construct a new {@link RadioCellEditor}. |
|
43 |
+ |
*/ |
|
44 |
+ |
public RadioCellEditor() |
|
45 |
+ |
{ |
|
46 |
+ |
this( GWT.<InlineCellEditorImages>create( InlineCellEditorImages.class ) ); |
|
47 |
+ |
} |
|
48 |
+ |
|
|
49 |
+ |
/** |
|
50 |
+ |
* Construct a new {@link RadioCellEditor} with the specified images. |
|
51 |
+ |
* |
|
52 |
+ |
* @param images the images to use for the accept/cancel buttons |
|
53 |
+ |
*/ |
|
54 |
+ |
public RadioCellEditor( InlineCellEditorImages images ) |
|
55 |
+ |
{ |
|
56 |
+ |
this( new VerticalPanel(), images ); |
|
57 |
+ |
} |
|
58 |
+ |
|
|
59 |
+ |
/** |
|
60 |
+ |
* Constructor. |
|
61 |
+ |
* |
|
62 |
+ |
* @param images the images to use for the accept/cancel buttons |
|
63 |
+ |
*/ |
|
64 |
+ |
private RadioCellEditor( VerticalPanel vPanel, InlineCellEditorImages images ) |
|
65 |
+ |
{ |
|
66 |
+ |
super( vPanel, images ); |
|
67 |
+ |
this.vpanel = vPanel; |
|
68 |
+ |
} |
|
69 |
+ |
|
|
70 |
+ |
/** |
|
71 |
+ |
* Add a {@link RadioButton} to the editor. |
|
72 |
+ |
* |
|
73 |
+ |
* @param radio the radio button to add |
|
74 |
+ |
* @param value the value associated with the {@link RadioButton} |
|
75 |
+ |
*/ |
|
76 |
+ |
public void addRadioButton( RadioButton radio, ColType value ) |
|
77 |
+ |
{ |
|
78 |
+ |
vpanel.add( radio ); |
|
79 |
+ |
radioMap.put( radio, value ); |
|
80 |
+ |
} |
|
81 |
+ |
|
|
82 |
+ |
@Override |
|
83 |
+ |
public void editCell( CellEditInfo cellEditInfo, ColType cellValue, Callback<ColType> callback ) |
|
84 |
+ |
{ |
|
85 |
+ |
super.editCell( cellEditInfo, cellValue, callback ); |
|
86 |
+ |
Iterator<Widget> it = vpanel.iterator(); |
|
87 |
+ |
while ( it.hasNext() ) |
|
88 |
+ |
{ |
|
89 |
+ |
RadioButton radio = (RadioButton) it.next(); |
|
90 |
+ |
if ( radio.getValue() ) |
|
91 |
+ |
{ |
|
92 |
+ |
radio.setFocus( true ); |
|
93 |
+ |
return; |
|
94 |
+ |
} |
|
95 |
+ |
} |
|
96 |
+ |
} |
|
97 |
+ |
|
|
98 |
+ |
/** |
|
99 |
+ |
* Insert a {@link RadioButton} at the specified index. |
|
100 |
+ |
* |
|
101 |
+ |
* @param radio the radio button to add |
|
102 |
+ |
* @param value the value associated with the {@link RadioButton} |
|
103 |
+ |
* @param beforeIndex the index before which it will be inserted |
|
104 |
+ |
* |
|
105 |
+ |
* @throws IndexOutOfBoundsException if <code>beforeIndex</code> is out of |
|
106 |
+ |
* range |
|
107 |
+ |
*/ |
|
108 |
+ |
public void insertRadioButton( RadioButton radio, ColType value, int beforeIndex ) |
|
109 |
+ |
throws IndexOutOfBoundsException |
|
110 |
+ |
{ |
|
111 |
+ |
vpanel.insert( radio, beforeIndex ); |
|
112 |
+ |
radioMap.put( radio, value ); |
|
113 |
+ |
} |
|
114 |
+ |
|
|
115 |
+ |
/** |
|
116 |
+ |
* Remove a {@link RadioButton} from the editor. |
|
117 |
+ |
* |
|
118 |
+ |
* @param radio the radio button to remove |
|
119 |
+ |
*/ |
|
120 |
+ |
public void removeRadioButton( RadioButton radio ) |
|
121 |
+ |
{ |
|
122 |
+ |
vpanel.remove( radio ); |
|
123 |
+ |
radioMap.remove( radio ); |
|
124 |
+ |
} |
|
125 |
+ |
|
|
126 |
+ |
@Override |
|
127 |
+ |
protected ColType getValue() |
|
128 |
+ |
{ |
|
129 |
+ |
Iterator<Widget> it = vpanel.iterator(); |
|
130 |
+ |
while ( it.hasNext() ) |
|
131 |
+ |
{ |
|
132 |
+ |
RadioButton radio = (RadioButton) it.next(); |
|
133 |
+ |
if ( radio.getValue() ) |
|
134 |
+ |
{ |
|
135 |
+ |
return radioMap.get( radio ); |
|
136 |
+ |
} |
|
137 |
+ |
} |
|
138 |
+ |
return null; |
|
139 |
+ |
} |
|
140 |
+ |
|
|
141 |
+ |
@Override |
|
142 |
+ |
protected void setValue( ColType cellValue ) |
|
143 |
+ |
{ |
|
144 |
+ |
for ( Map.Entry<RadioButton, ColType> entry : radioMap.entrySet() ) |
|
145 |
+ |
{ |
|
146 |
+ |
if ( entry.getValue().equals( cellValue ) ) |
|
147 |
+ |
{ |
|
148 |
+ |
entry.getKey().setValue( true ); |
|
149 |
+ |
} |
|
150 |
+ |
else |
|
151 |
+ |
{ |
|
152 |
+ |
entry.getKey().setValue( false ); |
|
153 |
+ |
} |
|
154 |
+ |
} |
140 |
155 |
|
} |
141 |
|
- |
} |
142 |
156 |
|
} |