001: /*
002: * Created on Feb 19, 2005
003: *
004: */
005: package net.sf.thingamablog.generator;
006:
007: import java.util.ArrayList;
008: import java.util.Hashtable;
009: import java.util.List;
010:
011: import net.sf.thingamablog.blog.ArchiveRange;
012: import net.sf.thingamablog.blog.BlogEntry;
013: import net.sf.thingamablog.blog.TBWeblog;
014:
015: /**
016: * @author BT
017: *
018: */
019: public class NextPreviousContainer implements TemplateContainer {
020: public static final int NEXT = -1;
021: public static final int PREV = -2;
022:
023: private int dir = NEXT;
024: private long entryID = -1;
025: private ArchiveRange arc = null;
026: private String cat = null;
027:
028: private TBWeblog blog = null;
029:
030: private boolean exists = true;
031:
032: private HyperTextTag pageTitleTag = new HyperTextTag("PageName");
033: private TextTag pageLinkTag = new TextTag("PageLink");
034:
035: private String pageTitle;
036: private String pageLink;
037:
038: public NextPreviousContainer(int dir) {
039: this .dir = dir;
040: }
041:
042: public NextPreviousContainer(TBWeblog blog, String cat, int dir) {
043: this (dir);
044: this .cat = cat;
045: this .blog = blog;
046: }
047:
048: public NextPreviousContainer(TBWeblog blog, ArchiveRange arc,
049: int dir) {
050: this (dir);
051: this .arc = arc;
052: this .blog = blog;
053: }
054:
055: public NextPreviousContainer(TBWeblog blog, long id, int dir) {
056: this (dir);
057: this .entryID = id;
058: this .blog = blog;
059: }
060:
061: public List getContainers() {
062: ArrayList list = new ArrayList(2);
063: list.add(new IfPageExists());
064: list.add(new IfNoPageExists());
065: return list;
066: }
067:
068: public List getTags() {
069: //return tags;
070: return null;
071: }
072:
073: public Hashtable getDefaultAttributes() {
074: return null;
075: }
076:
077: /* (non-Javadoc)
078: * @see net.sf.thingamablog.generator1.TemplateContainer#processAgain()
079: */
080: public boolean processAgain() {
081: return false;
082: }
083:
084: /* (non-Javadoc)
085: * @see net.sf.thingamablog.generator1.TemplateContainer#prefix()
086: */
087: public String prefix() {
088: return null;
089: }
090:
091: /* (non-Javadoc)
092: * @see net.sf.thingamablog.generator1.TemplateContainer#postfix()
093: */
094: public String postfix() {
095: return null;
096: }
097:
098: public void initialize(Hashtable attribs) {
099: if (blog == null) {
100: exists = false;
101: return;
102: }
103:
104: if (cat != null) {
105: try {
106: String cats[] = blog.getCategories();
107: Object o = getNextOrPrev(cat, cats);
108: if (o != null) {
109: String c = o.toString();
110: pageTitle = c;
111: pageLink = blog.getArchiveUrl()
112: + blog.getCategoryFileName(c);
113: exists = true;
114: } else
115: exists = false;
116: } catch (Exception ex) {
117: exists = false;
118: }
119: } else if (arc != null) {
120: try {
121: ArchiveRange arcs[] = blog.getArchives();
122: Object o = getNextOrPrev(arc, arcs);
123: if (o != null) {
124: ArchiveRange a = (ArchiveRange) o;
125: pageTitle = a.getFormattedRange();
126: pageLink = blog.getArchiveUrl()
127: + blog.getArchiveFileName(a);
128: exists = true;
129: } else
130: exists = false;
131: } catch (Exception ex) {
132: exists = false;
133: }
134: } else if (entryID > -1) {
135: try {
136: BlogEntry be = blog.getEntry(entryID);
137: if (dir == NEXT)
138: be = blog.getEntryAfter(be.getDate());
139: else
140: be = blog.getEntryBefore(be.getDate());
141: if (be != null) {
142: pageTitle = be.getTitle();
143: pageLink = blog.getUrlForEntry(be);
144: } else
145: exists = false;
146: } catch (Exception ex) {
147: exists = false;
148: }
149: }
150:
151: }
152:
153: private Object getNextOrPrev(Object o, Object ar[]) {
154: if (ar.length == 0)
155: return null;
156:
157: int i;
158: for (i = 0; i < ar.length; i++) {
159: if (ar[i].equals(o))
160: break;
161: }
162:
163: if (dir == NEXT) {
164: if (i < ar.length - 1)
165: return ar[i + 1];
166: } else {
167: if (i > 0)
168: return ar[i - 1];
169: }
170:
171: return null;
172: }
173:
174: public String getName() {
175: if (dir == NEXT)
176: return "NextPage";
177: return "PreviousPage";
178: }
179:
180: public boolean isVisible() {
181: return true;
182: }
183:
184: public Object getValueForTag(TemplateTag tag) {
185: return "";
186: }
187:
188: private class IfPageExists extends BasicContainer {
189: public IfPageExists() {
190: super ("IfPageExists");
191: registerTag(pageTitleTag);
192: registerTag(pageLinkTag);
193: }
194:
195: public boolean isVisible() {
196: return exists;
197: }
198:
199: public Object getValueForTag(TemplateTag tag) {
200: if (tag == pageLinkTag)
201: return pageLink;
202: if (tag == pageTitleTag)
203: return pageTitle;
204: return "";
205: }
206: }
207:
208: private class IfNoPageExists extends BasicContainer {
209: public IfNoPageExists() {
210: super ("IfNoPageExists");
211: }
212:
213: public boolean isVisible() {
214: return !exists;
215: }
216:
217: public Object getValueForTag(TemplateTag tag) {
218: return "";
219: }
220: }
221: }
|