1 54 55 package com.mullassery.act.gui; 56 57 import javax.swing.JComponent ; 58 import javax.swing.JMenu ; 59 import javax.swing.JSeparator ; 60 61 import org.w3c.dom.Document ; 62 import org.w3c.dom.Element ; 63 import org.w3c.dom.NodeList ; 64 65 import com.mullassery.act.util.DebugUtil; 66 import com.mullassery.act.util.GUIUtil; 67 import com.mullassery.act.util.ResourceUtil; 68 import com.mullassery.act.util.XMLUtil; 69 70 75 final class MRUManager { 76 77 public static final int MRU_LIST_MAX = 4; 78 private ACT act; 79 private Element mru; 80 private boolean changeMenu = true; 81 private boolean mruChanged = false; 82 private JComponent [] mruItems; 83 86 public MRUManager(ACT act) { 87 this.act = act; 88 try { 89 mru = XMLUtil.loadDocument(ResourceUtil.getMRUFile(), "mru-tasks"); 90 } catch (Exception e) { 91 } 93 } 94 95 private void importTask(Element vals, String sName) { 96 Document doc = mru.getOwnerDocument(); 97 Element m = (Element ) doc.importNode(vals, true); 98 m.setAttribute(GUIUtil.TASK_DISPLAY, sName); 99 addTask(m); 100 } 101 102 private void addTask(Element m) { 103 if (mru.hasChildNodes()) 104 mru.insertBefore(m, mru.getFirstChild()); 105 else 106 mru.appendChild(m); 107 108 NodeList nList = mru.getElementsByTagName("task"); 109 while (nList.getLength() > MRU_LIST_MAX) { 110 mru.removeChild(nList.item(nList.getLength() - 1)); 112 nList = mru.getElementsByTagName("task"); 113 } 114 115 changeMenu = true; 116 mruChanged = true; 117 } 118 119 public void addToMRU(TaskPanel tp) { 120 Element vals = tp.getTaskValues(); 121 String displayName = vals.getAttribute(GUIUtil.TASK_DISPLAY); 122 NodeList nList = mru.getElementsByTagName(GUIUtil.TASK); int maxSuffix = 0; 124 boolean intTaken[] = new boolean[MRU_LIST_MAX+1]; 125 for (int i = 0; i < nList.getLength(); i++) { 127 Element tsk = (Element ) nList.item(i); 128 if (tsk == vals) { mru.removeChild(tsk); 130 addTask(tsk); 131 return; 132 } 133 134 String mName = tsk.getAttribute(GUIUtil.TASK_DISPLAY); 135 if (mName.startsWith(displayName) 136 && mName.charAt(mName.length() - 2) == '_') { 137 try { 138 int temp = 139 Integer.parseInt("" + mName.charAt(mName.length() - 1)) 140 - 1; 141 if (temp >= 0 && temp < MRU_LIST_MAX) 142 intTaken[temp] = true; 143 } catch (Exception ex) { 144 DebugUtil.debug("Error while reading number suffix"); 145 } 146 } 147 } 148 for (int i = 0; i < intTaken.length; i++) { 150 if (!intTaken[i]) { 151 maxSuffix = i; 152 break; 153 } 154 } 155 importTask(vals, displayName + "_" + (maxSuffix + 1)); 156 saveMRU(); } 158 159 public void saveMRU() { 160 if (mru != null && mruChanged) { 161 try { 162 XMLUtil.saveElement(mru, ResourceUtil.getMRUFile(), false); 163 } catch (Exception e) { 164 } 166 } 167 } 168 169 172 public void updateMRU(JMenu fileMenu) { 173 if (!changeMenu) 175 return; 176 if (mruItems != null && mruItems.length > 0) { 177 for (int i = 0; i < mruItems.length; i++) { 178 fileMenu.remove(mruItems[i]); 179 } 180 } 181 if (mru == null || !mru.hasChildNodes()) 182 return; 183 184 NodeList nl = mru.getElementsByTagName("task"); 185 mruItems = new JComponent [nl.getLength() + 2]; 186 mruItems[0] = 187 (JComponent ) fileMenu.add( 188 new JSeparator (), 189 fileMenu.getItemCount() - 1); 190 for (int i = 0; i < nl.getLength(); i++) { 191 Element e = (Element ) nl.item(i); 192 try { 193 mruItems[i + 1] = 194 fileMenu.insert( 195 new OpenTaskAction(act, e, false), 196 fileMenu.getItemCount() - 1); 197 } catch (Exception ex) { 198 GUIUtil.showErrorMessage( 199 act, 200 "Failed task loading " + ex.getMessage()); 201 } 202 } 203 mruItems[mruItems.length - 1] = 204 (JComponent ) fileMenu.add( 205 new JSeparator (), 206 fileMenu.getItemCount() - 1); 207 changeMenu = false; 208 } 209 } 210 | Popular Tags |