001: /*
002: * Copyright 2002 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */
013: package com.sun.portal.providers.containers.template.tab;
014:
015: import java.util.List;
016: import java.util.ArrayList;
017:
018: import java.util.Map;
019: import java.util.HashMap;
020: import java.util.Hashtable;
021: import java.util.ResourceBundle;
022: import java.util.logging.Level;
023: import java.util.logging.Logger;
024:
025: import java.net.URL;
026: import java.net.MalformedURLException;
027:
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030: import com.sun.portal.providers.containers.ProviderWindowStates;
031: import com.sun.portal.providers.ProviderException;
032: import com.sun.portal.providers.InvalidEditFormDataException;
033:
034: import com.sun.portal.providers.containers.jsp.tab.TabContainer;
035: import com.sun.portal.providers.containers.jsp.tab.UnmodifiableTab;
036: import com.sun.portal.providers.containers.jsp.tab.ModifiableTab;
037: import com.sun.portal.providers.containers.jsp.tab.util.TabData;
038: import com.sun.portal.providers.containers.jsp.tab.util.TabException;
039: import com.sun.portal.providers.containers.template.TemplateContainerProvider;
040: import com.sun.portal.log.common.PortalLogger;
041:
042: /**
043: * TBD write Javadocs.
044: */
045:
046: public class TemplateTabContainerProvider extends
047: TemplateContainerProvider implements TabContainer {
048:
049: private TabData tabdata = null;
050: private ResourceBundle bundle = null;
051: private static Logger logger = PortalLogger
052: .getLogger(TemplateTabContainerProvider.class);
053:
054: public void init(String n, HttpServletRequest req)
055: throws ProviderException {
056: super .init(n, req);
057: //
058: // init TabData
059: //
060: bundle = getResourceBundle();
061: tabdata = new TabData(getContainerProviderContext(), getName(),
062: bundle);
063: }
064:
065: public StringBuffer getContent(HttpServletRequest req,
066: HttpServletResponse res) throws ProviderException {
067: //
068: // request to change the selected?
069: //
070: String s = req.getParameter(getName() + ".setSelected");
071: if ((s != null) && (s.length() > 0)) {
072: setSelectedTabName(s);
073: }
074:
075: StringBuffer tabTemplates = new StringBuffer();
076: //
077: // setup tag table for swapping individual tab templates. this object
078: // will be used each time through the loop, with different tag values
079: //
080: Hashtable tagTable = getStandardTags(req);
081:
082: String selected = getSelectedTabName();
083:
084: List tabs = getSelectedTabs();
085:
086: // left-to-right or right-to-left?
087: boolean righttoleft = getBooleanProperty("rtl");
088: if (righttoleft) {
089: tagTable.put("tabrtl", "true");
090: } else {
091: tagTable.put("tabrtl", "false");
092: }
093:
094: for (int i = 0; i < tabs.size(); i++) {
095: UnmodifiableTab tab = (UnmodifiableTab) tabs.get(i);
096:
097: //
098: // we have the tab, get the tab template and swap it based
099: // on the tab values. we must swap one-by-one because
100: // the value of the tags change depending on which tab we
101: // are processing
102: //
103: tagTable.put(getName() + ".tabName", tab.getDisplayname());
104:
105: URL url = getTabURL(tab, req);
106: tagTable.put(getName() + ".tabURL", url.toString());
107:
108: //
109: // set the template based on whether it was the selected tab or not.
110: //
111: String tmpl = null;
112: if (selected.equals(tab.getName())) {
113: tmpl = "selectedTab.template";
114: } else {
115: tmpl = "tab.template";
116: }
117:
118: StringBuffer tabTemplate = getTemplate(tmpl, tagTable);
119:
120: // if going right-to-left, insert new tabs at front of buffer
121: if (!righttoleft) {
122: tabTemplates.append(tabTemplate);
123: } else {
124: tabTemplates.insert(0, tabTemplate.toString());
125: }
126: }
127:
128: //
129: // insert tabs tag into tabs template. we're re-using the hashtable
130: // object so it may have extraneous tags, but who cares.
131: //
132: tagTable.put("tabs", tabTemplates);
133:
134: //
135: // get final tab content
136: //
137: StringBuffer tabcontent;
138: if (righttoleft) {
139: tabcontent = getTemplate("tabs_r.template", tagTable);
140: } else {
141: tabcontent = getTemplate("tabs.template", tagTable);
142: }
143:
144: //add selected channel content to the tag table.
145: req.setAttribute("selectedTabName", getSelectedTab()
146: .getDisplayname());
147: tagTable.put("tabContainerName", getName());
148: tagTable.put("frontContainerName", getSelectedTabName());
149: StringBuffer selectedChannelContent = getContainerProviderContext()
150: .getContent(req, res, getName(), selected);
151: tabcontent = tabcontent.append(selectedChannelContent
152: .toString());
153: tagTable.put("content", tabcontent.toString());
154:
155: //add helplink tag
156: tagTable.put("help_link", getHelpLink("frontPage", req));
157:
158: //get final content
159: StringBuffer content = getTemplate("display.template", tagTable);
160:
161: return content;
162: }
163:
164: public StringBuffer getEdit(HttpServletRequest req,
165: HttpServletResponse res) throws ProviderException {
166: String editPage = req.getParameter("page");
167: String inputTabName = req.getParameter("inputTabName");
168: String inputDescName = req.getParameter("inputDescName");
169: if (inputTabName == null)
170: inputTabName = "";
171: if (inputDescName == null)
172: inputDescName = "";
173: //
174: // setup tag table for swapping individual tab templates. this object
175: // will be used each time through the loop, with different tag values
176: //
177: Hashtable tagTable = getStandardTags(req);
178: tagTable.put(getName() + ".tabList", getTabList());
179: tagTable.put(getName() + ".tabTopics", getTabTopics());
180: tagTable.put("frontContainerName", getSelectedTabName());
181: tagTable.put("tabContainerName", getName());
182: tagTable.put("help_link", getHelp(req).toString());
183: tagTable.put("TemplateTabContainer", getName());
184: if (editPage != null && editPage.equals("make")) {
185: tagTable.put(getName() + ".editPage", getTemplate(
186: "makeNewTab.template").toString());
187: } else {
188: tagTable.put(getName() + ".editPage", getTemplate(
189: "removeRenameTab.template").toString());
190: }
191: tagTable.put("inputTabName", inputTabName);
192: tagTable.put("inputDescName", inputDescName);
193:
194: StringBuffer content = getTemplate("editForm.template",
195: tagTable);
196:
197: return content;
198:
199: }
200:
201: private String getTabList() throws ProviderException {
202: List tabs = getSelectedTabs();
203:
204: //
205: // note this, because we will not allow the user remove/rename
206: // their selected tab.
207: //
208: String selected = getSelectedTabName();
209: String start = getStartTabName();
210: //
211: // build tab pattern table. it should be the contents inside of a
212: // table (everything between the table tags).
213: //
214: StringBuffer content = new StringBuffer(1024);
215: for (int i = 0; i < tabs.size(); i++) {
216: UnmodifiableTab tab = (UnmodifiableTab) tabs.get(i);
217: boolean isStart = false;
218:
219: if (tab.getName().equals(start)) {
220: isStart = true;
221: }
222:
223: content.append(makeRemove(tab, isStart));
224: }
225:
226: return content.toString();
227: }
228:
229: private String getTabTopics() throws ProviderException {
230: List availableTabs = getAvailableTabs();
231: UnmodifiableTab makeTab = getMakeTab();
232:
233: //
234: // build tab pattern table. it should be the contents inside of a
235: // table (everything between the table tags).
236: //
237: StringBuffer content = new StringBuffer(1024);
238: for (int i = 0; i < availableTabs.size(); i++) {
239: UnmodifiableTab tab = (UnmodifiableTab) availableTabs
240: .get(i);
241: if (tab.isPredefined()) {
242: content.append(makeTopic(tab));
243: }
244: }
245:
246: //
247: // append the "make my own ..." option. this is defined separately as
248: // its own attr so the process page can also read it and understand
249: // that this is a special case
250: //
251: content.append(makeTopic(makeTab, true));
252:
253: return content.toString();
254: }
255:
256: private StringBuffer makeTopic(UnmodifiableTab t) {
257: return makeTopic(t, false);
258: }
259:
260: private StringBuffer makeTopic(UnmodifiableTab t, boolean checked) {
261: StringBuffer content = new StringBuffer(128);
262:
263: content.append("<tr>\n").append("<td>\n").append(
264: "<input type=\"radio\" name=\"").append(
265: getName() + ".tabTopic\" ")
266: // use original name in value, not localized name - system is clueless to it
267: .append("value=\"" + t.getName() + "\"");
268:
269: if (checked) {
270: content.append(" checked");
271: }
272:
273: content
274: .append(">\n")
275: .append("</td>\n")
276: .append("<td>\n")
277: .append("<font size=\"-1\" face=\"[tag:fontFace1]\">\n")
278: .append("<b>").append(t.getDisplayname()).append(
279: "</b>\n");
280:
281: if (t.getDesc() != null) {
282: content.append("<br>\n").append(
283: "<font size=\"-1\" face=\"[tag:fontFace1]\">\n")
284: .append(t.getDesc()).append("</font>\n");
285: }
286:
287: content.append("</font>\n").append("</td>\n").append("</tr>\n");
288:
289: return content;
290: }
291:
292: private StringBuffer makeRemove(UnmodifiableTab t, boolean isStart) {
293: StringBuffer content = new StringBuffer(128);
294:
295: content.append("<TR>\n")
296: // start radio
297: .append("<TD>\n").append("<CENTER>\n").append(
298: "<INPUT TYPE=\"RADIO\" VALUE=\"").append(
299: t.getName()).append("\" NAME=\"").append(
300: getName() + ".setStart\"").append(
301: " onClick=\"detectChecked(this.value)\"");
302: if (isStart) {
303: content.append(" checked");
304: }
305:
306: content
307: .append("></CENTER></TD>")
308:
309: // name
310: .append("<TD>\n")
311: .append("<FONT FACE=\"[tag:fontFace1]\" SIZE=\"-1\">\n")
312: .append(t.getDisplayname()).append("</font>");
313: if (!t.isRemovable()) {
314: content.append("<font color =\"#0000ff\">*</font>");
315: }
316: content.append("</td>").append(
317: "<TD NOWRAP><FONT FACE=\"sans-serif\" SIZE=\"-1\">\n");
318:
319: if (t.isRenamable()) {
320: content.append(
321: "<a href=\"#\" ONCLICK=\"javascript:renameTab('")
322: .append(getName() + "_rename_").append(t.getName())
323: .append("')\"><font face=\"sans-serif\">").append(
324: bundle.getString("rename")).append(
325: "</font></a>").append(
326: "<INPUT TYPE=\"HIDDEN\" VALUE=\"").append(
327: t.getDisplayname()).append("\" NAME=\"")
328: .append(getName() + "_rename_").append(t.getName())
329: .append("\">");
330: }
331:
332: if (t.isRemovable()) {
333: content.append(
334: "<a href=\"#\" ONCLICK=\"javascript:deleteTab('")
335: .append(getName() + "_remove_").append(t.getName())
336: .append("')\"><font face=\"sans-serif\">").append(
337: bundle.getString("delete")).append(
338: "</font></a>").append(
339: "<INPUT TYPE=\"HIDDEN\" VALUE=\"0\" ")
340: .append("NAME=\"").append(getName() + "_remove_")
341: .append(t.getName()).append("\">");
342: }
343: content.append("</td>\n");
344: content.append("</tr>\n");
345:
346: return content;
347: }
348:
349: public URL processEdit(HttpServletRequest req,
350: HttpServletResponse res) throws ProviderException {
351: URL url = null;
352: //
353: // process remove/rename/make start
354: //
355: try {
356: url = tabdata.removeRenameTab(req);
357: } catch (TabException te) {
358: throw new ProviderException(
359: "TemplateTabContainerProvider.processEdit():couldn't remove/rename tab",
360: te);
361: } catch (InvalidEditFormDataException e) {
362: if (logger.isLoggable(Level.WARNING))
363: logger.log(Level.WARNING, "", e);
364:
365: String error = getProviderContext().encodeURLParameter(
366: e.getMessage());
367: StringBuffer args = new StringBuffer(64);
368: args.append("?action=edit&provider=").append(getName())
369: .append("&error=").append(error);
370:
371: try {
372: url = new URL(getProviderContext().getDesktopURL(req)
373: + args.toString());
374: } catch (MalformedURLException me) {
375: if (logger.isLoggable(Level.WARNING))
376: logger.log(Level.WARNING, "PSDT_CSPPCTTB0002", url);
377: return null;
378: }
379:
380: }
381:
382: String makeparam = req.getParameter(getName() + ".make");
383: if ((makeparam != null) && (makeparam.equals("make"))) {
384: Map results = new HashMap();
385: // try to make new tab
386: try {
387: tabdata.makeNewTab(req, results);
388: if ((Boolean) results.get("make") != null) {
389: boolean make = ((Boolean) results.get("make"))
390: .booleanValue();
391: if (make) {
392: ModifiableTab tab = (ModifiableTab) results
393: .get("tab");
394: url = getContentURL(tab, req);
395: }
396: }
397: } catch (InvalidEditFormDataException e) {
398: if (logger.isLoggable(Level.WARNING))
399: logger.log(Level.WARNING, "PSDT_CSPPCTTB0001", e);
400: String error = getProviderContext().encodeURLParameter(
401: e.getMessage());
402: StringBuffer args = new StringBuffer(64);
403: args.append("?action=edit&provider=").append(getName())
404: .append("&error=").append(error).append(
405: "&page=make");
406:
407: String inputName = (String) results.get("inputTabName");
408: if (inputName != null) {
409: args.append("&inputTabName=").append(
410: getProviderContext().encodeURLParameter(
411: inputName));
412: }
413:
414: inputName = (String) results.get("inputDescName");
415: if (inputName != null) {
416: args.append("&inputDescName=").append(
417: getProviderContext().encodeURLParameter(
418: inputName));
419: }
420:
421: try {
422: url = new URL(getProviderContext().getDesktopURL(
423: req)
424: + args.toString());
425: } catch (MalformedURLException me) {
426: if (logger.isLoggable(Level.WARNING))
427: logger.log(Level.WARNING, "PSDT_CSPPCTTB0002",
428: url);
429: return null;
430: }
431:
432: }
433:
434: }
435: //
436: // else, send to main dt page
437: //
438: return url;
439: }
440:
441: public URL getTabURL(UnmodifiableTab tab, HttpServletRequest req)
442: throws ProviderException {
443: URL url = null;
444: try {
445: url = tabdata.getTabURL(tab, req);
446: } catch (TabException te) {
447: throw new ProviderException(
448: "TemplateTabContainerProvider.getTabURL():couldn't get the desktop URL",
449: te);
450: }
451: return url;
452: }
453:
454: public URL getContentURL(ModifiableTab tab, HttpServletRequest req)
455: throws ProviderException {
456: URL url = null;
457: String charset = getProviderContext().getCharset();
458: try {
459: StringBuffer urlbuffer = new StringBuffer(
460: getContainerProviderContext().getDesktopURL(req));
461: urlbuffer.append("?action=edit&provider=");
462: urlbuffer.append(tab.getName());
463: urlbuffer.append("&");
464: urlbuffer.append(tab.getName());
465: urlbuffer.append(".containerEdit=content");
466: urlbuffer.append("&selected=");
467: urlbuffer.append(getProviderContext().encodeURLParameter(
468: tab.getDisplayname()));
469: url = new URL(urlbuffer.toString());
470: } catch (MalformedURLException me) {
471: throw new ProviderException(
472: "TemplateTabContainerProvider.getContentURL():couldn't create the url",
473: me);
474: }
475: return url;
476: }
477:
478: /**
479: * remove tab.
480: * we do not allow internal structures to be modified, so to edit
481: * a tab, the client must remove the tab, modify it, then re-add it
482: */
483:
484: public void removeTab(String name) throws ProviderException {
485: try {
486: tabdata.removeTab(name);
487: } catch (TabException te) {
488: throw new ProviderException(
489: "TemplateTabContainerProvider.removeTab():couldn't remove tab from the selected tab",
490: te);
491: }
492: }
493:
494: public int getMaxTabs() throws ProviderException {
495: try {
496: return tabdata.getMaxTabs();
497: } catch (TabException te) {
498: throw new ProviderException(
499: "TemplateTabContainerProvider.getMaxTabs():couldn't get max tabs",
500: te);
501: }
502: }
503:
504: public List getSelectedTabs() throws ProviderException {
505: List tabs = new ArrayList();
506: try {
507: tabs = tabdata.getSelectedTabs();
508: } catch (TabException te) {
509: throw new ProviderException(
510: "TemplateTabContainerProvider.getSelectedTabs():couldn't get selectedtabs",
511: te);
512: }
513: return tabs;
514: }
515:
516: public List getAvailableTabs() throws ProviderException {
517: List tabs = new ArrayList();
518: try {
519: tabs = tabdata.getAvailableTabs();
520: } catch (TabException te) {
521: throw new ProviderException(
522: "TemplateTabContainerProvider.getAvailableTabs():couldn't get available tabs",
523: te);
524: }
525: return tabs;
526: }
527:
528: public UnmodifiableTab getStartTab() throws ProviderException {
529: UnmodifiableTab t = null;
530: try {
531: t = tabdata.getStartTab();
532: } catch (TabException te) {
533: throw new ProviderException(
534: "TemplateTabContainerProvider.getStartTab():couldn't get start tab",
535: te);
536: }
537: return t;
538: }
539:
540: public String getStartTabName() throws ProviderException {
541: try {
542: return tabdata.getStartTabName();
543: } catch (TabException te) {
544: throw new ProviderException(
545: "TemplateTabContainerProvider.getStartTabName():couldn't get start tab name ",
546: te);
547: }
548: }
549:
550: public UnmodifiableTab getMakeTab() throws ProviderException {
551: UnmodifiableTab t = null;
552: try {
553: t = tabdata.getMakeTab();
554: } catch (TabException te) {
555: throw new ProviderException(
556: "TemplateTabContainerProvider.getMakeTab():couldn't get make tab",
557: te);
558: }
559: return t;
560: }
561:
562: public String getMakeTabName() throws ProviderException {
563: try {
564: return tabdata.getMakeTabName();
565: } catch (TabException te) {
566: throw new ProviderException(
567: "TemplateTabContainerProvider.getMakeTabName():couldn't get make tab name ",
568: te);
569: }
570: }
571:
572: public String getMakeTabProviderName() throws ProviderException {
573: try {
574: return tabdata.getMakeTabProviderName();
575: } catch (TabException te) {
576: throw new ProviderException(
577: "TemplateTabContainerProvider.getMakeTabProviderName():couldn't get make tab provider name ",
578: te);
579: }
580: }
581:
582: public String getSelectedTabName() throws ProviderException {
583: String name = null;
584: try {
585: name = tabdata.getSelectedTabName();
586: } catch (TabException te) {
587: throw new ProviderException(
588: "TabContainerProvider.getSelectedTabName():couldn't get the selected tab name",
589: te);
590: }
591: return name;
592: }
593:
594: public UnmodifiableTab getSelectedTab() throws ProviderException {
595: UnmodifiableTab t = null;
596: try {
597: t = tabdata.getSelectedTab();
598: } catch (TabException te) {
599: throw new ProviderException(
600: "TemplateTabContainerProvider.getSelectedTab():couldn't get selected tab",
601: te);
602: }
603: return t;
604: }
605:
606: public UnmodifiableTab getTab(String name) throws ProviderException {
607: UnmodifiableTab t = null;
608: try {
609: t = tabdata.getTab(name);
610: } catch (TabException te) {
611: throw new ProviderException(
612: "TemplateTabContainerProvider.getTab():couldn't get the tab:name="
613: + name, te);
614: }
615: return t;
616: }
617:
618: public void setTab(ModifiableTab t) throws ProviderException {
619: setTab(t, false);
620: }
621:
622: public void setTab(ModifiableTab t, boolean selected)
623: throws ProviderException {
624: try {
625: tabdata.setTab(t, selected);
626: } catch (TabException te) {
627: throw new ProviderException(
628: "TemplateTabContainerProvider.setTab():couldn't set the tab",
629: te);
630: }
631: }
632:
633: public void setStartTabName() throws ProviderException {
634: setStartTabName(getSelectedTabName());
635: }
636:
637: public void setStartTabName(UnmodifiableTab t)
638: throws ProviderException {
639: setStartTabName(t.getName());
640: }
641:
642: public void setStartTabName(String name) throws ProviderException {
643: try {
644: tabdata.setStartTabName(name);
645: } catch (TabException te) {
646: throw new ProviderException(
647: "TemplateTabContainerProvider.setStartTabName():attempt to set start to non-existent tab",
648: te);
649: }
650: }
651:
652: public void setSelectedTab(ModifiableTab t)
653: throws ProviderException {
654: setSelectedTabName(t.getName());
655: }
656:
657: public void setSelectedTabName(String tabName)
658: throws ProviderException {
659: try {
660: tabdata.setSelectedTabName(tabName);
661: } catch (TabException te) {
662: throw new ProviderException(
663: "TemplateTabContainerProvider.setStartTabName():attempt to set selected to non-existent tab",
664: te);
665: }
666: }
667:
668: /**
669: * Gets the window state of the channel.
670: * <p/>
671: * This method just returns ProviderWindowStates.NORMAL.
672: *
673: * @param channelName channel for which the window state is requested.
674: * @return window state
675: * @throws ProviderException
676: * @see com.sun.portal.providers.containers.ProviderWindowStates#NORMAL
677: */
678: public int getWindowState(String channelName)
679: throws ProviderException {
680: return ProviderWindowStates.NORMAL;
681: }
682:
683: }
|