forked from liferay/liferay-portal
-
Notifications
You must be signed in to change notification settings - Fork 28
/
GoogleDocsUIItemsProcessor.java
137 lines (102 loc) · 4.02 KB
/
GoogleDocsUIItemsProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.document.library.google.docs.display.context;
import com.liferay.document.library.google.docs.util.GoogleDocsConstants;
import com.liferay.document.library.google.docs.util.GoogleDocsMetadataHelper;
import com.liferay.document.library.google.docs.util.ResourceUtil;
import com.liferay.document.library.kernel.display.context.DLUIItemKeys;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.servlet.taglib.ui.MenuItem;
import com.liferay.portal.kernel.servlet.taglib.ui.ToolbarItem;
import com.liferay.portal.kernel.servlet.taglib.ui.UIItem;
import com.liferay.portal.kernel.servlet.taglib.ui.URLMenuItem;
import com.liferay.portal.kernel.servlet.taglib.ui.URLToolbarItem;
import com.liferay.portal.kernel.servlet.taglib.ui.URLUIItem;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.WebKeys;
import java.util.List;
import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;
/**
* @author Iván Zaera
*/
public class GoogleDocsUIItemsProcessor {
public GoogleDocsUIItemsProcessor(
HttpServletRequest request,
GoogleDocsMetadataHelper googleDocsMetadataHelper) {
_request = request;
_googleDocsMetadataHelper = googleDocsMetadataHelper;
}
public void processMenuItems(List<MenuItem> menuItems) {
_removeUnsupportedUIItems(menuItems);
URLMenuItem urlMenuItem = _insertEditInGoogleURLUIItem(
new URLMenuItem(), menuItems);
urlMenuItem.setMethod("GET");
}
public void processToolbarItems(List<ToolbarItem> toolbarItems) {
_removeUnsupportedUIItems(toolbarItems);
_insertEditInGoogleURLUIItem(new URLToolbarItem(), toolbarItems);
}
private int _getIndex(List<? extends UIItem> uiItems, String key) {
for (int i = 0; i < uiItems.size(); i++) {
UIItem uiItem = uiItems.get(i);
if (key.equals(uiItem.getKey())) {
return i;
}
}
return -1;
}
private <T extends URLUIItem> T _insertEditInGoogleURLUIItem(
T urlUIItem, List<? super T> urlUIItems) {
if (!_googleDocsMetadataHelper.containsField(
GoogleDocsConstants.DDM_FIELD_NAME_URL)) {
return urlUIItem;
}
int index = _getIndex(
(List<? extends UIItem>)urlUIItems, DLUIItemKeys.EDIT);
if (index == -1) {
index = 0;
}
urlUIItem.setIcon("icon-edit");
urlUIItem.setKey(GoogleDocsUIItemKeys.EDIT_IN_GOOGLE);
ThemeDisplay themeDisplay = (ThemeDisplay)_request.getAttribute(
WebKeys.THEME_DISPLAY);
ResourceBundle resourceBundle = ResourceUtil.getResourceBundle(
themeDisplay.getLocale());
String message = LanguageUtil.get(
resourceBundle, "edit-in-google-docs");
urlUIItem.setLabel(message);
urlUIItem.setTarget("_blank");
String editURL = _googleDocsMetadataHelper.getFieldValue(
GoogleDocsConstants.DDM_FIELD_NAME_URL);
urlUIItem.setURL(editURL);
urlUIItems.add(index, urlUIItem);
return urlUIItem;
}
private void _removeUIItem(List<? extends UIItem> uiItems, String key) {
int index = _getIndex(uiItems, key);
if (index != -1) {
uiItems.remove(index);
}
}
private void _removeUnsupportedUIItems(List<? extends UIItem> uiItems) {
_removeUIItem(uiItems, DLUIItemKeys.CANCEL_CHECKOUT);
_removeUIItem(uiItems, DLUIItemKeys.CHECKIN);
_removeUIItem(uiItems, DLUIItemKeys.CHECKOUT);
_removeUIItem(uiItems, DLUIItemKeys.DOWNLOAD);
_removeUIItem(uiItems, DLUIItemKeys.OPEN_IN_MS_OFFICE);
}
private final GoogleDocsMetadataHelper _googleDocsMetadataHelper;
private final HttpServletRequest _request;
}