| 1 |
/* |
|---|
| 2 |
* (C) Copyright 2007 Nuxeo SAS (http://nuxeo.com/) and contributors. |
|---|
| 3 |
* |
|---|
| 4 |
* All rights reserved. This program and the accompanying materials |
|---|
| 5 |
* are made available under the terms of the GNU Lesser General Public License |
|---|
| 6 |
* (LGPL) version 2.1 which accompanies this distribution, and is available at |
|---|
| 7 |
* http://www.gnu.org/licenses/lgpl.html |
|---|
| 8 |
* |
|---|
| 9 |
* This library is distributed in the hope that it will be useful, |
|---|
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 12 |
* Lesser General Public License for more details. |
|---|
| 13 |
* |
|---|
| 14 |
* Contributors: |
|---|
| 15 |
* Nuxeo - initial API and implementation |
|---|
| 16 |
* |
|---|
| 17 |
* $Id$ |
|---|
| 18 |
*/ |
|---|
| 19 |
|
|---|
| 20 |
package org.nuxeo.ecm.platform.ui.web.tag.handler; |
|---|
| 21 |
|
|---|
| 22 |
import javax.faces.application.Application; |
|---|
| 23 |
import javax.faces.component.ActionSource; |
|---|
| 24 |
import javax.faces.component.UIComponent; |
|---|
| 25 |
import javax.faces.el.MethodBinding; |
|---|
| 26 |
|
|---|
| 27 |
import org.apache.commons.logging.Log; |
|---|
| 28 |
import org.apache.commons.logging.LogFactory; |
|---|
| 29 |
|
|---|
| 30 |
import com.sun.facelets.FaceletContext; |
|---|
| 31 |
import com.sun.facelets.tag.MetaRuleset; |
|---|
| 32 |
import com.sun.facelets.tag.TagAttribute; |
|---|
| 33 |
import com.sun.facelets.tag.jsf.ComponentConfig; |
|---|
| 34 |
import com.sun.facelets.tag.jsf.html.HtmlComponentHandler; |
|---|
| 35 |
|
|---|
| 36 |
/** |
|---|
| 37 |
* Component tag handler that wires a document link tag to a command link tag. |
|---|
| 38 |
* <p> |
|---|
| 39 |
* Useful when redirecting to a document using a post. |
|---|
| 40 |
* |
|---|
| 41 |
* @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a> |
|---|
| 42 |
* |
|---|
| 43 |
*/ |
|---|
| 44 |
public class DocumentLinkTagHandler extends HtmlComponentHandler { |
|---|
| 45 |
|
|---|
| 46 |
private static final Log log = LogFactory.getLog(DocumentLinkTagHandler.class); |
|---|
| 47 |
|
|---|
| 48 |
private final TagAttribute document; |
|---|
| 49 |
|
|---|
| 50 |
private final TagAttribute view; |
|---|
| 51 |
|
|---|
| 52 |
public DocumentLinkTagHandler(ComponentConfig config) { |
|---|
| 53 |
super(config); |
|---|
| 54 |
document = getRequiredAttribute("document"); |
|---|
| 55 |
view = getAttribute("view"); |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
@Override |
|---|
| 59 |
protected MetaRuleset createMetaRuleset(Class type) { |
|---|
| 60 |
// expected created tag is an html command link |
|---|
| 61 |
MetaRuleset mr = super.createMetaRuleset(type); |
|---|
| 62 |
// alias title |
|---|
| 63 |
mr.alias("title", "value"); |
|---|
| 64 |
mr.ignore("document"); |
|---|
| 65 |
mr.ignore("action"); |
|---|
| 66 |
mr.ignore("view"); |
|---|
| 67 |
return mr; |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
/** |
|---|
| 71 |
* Sets action after component has been created. |
|---|
| 72 |
* |
|---|
| 73 |
*/ |
|---|
| 74 |
@Override |
|---|
| 75 |
protected void onComponentCreated(FaceletContext ctx, UIComponent c, |
|---|
| 76 |
UIComponent parent) { |
|---|
| 77 |
if (c instanceof ActionSource) { |
|---|
| 78 |
ActionSource command = (ActionSource) c; |
|---|
| 79 |
String docValue = getDocumentValue(); |
|---|
| 80 |
String viewId = getViewValue(); |
|---|
| 81 |
String actionValue; |
|---|
| 82 |
if (viewId == null) { |
|---|
| 83 |
actionValue = "#{navigationContext.navigateToDocument(" |
|---|
| 84 |
+ docValue + ")}"; |
|---|
| 85 |
} else { |
|---|
| 86 |
actionValue = "#{navigationContext.navigateToDocumentWithView(" |
|---|
| 87 |
+ docValue + ", " + viewId + ")}"; |
|---|
| 88 |
} |
|---|
| 89 |
Application app = ctx.getFacesContext().getApplication(); |
|---|
| 90 |
MethodBinding meth = app.createMethodBinding(actionValue, null); |
|---|
| 91 |
command.setAction(meth); |
|---|
| 92 |
} |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
private String getDocumentValue() { |
|---|
| 96 |
String docValue = document.getValue(); |
|---|
| 97 |
docValue = docValue.trim(); |
|---|
| 98 |
if (!((docValue.startsWith("${") || docValue.startsWith("#{")) && docValue.endsWith("}"))) { |
|---|
| 99 |
log.error("Invalid value for document " + docValue); |
|---|
| 100 |
} |
|---|
| 101 |
docValue = docValue.substring(2, docValue.length() - 1); |
|---|
| 102 |
return docValue; |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
private String getViewValue() { |
|---|
| 106 |
String viewName = null; |
|---|
| 107 |
if (view != null) { |
|---|
| 108 |
viewName = view.getValue(); |
|---|
| 109 |
if (viewName != null) { |
|---|
| 110 |
viewName = viewName.trim(); |
|---|
| 111 |
if ((viewName.startsWith("${") || viewName.startsWith("#{")) |
|---|
| 112 |
&& viewName.endsWith("}")) { |
|---|
| 113 |
viewName = viewName.substring(2, viewName.length() - 1); |
|---|
| 114 |
} else { |
|---|
| 115 |
viewName = '\"' + viewName + '\"'; |
|---|
| 116 |
} |
|---|
| 117 |
} |
|---|
| 118 |
} |
|---|
| 119 |
return viewName; |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
} |
|---|