KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > erpCommon > businessUtility > WindowTabs


1 /*
2  *************************************************************************
3  * The contents of this file are subject to the Openbravo Public License
4  * Version 1.0 (the "License"), being the Mozilla Public License
5  * Version 1.1 with a permitted attribution clause; you may not use this
6  * file except in compliance with the License. You may obtain a copy of
7  * the License at http://www.openbravo.com/legal/license.html
8  * Software distributed under the License is distributed on an "AS IS"
9  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
10  * License for the specific language governing rights and limitations
11  * under the License.
12  * The Original Code is Openbravo ERP.
13  * The Initial Developer of the Original Code is Openbravo SL
14  * All portions are Copyright (C) 2001-2006 Openbravo SL
15  * All Rights Reserved.
16  * Contributor(s): ______________________________________.
17  ************************************************************************
18 */

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 JavaDoc className = "";
34   private String JavaDoc TabID = "";
35   private String JavaDoc WindowID = "";
36   private String JavaDoc Title = "";
37   private String JavaDoc ID = "";
38   private String JavaDoc action = "";
39   private int level = 0;
40   private Hashtable<String JavaDoc, Stack<WindowTabsData>> tabs = new Hashtable<String JavaDoc, Stack<WindowTabsData>>();
41   private Stack<WindowTabsData> breadcrumb = new Stack<WindowTabsData>();
42
43   public WindowTabs(ConnectionProvider _conn, VariablesSecureApp _vars, String JavaDoc _tabId, String JavaDoc _windowId) throws Exception JavaDoc {
44     if (_conn==null || _vars==null || _tabId==null || _tabId.equals("") || _windowId==null || _windowId.equals("")) throw new Exception JavaDoc("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 JavaDoc _className) throws Exception JavaDoc {
55     if (_conn==null || _vars==null || _className==null || _className.equals("")) throw new Exception JavaDoc("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 JavaDoc {
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 JavaDoc getMenuInfo() throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc("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 JavaDoc {
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 JavaDoc 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 JavaDoc("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 JavaDoc parentTabs() {
137     StringBuffer JavaDoc text = new StringBuffer JavaDoc();
138     if (this.tabs==null) return text.toString();
139     String JavaDoc strShowAcct = "N";
140     String JavaDoc 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 JavaDoc 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\">&nbsp;</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\">&nbsp;</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\">&nbsp;</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 JavaDoc mainTabs() {
189     StringBuffer JavaDoc text = new StringBuffer JavaDoc();
190     if (this.tabs==null) return text.toString();
191     String JavaDoc strShowAcct = "N";
192     String JavaDoc 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 JavaDoc 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\">&nbsp;</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\">&nbsp;</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\">&nbsp;</span></td>\n");
231       text.append(" </tr></table>\n");
232     }
233     text.append("</td>\n");
234     return text.toString();
235   }
236
237   public String JavaDoc childTabs() {
238     StringBuffer JavaDoc text = new StringBuffer JavaDoc();
239     if (this.tabs==null) return text.append("<td class=\"tabTabbarBackGround\"></td>").toString();
240     String JavaDoc strShowAcct = "N";
241     String JavaDoc 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 JavaDoc 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\">&nbsp;</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\">&nbsp;</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\">&nbsp;</span></td>\n");
268     text.append(" </tr></table>\n");
269     text.append("</td>\n");
270     return text.toString();
271   }
272
273 /* public String childTabs() {
274     StringBuffer text = new StringBuffer();
275     if (this.tabs==null) return text.toString();
276     String strShowAcct = "N";
277     String strShowTrl = "N";
278     try {
279       strShowAcct = Utility.getContext(this.conn, this.vars, "#ShowAcct", this.WindowID);
280       strShowTrl = Utility.getContext(this.conn, this.vars, "#ShowTrl", this.WindowID);
281     } catch (Exception ex) {
282       ex.printStackTrace();
283       log4j.error(ex);
284     }
285     Stack<WindowTabsData> aux = (Stack<WindowTabsData>) this.tabs.get(Integer.toString(this.level+1));
286     if (aux==null) return text.toString();
287     text.append("<td class=\"tabPaneBackGround dojoTabContainerChild dojoTabLabels-bottom\">\n");
288     text.append("<div>\n");
289     while (!aux.empty()) {
290       WindowTabsData data = (WindowTabsData) aux.pop();
291       if (!data.adTabId.equals(this.TabID) && strShowAcct.equals("N") && data.isinfotab.equals("Y")) continue;
292       else if (!data.adTabId.equals(this.TabID) && strShowTrl.equals("N") && data.istranslationtab.equals("Y")) continue;
293       text.append("<span class=\"dojoTab").append(((data.adTabId.equals(this.TabID) || data.isbreadcrumb.equals("Y"))?" current":"")).append("\">");
294       text.append("<div><a class=\"dojoTabLink\" HREF=\"#\" onclick=\"");
295       text.append(getUrlCommand(data.adTabId, data.name, Integer.valueOf(data.tablevel).intValue()));
296       text.append("\" onMouseOver=\"return true;\" onMouseOut=\"return true;\">").append(data.tabname).append("</a></div></span>\n");
297     }
298     text.append("</div>\n");
299     text.append("</td>\n");
300     return text.toString();
301   }*/

302
303   public String JavaDoc breadcrumb() {
304     StringBuffer JavaDoc text = new StringBuffer JavaDoc();
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("&nbsp;||&nbsp;\n");
310     } catch (Exception JavaDoc 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("&nbsp;&gt;&gt;&nbsp;\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 JavaDoc getUrlCommand(String JavaDoc _tabId, String JavaDoc _tabName, int _level) {
328     StringBuffer JavaDoc text = new StringBuffer JavaDoc();
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