1 54 55 package com.mullassery.act.gui; 56 57 import java.util.StringTokenizer ; 58 59 import javax.swing.BorderFactory ; 60 import javax.swing.JMenu ; 61 import javax.swing.JMenuItem ; 62 import javax.swing.JSeparator ; 63 64 import org.w3c.dom.Element ; 65 import org.w3c.dom.Node ; 66 import org.w3c.dom.NodeList ; 67 68 import com.mullassery.act.util.DebugUtil; 69 import com.mullassery.act.util.GUIUtil; 70 import com.mullassery.act.util.ResourceUtil; 71 import com.mullassery.act.util.XMLUtil; 72 73 78 public class SettingsManager { 79 public static Element actSettingsElement; 80 81 private ACT act; 82 84 87 public SettingsManager(ACT act) { 88 this.act = act; 89 } 90 91 94 private int addTaskItems(JMenu menu, Element n) { 95 int added = 0; 96 if (n == null || !n.hasChildNodes()) 97 return added; 98 String display = null; 99 String name = null; 100 String visible = null; 101 String clazz = null; 102 Element node = null; 103 JMenuItem menuItem = null; 104 NodeList nTasks = n.getChildNodes(); 105 DebugUtil.debug( 106 "\t" + n.getNodeName() + " has children : " + nTasks.getLength()); 107 for (int i = 0; i < nTasks.getLength(); i++) { 108 try { 109 if (nTasks.item(i).getNodeType() != Node.ELEMENT_NODE 110 || !nTasks.item(i).getNodeName().equals("task")) { 111 continue; 112 } 113 node = (Element ) nTasks.item(i); 114 name = node.getAttribute(GUIUtil.TASK_NAME); 115 display = node.getAttribute(GUIUtil.TASK_DISPLAY); 116 if (display == null || display.length() < 1) { 117 display = name; 118 node.setAttribute(GUIUtil.TASK_DISPLAY, display); 119 } else if (name == null || name.length() < 1) { 120 name = display.toLowerCase(); 121 node.setAttribute(GUIUtil.TASK_NAME, name); 122 } 123 visible = node.getAttribute(GUIUtil.TASK_VISIBLE); 124 clazz = node.getAttribute(GUIUtil.TASK_CLASS); 125 if (clazz != null && clazz.length() > 0) 126 act.getTaskMaster().addTask(name, clazz); 127 128 if (name != null 129 && name.length() > 0 130 && !visible.equalsIgnoreCase("FALSE")) { 131 menuItem = new JMenuItem (new OpenTaskAction(act, node)); 132 if (!act.getTaskMaster().getAllTasks().containsKey(name)) { 133 menuItem.setEnabled(false); 134 menuItem.setToolTipText( 135 getWrappedText( 136 "<b>Cannot use this task since some of its dependencies" 137 + "are NOT available in the CLASSPATH.</b><br><br>" 138 + "[" 139 + node.getAttribute(GUIUtil.TASK_DESCRIPTION) 140 + "]", 141 40)); 142 } else { 143 menuItem.setToolTipText( 144 getWrappedText( 145 "<b>[</b>" 146 + node.getAttribute(GUIUtil.TASK_DESCRIPTION) 147 + "<b>]</b>", 148 40)); 149 } 150 menu.add(menuItem); 151 added++; 152 } 153 } catch (Throwable t) { 154 continue; 155 } 156 } 157 menu.add(new JSeparator ()); 158 JMenu editMenu = new JMenu ("Add/ Edit/ Delete"); 159 menu.add(editMenu); 160 editMenu.add( 161 new JMenuItem ( 162 new TaskAction( 163 act, 164 n, 165 TaskAction.ADD_TASK_LABEL, 166 TaskAction.ADD_TASK))); 167 editMenu.add( 168 new JMenuItem ( 169 new TaskAction( 170 act, 171 n, 172 TaskAction.EDIT_TASK_LABEL, 173 TaskAction.EDIT_TASK))); 174 editMenu.add( 175 new JMenuItem ( 176 new TaskAction( 177 act, 178 n, 179 TaskAction.DELETE_TASK_LABEL, 180 TaskAction.DELETE_TASK))); 181 return added; 182 } 183 184 private String getWrappedText(String str, int lineLength) { 185 StringBuffer sb = new StringBuffer ("<html>"); 186 StringTokenizer st = new StringTokenizer (str, " \t\n\r\f", true); 187 int count = sb.length(); 188 while (st.hasMoreTokens()) { 189 sb.append(st.nextToken()); 190 if (sb.length() >= count + lineLength) { 191 sb.append("<br>"); 192 count += lineLength; 193 } 194 } 195 return sb.toString(); 196 } 197 198 public Element getSettings() { 199 try { 200 if (actSettingsElement != null) 201 return actSettingsElement; 202 actSettingsElement = 203 XMLUtil.getDocumentElement(ResourceUtil.getSettingsFile()); 204 return actSettingsElement; 205 } catch (Exception e) { 206 try { 207 return XMLUtil.loadDocument( 208 this.getClass().getResourceAsStream("act-settings.xml"), 209 "act-settings"); 210 } catch (Exception ex) { 211 return null; 212 } 213 } 214 } 215 216 public Element getMergedSettings() { 217 return null; 218 } 219 220 public JMenu getTaskMenu() { 221 JMenu top = new JMenu ("Select a task to Execute"); 222 top.setBorder(BorderFactory.createEtchedBorder()); 223 Element settings = getSettings(); 224 if (settings == null || !settings.hasChildNodes()) { 225 top.setText("No Tasks. Settings file not found!"); 226 return top; 227 } 228 229 addGroups(top, settings); 230 return top; 231 } 232 233 private int addGroups(JMenu jm, Element group) { 234 NodeList nGroups = group.getChildNodes(); 235 DebugUtil.debug( 236 "\t" 237 + group.getNodeName() 238 + " has children : " 239 + nGroups.getLength()); 240 int children = 0; 241 for (int i = 0; i < nGroups.getLength(); i++) { 243 try { 244 if (nGroups.item(i).getNodeType() != Node.ELEMENT_NODE 245 || !nGroups.item(i).getNodeName().equals("task-group")) { 246 continue; 247 } 248 Element node = (Element ) nGroups.item(i); 249 String name = node.getAttribute(GUIUtil.TASK_DISPLAY); 250 String visible = node.getAttribute(GUIUtil.TASK_VISIBLE); 251 if (name != null 252 && name.length() > 0 253 && !visible.equalsIgnoreCase("FALSE")) { 254 DebugUtil.debug( 255 "Adding group " + name + " to " + jm.getText()); 256 JMenu groupMenu = new JMenu (name); 257 jm.add(groupMenu); 258 children += addGroups(groupMenu, node); 259 } 260 } catch (Throwable t) { 261 continue; 262 } 263 } 264 children += addTaskItems(jm, group); 265 jm.setText(jm.getText() + " (" + children + ")"); 266 return children; 267 } 268 269 public void saveSettings() { 270 if (actSettingsElement != null) { 271 try { 272 XMLUtil.saveElement( 273 actSettingsElement, 274 ResourceUtil.getSettingsFile(), 275 false); 276 } catch (Exception e) { } 278 } 279 } 280 } 281 | Popular Tags |