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.

Panel.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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.util.Iterator;
  20. import java.util.LinkedList;
  21. import java.util.Map;
  22. import com.itmill.toolkit.event.Action;
  23. import com.itmill.toolkit.event.ShortcutAction;
  24. import com.itmill.toolkit.event.Action.Handler;
  25. import com.itmill.toolkit.terminal.KeyMapper;
  26. import com.itmill.toolkit.terminal.PaintException;
  27. import com.itmill.toolkit.terminal.PaintTarget;
  28. import com.itmill.toolkit.terminal.Scrollable;
  29. /**
  30. * Panel - a simple single component container.
  31. *
  32. * @author IT Mill Ltd.
  33. * @version
  34. * @VERSION@
  35. * @since 3.0
  36. */
  37. public class Panel extends AbstractLayout implements Scrollable,
  38. ComponentContainer.ComponentAttachListener,
  39. ComponentContainer.ComponentDetachListener, Action.Container {
  40. public static final String STYLE_LIGHT = "light";
  41. public static final String STYLE_EMPHASIZE = "emphasize";
  42. /**
  43. * Layout of the panel.
  44. */
  45. private Layout layout;
  46. /**
  47. * Scroll X position.
  48. */
  49. private int scrollOffsetX = 0;
  50. /**
  51. * Scroll Y position.
  52. */
  53. private int scrollOffsetY = 0;
  54. /**
  55. * Scrolling mode.
  56. */
  57. private boolean scrollable = false;
  58. /** List of action handlers */
  59. private LinkedList actionHandlers = null;
  60. /** Action mapper */
  61. private KeyMapper actionMapper = null;
  62. /**
  63. * Creates a new empty panel. Ordered layout is used.
  64. */
  65. public Panel() {
  66. this(new OrderedLayout());
  67. }
  68. /**
  69. * Creates a new empty panel with given layout. Layout must be non-null.
  70. *
  71. * @param layout
  72. * the layout used in the panel.
  73. */
  74. public Panel(Layout layout) {
  75. setLayout(layout);
  76. }
  77. /**
  78. * Creates a new empty panel with caption. Ordered layout is used.
  79. *
  80. * @param caption
  81. * the caption used in the panel.
  82. */
  83. public Panel(String caption) {
  84. this(caption, new OrderedLayout());
  85. }
  86. /**
  87. * Creates a new empty panel with caption.
  88. *
  89. * @param caption
  90. * the caption of the panel.
  91. * @param layout
  92. * the layout used in the panel.
  93. */
  94. public Panel(String caption, Layout layout) {
  95. this(layout);
  96. setCaption(caption);
  97. }
  98. /**
  99. * Gets the current layout of the panel.
  100. *
  101. * @return the Current layout of the panel.
  102. */
  103. public Layout getLayout() {
  104. return this.layout;
  105. }
  106. /**
  107. * Sets the layout of the panel. All the components are moved to new layout.
  108. *
  109. * @param layout
  110. * the New layout of the panel.
  111. */
  112. public void setLayout(Layout layout) {
  113. // Only allow non-null layouts
  114. if (layout == null)
  115. layout = new OrderedLayout();
  116. // Sets the panel to be parent for the layout
  117. layout.setParent(this);
  118. dependsOn(layout);
  119. // If panel already contains a layout, move the contents to new one
  120. // and detach old layout from the panel
  121. if (this.layout != null) {
  122. layout.moveComponentsFrom(this.layout);
  123. removeDirectDependency(this.layout);
  124. this.layout.setParent(null);
  125. }
  126. // Removes the event listeners from the old layout
  127. if (this.layout != null) {
  128. this.layout
  129. .removeListener((ComponentContainer.ComponentAttachListener) this);
  130. this.layout
  131. .removeListener((ComponentContainer.ComponentDetachListener) this);
  132. }
  133. // Sets the new layout
  134. this.layout = layout;
  135. // Adds the event listeners for new layout
  136. layout.addListener((ComponentContainer.ComponentAttachListener) this);
  137. layout.addListener((ComponentContainer.ComponentDetachListener) this);
  138. }
  139. /**
  140. * Paints the content of this component.
  141. *
  142. * @param target
  143. * the Paint Event.
  144. * @throws PaintException
  145. * if the paint operation failed.
  146. */
  147. public void paintContent(PaintTarget target) throws PaintException {
  148. layout.paint(target);
  149. super.paintContent(target);
  150. if (isScrollable()) {
  151. target.addVariable(this, "scrollleft", getScrollOffsetX());
  152. target.addVariable(this, "scrolldown", getScrollOffsetY());
  153. }
  154. if (actionHandlers != null && !actionHandlers.isEmpty()) {
  155. target.addVariable(this, "action", "");
  156. target.startTag("actions");
  157. for (Iterator ahi = actionHandlers.iterator(); ahi.hasNext();) {
  158. Action[] aa = ((Action.Handler) ahi.next()).getActions(null,
  159. this);
  160. if (aa != null) {
  161. for (int ai = 0; ai < aa.length; ai++) {
  162. Action a = aa[ai];
  163. target.startTag("action");
  164. String akey = actionMapper.key(aa[ai]);
  165. target.addAttribute("key", akey);
  166. if (a.getCaption() != null)
  167. target.addAttribute("caption", a.getCaption());
  168. if (a.getIcon() != null)
  169. target.addAttribute("icon", a.getIcon());
  170. if (a instanceof ShortcutAction) {
  171. ShortcutAction sa = (ShortcutAction) a;
  172. target.addAttribute("kc", sa.getKeyCode());
  173. int[] modifiers = sa.getModifiers();
  174. if (modifiers != null) {
  175. String[] smodifiers = new String[modifiers.length];
  176. for (int i = 0; i < modifiers.length; i++)
  177. smodifiers[i] = String
  178. .valueOf(modifiers[i]);
  179. target.addAttribute("mk", smodifiers);
  180. }
  181. }
  182. target.endTag("action");
  183. }
  184. }
  185. }
  186. target.endTag("actions");
  187. }
  188. }
  189. /**
  190. * Gets the component UIDL tag.
  191. *
  192. * @return the Component UIDL tag as string.
  193. */
  194. public String getTag() {
  195. return "panel";
  196. }
  197. /**
  198. * Adds the component into this container.
  199. *
  200. * @param c
  201. * the component to be added.
  202. * @see com.itmill.toolkit.ui.AbstractComponentContainer#addComponent(com.itmill.toolkit.ui.Component)
  203. */
  204. public void addComponent(Component c) {
  205. layout.addComponent(c);
  206. // No repaint request is made as we except the underlaying container to
  207. // request repaints
  208. }
  209. /**
  210. * Removes the component from this container.
  211. *
  212. * @param c
  213. * The component to be added.
  214. * @see com.itmill.toolkit.ui.AbstractComponentContainer#removeComponent(com.itmill.toolkit.ui.Component)
  215. */
  216. public void removeComponent(Component c) {
  217. layout.removeComponent(c);
  218. // No repaint request is made as we except the underlaying container to
  219. // request repaints
  220. }
  221. /**
  222. * Gets the component container iterator for going trough all the components
  223. * in the container.
  224. *
  225. * @return the Iterator of the components inside the container.
  226. * @see com.itmill.toolkit.ui.ComponentContainer#getComponentIterator()
  227. */
  228. public Iterator getComponentIterator() {
  229. return layout.getComponentIterator();
  230. }
  231. /**
  232. * Called when one or more variables handled by the implementing class are
  233. * changed.
  234. *
  235. * @see com.itmill.toolkit.terminal.VariableOwner#changeVariables(Object,
  236. * Map)
  237. */
  238. public void changeVariables(Object source, Map variables) {
  239. super.changeVariables(source, variables);
  240. // Get new size
  241. Integer newWidth = (Integer) variables.get("width");
  242. Integer newHeight = (Integer) variables.get("height");
  243. if (newWidth != null && newWidth.intValue() != getWidth()) {
  244. setWidth(newWidth.intValue());
  245. // ensure units as we are reading pixels
  246. setWidthUnits(UNITS_PIXELS);
  247. }
  248. if (newHeight != null && newHeight.intValue() != getHeight()) {
  249. setHeight(newHeight.intValue());
  250. // ensure units as we are reading pixels
  251. setHeightUnits(UNITS_PIXELS);
  252. }
  253. // Scrolling
  254. Integer newScrollX = (Integer) variables.get("scrollleft");
  255. Integer newScrollY = (Integer) variables.get("scrolldown");
  256. if (newScrollX != null && newScrollX.intValue() != getScrollOffsetX())
  257. setScrollOffsetX(newScrollX.intValue());
  258. if (newScrollY != null && newScrollY.intValue() != getScrollOffsetY())
  259. setScrollOffsetY(newScrollY.intValue());
  260. // Actions
  261. if (variables.containsKey("action")) {
  262. String key = (String) variables.get("action");
  263. Action action = (Action) actionMapper.get(key);
  264. if (action != null && actionHandlers != null)
  265. for (Iterator i = actionHandlers.iterator(); i.hasNext();)
  266. ((Action.Handler) i.next())
  267. .handleAction(action, this, this);
  268. }
  269. }
  270. /* Scrolling functionality */
  271. /* Documented in interface */
  272. public int getScrollOffsetX() {
  273. return scrollOffsetX;
  274. }
  275. /* Documented in interface */
  276. public int getScrollOffsetY() {
  277. return scrollOffsetY;
  278. }
  279. /* Documented in interface */
  280. public boolean isScrollable() {
  281. return scrollable;
  282. }
  283. /* Documented in interface */
  284. public void setScrollable(boolean isScrollingEnabled) {
  285. if (scrollable != isScrollingEnabled) {
  286. scrollable = isScrollingEnabled;
  287. requestRepaint();
  288. }
  289. }
  290. /* Documented in interface */
  291. public void setScrollOffsetX(int pixelsScrolledLeft) {
  292. if (pixelsScrolledLeft < 0)
  293. throw new IllegalArgumentException(
  294. "Scroll offset must be at least 0");
  295. if (this.scrollOffsetX != pixelsScrolledLeft) {
  296. scrollOffsetX = pixelsScrolledLeft;
  297. requestRepaint();
  298. }
  299. }
  300. /* Documented in interface */
  301. public void setScrollOffsetY(int pixelsScrolledDown) {
  302. if (pixelsScrolledDown < 0)
  303. throw new IllegalArgumentException(
  304. "Scroll offset must be at least 0");
  305. if (this.scrollOffsetY != pixelsScrolledDown) {
  306. scrollOffsetY = pixelsScrolledDown;
  307. requestRepaint();
  308. }
  309. }
  310. /* Documented in superclass */
  311. public void replaceComponent(Component oldComponent, Component newComponent) {
  312. layout.replaceComponent(oldComponent, newComponent);
  313. }
  314. /**
  315. * A new component is attached to container.
  316. *
  317. * @see com.itmill.toolkit.ui.ComponentContainer.ComponentAttachListener#componentAttachedToContainer(com.itmill.toolkit.ui.ComponentContainer.ComponentAttachEvent)
  318. */
  319. public void componentAttachedToContainer(ComponentAttachEvent event) {
  320. if (event.getContainer() == layout)
  321. fireComponentAttachEvent(event.getAttachedComponent());
  322. }
  323. /**
  324. * A component has been detached from container.
  325. *
  326. * @see com.itmill.toolkit.ui.ComponentContainer.ComponentDetachListener#componentDetachedFromContainer(com.itmill.toolkit.ui.ComponentContainer.ComponentDetachEvent)
  327. */
  328. public void componentDetachedFromContainer(ComponentDetachEvent event) {
  329. if (event.getContainer() == layout)
  330. fireComponentDetachEvent(event.getDetachedComponent());
  331. }
  332. /**
  333. * Notifies the component that it is connected to an application.
  334. *
  335. * @see com.itmill.toolkit.ui.Component#attach()
  336. */
  337. public void attach() {
  338. super.attach();
  339. if (layout != null)
  340. layout.attach();
  341. }
  342. /**
  343. * Notifies the component that it is detached from the application.
  344. *
  345. * @see com.itmill.toolkit.ui.Component#detach()
  346. */
  347. public void detach() {
  348. if (layout != null)
  349. layout.detach();
  350. }
  351. /**
  352. * Removes all components from this container.
  353. *
  354. * @see com.itmill.toolkit.ui.ComponentContainer#removeAllComponents()
  355. */
  356. public void removeAllComponents() {
  357. layout.removeAllComponents();
  358. }
  359. public void addActionHandler(Handler actionHandler) {
  360. if (actionHandler != null) {
  361. if (actionHandlers == null) {
  362. actionHandlers = new LinkedList();
  363. actionMapper = new KeyMapper();
  364. }
  365. if (!actionHandlers.contains(actionHandler)) {
  366. actionHandlers.add(actionHandler);
  367. requestRepaint();
  368. }
  369. }
  370. }
  371. /**
  372. * Removes an action handler.
  373. *
  374. * @see com.itmill.toolkit.event.Action.Container#removeActionHandler(Action.Handler)
  375. */
  376. public void removeActionHandler(Action.Handler actionHandler) {
  377. if (actionHandlers != null && actionHandlers.contains(actionHandler)) {
  378. actionHandlers.remove(actionHandler);
  379. if (actionHandlers.isEmpty()) {
  380. actionHandlers = null;
  381. actionMapper = null;
  382. }
  383. requestRepaint();
  384. }
  385. }
  386. }