From b1ccfc0d51d1cd87358b69fe01ee11b4fff29b19 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Tue, 16 Aug 2011 08:18:43 +0000 Subject: [PATCH] #5880 Add style name support for Tabsheet's Tab interface svn changeset:20402/svn branch:6.7 --- .../terminal/gwt/client/ui/VTabsheet.java | 40 ++++++++++++- src/com/vaadin/ui/TabSheet.java | 56 +++++++++++++++++++ .../tabsheet/TabSheetTabStyleNames.html | 42 ++++++++++++++ .../tabsheet/TabSheetTabStyleNames.java | 49 ++++++++++++++++ 4 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 tests/src/com/vaadin/tests/components/tabsheet/TabSheetTabStyleNames.html create mode 100644 tests/src/com/vaadin/tests/components/tabsheet/TabSheetTabStyleNames.java diff --git a/src/com/vaadin/terminal/gwt/client/ui/VTabsheet.java b/src/com/vaadin/terminal/gwt/client/ui/VTabsheet.java index 9b99391dda..0a69fd3b9d 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VTabsheet.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VTabsheet.java @@ -4,8 +4,10 @@ package com.vaadin.terminal.gwt.client.ui; +import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Set; import com.google.gwt.core.client.Scheduler; @@ -187,7 +189,8 @@ public class VTabsheet extends VTabsheetBase { public void addTab(VCaption c) { Element td = DOM.createTD(); - setStyleName(td, CLASSNAME + "-tabitemcell"); + setStyleName(td, ITEMCELL_CLASSNAME); + tabStyles.add(null); if (getWidgetCount() == 0) { setStyleName(td, CLASSNAME + "-tabitemcell-first", true); @@ -295,6 +298,11 @@ public class VTabsheet extends VTabsheetBase { public static final String TABS_CLASSNAME = "v-tabsheet-tabcontainer"; public static final String SCROLLER_CLASSNAME = "v-tabsheet-scroller"; + public static final String ITEMCELL_CLASSNAME = CLASSNAME + "-tabitemcell"; + + // Can't use "style" as it's already in use + public static final String TAB_STYLE_NAME = "tabstyle"; + private final Element tabs; // tabbar and 'scroller' container private final Element scroller; // tab-scroller element private final Element scrollerNext; // tab-scroller next button element @@ -329,6 +337,12 @@ public class VTabsheet extends VTabsheetBase { private String currentStyle; + /** + * Keeps track of the currently set styleName for each tab so it can be + * removed without affecting the other styles set to the same DOM element + */ + private List tabStyles = new ArrayList(); + private void onTabSelected(final int tabIndex) { if (disabled || waitingForResponse) { return; @@ -639,6 +653,30 @@ public class VTabsheet extends VTabsheetBase { } c.updateCaption(tabUidl); + // Apply the styleName set for the tab + String styleName = tabUidl.getStringAttribute(TAB_STYLE_NAME); + String oldStyleName = tabStyles.get(index); + // Find the nth td element + Element td = (Element) tb.tr.getChild(index); + if (styleName != null && !styleName.isEmpty()) { + if (!styleName.equals(oldStyleName)) { + // If we have a new style name + if (oldStyleName != null && !oldStyleName.isEmpty()) { + // Remove old style name if present + td.removeClassName(ITEMCELL_CLASSNAME + "-" + oldStyleName); + } + // Set new style name + td.addClassName(ITEMCELL_CLASSNAME + "-" + styleName); + // Update bookkeeping + tabStyles.set(index, styleName); + } + } else if (oldStyleName != null) { + // Remove the set stylename if no stylename is present in the uidl + td.removeClassName(ITEMCELL_CLASSNAME + "-" + oldStyleName); + // Also update the bookkeeping + tabStyles.set(index, null); + } + c.setHidden(hidden); if (scrolledOutOfView(index)) { // Should not set tabs visible if they are scrolled out of view diff --git a/src/com/vaadin/ui/TabSheet.java b/src/com/vaadin/ui/TabSheet.java index 8a1fd43b9a..212f13328b 100644 --- a/src/com/vaadin/ui/TabSheet.java +++ b/src/com/vaadin/ui/TabSheet.java @@ -406,6 +406,11 @@ public class TabSheet extends AbstractComponentContainer { componentError.paint(target); } + final String styleName = tab.getStyleName(); + if (styleName != null) { + target.addAttribute(VTabsheet.TAB_STYLE_NAME, styleName); + } + target.addAttribute("key", keyMapper.key(component)); if (component.equals(selected)) { target.addAttribute("selected", true); @@ -991,6 +996,47 @@ public class TabSheet extends AbstractComponentContainer { * Get the component related to the Tab */ public Component getComponent(); + + /** + * Sets a style name for the tab. The style name will be rendered as a + * HTML class name, which can be used in a CSS definition. + * + *
+         * Tab tab = tabsheet.addTab(tabContent, "Tab text");
+         * tab.setStyleName("mystyle");
+         * 
+ *

+ * The used style name will be prefixed with " + * {@code v-tabsheet-tabitemcell-}". For example, if you give a tab the + * style "{@code mystyle}", the tab will get a " + * {@code v-tabsheet-tabitemcell-mystyle}" style. You could then style + * the component with: + *

+ * + *
+         * .v-tabsheet-tabitemcell-mystyle {font-style: italic;}
+         * 
+ * + *

+ * This method will trigger a + * {@link com.vaadin.terminal.Paintable.RepaintRequestEvent + * RepaintRequestEvent} on the TabSheet to which the Tab belongs. + *

+ * + * @param styleName + * the new style to be set for tab + * @see #getStyleName() + */ + public void setStyleName(String styleName); + + /** + * Gets the user-defined CSS style name of the tab. Built-in style names + * defined in Vaadin or GWT are not returned. + * + * @return the style name or of the tab + * @see #setStyleName(String) + */ + public String getStyleName(); } /** @@ -1005,6 +1051,7 @@ public class TabSheet extends AbstractComponentContainer { private boolean closable = false; private String description = null; private ErrorMessage componentError = null; + private String styleName; public TabSheetTabImpl(String caption, Resource icon) { if (caption == null) { @@ -1098,6 +1145,15 @@ public class TabSheet extends AbstractComponentContainer { } return null; } + + public void setStyleName(String styleName) { + this.styleName = styleName; + requestRepaint(); + } + + public String getStyleName() { + return styleName; + } } /** diff --git a/tests/src/com/vaadin/tests/components/tabsheet/TabSheetTabStyleNames.html b/tests/src/com/vaadin/tests/components/tabsheet/TabSheetTabStyleNames.html new file mode 100644 index 0000000000..0ff83bbaca --- /dev/null +++ b/tests/src/com/vaadin/tests/components/tabsheet/TabSheetTabStyleNames.html @@ -0,0 +1,42 @@ + + + + + + +TabSheetTabStyleNames + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
TabSheetTabStyleNames
open/run/com.vaadin.tests.components.tabsheet.TabSheetTabStyleNames?debug&restartApplication
assertCSSClassvaadin=runcomvaadintestscomponentstabsheetTabSheetTabStyleNames::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[1]/VTabsheet[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]v-tabsheet-tabitemcell-ticket5880
clickvaadin=runcomvaadintestscomponentstabsheetTabSheetTabStyleNames::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[0]/VButton[0]/domChild[0]/domChild[0]
assertNotCSSClassvaadin=runcomvaadintestscomponentstabsheetTabSheetTabStyleNames::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[1]/VTabsheet[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]v-tabsheet-tabitemcell-ticket5880
assertCSSClassvaadin=runcomvaadintestscomponentstabsheetTabSheetTabStyleNames::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[1]/VTabsheet[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[1]v-tabsheet-tabitemcell-ticket5880_0
+ + diff --git a/tests/src/com/vaadin/tests/components/tabsheet/TabSheetTabStyleNames.java b/tests/src/com/vaadin/tests/components/tabsheet/TabSheetTabStyleNames.java new file mode 100644 index 0000000000..00b6e142b3 --- /dev/null +++ b/tests/src/com/vaadin/tests/components/tabsheet/TabSheetTabStyleNames.java @@ -0,0 +1,49 @@ +package com.vaadin.tests.components.tabsheet; + +import com.vaadin.tests.components.TestBase; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.Label; +import com.vaadin.ui.TabSheet; +import com.vaadin.ui.TabSheet.Tab; + +public class TabSheetTabStyleNames extends TestBase { + + private static final String STYLE_NAME = "ticket5880"; + + @Override + public void setup() { + TabSheet tabsheet = new TabSheet(); + final Tab tab1 = tabsheet.addTab(new Label(), "Tab 1"); + final Tab tab2 = tabsheet.addTab(new Label(), "Tab 2"); + + tab1.setStyleName(STYLE_NAME); + + addComponent(new Button("Update style names", + new Button.ClickListener() { + int counter = 0; + + public void buttonClick(ClickEvent event) { + if (tab1.getStyleName() == null) { + tab1.setStyleName(STYLE_NAME); + } else { + tab1.setStyleName(null); + } + + tab2.setStyleName(STYLE_NAME + "_" + (counter++)); + } + })); + + addComponent(tabsheet); + } + + @Override + protected String getDescription() { + return "Tests setting style names for individual tabs."; + } + + @Override + protected Integer getTicketNumber() { + return 5880; + } +} \ No newline at end of file -- 2.39.5