001 /* 002 Copyright (C) 2001-2003 Laurent Martelli <laurent@aopsys.com> 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU Lesser General Public License as 006 published by the Free Software Foundation; either version 2 of the 007 License, or (at your option) any later version. 008 009 This program is distributed in the hope that it will be useful, 010 but WITHOUT ANY WARRANTY; without even the implied warranty of 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 GNU Lesser General Public License for more details. 013 014 You should have received a copy of the GNU Lesser General Public License 015 along with this program; if not, write to the Free Software 016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 017 018 package org.objectweb.jac.aspects.gui.web; 019 020 import java.io.IOException; 021 import java.io.PrintWriter; 022 import java.util.Iterator; 023 import java.util.Vector; 024 import org.apache.log4j.Logger; 025 import org.objectweb.jac.aspects.gui.*; 026 import org.objectweb.jac.aspects.gui.web.html.*; 027 028 /** 029 * A tabs component. 030 */ 031 public class Tabs extends AbstractCompositeView 032 implements TabsListener, TabsView 033 { 034 static Logger logger = Logger.getLogger("gui.web"); 035 static Logger loggerEditor = Logger.getLogger("gui.editor"); 036 037 /* name of the tabs */ 038 Vector tabs = new Vector(); 039 040 /* icons */ 041 Vector icons = new Vector(); 042 043 /* the selected tab */ 044 View selected; 045 046 public Tabs() { 047 } 048 049 /** 050 * Add a tab 051 * 052 * @param extraInfos a String which is the title of the pane 053 */ 054 public void addView(View view, Object extraInfos) { 055 logger.debug("TabbedPane.addView("+view+","+extraInfos+")"); 056 add(view); 057 tabs.add((String)extraInfos); 058 icons.add(""); 059 if (selected==null) { 060 setSelected(view); 061 } 062 } 063 064 public void addTab(View component, String category, String icon) { 065 logger.debug("TabbedPane.addView("+component+","+category+")"); 066 067 add(component); 068 tabs.add((String) category); 069 icons.add(icon); 070 if (selected==null) { 071 setSelected(component); 072 } 073 } 074 075 public View getView(Object id) { 076 if (id instanceof String) 077 try { 078 return (View)components.get(Integer.parseInt((String)id)); 079 } catch (NumberFormatException e) { 080 return getTab((String)id); 081 } 082 else if (id instanceof Integer) 083 return (View)components.get(((Integer)id).intValue()); 084 else 085 throw new RuntimeException("getView(): bad id "+id); 086 } 087 088 public void select(String tab) { 089 setSelected(getTab(tab)); 090 } 091 092 /** 093 * Disable editors which are not an the selected tab 094 */ 095 protected void setSelected(View selected) { 096 this.selected = selected; 097 loggerEditor.debug("setSelected "+selected); 098 Iterator it = context.getEditors().iterator(); 099 while (it.hasNext()) { 100 Object view = it.next(); 101 if (view instanceof FieldEditor) { 102 FieldEditor editor = (FieldEditor)view; 103 if (((View)editor).isDescendantOf(selected)) 104 editor.setEnabled(true); 105 else 106 editor.setEnabled(false); 107 } 108 } 109 } 110 111 /** 112 * Returns the tab with a given name 113 * @param tab the name of the tab 114 */ 115 public View getTab(String tab) { 116 return getView(new Integer(tabs.indexOf(tab))); 117 } 118 119 // HTMLViewer interface 120 121 public void genHTML(PrintWriter out) throws IOException { 122 Iterator i = tabs.iterator(); 123 Iterator j = icons.iterator(); 124 int index = 0; 125 126 if (tabs.size() != icons.size()) 127 throw new RuntimeException("Number of tabs and number" + 128 " of icons are different"); 129 130 out.println("<div class=\""+type+"\">"); 131 JacRequest request=WebDisplay.getRequest(); 132 if (request.isIEUserAgent()) { 133 //out.println(" <div class=\"ieheader\">"); 134 out.println(" <table class=\"ieheader\"><tr>"); 135 } else { 136 out.println(" <div class=\"header\">"); 137 } 138 139 while (i.hasNext()) { 140 String icon = (String) j.next(); 141 String label = (String)i.next(); 142 String str; 143 if (icon != null) 144 str = iconElement(icon, "") + label; 145 else 146 str = label; 147 Element element = (Element)eventURL(str, "onSelect", 148 "&index=" + index); 149 if (selected==components.get(index)) { 150 element.cssClass("selected"); 151 } 152 try { 153 if (request.isIEUserAgent()) { 154 if (selected==components.get(index)) 155 out.println("<td class=\"td-selected\">"); 156 else 157 out.println("<td class=\"td\">"); 158 } 159 element.write(out); 160 if (request.isIEUserAgent()) { 161 out.println("</td>"); 162 if (i.hasNext()) { 163 out.println("<td> </td>"); 164 } 165 } 166 } catch(Exception e) { 167 e.printStackTrace(); 168 } 169 index++; 170 } 171 if (request.isIEUserAgent()) { 172 out.println(" </tr></table>"); 173 } else { 174 out.println(" </div>"); 175 } 176 out.println(" <div class=\"body\">"); 177 if (selected!=null) 178 ((HTMLViewer)selected).genHTML(out); 179 out.println(" </div>"); 180 out.println("</div>"); 181 } 182 183 // TabsListener interface 184 185 public void onSelect(int index) { 186 try { 187 setSelected((View)components.get(index)); 188 } finally { 189 context.getDisplay().refresh(); 190 } 191 } 192 }