View Javadoc
1   /**
2    * This file Copyright (c) 2012-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.vaadin.gwt.client.grid;
35  
36  import com.google.gwt.dom.client.Document;
37  import com.google.gwt.dom.client.Element;
38  import com.google.gwt.dom.client.ImageElement;
39  import com.google.gwt.dom.client.Style;
40  import com.google.gwt.dom.client.Style.Display;
41  import com.google.gwt.dom.client.Style.Unit;
42  import com.google.gwt.user.client.DOM;
43  import com.google.gwt.user.client.Event;
44  import com.vaadin.client.UIDL;
45  import com.vaadin.v7.client.ui.VTreeTablePatched;
46  
47  /**
48   * VMagnoliaTreeTable extends VTreeTable by ways that patching is required to expose the necessary private fields.
49   */
50  public class VMagnoliaTreeTable extends VTreeTablePatched {
51  
52      @Override
53      protected VScrollTableBody createScrollBody() {
54          scrollBody = new VMagnoliaTreeTableScrollBody();
55          return scrollBody;
56      }
57  
58      @Override
59      protected String buildCaptionHtmlSnippet(UIDL uidl) {
60          return (uidl.getTag().equals("column")) ? super.buildCaptionHtmlSnippet(uidl) : uidl.getStringAttribute("caption");
61      }
62  
63      /**
64       * Extension for Scroll body.
65       */
66      public class VMagnoliaTreeTableScrollBody extends VTreeTableScrollBody {
67  
68          protected VMagnoliaTreeTableScrollBody() {
69              super();
70          }
71  
72          @Override
73          protected VScrollTableRow createRow(UIDL uidl, char[] aligns2) {
74              if (uidl.hasAttribute("gen_html")) {
75                  // This is a generated row.
76                  return new VTreeTableGeneratedRow(uidl, aligns2);
77              }
78              return new VMagnoliaTreeTableRow(uidl, aligns2);
79          }
80  
81          /**
82           * Extension for table row.
83           */
84          class VMagnoliaTreeTableRow extends VTreeTableRow {
85  
86              public VMagnoliaTreeTableRow(UIDL uidl, char[] aligns2) {
87                  super(uidl, aligns2);
88              }
89  
90              /*
91               * Forked from VTreeTable.
92               */
93              @Override
94              protected boolean addTreeSpacer(UIDL rowUidl) {
95                  if (cellShowsTreeHierarchy(getElement().getChildCount() - 1)) {
96                      Element container = (Element) getElement().getLastChild().getChild(0);
97  
98                      if (rowUidl.hasAttribute("icon")) {
99                          // icons are in first content cell in TreeTable
100                         ImageElement icon = Document.get().createImageElement();
101                         icon.setClassName("v-icon");
102                         icon.setAlt("icon");
103                         icon.setSrc(client.translateVaadinUri(rowUidl.getStringAttribute("icon")));
104                         container.insertFirst(icon);
105                     }
106 
107                     String classname = "v-treetable-treespacer-patched";
108                     if (rowUidl.getBooleanAttribute("ca")) {
109                         canHaveChildren = true;
110                         open = rowUidl.getBooleanAttribute("open");
111                         classname += open ? " v-treetable-node-open" : " v-treetable-node-closed";
112                         classname += open ? " icon-arrow1_s" : " icon-arrow1_e";
113                     }
114 
115                     treeSpacer = Document.get().createDivElement();
116                     treeSpacer.getStyle().setDisplay(Display.INLINE_BLOCK);
117                     treeSpacer.setClassName(classname);
118                     container.insertAfter(treeSpacer, container.getFirstChild());
119                     depth = rowUidl.hasAttribute("depth") ? rowUidl.getIntAttribute("depth") : 0;
120                     setIndent();
121                     isTreeCellAdded = true;
122                     return true;
123                 }
124                 return false;
125             }
126 
127             @Override
128             public void onBrowserEvent(Event event) {
129                 if (event.getEventTarget().cast() == treeSpacer &&
130                         treeSpacer.getClassName().contains("node")) {
131                     if (event.getTypeInt() == Event.ONMOUSEDOWN || event.getTypeInt() == Event.ONTOUCHSTART) {
132                         sendToggleCollapsedUpdate(getKey());
133                         event.stopPropagation();
134                         event.preventDefault();
135                     }
136                     return;
137                 }
138                 super.onBrowserEvent(event);
139             }
140 
141             @Override
142             protected void setIndent() {
143                 if (getIndentWidth() > 0) {
144                     treeSpacer.getStyle().setWidth(getIndent(), Unit.PX);
145                 }
146             }
147 
148             @Override
149             protected void setCellWidth(int cellIx, int width) {
150                 super.setCellWidth(cellIx, width);
151                 // MGNLUI-1170: first column need separate width calculation due to tree spacer.
152                 if (cellIx == 0) {
153                     Element cell = DOM.getChild(getElement(), cellIx);
154                     Style wrapperStyle = cell.getFirstChildElement().getStyle();
155                     wrapperStyle.setPropertyPx("width", width);
156                     cell.getStyle().setPropertyPx("width", width);
157                 }
158             }
159 
160             @Override
161             protected boolean isRenderHtmlInCells() {
162                 return true;
163             }
164         }
165     }
166 }