You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TabSheet.java 12KB

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