Some cleanup by Eclipse
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / place / HasTablePlace.java
1 /*
2  * Copyright (C) 2011 Openismus GmbH
3  *
4  * This file is part of GWT-Glom.
5  *
6  * GWT-Glom is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with GWT-Glom.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package org.glom.web.client.place;
21
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import org.glom.web.client.StringUtils;
26
27 import com.google.gwt.place.shared.Place;
28
29 /**
30  * Super type for Place objects (bookmarkable URLs) that display the TableSelectionView.
31  * 
32  */
33 public abstract class HasTablePlace extends Place {
34
35         private final String documentID;
36         private final String tableName;
37
38         public HasTablePlace(final String documentID, final String tableName) {
39                 this.documentID = documentID;
40                 this.tableName = tableName;
41         }
42
43         public String getDocumentID() {
44                 return documentID;
45         }
46
47         public String getTableName() {
48                 return tableName;
49         }
50
51         public static class Tokenizer {
52                 protected final String documentKey = "document";
53                 protected final String tableKey = "table";
54                 private final String separator = "&";
55                 private final String equals = "=";
56
57                 /**
58                  * Get a HashMap of parameter names and values from the history token. This can parse tokens built by
59                  * buildParamsToken().
60                  * 
61                  * @param historyToken
62                  *            The historyToken provided to getPlace().
63                  * @return A HasMap of names to values.
64                  */
65                 protected HashMap<String, String> getTokenParams(final String historyToken) {
66                         final String[] arStr = historyToken.substring(0, historyToken.length()).split(separator);
67                         final HashMap<String, String> params = new HashMap<String, String>();
68                         for (String element : arStr) {
69                                 final String[] substr = element.split(equals);
70                                 if (substr.length != 2) {
71                                         continue;
72                                 }
73
74                                 String key = "";
75                                 String value = "";
76                                 if (!StringUtils.isEmpty(substr[0])) {
77                                         key = substr[0];
78                                 }
79
80                                 if (!StringUtils.isEmpty(substr[1])) {
81                                         value = substr[1];
82                                 }
83
84                                 if (!StringUtils.isEmpty(key) && !StringUtils.isEmpty(value)) {
85                                         params.put(key, value);
86                                 }
87                         }
88
89                         return params;
90                 }
91
92                 /**
93                  * Build a history token based on a HashMap of parameter names and values. This can later be parsed by
94                  * getTokenParams().
95                  * 
96                  * @param params
97                  *            A HashMap of names and values.
98                  * @return A history string for use by getToken() implementation.
99                  */
100                 protected String buildParamsToken(final HashMap<String, String> params) {
101                         String token = "";
102                         for (Map.Entry<String, String> entry : params.entrySet()) {
103                                 final String key = entry.getKey();
104                                 final String value = entry.getValue();
105                                 if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value))
106                                         continue;
107
108                                 if (token != "") {
109                                         token += separator;
110                                 }
111
112                                 token += key + equals + value;
113                         }
114
115                         return token;
116                 }
117         }
118
119 }