Author: tchemit Date: 2008-01-19 14:42:29 +0000 (Sat, 19 Jan 2008) New Revision: 221 Added: trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/SimExplorerTabManager.java Log: lanager de tab avec configuration du TabbedPanel de l'appli m?\195?\169thodes de gestion d'onglet : showTab, closeTab, toggleTab, reloadTab. Added: trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/SimExplorerTabManager.java =================================================================== --- trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/SimExplorerTabManager.java (rev 0) +++ trunk/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/swing/SimExplorerTabManager.java 2008-01-19 14:42:29 UTC (rev 221) @@ -0,0 +1,267 @@ +/* +* \#\#% 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 fr.cemagref.simexplorer.is.ui.swing; + +import fr.cemagref.simexplorer.is.SimExplorer; +import fr.cemagref.simexplorer.is.ui.swing.tab.JApplicationDetailTab; +import fr.cemagref.simexplorer.is.ui.swing.tab.JApplicationListTab; +import fr.cemagref.simexplorer.is.ui.swing.tab.JApplicationSynchronizeTab; +import fr.cemagref.simexplorer.is.ui.swing.util.MyTabHeader; +import fr.cemagref.simexplorer.is.ui.swing.util.MyToggleButton; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import static org.codelutin.i18n.I18n._; +import static org.codelutin.i18n.I18n.n_; + +import javax.swing.JComponent; +import javax.swing.JDialog; +import javax.swing.JFrame; +import javax.swing.JTabbedPane; +import java.awt.Component; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +/** @author tony */ +public class SimExplorerTabManager { + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Log log = LogFactory.getLog(SimExplorerTabManager.class); + + public enum Tab { + + @TabContent(impl = JApplicationDetailTab.class, parentImpl = JTabContainer.class) + detail(n_("simexplorer.tab.detail"), n_("simexplorer.tab.detail.tooltip")) { + public JApplicationDetailTab get() { + return (JApplicationDetailTab) get0(); + } + }, + + @TabContent(impl = JApplicationListTab.class, parentImpl = JTabContainer.class, useToogle = true) + local(n_("simexplorer.tab.local"), n_("simexplorer.tab.local.tooltip")) { + public JApplicationListTab get() { + return (JApplicationListTab) get0(); + } + }, + + @TabContent(impl = JApplicationListTab.class, parentImpl = JTabContainer.class, useToogle = true) + remote(n_("simexplorer.tab.remote"), n_("simexplorer.tab.remote.tooltip")) { + public JApplicationListTab get() { + return (JApplicationListTab) get0(); + } + }, + + @TabContent(impl = JApplicationSynchronizeTab.class, parentImpl = JTabContainer.class, useToogle = true) + synchronize(n_("simexplorer.tab.synchronize"), n_("simexplorer.tab.synchronize.tooltip")) { + public JApplicationSynchronizeTab get() { + return (JApplicationSynchronizeTab) get0(); + } + }; + + + public abstract JComponent get(); + + private final String text; + + private final String toolTipText; + + private JComponent instance; + + private TabContent annotation; + + Tab(String text, String toolTipText) { + this.text = text; + this.toolTipText = toolTipText; + } + + public String getText() { + return _(text); + } + + public String getToolTipText() { + return _(toolTipText); + } + + public boolean isUseToggle() { + return getAnnotation().useToogle(); + } + + JComponent get0() { + if (instance == null) { + try { + instance = getAnnotation().impl().newInstance(); + } catch (InstantiationException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + return instance; + } + + TabContent getAnnotation() { + if (annotation == null) { + try { + annotation = getClass().getField(name()).getAnnotation(TabContent.class); + } catch (NoSuchFieldException e) { + throw new IllegalArgumentException("could not found annotation " + TabContent.class + " on enum " + this); + } + } + if (annotation == null) { + throw new IllegalStateException("could not found annotation " + TabContent.class + " on enum " + this); + } + return annotation; + } + + } + + public static void reloadUI() { + for (Tab tab : Tab.values()) { + tab.instance=null; + } + } + + public static void showTab(final JTabbedPane container, Tab tab) { + + final JComponent comp = tab.get(); + int index = getTabIndex(container, tab); + if (index == -1) { + addTab(container, tab, comp); + } + container.setSelectedComponent(comp); + + int index1 = getTabIndex(container, tab); + log.info(tab + " index " + index1); + if (tab.isUseToggle()) { + MyToggleButton buton = (MyToggleButton) SimExplorer.getUI().getToolbar().getObjectById("toggleTab_" + tab.name()); + buton.setSelected(true); + } + } + + public static void closeTab(JTabbedPane container, Tab tab) { + + int index = getTabIndex(container, tab); + if (index != -1) { + container.removeTabAt(index); + log.info(tab + " index " + index); + } + if (tab.isUseToggle()) { + MyToggleButton buton = (MyToggleButton) SimExplorer.getUI().getToolbar().getObjectById("toggleTab_" + tab.name()); + buton.setSelected(false); + } + } + + public static void toggleTab(JTabbedPane container, Tab tab) { + if (getTabIndex(container, tab) > -1) { + closeTab(container, tab); + } else { + showTab(container, tab); + } + } + + protected static void addTab(final JTabbedPane container, final Tab tab, final JComponent comp) { + + container.addTab(tab.getText(), comp); + final MyTabHeader panel = new MyTabHeader(); + + // add a dynamic reference to tab close button to enable + // dynamic action instanciation + panel.get$objectMap().put(panel.getCloseTab().getName() + "_" + tab.name(), panel.getCloseTab()); + + // chargement dans l'ui des actions + SimExplorerActionManager.loadActions(panel); + + panel.setToolTipText(tab.getToolTipText()); + panel.getLabel().setText(tab.getText()); + + panel.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + Component compOld = container.getSelectedComponent(); + if (!comp.equals(compOld)) { + container.setSelectedComponent(comp); + } + } + }); + + container.setTabComponentAt(container.getTabCount() - 1, panel); + container.repaint(); + } + + public static int getTabIndex(final JTabbedPane container, Tab tab) { + if (container != null) { + for (int i = 0; i < container.getTabCount(); i++) { + Component o = container.getComponentAt(i); + if (o.equals(tab.instance)) { + return i; + } + } + } + return -1; + } + + + protected SimExplorerTabManager() { + // do not instanciate + } + + public static void main(String[] args) { + SimExplorer.init(); + JTabContainer container = SimExplorer.getUI().getContent(); + + JDialog dialog = new JDialog(); + dialog.setSize(800, 600); + dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + dialog.setContentPane(container); + + for (Tab tab : Tab.values()) { + SimExplorerTabManager.showTab(container, tab); + } + dialog.setVisible(true); + } + + /** @author chemit */ + public static class ToggleTabAction extends SimExplorerAbstractTabAction { + private static final long serialVersionUID = -8701628964349439830L; + + + public ToggleTabAction(String name) { + super(name, "toggleTab"); + } + + } + + /** @author chemit */ + public static class ShowTabAction extends SimExplorerAbstractTabAction { + private static final long serialVersionUID = 2095444969568347534L; + + public ShowTabAction(String name) { + super(name, "showTab"); + } + } + + /** @author chemit */ + public static class CloseTabAction extends SimExplorerAbstractTabAction { + private static final long serialVersionUID = 352590468112645109L; + + public CloseTabAction(String name) { + super(name, "closeTab"); + } + + } +}