1 54 55 package com.mullassery.act.gui; 56 57 import java.io.File ; 58 59 import javax.swing.JFileChooser ; 60 import javax.swing.JPopupMenu ; 61 import javax.swing.filechooser.FileFilter ; 62 63 import org.w3c.dom.Element ; 64 import org.w3c.dom.NodeList ; 65 66 import com.mullassery.act.util.GUIUtil; 67 import com.mullassery.act.util.XMLUtil; 68 69 74 public class TaskManager { 75 private ACT act; 76 private final JFileChooser jfc; 77 private File lastFile = new File ("."); 78 81 public TaskManager(ACT act) { 82 this.act = act; 83 jfc = new JFileChooser (); 84 jfc.setMultiSelectionEnabled(false); 85 jfc.setAcceptAllFileFilterUsed(true); 86 jfc.addChoosableFileFilter(new FileFilter () { 87 public boolean accept(File f) { 88 return ( 89 f.isDirectory() 90 || (f.isFile() && f.getName().endsWith(".xml"))); 91 } 92 public String getDescription() { 93 return "ACT task list file (*.xml)"; 94 } 95 }); 96 } 97 98 100 public void openTask() { 101 jfc.setDialogTitle("Open a saved task"); 102 jfc.setCurrentDirectory(lastFile); 103 int ret = jfc.showOpenDialog(act); 104 if (ret == JFileChooser.APPROVE_OPTION) { 105 File f = jfc.getSelectedFile(); 106 setLastFile(f); 107 try { 108 Element el = XMLUtil.loadDocument(f, GUIUtil.TASK_LIST); 109 if (el != null && el.hasChildNodes()) { 110 NodeList nl = el.getElementsByTagName(GUIUtil.TASK); 111 Element node = null; 112 JPopupMenu jp = new JPopupMenu ("Select task"); 113 for (int i = 0; i < nl.getLength(); i++) { 114 node = (Element ) nl.item(i); 115 try { 116 if (!act 117 .getTaskMaster() 118 .getAllTasks() 119 .containsKey( 120 node.getAttribute(GUIUtil.TASK_NAME))) { 121 act.getTaskMaster().addTask( 122 node.getAttribute(GUIUtil.TASK_NAME), 123 node.getAttribute(GUIUtil.TASK_CLASS)); 124 } 125 jp.add(new OpenTaskAction(act, node, false)); 126 } catch (Throwable t) { 127 GUIUtil.showErrorMessage( 128 act, 129 "This task was not loaded/ included during startup. " 130 + "Task might have been renamed/ hidden/ deleted in the settings."); 131 } 132 } 133 jp.pack(); 134 jp.show(act, 30, 30); 135 } else { 136 GUIUtil.showErrorMessage( 137 act, 138 "No tasks to load.\n" 139 + "You might have selected an empty or invalid ACT task list file"); 140 } 141 } catch (Exception e) { 142 GUIUtil.showErrorMessage(act, "Error loading tasks.." + e); 143 } 144 } 145 } 146 147 public boolean saveTask(TaskPanel focusedTP) { 148 if (focusedTP == null) { 149 GUIUtil.showErrorMessage(act, "No Task available to save!"); 150 return false; 151 } 152 Element root = null; 153 File f = null; 154 do { 155 jfc.setDialogTitle( 156 "Save " + focusedTP.getTaskName() + " task to.."); 157 jfc.setCurrentDirectory(lastFile); 158 if (jfc.showSaveDialog(act) != JFileChooser.APPROVE_OPTION) { 159 return false; 160 } 161 f = jfc.getSelectedFile(); 162 if (!f.exists()) { 163 String fStr = f.getAbsolutePath(); 164 if (fStr.indexOf('.') == -1) { 165 f = new File (fStr + ".xml"); 167 } 168 } else if (!f.canWrite()) { 169 GUIUtil.showErrorMessage( 170 focusedTP, 171 "The xml file selected is not writable." 172 + "\nIt might be accessed by other programs or must be read-only."); 173 continue; 174 } 175 try { 176 if (f.length() < 1) { 177 root = 178 XMLUtil 179 .getDocumentBuilder() 180 .newDocument() 181 .createElement( 182 GUIUtil.TASK_LIST); 183 } else { 184 root = XMLUtil.loadDocument(f, null); 185 } 186 } catch (Exception e) { 187 } 188 if (root == null 189 || !root.getNodeName().equals(GUIUtil.TASK_LIST)) { 190 GUIUtil.showErrorMessage( 191 focusedTP, 192 "The xml file selected is not a valid ACT task list file." 193 + "\nChoose another or create new."); 194 continue; 195 } 196 setLastFile(f); 197 } while (root == null); 198 199 try { 200 Element vals = 201 (Element ) root.getOwnerDocument().importNode( 202 focusedTP.getTaskValues(), 203 true); 204 if (!vals.hasChildNodes() 206 && !vals.hasAttributes() 207 && !GUIUtil.getConfirmation( 208 focusedTP, 209 "The Task has no propertes set. Do you still want to save?")) { 210 return true; 211 } 212 String saveName = focusedTP.getTaskName(); 213 if (GUIUtil 214 .getConfirmation( 215 focusedTP, 216 "Would you like to save the task (" 217 + saveName 218 + ") by some other name?")) { 219 do { 220 saveName = GUIUtil.getInput(focusedTP, "Save task as.."); 221 if (saveName == null || saveName.length() == 0) { 222 GUIUtil.showErrorMessage( 223 focusedTP, 224 "Cannot save task without a name."); 225 saveName = null; 226 continue; 227 } 228 NodeList nl = root.getElementsByTagName(GUIUtil.TASK); 229 Element node = null; 230 for (int i = 0; i < nl.getLength(); i++) { 231 node = (Element ) nl.item(i); 232 if (node 233 .getAttribute(GUIUtil.TASK_DISPLAY) 234 .equals(saveName.trim())) { 235 if (GUIUtil 236 .getConfirmation( 237 focusedTP, 238 "The name entered already exists in the file. " 239 + "\nWould you like to overwrite?")) { 240 root.removeChild(node); 241 } else { 242 saveName = null; 243 continue; 244 } 245 } 246 } 247 } while (saveName == null); 248 } 249 vals.setAttribute(GUIUtil.TASK_DISPLAY, saveName.trim()); 250 root.appendChild(vals); 251 XMLUtil.saveElement(root, f, false); 252 } catch (Exception e) { 253 GUIUtil.showErrorMessage( 254 focusedTP, 255 "Error while saving.\n" + e.getMessage()); 256 return false; 257 } 258 return true; 259 } 260 261 private void setLastFile(File f) { 263 lastFile = f; 264 if (!lastFile.isDirectory()) 266 lastFile = lastFile.getParentFile(); 267 } 268 269 } 270 | Popular Tags |