1 19 package org.openbravo.erpCommon.businessUtility; 20 21 import org.openbravo.erpCommon.utility.Utility; 22 import org.openbravo.utils.FormatUtilities; 23 import org.openbravo.database.ConnectionProvider; 24 import org.openbravo.base.secureApp.VariablesSecureApp; 25 import java.util.*; 26 import org.apache.log4j.Logger ; 27 28 29 public class WindowTabs { 30 static Logger log4j = Logger.getLogger(WindowTabs.class); 31 private VariablesSecureApp vars; 32 private ConnectionProvider conn; 33 private String className = ""; 34 private String TabID = ""; 35 private String WindowID = ""; 36 private String Title = ""; 37 private String ID = ""; 38 private String action = ""; 39 private int level = 0; 40 private Hashtable<String , Stack<WindowTabsData>> tabs = new Hashtable<String , Stack<WindowTabsData>>(); 41 private Stack<WindowTabsData> breadcrumb = new Stack<WindowTabsData>(); 42 43 public WindowTabs(ConnectionProvider _conn, VariablesSecureApp _vars, String _tabId, String _windowId) throws Exception { 44 if (_conn==null || _vars==null || _tabId==null || _tabId.equals("") || _windowId==null || _windowId.equals("")) throw new Exception ("Missing parameters"); 45 this.conn = _conn; 46 this.vars = _vars; 47 this.TabID = _tabId; 48 this.WindowID = _windowId; 49 this.action = "W"; 50 this.Title = WindowTabsData.selectWindowInfo(this.conn, this.vars.getLanguage(), this.WindowID); 51 getTabs(); 52 } 53 54 public WindowTabs(ConnectionProvider _conn, VariablesSecureApp _vars, String _className) throws Exception { 55 if (_conn==null || _vars==null || _className==null || _className.equals("")) throw new Exception ("Missing parameters"); 56 this.conn = _conn; 57 this.vars = _vars; 58 this.className = _className; 59 getWindowInfo(); 60 getTabs(); 61 } 62 63 private void getWindowInfo() throws Exception { 64 WindowTabsData[] windowInfo = WindowTabsData.selectJavaInfo(this.conn, this.className); 65 if (windowInfo==null || windowInfo.length==0) { 66 log4j.warn("Error while trying to obtain window info for class: " + this.className); 67 return; 68 } 69 this.TabID = windowInfo[0].adTabId; 70 this.action = windowInfo[0].action; 71 this.WindowID = windowInfo[0].tabname; 72 windowInfo = WindowTabsData.selectMenuInfo(this.conn, this.vars.getLanguage(), this.action, this.TabID); 73 if (windowInfo==null || windowInfo.length==0) { 74 log4j.warn("Error while trying to obtain window info for class: " + this.className); 75 return; 76 } 77 this.ID = windowInfo[0].id; 78 this.Title = windowInfo[0].name; 79 } 80 81 private String getMenuInfo() throws Exception { 82 if (this.action.equals("W")) return WindowTabsData.selectMenu(this.conn, this.vars.getLanguage(), this.WindowID); 83 else return WindowTabsData.selectMenuManual(this.conn, this.vars.getLanguage(), this.ID); 84 } 85 86 private void getTabs() throws Exception { 87 WindowTabsData[] tabsAux = null; 88 if (this.action.equals("W")) tabsAux = WindowTabsData.select(this.conn, this.vars.getLanguage(), this.WindowID); 89 else tabsAux = WindowTabsData.selectManual(this.conn, this.TabID, this.vars.getLanguage(), this.ID); 90 int pos=-1; 91 if (tabsAux==null || tabsAux.length==0) { 92 log4j.warn("Error while trying to obtain tabs for id: " + this.TabID); 93 return; 94 } 95 for (int i=0;i<tabsAux.length;i++) { 96 if (tabsAux[i].adTabId.equals(this.TabID)) { 97 pos = i; 98 this.level = Integer.valueOf(tabsAux[i].tablevel).intValue(); 99 break; 100 } 101 } 102 if (pos==-1) throw new Exception ("Error while trying to locate the tab: " + this.TabID); 103 if (pos < tabsAux.length-1) { 104 if (Integer.valueOf(tabsAux[pos+1].tablevel).intValue() > this.level) getTabsByLevel(tabsAux, pos+1, false); 105 else getTabsByLevel(tabsAux, pos, true); 106 } else getTabsByLevel(tabsAux, pos, true); 107 } 108 109 private void getTabsByLevel(WindowTabsData[] tabsAux, int pos, boolean register) throws Exception { 110 if (register) { 111 tabsAux[pos].isbreadcrumb = "Y"; 112 this.breadcrumb.push(tabsAux[pos]); 113 } 114 Stack<WindowTabsData> aux = new Stack<WindowTabsData>(); 115 aux.push(tabsAux[pos]); 116 String actualLevel = tabsAux[pos].tablevel; 117 for (int i=pos+1;i<tabsAux.length;i++) { 118 if (tabsAux[pos].tablevel.equals(tabsAux[i].tablevel)) aux.push(tabsAux[i]); 119 else if (Integer.valueOf(tabsAux[pos].tablevel).intValue() > Integer.valueOf(tabsAux[i].tablevel).intValue()) break; 120 } 121 int nextPos = -1; 122 Stack<WindowTabsData> result = new Stack<WindowTabsData>(); 123 while (!aux.empty()) result.push(aux.pop()); 124 for (int i=pos-1;i>=0;i--) { 125 if (tabsAux[pos].tablevel.equals(tabsAux[i].tablevel)) result.push(tabsAux[i]); 126 else if (Integer.valueOf(tabsAux[pos].tablevel).intValue() > Integer.valueOf(tabsAux[i].tablevel).intValue()) { 127 nextPos = i; 128 break; 129 } 130 } 131 if (result.empty()) throw new Exception ("Level missed for tab: " + this.TabID + " in level: " + tabsAux[pos].tablevel); 132 this.tabs.put(actualLevel, result); 133 if (nextPos!=-1) getTabsByLevel(tabsAux, nextPos, true); 134 } 135 136 public String parentTabs() { 137 StringBuffer text = new StringBuffer (); 138 if (this.tabs==null) return text.toString(); 139 String strShowAcct = "N"; 140 String strShowTrl = "N"; 141 try { 142 strShowAcct = Utility.getContext(this.conn, this.vars, "#ShowAcct", this.WindowID); 143 strShowTrl = Utility.getContext(this.conn, this.vars, "#ShowTrl", this.WindowID); 144 } catch (Exception ex) { 145 ex.printStackTrace(); 146 log4j.error(ex); 147 } 148 boolean isFirst = true; 149 boolean hasParent = (this.level>0); 150 if (!hasParent) return text.append("<td class=\"tabBackGroundInit\"></td>").toString(); 151 for (int i = 0;i<this.level;i++) { 152 Stack<WindowTabsData> aux = this.tabs.get(Integer.toString(i)); 153 if (aux==null) continue; 154 if (!isFirst) text.append("</tr>\n<tr>\n"); 155 if (isFirst) text.append("<td class=\"tabBackGroundInit\"><div><span class=\"tabTitle\"><div><span>").append(this.Title).append("</span></div></span></div></td></tr><tr><td class=\"tabBackGround\">"); 156 else text.append("<td class=\"tabBackGround\">"); 157 if (isFirst) text.append(" <div class=\"marginLeft\">\n"); 158 else { 159 text.append(" <table class=\"tabTable\"><tr>\n"); 160 text.append(" <td valign=\"top\"><span class=\"tabLeft\"> </span></td>\n"); 161 text.append(" <td class=\"tabPaneBackGround\">\n");} 162 boolean isFirstTab = true; 163 if (this.action.equals("W")) { 164 while (!aux.empty()) { 165 WindowTabsData data = aux.pop(); 166 if (!data.adTabId.equals(this.TabID) && strShowAcct.equals("N") && data.isinfotab.equals("Y")) continue; 167 else if (!data.adTabId.equals(this.TabID) && strShowTrl.equals("N") && data.istranslationtab.equals("Y")) continue; 168 if (!isFirstTab) text.append("<span class=\"tabSeparator\"> </span>\n"); 169 text.append((isFirstTab && !((data.adTabId.equals(this.TabID) || data.isbreadcrumb.equals("Y"))))?"<div class=\"marginLeft1\">":"<div>").append("<span class=\"dojoTab").append(((data.adTabId.equals(this.TabID) || data.isbreadcrumb.equals("Y"))?(isFirst?" dojoTabparentfirst":" dojoTabparent"):"")).append("\">"); 170 text.append("<div><span><a class=\"dojoTabLink\" HREF=\"#\" onclick=\""); 171 text.append(getUrlCommand(data.adTabId, data.name, Integer.valueOf(data.tablevel).intValue())); 172 text.append("\" onMouseOver=\"return true;\" onMouseOut=\"return true;\" id=\""+data.tabnameid+"\">").append(data.tabname).append("</a></span></div></span>\n"); 173 isFirstTab = false; 174 } 175 text.append(" </div>\n"); 176 } 177 if (isFirst) text.append(" </div>\n"); 178 else { 179 text.append(" <td valign=\"top\"><span class=\"tabRight\"> </span></td>\n"); 180 text.append(" </tr></table>\n"); 181 } 182 text.append("</td>\n"); 183 isFirst = false; 184 } 185 return text.toString(); 186 } 187 188 public String mainTabs() { 189 StringBuffer text = new StringBuffer (); 190 if (this.tabs==null) return text.toString(); 191 String strShowAcct = "N"; 192 String strShowTrl = "N"; 193 try { 194 strShowAcct = Utility.getContext(this.conn, this.vars, "#ShowAcct", this.WindowID); 195 strShowTrl = Utility.getContext(this.conn, this.vars, "#ShowTrl", this.WindowID); 196 } catch (Exception ex) { 197 ex.printStackTrace(); 198 log4j.error(ex); 199 } 200 boolean isFirst = true; 201 boolean hasParent = (this.level>0); 202 Stack<WindowTabsData> aux = this.tabs.get(Integer.toString(this.level)); 203 if (aux==null) return text.toString(); 204 if (!hasParent) text.append("<td class=\"tabBackGroundInit\"><div><span class=\"tabTitle\"><div><span>").append(this.Title).append("</span></div></span></div></td></tr><tr>"); 205 text.append("<td class=\"tabBackGround\">\n"); 206 if (!hasParent) text.append(" <div class=\"marginLeft\">\n"); 207 else { 208 text.append(" <table class=\"tabTable\"><tr>\n"); 209 text.append(" <td valign=\"top\"><span class=\"tabLeft\"> </span></td>\n"); 210 text.append(" <td class=\"tabPaneBackGround\">\n");} 211 boolean isFirstTab = true; 212 if (this.action.equals("W")) { 213 while (!aux.empty()) { 214 WindowTabsData data = aux.pop(); 215 if (!data.adTabId.equals(this.TabID) && strShowAcct.equals("N") && data.isinfotab.equals("Y")) continue; 216 else if (!data.adTabId.equals(this.TabID) && strShowTrl.equals("N") && data.istranslationtab.equals("Y")) continue; 217 if (!isFirstTab) text.append("<span class=\"tabSeparator\"> </span>\n"); 218 text.append((isFirstTab && !((data.adTabId.equals(this.TabID) || data.isbreadcrumb.equals("Y"))))?"<div class=\"marginLeft1\">":"<div>").append("<span class=\"").append(((data.adTabId.equals(this.TabID) || data.isbreadcrumb.equals("Y"))?(!hasParent?" dojoTabcurrentfirst":" dojoTabcurrent"):"dojoTab")).append("\">"); 219 text.append("<div><span><a class=\"dojoTabLink\" HREF=\"#\" onclick=\""); 220 text.append(getUrlCommand(data.adTabId, data.name, Integer.valueOf(data.tablevel).intValue())); 221 text.append("\" onMouseOver=\"return true;\" onMouseOut=\"return true;\" id=\""+data.tabnameid+"\">").append(data.tabname).append("</a></span></div></span>\n"); 222 isFirstTab = false; 223 } 224 text.append(" </div>\n"); 225 isFirst = false; 226 } 227 228 if (!hasParent) text.append(" </div>\n"); 229 else { 230 text.append(" <td valign=\"top\"><span class=\"tabRight\"> </span></td>\n"); 231 text.append(" </tr></table>\n"); 232 } 233 text.append("</td>\n"); 234 return text.toString(); 235 } 236 237 public String childTabs() { 238 StringBuffer text = new StringBuffer (); 239 if (this.tabs==null) return text.append("<td class=\"tabTabbarBackGround\"></td>").toString(); 240 String strShowAcct = "N"; 241 String strShowTrl = "N"; 242 try { 243 strShowAcct = Utility.getContext(this.conn, this.vars, "#ShowAcct", this.WindowID); 244 strShowTrl = Utility.getContext(this.conn, this.vars, "#ShowTrl", this.WindowID); 245 } catch (Exception ex) { 246 ex.printStackTrace(); 247 log4j.error(ex); 248 } 249 Stack<WindowTabsData> aux = this.tabs.get(Integer.toString(this.level+1)); 250 if (aux==null) return text.append("<td class=\"tabTabbarBackGround\"></td>").toString(); 251 text.append("<td class=\"tabBackGround\">\n"); 252 text.append(" <table class=\"tabTable\"><tr>\n"); 253 text.append(" <td valign=\"top\"><span class=\"tabLeft\"> </span></td>\n"); 254 text.append(" <td class=\"tabPaneBackGround\"><div class=\"marginLeft1\">\n"); 255 boolean isFirst = true; 256 while (!aux.empty()) { 257 WindowTabsData data = aux.pop(); 258 if (!data.adTabId.equals(this.TabID) && strShowAcct.equals("N") && data.isinfotab.equals("Y")) continue; 259 else if (!data.adTabId.equals(this.TabID) && strShowTrl.equals("N") && data.istranslationtab.equals("Y")) continue; 260 if (!isFirst) text.append("<span class=\"tabSeparator\"> </span>\n"); 261 isFirst = false; 262 text.append("<span class=\"dojoTab").append(((data.adTabId.equals(this.TabID) || data.isbreadcrumb.equals("Y"))?" current":"")).append("\">"); 263 text.append("<div><a class=\"dojoTabLink\" HREF=\"#\" onclick=\""); 264 text.append(getUrlCommand(data.adTabId, data.name, Integer.valueOf(data.tablevel).intValue())); 265 text.append("\" onMouseOver=\"return true;\" onMouseOut=\"return true;\" id=\""+data.tabnameid+"\">").append(data.tabname).append("</a></div></span>\n"); 266 } 267 text.append(" </div></td><td valign=\"top\"><span class=\"tabRight\"> </span></td>\n"); 268 text.append(" </tr></table>\n"); 269 text.append("</td>\n"); 270 return text.toString(); 271 } 272 273 302 303 public String breadcrumb() { 304 StringBuffer text = new StringBuffer (); 305 if (this.breadcrumb==null || this.breadcrumb.empty()) return text.toString(); 306 boolean isFirst = true; 307 try { 308 text.append("<span>").append(getMenuInfo()).append("</span>\n"); 309 text.append(" || \n"); 310 } catch (Exception ex) { 311 ex.printStackTrace(); 312 log4j.error("Failed when trying to get parent menu element for breadcrumb"); 313 } 314 while (!this.breadcrumb.empty()) { 315 WindowTabsData data = this.breadcrumb.pop(); 316 if (!isFirst) text.append(" >> \n"); 317 else isFirst = false; 318 if (!this.breadcrumb.empty()) { 319 text.append("<a class=\"Link\" onmouseover=\"return true;\" HREF=\"#\" onclick=\""); 320 text.append(getUrlCommand(data.adTabId, data.name, Integer.valueOf(data.tablevel).intValue())); 321 text.append("\" onmouseout=\"return true;\">").append(data.tabname).append("</a>\n"); 322 } else text.append(data.tabname).append("\n"); 323 } 324 return text.toString(); 325 } 326 327 private String getUrlCommand(String _tabId, String _tabName, int _level) { 328 StringBuffer text = new StringBuffer (); 329 if (!_tabId.equals(this.TabID) && this.level+1>=_level) { 330 text.append("submitCommandForm('").append(((this.level>_level)?"DEFAULT":"TAB")).append("', "); 331 text.append(((this.level>=_level)?"false":"true")).append(", null, '"); 332 text.append(FormatUtilities.replace(_tabName)).append("_Relation.html', '_self', null, true);"); 333 } 334 text.append("return false;"); 335 return text.toString(); 336 } 337 } 338 | Popular Tags |