001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.theme.impl.render.div;
023:
024: import org.jboss.portal.Mode;
025: import org.jboss.portal.WindowState;
026: import org.jboss.portal.theme.render.AbstractObjectRenderer;
027: import org.jboss.portal.theme.render.RenderException;
028: import org.jboss.portal.theme.render.RendererContext;
029: import org.jboss.portal.theme.render.renderer.ActionRendererContext;
030: import org.jboss.portal.theme.render.renderer.DecorationRenderer;
031: import org.jboss.portal.theme.render.renderer.DecorationRendererContext;
032:
033: import java.io.PrintWriter;
034: import java.util.Collection;
035: import java.util.Collections;
036: import java.util.Comparator;
037: import java.util.HashMap;
038: import java.util.Iterator;
039: import java.util.List;
040: import java.util.Map;
041:
042: /**
043: * Implementation of a decoration renderer, based on div tags.
044: *
045: * @author <a href="mailto:mholzner@novell.com>Martin Holzner</a>
046: * @version $LastChangedRevision: 9524 $, $LastChangedDate: 2008-01-16 21:12:48 -0500 (Wed, 16 Jan 2008) $
047: * @see org.jboss.portal.theme.render.renderer.DecorationRenderer
048: */
049: public class DivDecorationRenderer extends AbstractObjectRenderer
050: implements DecorationRenderer {
051:
052: public void render(RendererContext rendererContext,
053: DecorationRendererContext drc) throws RenderException {
054: PrintWriter markup = rendererContext.getWriter();
055:
056: //
057: renderTitle(rendererContext, drc);
058:
059: //End the dyna-handle early so it doesn't encompass
060: //the mode container -
061: markup.print("</div>");
062: markup.print("</div>");
063:
064: markup.print("<div class=\"portlet-mode-container\">");
065: renderTriggerableActions(rendererContext, drc,
066: ActionRendererContext.MODES_KEY);
067: renderTriggerableActions(rendererContext, drc,
068: ActionRendererContext.WINDOWSTATES_KEY);
069: markup.print("</div>");
070: }
071:
072: private static void renderTitle(RendererContext ctx,
073: DecorationRendererContext drc) {
074: PrintWriter out = ctx.getWriter();
075: out.print("<div class=\"portlet-titlebar-decoration\"></div>");
076: out.print("<span class=\"portlet-titlebar-title\">");
077: out.print(drc.getTitle());
078: out.print("</span>");
079: }
080:
081: private static void renderTriggerableActions(RendererContext ctx,
082: DecorationRendererContext drc, String selector) {
083: Collection modesOrStates = drc.getTriggerableActions(selector);
084: if (modesOrStates == null) {
085: return;
086: }
087:
088: //
089: if (modesOrStates instanceof List) {
090: List list = (List) modesOrStates;
091: Collections.sort(list, new ModeAndStateComparator());
092: modesOrStates = list;
093: }
094:
095: //
096: for (Iterator i = modesOrStates.iterator(); i.hasNext();) {
097: ActionRendererContext action = (ActionRendererContext) i
098: .next();
099: if (action.isEnabled()) {
100: PrintWriter out = ctx.getWriter();
101: out.print("<span class=\"mode-button\" title=\"");
102: out.print(action.getName());
103: out.print("\"><a class=\"portlet-mode-");
104: out.print(action.getName());
105: out.print("\" href=\"");
106: out.print(action.getURL());
107: out.print("\"> </a></span>");
108: }
109: }
110: }
111:
112: private static class ModeAndStateComparator implements Comparator {
113:
114: /** . */
115: private final Map modeOrState2Index = new HashMap();
116:
117: /** . */
118: private int lastModeIndex = 1;
119:
120: /** . */
121: private int lastStateIndex = 101;
122:
123: public ModeAndStateComparator() {
124: modeOrState2Index
125: .put(Mode.EDIT.toString(), new Integer(97));
126: modeOrState2Index
127: .put(Mode.HELP.toString(), new Integer(98));
128: modeOrState2Index
129: .put(Mode.VIEW.toString(), new Integer(99));
130: modeOrState2Index.put(Mode.ADMIN.toString(), new Integer(
131: 100));
132: modeOrState2Index.put(WindowState.MINIMIZED.toString(),
133: new Integer(198));
134: modeOrState2Index.put(WindowState.NORMAL.toString(),
135: new Integer(199));
136: modeOrState2Index.put(WindowState.MAXIMIZED.toString(),
137: new Integer(200));
138: }
139:
140: public int compare(Object o1, Object o2) {
141: ActionRendererContext action1 = (ActionRendererContext) o1;
142: ActionRendererContext action2 = (ActionRendererContext) o2;
143:
144: //
145: Object origin1 = action1.getName();
146: Object origin2 = action2.getName();
147:
148: //
149: int index1 = getIndexFor(origin1);
150: int index2 = getIndexFor(origin2);
151:
152: //
153: return index1 - index2;
154: }
155:
156: private int getIndexFor(Object origin) {
157: Integer index = (Integer) modeOrState2Index.get(origin);
158: if (index == null) {
159: index = (origin instanceof Mode) ? new Integer(
160: lastModeIndex++)
161: : new Integer(lastStateIndex++);
162: modeOrState2Index.put(origin, index);
163: }
164: return index.intValue();
165: }
166: }
167: }
|