KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > snip > label > CategoryLabel


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.snip.label;
27
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 import org.snipsnap.app.Application;
33 import org.snipsnap.container.Components;
34 import org.snipsnap.snip.Snip;
35 import org.snipsnap.snip.SnipLink;
36 import org.snipsnap.snip.SnipSpace;
37 import org.snipsnap.snip.SnipSpaceFactory;
38 import org.snipsnap.snip.label.BaseLabel;
39 import org.snipsnap.snip.label.Label;
40 import org.snipsnap.snip.label.Labels;
41 import org.snipsnap.user.AuthenticationService;
42 import org.snipsnap.util.URLEncoderDecoder;
43
44 /**
45  * Label which categoriezes snips
46  *
47  * @author Franziska Wohl
48  * @version $id$
49  */

50 public class CategoryLabel extends BaseLabel {
51
52   private String JavaDoc type = "CategoryLabel";
53
54   public CategoryLabel() {
55     name = "Category";
56     value = "";
57   }
58
59   public CategoryLabel(String JavaDoc value) {
60     this();
61     this.value = value;
62   }
63
64   /**
65    * Return type of Label
66    * @return
67    */

68   public String JavaDoc getType() {
69     return type;
70   }
71
72   /**
73    * Return a html input form fragment to
74    * input label data. See visual proxy pattern
75    *
76    * @return
77    */

78   public String JavaDoc getInputProxy() {
79     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
80     SnipSpace snipspace = (SnipSpace) Components.getComponent(SnipSpace.class);
81     List JavaDoc snipList = snipspace.getAll();
82
83     buffer.append("Category: ");
84     Iterator JavaDoc iterator = snipList.iterator();
85     buffer.append("<select name=\"label.value\" size=\"1\">");
86     while (iterator.hasNext()) {
87       Snip snip = (Snip) iterator.next();
88       Labels labels = snip.getLabels();
89       boolean noLabelsAll = labels.getAll().isEmpty();
90
91       if (!noLabelsAll) {
92         Collection JavaDoc LabelsCat;
93 // Search for all type labels
94
LabelsCat = labels.getLabels("TypeLabel");
95         if (!LabelsCat.isEmpty()) { //true = leer
96
Iterator JavaDoc iter = LabelsCat.iterator();
97           while (iter.hasNext()) {
98 // We only want snips with a label Type:Category
99
Label label = (Label) iter.next();
100             if (label.getValue().equals("Category")) {
101               String JavaDoc category = snip.getName();
102               buffer.append("<option>");
103               buffer.append(category);
104               buffer.append("</option>");
105             }
106           }
107         }
108       }
109     }
110     buffer.append("</select>");
111
112     return buffer.toString();
113   }
114
115   public String JavaDoc getListProxy() {
116     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
117     buffer.append("<td>");
118     buffer.append("Category:");
119     buffer.append("</td><td>");
120     getSnipLink(buffer, value);
121     buffer.append("</td><td>");
122     return buffer.toString();
123   }
124
125   /*
126    * Create s snipLink, should probably moved around
127    */

128   private StringBuffer JavaDoc getSnipLink(StringBuffer JavaDoc buffer, String JavaDoc name) {
129     AuthenticationService service = (AuthenticationService) Components.getComponent(AuthenticationService.class);
130
131     if (SnipSpaceFactory.getInstance().exists(name)) {
132       String JavaDoc[] strings = name.split("/");
133       String JavaDoc string = strings[strings.length - 1];
134       SnipLink.appendLink(buffer, name, string);
135     } else if (!service.isAuthenticated(Application.get().getUser())) {
136       buffer.append(name);
137     } else {
138       SnipLink.appendCreateLink(buffer, name);
139     }
140     return buffer;
141   }
142 }
143
Popular Tags