1 54 55 package com.mullassery.act.gui; 56 57 import java.awt.BorderLayout ; 58 import java.awt.FlowLayout ; 59 import java.awt.GridLayout ; 60 import java.awt.event.ActionEvent ; 61 import java.awt.event.ActionListener ; 62 import java.util.HashMap ; 63 64 import javax.swing.AbstractAction ; 65 import javax.swing.JButton ; 66 import javax.swing.JDialog ; 67 import javax.swing.JPanel ; 68 69 import org.w3c.dom.Element ; 70 import org.w3c.dom.Node ; 71 import org.w3c.dom.NodeList ; 72 73 import com.mullassery.act.gui.components.TBooleanField; 74 import com.mullassery.act.gui.components.TComboBox; 75 import com.mullassery.act.gui.components.TLabel; 76 import com.mullassery.act.gui.components.TTextField; 77 import com.mullassery.act.util.GUIUtil; 78 79 84 public class TaskAction extends AbstractAction { 85 public static final int ADD_TASK = 0; 86 87 public static final String ADD_TASK_LABEL = "Add new Task"; 88 public static final int DELETE_TASK = 2; 89 public static final String DELETE_TASK_LABEL = "Delete a Task"; 90 public static final int EDIT_TASK = 1; 91 public static final String EDIT_TASK_LABEL = "Edit a Task"; 92 93 ACT act; 94 Element category; 95 private int type; 96 97 TaskAction(ACT act, Element category, String display, int type) { 98 super(display); 99 this.act = act; 100 this.category = category; 101 this.type = type; 102 } 103 104 public void actionPerformed(ActionEvent arg0) { 105 if (type == ADD_TASK) 106 new AddTaskDialog().show(); 107 else 108 new SelectTaskDialog(); 109 } 110 111 private class AddTaskDialog extends JDialog { 112 AddTaskDialog() { 113 this(null); 114 } 115 116 AddTaskDialog(Element val) { 117 super(act, "Enter details of the task"); 118 init(val); 119 } 120 121 private void init(final Element val) { 122 final boolean edit = (val != null); 123 final JPanel jp = new JPanel (new GridLayout (5, 2, 2, 1)); 124 jp.add(new TLabel("Display Name")); 125 final TTextField dt = new TTextField(); 126 if (edit) 127 dt.setText(val.getAttribute(GUIUtil.TASK_DISPLAY)); 128 jp.add(dt); 129 jp.add(new TLabel("Task Name")); 130 final TTextField t = new TTextField(); 131 if (edit) 132 t.setText(val.getAttribute(GUIUtil.TASK_NAME)); 133 jp.add(t); 134 jp.add(new TLabel("Task class")); 135 final TTextField tc = new TTextField(); 136 if (edit) 137 tc.setText(val.getAttribute(GUIUtil.TASK_CLASS)); 138 jp.add(tc); 139 jp.add(new TLabel("Task description")); 140 final TTextField td = new TTextField(); 141 if (edit) 142 td.setText(val.getAttribute(GUIUtil.TASK_DESCRIPTION)); 143 jp.add(td); 144 jp.add(new TLabel("Show/ Hide")); 145 final TBooleanField tv = new TBooleanField(); 146 if (edit 147 && "false".equalsIgnoreCase( 148 val.getAttribute(GUIUtil.TASK_VISIBLE))) 149 tv.setText("false"); 150 else 151 tv.setText("true"); 152 jp.add(tv); 153 getContentPane().add(jp); 154 155 final JPanel bb = new JPanel (new FlowLayout (FlowLayout.CENTER)); 156 final JButton cancel = new JButton ("Cancel"); 157 cancel.addActionListener(new ActionListener () { 158 public void actionPerformed(ActionEvent evt) { 159 AddTaskDialog.this.dispose(); 160 } 161 }); 162 bb.add(cancel); 163 final JButton ok = new JButton ("OK"); 164 ok.addActionListener( 165 new EditActionListener(this, tc, td, tv, t, dt, val)); 166 bb.add(ok); 167 getContentPane().add(bb, BorderLayout.SOUTH); 168 169 pack(); 170 setLocationRelativeTo(act); 171 show(); 172 } 173 } 174 175 private final class EditActionListener implements ActionListener { 176 private final TTextField dt; 177 private final JDialog par; 178 private final TTextField t; 179 private final TTextField tc; 180 private final TTextField td; 181 private final TBooleanField tv; 182 private final Element val; 183 private EditActionListener( 184 JDialog par, 185 TTextField tc, 186 TTextField td, 187 TBooleanField tv, 188 TTextField t, 189 TTextField dt, 190 Element val) { 191 super(); 192 this.par = par; 193 this.tc = tc; 194 this.td = td; 195 this.tv = tv; 196 this.t = t; 197 this.dt = dt; 198 this.val = val; 199 } 200 201 public void actionPerformed(ActionEvent evt) { 202 final String tStr = t.getText(); final String tcStr = tc.getText(); final String dtStr = dt.getText(); final String tdStr = td.getText(); final String tvStr = tv.getText(); 208 if (tStr == null || tStr.length() < 1) { 209 GUIUtil.showErrorMessage( 210 par, 211 "Please specify a valid task name."); 212 return; 213 } 214 215 try { 216 if (tcStr == null || tcStr.length() < 1) { 217 if (!GUIUtil 218 .getConfirmation( 219 par, 220 "Is the task part of the default ANT distribution (core or optional)?")) { 221 GUIUtil.showErrorMessage( 222 par, 223 "Full class-name should be specified for a custom task."); 224 return; 225 } 226 } else { 227 act.getTaskMaster().addTask(tStr, tcStr); 228 } 229 Element newTask = val; 230 if (val == null) 231 newTask = category.getOwnerDocument().createElement("task"); 232 if (dtStr != null) 234 newTask.setAttribute(GUIUtil.TASK_DISPLAY, dtStr); 235 if (tStr != null) 236 newTask.setAttribute(GUIUtil.TASK_NAME, tStr); 237 if (tcStr != null) 238 newTask.setAttribute(GUIUtil.TASK_CLASS, tcStr); 239 if (tdStr != null) 240 newTask.setAttribute(GUIUtil.TASK_DESCRIPTION, tdStr); 241 if (tvStr != null && tvStr.equalsIgnoreCase("false")) 242 newTask.setAttribute(GUIUtil.TASK_VISIBLE, tvStr); 243 else 244 newTask.removeAttribute(GUIUtil.TASK_VISIBLE); 245 246 if (val == null) { 247 category.appendChild(newTask); 248 } 249 act.reStart(); 250 par.dispose(); 251 } catch (Exception e) { 252 GUIUtil.showErrorMessage( 253 par, 254 "Could not edit/ add the Task. Please fill all fields with valid values. " 255 + e.getMessage()); 256 } 257 } 258 } 259 260 private final class RemoveActionListener implements ActionListener { 261 private final TComboBox cb; 262 private final JDialog par; 263 private RemoveActionListener(JDialog par, TComboBox cb) { 264 super(); 265 this.cb = cb; 266 this.par = par; 267 } 268 public void actionPerformed(ActionEvent evt) { 269 final Element t = (Element ) cb.getItemValue(cb.getText()); 270 if (t != null) { 271 try { 272 act.getTaskMaster().removeTask( 273 t.getAttribute(GUIUtil.TASK_NAME)); 274 category.removeChild(t); 275 act.reStart(); 276 par.dispose(); 277 } catch (Exception e) { 278 GUIUtil.showErrorMessage( 279 par, 280 "Could not remove the Task. " + e.getMessage()); 281 } 282 } 283 } 284 } 285 286 private final class SelectEditActionListener implements ActionListener { 287 private final TComboBox cb; 288 private final JDialog par; 289 private SelectEditActionListener(JDialog par, TComboBox cb) { 290 super(); 291 this.cb = cb; 292 this.par = par; 293 } 294 public void actionPerformed(ActionEvent evt) { 295 final Element t = (Element ) cb.getItemValue(cb.getText()); 296 if (t != null) { 297 try { 298 new AddTaskDialog(t).show(); 299 par.dispose(); 300 } catch (Exception e) { 301 GUIUtil.showErrorMessage( 302 par, 303 "Could not select the Task. " + e.getMessage()); 304 } 305 } 306 } 307 } 308 309 private class SelectTaskDialog extends JDialog { 310 311 SelectTaskDialog() { 312 super(act, "Select the task to remove/hide"); 313 init(); 314 } 315 316 private void init() { 317 HashMap tasks = new HashMap (); 318 int count = 0; 319 NodeList nl = category.getChildNodes(); 320 for (int i = 0; i < nl.getLength(); i++) { 321 if (nl.item(i).getNodeType() != Node.ELEMENT_NODE 322 || !nl.item(i).getNodeName().equals("task")) { 323 continue; 324 } 325 Element child = (Element ) nl.item(i); 326 tasks.put(child.getAttribute(GUIUtil.TASK_DISPLAY), child); 327 count++; 328 } 329 if (count == 0) { 330 GUIUtil.showInfoMessage( 331 this, 332 "No Tasks in this group to edit/ delete!"); 333 return; 334 } 335 final JPanel jp = new JPanel (new GridLayout (1, 2, 2, 1)); 336 337 jp.add(new TLabel("Select Task")); 338 final TComboBox cb = 339 new TComboBox(tasks, type == EDIT_TASK ? "Edit" : "Delete"); 340 jp.add(cb); 341 342 getContentPane().add(jp); 343 if (type == EDIT_TASK) 344 cb.addActionListener(new SelectEditActionListener(this, cb)); 345 else 346 cb.addActionListener(new RemoveActionListener(this, cb)); 347 pack(); 348 setLocationRelativeTo(act); 349 show(); 350 } 351 } 352 353 } 354 | Popular Tags |