Author: tchemit Date: 2008-01-22 01:01:24 +0000 (Tue, 22 Jan 2008) New Revision: 187 Added: trunk/jaxx/src/main/java/jaxx/runtime/builder/TabFactory.java Log: introduction factory de Tab et TabModel Added: trunk/jaxx/src/main/java/jaxx/runtime/builder/TabFactory.java =================================================================== --- trunk/jaxx/src/main/java/jaxx/runtime/builder/TabFactory.java (rev 0) +++ trunk/jaxx/src/main/java/jaxx/runtime/builder/TabFactory.java 2008-01-22 01:01:24 UTC (rev 187) @@ -0,0 +1,188 @@ +/* +* \#\#% Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Code Lutin, +* Tony Chemit +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* \#\#% */ +package jaxx.runtime.builder; + +import jaxx.runtime.JAXXObject; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import javax.swing.JComponent; +import javax.swing.JTabbedPane; +import java.awt.Component; +import java.lang.reflect.Field; +import java.util.Map; +import java.util.TreeMap; + +/** + * Une usine pour les Tabs, leur configs et leur mod�les + * + * @author tony + * @see jaxx.runtime.builder.TabContentConfig + * @see TabModel + */ + +public abstract class TabFactory { + + /** to use log facility, just put in your code: log.info(\"...\"); */ + protected static Log log = LogFactory.getLog(TabFactory.class); + + /** dictionary of configs */ + protected Map<String, TabContentConfig> configs; + + /** dictionary of instanciated actions */ + protected Map<String, JComponent> cache; + + /** dictionary of instanciated models */ + protected Map<String, TabModel> models; + + protected abstract Map<String, TabContentConfig> initFactory(); + + protected abstract void initTab(JComponent tab, String tabName, TabContentConfig config); + + protected JComponent newTab(String tabName) { + + checkRegistredConfig(tabName); + + // try in cache + if (cache.containsKey(tabName)) { + // use cached tab + return cache.get(tabName); + } + + TabContentConfig config = configs.get(tabName); + + JComponent instance; + try { + instance = (JComponent) config.impl().newInstance(); + log.info("new tab : " + instance); + + initJAXXTab(config, instance); + + initTab(instance, tabName, config); + + cache.put(tabName, instance); + return instance; + } catch (Exception e) { + throw new RuntimeException(e); + } + + } + + public TabContentConfig getConfig(String tabName) { + checkRegistredConfig(tabName); + return configs.get(tabName); + } + + public JComponent getUI(String tabName) { + return cache.get(tabName); + } + + public TabModel getModel(String tabName) { + return models.get(tabName); + } + + public void showTab(final JTabbedPane container, String tabName) { + + TabContentConfig config = getConfig(tabName); + + JComponent comp = newTab(tabName); + + int index = getTabIndex(container, comp); + if (index == -1) { + registerTab(container, tabName, config, comp); + } + + container.setSelectedComponent(comp); + } + + public void closeTab(JTabbedPane container, String tabName) { + + TabContentConfig config = getConfig(tabName); + + final JComponent comp = cache.get(tabName); + + int index = getTabIndex(container, comp); + if (index != -1) { + container.removeTabAt(index); + log.debug(config + " index " + index); + } + } + + public int getTabIndex(final JTabbedPane container, JComponent comp) { + if (container != null && comp != null) { + for (int i = 0; i < container.getTabCount(); i++) { + Component o = container.getComponentAt(i); + if (o.equals(comp)) { + return i; + } + } + } + return -1; + } + + public void resetCache() { + cache.clear(); + } + + + protected TabFactory() { + configs = initFactory(); + cache = new TreeMap<String, JComponent>(); + models = new TreeMap<String, TabModel>(); + } + + protected JComponent addTabHeader(final JTabbedPane container, final String tabName, final TabContentConfig config, final JComponent comp) { + // by default, no tab header + return null; + } + + protected void registerTab(final JTabbedPane container, final String tabName, final TabContentConfig config, final JComponent comp) { + + container.addTab(tabName, comp); + + JComponent header = addTabHeader(container, tabName, config, comp); + + if (header != null) { + + container.setTabComponentAt(container.getTabCount() - 1, header); + } + } + + @SuppressWarnings({"unchecked"}) + protected void initJAXXTab(TabContentConfig config, JComponent instance) throws NoSuchFieldException, IllegalAccessException { + if (instance instanceof JAXXObject) { + JAXXObject ui = (JAXXObject) instance; + for (String dynamicField : config.dynamicFields()) { + String dynamciName = dynamicField + '_' + config.name(); + Object obj = ui.getObjectById(dynamicField); + Field m = ui.getClass().getDeclaredField("$objectMap"); + m.setAccessible(true); + Map<Object, Object> map = (Map<Object, Object>) m.get(ui); + map.put(dynamciName, obj); + } + } + } + + protected void checkRegistredConfig(String tabName) { + if (!configs.containsKey(tabName)) { + throw new IllegalStateException("can not find a registered TabContentConfig for tab name " + tabName); + } + } + +} \ No newline at end of file
participants (1)
-
tchemit@users.labs.libre-entreprise.org