001 /* sillyview : a free model-view-controller system for Java 002 * Copyright (C) 2004 T.J. Willis 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 006 * as published by the Free Software Foundation; either version 2 007 * of the License, or (at your option) any later version. 008 * 009 * This program is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 * GNU General Public License for more details. 013 * 014 * You should have received a copy of the GNU General Public License 015 * along with this program; if not, write to the Free Software 016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. 017 * 018 * $Header: /cvsroot/sillyview/sillyview/src/net/sourceforge/sillyview/StringBufferView.java,v 1.3 2004/05/22 07:58:27 tj_willis Exp $ 019 */ 020 package net.sourceforge.sillyview; 021 022 import javax.swing.*; 023 import javax.swing.event.HyperlinkListener; 024 import javax.swing.border.*; 025 import java.util.Map; 026 import java.util.Set; 027 028 /** 029 * A StringBuffer that has a backing WidgetModel to provide its data. Useful 030 * mostly for debugging and servlets. 031 * 032 * @author <a href="mailto:tj_willis@users.sourceforge.net">T.J. Willis</a> 033 * @version 1.0 034 */ 035 public class StringBufferView implements WidgetView { 036 /** 037 * This view's data. 038 * 039 */ 040 private StringBuffer buf; 041 042 /** 043 * The model this view gets its data from. 044 * 045 */ 046 private WidgetModel mod; 047 048 /** 049 * Creates a new <code>StringBufferView</code> instance. 050 * 051 * @param model a <code>WidgetModel</code> value 052 */ 053 public StringBufferView (WidgetModel model) { 054 mod = model; 055 buf = new StringBuffer (model.getCurrentModel ().toString ()); 056 } 057 058 /** 059 * Adds all the name/value pairs and then updates the model. If 060 * the named token already exists, replace the value. This is more 061 * efficient than many calls to setToken(), because model.getCurrentModel 062 * is only called once. 063 * 064 */ 065 public void addTokens (final Map < Object, Object > props) { 066 if (props == null) { 067 return; // FIXME: clear all tokens in model 068 } 069 Set < Object > keys = props.keySet (); 070 for (Object k:keys) { 071 String key = k.toString (); 072 mod.setToken (key, props.get (key)); 073 } 074 buf = new StringBuffer (mod.getCurrentModel ().toString ()); 075 076 } 077 078 /** 079 * Sets the named token to the given value. Most tokens will be 080 * propegated to the backing model. Resets the StringBuffer's 081 * contents to model.getCurrentModel().toString(). 082 * 083 * @param key an <code>Object</code> value 084 * @param value an <code>Object</code> value 085 */ 086 public void setToken (final Object key, Object value) { 087 if (key == null) 088 return; 089 mod.setToken (key, value); 090 buf = new StringBuffer (mod.getCurrentModel ().toString ()); 091 } 092 093 /** 094 * Gets the value of the named token. Most values will be taken 095 * from the backing model. 096 * 097 * @param key an <code>Object</code> value 098 * @return an <code>Object</code> value 099 */ 100 public Object getValue (final Object key) { 101 if (key == null) 102 return null; 103 return mod.getValue (key); 104 } 105 106 /** 107 * Returns the backing model. 108 * 109 * @return a <code>WidgetModel</code> value 110 */ 111 public WidgetModel getModel () { 112 return mod; 113 } 114 115 /** 116 * Sets the backing model. 117 * 118 * @param newModel a <code>WidgetModel</code> value 119 */ 120 public void setModel (WidgetModel newModel) { 121 mod = newModel; 122 } 123 124 /** 125 * Uses StringBuffer.equals() to test for equality. 126 * 127 * @param o an <code>Object</code> value 128 * @return a <code>boolean</code> value 129 */ 130 public boolean equals (Object o) { 131 if (buf == null && o == null) 132 return true; 133 if (buf == null) 134 return o.equals (buf); 135 return buf.equals (o); 136 } 137 138 public String toString(){ 139 return buf.toString(); 140 } 141 }