001: //The Salmon Open Framework for Internet Applications (SOFIA)
002: //Copyright (C) 1999 - 2002, Salmon LLC
003: //
004: //This program is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU General Public License version 2
006: //as published by the Free Software Foundation;
007: //
008: //This program is distributed in the hope that it will be useful,
009: //but WITHOUT ANY WARRANTY; without even the implied warranty of
010: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: //GNU General Public License for more details.
012: //
013: //You should have received a copy of the GNU General Public License
014: //along with this program; if not, write to the Free Software
015: //Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
016: //
017: //For more information please visit http://www.salmonllc.com
018:
019: package com.salmonllc.examples.example16;
020:
021: import com.salmonllc.jsp.*;
022: import com.salmonllc.examples.SiteMapConstants;
023: import com.salmonllc.html.*;
024: import com.salmonllc.html.events.*;
025: import com.salmonllc.personalization.Skin;
026: import com.salmonllc.personalization.SkinManager;
027: import com.salmonllc.personalization.Attribute;
028: import com.salmonllc.properties.*;
029: import com.salmonllc.servlets.PersonalizationServer;
030:
031: import java.util.*;
032:
033: /**
034: * A base controller for skin data entry. It provides the functionality to build a GUI, load and save and apply skins.
035: */
036: public abstract class CustomizeSkinController extends JspController
037: implements SubmitListener, PageListener {
038:
039: public HtmlSubmitButton _cancel;
040: public HtmlSubmitButton _save;
041:
042: private String _skinName;
043: private Skin _skin;
044: private Properties _skinProperties;
045: private Hashtable _components = new Hashtable();
046: protected HtmlTableCellProperties _alignTop;
047: protected static final int TYPE_INT = 0;
048: protected static final int TYPE_COLOR = 1;
049: protected static final int TYPE_FONT = 2;
050: protected static final int TYPE_STYLE_FONT = 3;
051: private String _retParm = "webSkin";
052: private String _skinPrefix = "browser";
053: private String _firstTab = "page";
054: private boolean _swing = false;
055:
056: /**
057: * an inner class that contains info on each component added with the add property method
058: */
059: private class ComponentEntry {
060: public ComponentEntry(HtmlComponent comp, int type) {
061: this .comp = comp;
062: this .type = type;
063: }
064:
065: public ComponentEntry(HtmlComponent comp, HtmlComponent comp2,
066: int type) {
067: this .comp = comp;
068: this .comp2 = comp2;
069: this .type = type;
070: }
071:
072: int type;
073: HtmlComponent comp;
074: HtmlComponent comp2;
075: }
076:
077: /**
078: *set up the page
079: */
080: public void initialize() {
081: addPageListener(this );
082: _save.addSubmitListener(this );
083: _cancel.addSubmitListener(this );
084: _alignTop = new HtmlTableCellProperties();
085: _alignTop.setVertAlign(HtmlTable.VALIGN_TOP);
086: }
087:
088: /**
089: *fired when the user clicks on a button or a submit link
090: */
091: public boolean submitPerformed(SubmitEvent e) throws Exception {
092: if (e.getSource() instanceof JspLink) {
093: showTab(e.getComponent().getName());
094: scrollToItem("#Top");
095: } else if (e.getSource() == _save) {
096: saveChanges();
097: gotoSiteMapPage(SiteMapConstants.PERSONALIZATION, "?"
098: + _retParm + "=" + _skinName);
099: } else if (e.getSource() == _cancel) {
100: gotoSiteMapPage(SiteMapConstants.PERSONALIZATION);
101: } else {
102: applyChanges();
103: scrollToItem("#Top");
104: }
105: return true;
106: }
107:
108: /**
109: *fired each time the page is requested. If we are comming from the Personilization page, load up the skin specified in the skin parm
110: */
111: public void pageRequested(PageEvent p) {
112: if (!isReferredByCurrentPage()) {
113: String skinName = getParameter("skin");
114: if (skinName != null) {
115: _skinName = skinName;
116: SkinManager man = SkinManager
117: .getSkinManager(getApplicationName());
118: _skin = new Skin();
119: man.load(_skinName, _skin);
120: _skinProperties = _skin.getProperties();
121: if (!_swing) {
122: Enumeration en = _components.keys();
123: setSkin(_skin);
124: while (en.hasMoreElements()) {
125: String key = (String) en.nextElement();
126: ComponentEntry entry = (ComponentEntry) _components
127: .get(key);
128: if (entry.type == TYPE_COLOR) {
129: ((HtmlColorChooser) entry.comp)
130: .setColorValue(_skinProperties
131: .getProperty(key));
132: } else if (entry.type == TYPE_FONT) {
133: ((HtmlColorChooser) entry.comp)
134: .setFontValue(_skinProperties
135: .getProperty(key
136: + Props.TAG_START));
137: ((HtmlFontChooser) entry.comp2)
138: .setFontValue(_skinProperties
139: .getProperty(key
140: + Props.TAG_START));
141: } else if (entry.type == TYPE_STYLE_FONT) {
142: ((HtmlColorChooser) entry.comp)
143: .setStyleValue(_skinProperties
144: .getProperty(key));
145: ((HtmlFontChooser) entry.comp2)
146: .setStyleValue(_skinProperties
147: .getProperty(key));
148: } else if (entry.type == TYPE_INT) {
149: ((HtmlTextEdit) entry.comp)
150: .setValue(_skinProperties
151: .getProperty(key));
152: }
153: }
154: } else {
155: Enumeration en = _components.keys();
156: while (en.hasMoreElements()) {
157: ComponentEntry entry = (ComponentEntry) _components
158: .get(en.nextElement());
159: if (entry.comp != null)
160: ((HtmlFormComponent) entry.comp)
161: .setValue(null);
162: if (entry.comp2 != null)
163: ((HtmlFormComponent) entry.comp2)
164: .setValue(null);
165: }
166: Attribute[] att = _skin
167: .getInstanceAttributes("SMetalTheme");
168: PersonalizationServer.setTempSkin(getSession(),
169: _skin);
170: for (int i = 0; i < att.length; i++) {
171: String key = att[i].getAttribute();
172: ComponentEntry entry = (ComponentEntry) _components
173: .get(key);
174: if (entry != null) {
175: if (entry.type == TYPE_COLOR)
176: ((HtmlColorChooser) entry.comp)
177: .setRGBValue(att[i].getValue());
178: else if (entry.type == TYPE_FONT)
179: ((HtmlFontChooser) entry.comp)
180: .setValue(att[i].getValue());
181: }
182: }
183: }
184: } else {
185: _skin = new Skin();
186: _skinProperties = _skin.getProperties();
187: _skinName = null;
188:
189: }
190: showTab(_firstTab);
191: }
192: }
193:
194: public void pageRequestEnd(PageEvent p) {
195: }
196:
197: public void pageSubmitEnd(PageEvent p) {
198: }
199:
200: public void pageSubmitted(PageEvent p) {
201: }
202:
203: /**
204: *save the changes on the skin to the file system. This saveChanges saves the data under the name prefixUser#, but in a real application with a user login it should save it as prefixUserName.
205: */
206: private void saveChanges() {
207: applyChanges();
208: SkinManager man = SkinManager
209: .getSkinManager(getApplicationName());
210: if (_skinName == null || _skinName.indexOf("User") == -1) {
211: int maxNumber = 1;
212: String names[] = man.getSkinNames();
213: String prefix = _skinPrefix + "User";
214: for (int i = 0; i < names.length; i++) {
215: if (names[i].startsWith(prefix)) {
216: try {
217: int no = Integer.parseInt(names[i]
218: .substring(prefix.length()));
219: if (no >= maxNumber)
220: maxNumber = no + 1;
221: } catch (Exception ex) {
222: }
223: }
224: }
225: _skinName = prefix + maxNumber;
226: }
227: man.save(_skinName, _skin);
228: }
229:
230: /**
231: * Copies the values from the form components into the skin properties
232: */
233: private void applyChanges() {
234: Enumeration en = _components.keys();
235: while (en.hasMoreElements()) {
236: String key = (String) en.nextElement();
237: ComponentEntry entry = (ComponentEntry) _components
238: .get(key);
239: if (!_swing) {
240: if (entry.type == TYPE_COLOR) {
241: String colorValue = null;
242: colorValue = ((HtmlColorChooser) entry.comp)
243: .getValueAsHtml();
244: if (colorValue == null)
245: _skinProperties.remove(key);
246: else
247: _skinProperties.put(key, colorValue);
248: } else if (entry.type == TYPE_FONT) {
249: String colorValue = ((HtmlColorChooser) entry.comp)
250: .getValueAsHtml();
251: String fontStartTag = null;
252: String fontEndTag = null;
253: fontStartTag = ((HtmlFontChooser) entry.comp2)
254: .getValueAsFontStartTag(colorValue);
255: fontEndTag = ((HtmlFontChooser) entry.comp2)
256: .getValueAsFontEndTag();
257: if (fontStartTag == null) {
258: _skinProperties.remove(key + Props.TAG_START);
259: _skinProperties.remove(key + Props.TAG_END);
260: } else {
261: _skinProperties.put(key + Props.TAG_START,
262: fontStartTag);
263: _skinProperties.put(key + Props.TAG_END,
264: fontEndTag);
265: }
266:
267: } else if (entry.type == TYPE_STYLE_FONT) {
268: String colorValue = ((HtmlColorChooser) entry.comp)
269: .getValueAsHtml();
270: String font = ((HtmlFontChooser) entry.comp2)
271: .getValueAsStyle(colorValue);
272: if (font == null)
273: _skinProperties.remove(key);
274: else
275: _skinProperties.put(key, font);
276: } else if (entry.type == TYPE_INT) {
277: String val = ((HtmlTextEdit) entry.comp).getValue();
278: if (val != null) {
279: try {
280: Integer.parseInt(val);
281: _skinProperties.put(key, val);
282: } catch (Exception ex) {
283: }
284: } else
285: _skinProperties.remove(key);
286: }
287: } else {
288: if (entry.type == TYPE_COLOR) {
289: String colorValue = null;
290: colorValue = ((HtmlColorChooser) entry.comp)
291: .getValueAsRGB();
292: if (colorValue != null)
293: _skin.setInstanceAttribute("SMetalTheme", key,
294: colorValue);
295: } else if (entry.type == TYPE_FONT) {
296: String font = ((HtmlFontChooser) entry.comp)
297: .getValue();
298: if (font != null)
299: _skin.setInstanceAttribute("SMetalTheme", key,
300: font);
301: }
302: }
303: }
304:
305: if (_swing)
306: PersonalizationServer.setTempSkin(getSession(), _skin);
307: else
308: setSkin(_skin);
309:
310: }
311:
312: /**
313: * Used to build the user interface dynamically, call this method once for each property you want to edit
314: * @param tab The table the property will go in
315: * @param row The row on the table to place the property component
316: * @param caption The caption to display
317: * @param property The property you are editing
318: * @param type One of the TYPE constants described at the top of the class
319: * @param separator true to put a seperator bar after the component
320: * @return the row in the table to put the next component
321: */
322: protected int addProperty(HtmlTable tab, int row, String caption,
323: String property, int type, boolean separator) {
324: tab.setComponentAt(row, 0, new HtmlText(caption + ":", this ),
325: _alignTop);
326: if (type == TYPE_COLOR) {
327: HtmlColorChooser chooser = new HtmlColorChooser("property"
328: + property, this );
329: tab.setComponentAt(row++, 1, chooser);
330: if (separator)
331: tab.setComponentAt(row++, 1, new HtmlLine(this ));
332: _components
333: .put(property, new ComponentEntry(chooser, type));
334: } else if (type == TYPE_FONT || type == TYPE_STYLE_FONT) {
335: if (_swing) {
336: HtmlFontChooser fontChooser = new HtmlFontChooser(
337: "fontProperty" + property, this );
338: tab.setComponentAt(row++, 1, fontChooser);
339: if (separator)
340: tab.setComponentAt(row++, 1, new HtmlLine(this ));
341: _components.put(property, new ComponentEntry(
342: fontChooser, type));
343: } else {
344: HtmlColorChooser colorChooser = new HtmlColorChooser(
345: "colorProperty" + property, this );
346: HtmlFontChooser fontChooser = new HtmlFontChooser(
347: "fontProperty" + property, this );
348: tab.setComponentAt(row++, 1, colorChooser);
349: tab.setComponentAt(row++, 1, fontChooser);
350: if (separator)
351: tab.setComponentAt(row++, 1, new HtmlLine(this ));
352: _components.put(property, new ComponentEntry(
353: colorChooser, fontChooser, type));
354: }
355: } else if (type == TYPE_INT) {
356: HtmlTextEdit comp = new HtmlTextEdit(property + property,
357: this );
358: tab.setComponentAt(row++, 1, comp);
359: if (separator)
360: tab.setComponentAt(row++, 1, new HtmlLine(this ));
361: _components.put(property, new ComponentEntry(comp, type));
362: }
363: return row;
364: }
365:
366: /**
367: * display a particular table when the user clicks a link
368: */
369: protected abstract void showTab(String name);
370:
371: /**
372: * Call this from the initialize method to set whether the page is editing Swing skin or a web skin
373: * @param swing True if it is a web skin
374: * @param firstTab The name of the first tab to display
375: */
376: protected void setSwing(boolean swing, String firstTab) {
377: _swing = swing;
378: if (swing) {
379: _retParm = "swingSkin";
380: _skinPrefix = "swing";
381: _firstTab = firstTab;
382: } else {
383: _retParm = "webSkin";
384: _skinPrefix = "browser";
385: _firstTab = firstTab;
386: }
387:
388: }
389: }
|