1 16 package org.apache.cocoon.forms.formmodel; 17 18 import org.apache.cocoon.forms.event.ActionEvent; 19 import org.apache.cocoon.forms.event.ActionListener; 20 21 29 public abstract class RepeaterActionDefinition extends ActionDefinition { 30 31 private String name = null; 32 33 36 public RepeaterActionDefinition() { 37 } 38 39 43 public RepeaterActionDefinition(String repeaterName) { 44 this.name = repeaterName; 45 } 46 47 50 public void initializeFrom(WidgetDefinition definition) throws Exception { 51 super.initializeFrom(definition); 52 53 if(definition instanceof RepeaterActionDefinition) { 54 RepeaterActionDefinition other = (RepeaterActionDefinition)definition; 55 56 this.name = other.name; 57 58 } else { 59 throw new Exception ("Definition to inherit from is not of the right type! (at "+getLocation()+")"); 60 } 61 } 62 63 public Widget createInstance() { 64 return new RepeaterAction(this); 65 } 66 67 74 public String getRepeaterName() { 75 return this.name; 76 } 77 78 80 86 public static class DeleteRowsActionDefinition extends RepeaterActionDefinition { 87 88 private String selectName; 89 90 public DeleteRowsActionDefinition(String repeaterName, String selectName) { 91 super(repeaterName); 92 this.selectName = selectName; 93 } 94 95 98 public void initializeFrom(WidgetDefinition definition) throws Exception { 99 super.initializeFrom(definition); 100 101 if(definition instanceof DeleteRowsActionDefinition) { 102 DeleteRowsActionDefinition other = (DeleteRowsActionDefinition)definition; 103 104 this.selectName = other.selectName; 105 106 } else { 107 throw new Exception ("Definition to inherit from is not of the right type! (at "+getLocation()+")"); 108 } 109 } 110 111 public boolean hasActionListeners() { 112 return true; 114 } 115 116 public void fireActionEvent(ActionEvent event) { 117 super.fireActionEvent(event); 119 120 Repeater repeater = ((RepeaterAction)event.getSource()).getRepeater(); 122 for (int i = repeater.getSize() - 1; i >= 0; i--) { 123 Repeater.RepeaterRow row = repeater.getRow(i); 124 if (Boolean.TRUE.equals(row.getChild(this.selectName).getValue())) { 125 repeater.removeRow(i); 126 } 127 } 128 } 129 } 130 131 133 136 public static class AddRowActionDefinition extends RepeaterActionDefinition { 137 138 public AddRowActionDefinition(String repeaterName) { 139 super(repeaterName); 140 141 this.addActionListener(new ActionListener() { 142 public void actionPerformed(ActionEvent event) { 143 Repeater repeater = ((RepeaterAction)event.getSource()).getRepeater(); 144 repeater.addRow(); 145 } 146 }); 147 } 148 } 149 150 152 156 public static class InsertRowsActionDefinition extends RepeaterActionDefinition { 157 158 private String selectName; 159 160 163 public void initializeFrom(WidgetDefinition definition) throws Exception { 164 super.initializeFrom(definition); 165 166 if(definition instanceof InsertRowsActionDefinition) { 167 InsertRowsActionDefinition other = (InsertRowsActionDefinition)definition; 168 169 this.selectName = other.selectName; 170 171 } else { 172 throw new Exception ("Definition to inherit from is not of the right type! (at "+getLocation()+")"); 173 } 174 } 175 176 public InsertRowsActionDefinition(String repeaterName, String selectWidgetName) { 177 super(repeaterName); 178 this.selectName = selectWidgetName; 179 180 this.addActionListener(new ActionListener() { 181 public void actionPerformed(ActionEvent event) { 182 Repeater repeater = ((RepeaterAction)event.getSource()).getRepeater(); 183 boolean foundSelection = false; 184 for (int i = repeater.getSize() - 1; i >= 0; i--) { 185 Repeater.RepeaterRow row = repeater.getRow(i); 186 Widget selectWidget = row.getChild(selectName); 187 if (Boolean.TRUE.equals(selectWidget.getValue())) { 188 repeater.addRow(i); 190 foundSelection = true; 191 } 192 } 193 194 if (!foundSelection) { 195 repeater.addRow(); 197 } 198 } 199 }); 200 } 201 } 202 203 public static class MoveRowActionDefinition extends RepeaterActionDefinition { 204 205 public MoveRowActionDefinition(String repeaterName) { 206 super(repeaterName); 207 this.addActionListener(new ActionListener() { 208 public void actionPerformed(ActionEvent event) { 209 RepeaterAction.Move move = (RepeaterAction.Move)event.getSource(); 210 Repeater repeater = move.getRepeater(); 211 repeater.moveRow(move.getFrom(), move.getTo()); 212 }; 213 }); 214 } 215 public Widget createInstance() { 216 return new RepeaterAction.Move(this); 217 } 218 } 219 } 220 | Popular Tags |