Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TabSheet.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /* *************************************************************************
  2. IT Mill Toolkit
  3. Development of Browser User Interfaces Made Easy
  4. Copyright (C) 2000-2006 IT Mill Ltd
  5. *************************************************************************
  6. This product is distributed under commercial license that can be found
  7. from the product package on license.pdf. Use of this product might
  8. require purchasing a commercial license from IT Mill Ltd. For guidelines
  9. on usage, see licensing-guidelines.html
  10. *************************************************************************
  11. For more information, contact:
  12. IT Mill Ltd phone: +358 2 4802 7180
  13. Ruukinkatu 2-4 fax: +358 2 4802 7181
  14. 20540, Turku email: info@itmill.com
  15. Finland company www: www.itmill.com
  16. Primary source for information and releases: www.itmill.com
  17. ********************************************************************** */
  18. package com.itmill.toolkit.ui;
  19. import java.lang.reflect.Method;
  20. import java.util.Hashtable;
  21. import java.util.LinkedList;
  22. import java.util.Iterator;
  23. import java.util.Map;
  24. import com.itmill.toolkit.terminal.*;
  25. /**
  26. * Tabsheet component.
  27. *
  28. * @author IT Mill Ltd.
  29. * @version
  30. * @VERSION@
  31. * @since 3.0
  32. */
  33. public class TabSheet extends AbstractLayout {
  34. /**
  35. * Use this stylename with {@link #addStyleName(String)} to remove padding
  36. * between TabSheet borders and content. The actual client-side
  37. * implementation will determine which stylenames it implements.
  38. */
  39. public static final String STYLE_NO_PADDING = "nopad";
  40. /**
  41. * Linked list of component tabs.
  42. */
  43. private LinkedList tabs = new LinkedList();
  44. /**
  45. * Tab -> caption mapping.
  46. */
  47. private Hashtable tabCaptions = new Hashtable();
  48. /**
  49. * Tab -> icon mapping .
  50. */
  51. private Hashtable tabIcons = new Hashtable();
  52. /**
  53. * Selected tab.
  54. */
  55. private Component selected = null;
  56. private KeyMapper keyMapper = new KeyMapper();
  57. /**
  58. * Holds the value of property tabsHIdden.
  59. */
  60. private boolean tabsHidden;
  61. /**
  62. * Constructs a new Tabsheet. Tabsheet is immediate by default.
  63. */
  64. public TabSheet() {
  65. super();
  66. setImmediate(true);
  67. }
  68. /**
  69. * Gets the component container iterator for going trough all the components
  70. * in the container.
  71. *
  72. * @return the Iterator of the components inside the container.
  73. */
  74. public Iterator getComponentIterator() {
  75. return java.util.Collections.unmodifiableList(tabs).iterator();
  76. }
  77. /**
  78. * Removes the component from this container.
  79. *
  80. * @param c
  81. * the component to be removed.
  82. */
  83. public void removeComponent(Component c) {
  84. if (c != null && tabs.contains(c)) {
  85. super.removeComponent(c);
  86. keyMapper.remove(c);
  87. tabs.remove(c);
  88. tabCaptions.remove(c);
  89. if (c.equals(selected)) {
  90. if (tabs.isEmpty())
  91. selected = null;
  92. else {
  93. selected = (Component) tabs.getFirst();
  94. fireSelectedTabChange();
  95. }
  96. }
  97. requestRepaint();
  98. }
  99. }
  100. /**
  101. * Adds the component into this container. The component is added as a tab
  102. * where its default tab-caption is the caption of the component.
  103. *
  104. * @param c
  105. * the component to be added.
  106. */
  107. public void addComponent(Component c) {
  108. addTab(c, c.getCaption(), getIcon());
  109. }
  110. /**
  111. * Adds the new tab into TabSheet.
  112. *
  113. * @param c
  114. * the component to be added onto tab.
  115. * @param caption
  116. * the caption of the tab.
  117. * @param icon
  118. * the Set the icon of the tab.
  119. */
  120. public void addTab(Component c, String caption, Resource icon) {
  121. if (c != null) {
  122. tabs.addLast(c);
  123. tabCaptions.put(c, caption != null ? caption : "");
  124. if (icon != null)
  125. tabIcons.put(c, icon);
  126. if (selected == null) {
  127. selected = c;
  128. fireSelectedTabChange();
  129. }
  130. super.addComponent(c);
  131. requestRepaint();
  132. }
  133. }
  134. /**
  135. * Gets the component UIDL tag.
  136. *
  137. * @return the Component UIDL tag as string.
  138. */
  139. public String getTag() {
  140. return "tabsheet";
  141. }
  142. /**
  143. * Moves all components from another container to this container. The
  144. * components are removed from the other container.
  145. *
  146. * @param source
  147. * the container components are removed from.
  148. */
  149. public void moveComponentsFrom(ComponentContainer source) {
  150. for (Iterator i = source.getComponentIterator(); i.hasNext();) {
  151. Component c = (Component) i.next();
  152. String caption = null;
  153. Resource icon = null;
  154. if (TabSheet.class.isAssignableFrom(source.getClass())) {
  155. caption = ((TabSheet) source).getTabCaption(c);
  156. icon = ((TabSheet) source).getTabIcon(c);
  157. }
  158. source.removeComponent(c);
  159. addTab(c, caption, icon);
  160. }
  161. }
  162. /**
  163. * Paints the content of this component.
  164. *
  165. * @param event
  166. * the Paint Event.
  167. * @throws PaintException
  168. * if the paint operation failed.
  169. */
  170. public void paintContent(PaintTarget target) throws PaintException {
  171. super.paintContent(target);
  172. if (areTabsHidden())
  173. target.addAttribute("hidetabs", true);
  174. target.startTag("tabs");
  175. for (Iterator i = getComponentIterator(); i.hasNext();) {
  176. Component c = (Component) i.next();
  177. if (!c.isVisible())
  178. continue;
  179. target.startTag("tab");
  180. Resource icon = getTabIcon(c);
  181. if (icon != null)
  182. target.addAttribute("icon", icon);
  183. String caption = getTabCaption(c);
  184. if (!c.isEnabled()) {
  185. target.addAttribute("disabled", true);
  186. }
  187. if (caption != null && caption.length() > 0)
  188. target.addAttribute("caption", caption);
  189. target.addAttribute("key", keyMapper.key(c));
  190. if (c.equals(selected)) {
  191. target.addAttribute("selected", true);
  192. c.paint(target);
  193. }
  194. target.endTag("tab");
  195. }
  196. target.endTag("tabs");
  197. if (selected != null)
  198. target.addVariable(this, "selected", keyMapper.key(selected));
  199. }
  200. /**
  201. * Are tabs hidden.
  202. *
  203. * @return the Property visibility.
  204. */
  205. public boolean areTabsHidden() {
  206. return this.tabsHidden;
  207. }
  208. /**
  209. * Setter for property tabsHidden.
  210. *
  211. * @param tabsHidden
  212. * True if the tabs should be hidden.
  213. */
  214. public void hideTabs(boolean tabsHidden) {
  215. this.tabsHidden = tabsHidden;
  216. requestRepaint();
  217. }
  218. /**
  219. * Gets the caption for a component.
  220. *
  221. * @param c
  222. * the component.
  223. */
  224. public String getTabCaption(Component c) {
  225. String caption = (String) tabCaptions.get(c);
  226. if (caption == null)
  227. caption = "";
  228. return caption;
  229. }
  230. /**
  231. * Sets the caption for a component.
  232. *
  233. * @param c
  234. * the component.
  235. * @param caption
  236. * the caption to set.
  237. */
  238. public void setTabCaption(Component c, String caption) {
  239. tabCaptions.put(c, caption);
  240. requestRepaint();
  241. }
  242. /**
  243. * Gets the icon for a component.
  244. *
  245. * @param c
  246. * the component.
  247. */
  248. public Resource getTabIcon(Component c) {
  249. return (Resource) tabIcons.get(c);
  250. }
  251. /**
  252. * ] Sets the icon for a component.
  253. *
  254. * @param c
  255. * @param icon
  256. */
  257. public void setTabIcon(Component c, Resource icon) {
  258. if (icon == null)
  259. tabIcons.remove(c);
  260. else
  261. tabIcons.put(c, icon);
  262. requestRepaint();
  263. }
  264. /**
  265. * Sets the selected tab.
  266. *
  267. * @param c
  268. */
  269. public void setSelectedTab(Component c) {
  270. if (c != null && tabs.contains(c) && !selected.equals(c)) {
  271. selected = c;
  272. fireSelectedTabChange();
  273. requestRepaint();
  274. }
  275. }
  276. /**
  277. * Gets the selected tab.
  278. *
  279. * @return the selected tab.
  280. */
  281. public Component getSelectedTab() {
  282. return selected;
  283. }
  284. /**
  285. * Invoked when the value of a variable has changed.
  286. *
  287. * @see com.itmill.toolkit.ui.AbstractComponent#changeVariables(java.lang.Object,
  288. * java.util.Map)
  289. */
  290. public void changeVariables(Object source, Map variables) {
  291. if (variables.containsKey("selected"))
  292. setSelectedTab((Component) keyMapper.get((String) variables
  293. .get("selected")));
  294. }
  295. /* Documented in superclass */
  296. public void replaceComponent(Component oldComponent, Component newComponent) {
  297. // Gets the captions
  298. String oldCaption = getTabCaption(oldComponent);
  299. Resource oldIcon = getTabIcon(oldComponent);
  300. String newCaption = getTabCaption(newComponent);
  301. Resource newIcon = getTabIcon(newComponent);
  302. // Gets the locations
  303. int oldLocation = -1;
  304. int newLocation = -1;
  305. int location = 0;
  306. for (Iterator i = tabs.iterator(); i.hasNext();) {
  307. Component component = (Component) i.next();
  308. if (component == oldComponent)
  309. oldLocation = location;
  310. if (component == newComponent)
  311. newLocation = location;
  312. location++;
  313. }
  314. if (oldLocation == -1)
  315. addComponent(newComponent);
  316. else if (newLocation == -1) {
  317. removeComponent(oldComponent);
  318. keyMapper.remove(oldComponent);
  319. addComponent(newComponent);
  320. tabs.remove(newComponent);
  321. tabs.add(oldLocation, newComponent);
  322. setTabCaption(newComponent, oldCaption);
  323. setTabIcon(newComponent, oldIcon);
  324. } else {
  325. if (oldLocation > newLocation) {
  326. tabs.remove(oldComponent);
  327. tabs.add(newLocation, oldComponent);
  328. tabs.remove(newComponent);
  329. tabs.add(oldLocation, newComponent);
  330. } else {
  331. tabs.remove(newComponent);
  332. tabs.add(oldLocation, newComponent);
  333. tabs.remove(oldComponent);
  334. tabs.add(newLocation, oldComponent);
  335. }
  336. setTabCaption(newComponent, oldCaption);
  337. setTabIcon(newComponent, oldIcon);
  338. setTabCaption(oldComponent, newCaption);
  339. setTabIcon(oldComponent, newIcon);
  340. requestRepaint();
  341. }
  342. }
  343. /* Click event ************************************************ */
  344. private static final Method SELECTED_TAB_CHANGE_METHOD;
  345. static {
  346. try {
  347. SELECTED_TAB_CHANGE_METHOD = SelectedTabChangeListener.class
  348. .getDeclaredMethod("selectedTabChange",
  349. new Class[] { SelectedTabChangeEvent.class });
  350. } catch (java.lang.NoSuchMethodException e) {
  351. // This should never happen
  352. throw new java.lang.RuntimeException();
  353. }
  354. }
  355. /**
  356. * Selected Tab Change event. This event is thrown, when the selected tab in
  357. * the tab sheet is changed.
  358. *
  359. * @author IT Mill Ltd.
  360. * @version
  361. * @VERSION@
  362. * @since 3.0
  363. */
  364. public class SelectedTabChangeEvent extends Component.Event {
  365. /**
  366. * Serial generated by eclipse.
  367. */
  368. private static final long serialVersionUID = 3258129141914940469L;
  369. /**
  370. * New instance of selected tab change event
  371. *
  372. * @param source
  373. * the Source of the event.
  374. */
  375. public SelectedTabChangeEvent(Component source) {
  376. super(source);
  377. }
  378. /**
  379. * Select where the event occurred
  380. *
  381. * @return the Source of the event.
  382. */
  383. public Select getSelect() {
  384. return (Select) getSource();
  385. }
  386. }
  387. /**
  388. * Selected Tab Change Event listener
  389. *
  390. * @author IT Mill Ltd.
  391. *
  392. * @version
  393. * @VERSION@
  394. * @since 3.0
  395. */
  396. public interface SelectedTabChangeListener {
  397. /**
  398. * Visible tab in tab sheet has has been changed.
  399. *
  400. * @param event
  401. * the Selected tab change event.
  402. */
  403. public void selectedTabChange(SelectedTabChangeEvent event);
  404. }
  405. /**
  406. * Adds the selected tab change listener
  407. *
  408. * @param listener
  409. * the Listener to be added.
  410. */
  411. public void addListener(SelectedTabChangeListener listener) {
  412. addListener(SelectedTabChangeEvent.class, listener,
  413. SELECTED_TAB_CHANGE_METHOD);
  414. }
  415. /**
  416. * Removes the selected tab change listener
  417. *
  418. * @param listener
  419. * the Listener to be removed.
  420. */
  421. public void removeListener(SelectedTabChangeListener listener) {
  422. removeListener(SelectedTabChangeEvent.class, listener,
  423. SELECTED_TAB_CHANGE_METHOD);
  424. }
  425. /**
  426. * Emits the options change event.
  427. */
  428. protected void fireSelectedTabChange() {
  429. fireEvent(new SelectedTabChangeEvent(this));
  430. }
  431. }