001: /*
002: * Copyright (c) 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.binding.tutorial;
032:
033: import java.util.Arrays;
034: import java.util.List;
035:
036: import com.jgoodies.binding.beans.Model;
037:
038: /**
039: * Describes a musical Album and provides bound bean properties.
040: * This class is used throughout the different tutorial examples.<p>
041: *
042: * This class has not been marked as final although it is not intended
043: * to be subclassed. However some persistency frameworks (like Hibernate)
044: * can optimize the data transfer and locking for extensible classes only.
045: *
046: * @author Karsten Lentzsch
047: * @version $Revision: 1.8 $
048: */
049: public class Album extends Model {
050:
051: // Examples ***************************************************************
052:
053: /** An example Album. */
054: public static final Album ALBUM1 = createExample1();
055:
056: /** An example Album. */
057: public static final Album ALBUM2 = createExample2();
058:
059: /** An example Album. */
060: public static final Album ALBUM3 = createExample3();
061:
062: /** An example Album. */
063: public static final Album ALBUM4 = createExample4();
064:
065: /** A List of Albums made of the examples 1 to 4. */
066: public static final List<Album> ALBUMS = Arrays.asList(new Album[] {
067: ALBUM1, ALBUM2, ALBUM3, ALBUM4 });
068:
069: // Names of the Bound Bean Properties *************************************
070:
071: public static final String PROPERTYNAME_ARTIST = "artist";
072: public static final String PROPERTYNAME_CLASSICAL = "classical";
073: public static final String PROPERTYNAME_COMPOSER = "composer";
074: public static final String PROPERTYNAME_TITLE = "title";
075:
076: // Instance Fields ********************************************************
077:
078: /**
079: * This Album's title as associated with its ISBN,
080: * for example "Symphony No. 5".
081: */
082: private String title;
083:
084: /**
085: * Holds this Album's artist, for example: "Albert Ayler",
086: * or "Berliner Philharmoniker".
087: */
088: private String artist;
089:
090: /**
091: * Describes if this Album is classical music; in this case
092: * it has a composer.
093: */
094: private boolean classical;
095:
096: /**
097: * Holds the composer of this Album's music, for example "Beethoven".
098: * Available if and only if this is a classical album.
099: */
100: private String composer;
101:
102: // Instance Creation ******************************************************
103:
104: /**
105: * Constructs an empty Album: empty title and artist, not classical
106: * and no composer set.
107: */
108: public Album() {
109: this ("", "");
110: }
111:
112: private Album(String title, String artist) {
113: setTitle(title);
114: setArtist(artist);
115: setClassical(false);
116: setComposer(null);
117: }
118:
119: private Album(String title, String artist, String composer) {
120: setTitle(title);
121: setArtist(artist);
122: setClassical(true);
123: setComposer(composer);
124: }
125:
126: // Creating Example Instances *********************************************
127:
128: private static Album createExample1() {
129: return new Album("A Love Supreme", "John Coltrane");
130: }
131:
132: private static Album createExample2() {
133: return new Album("In a Silent Way", "Miles Davis");
134: }
135:
136: private static Album createExample3() {
137: return new Album("Sheik Yerbouti", "Frank Zappa");
138: }
139:
140: private static Album createExample4() {
141: return new Album("Tristan und Isolde",
142: "Berliner Philharmoniker", "Richard Wagner");
143: }
144:
145: // Accessors **************************************************************
146:
147: /**
148: * Returns this album's title, for example "A Love Supreme",
149: * or "Symphony No. 5".
150: *
151: * @return this album's title.
152: */
153: public String getTitle() {
154: return title;
155: }
156:
157: /**
158: * Returns this album's artist, for example "Albert Ayler"
159: * or "Berliner Philharmoniker".
160: *
161: * @return this album's artist.
162: */
163: public String getArtist() {
164: return artist;
165: }
166:
167: /**
168: * Answers whether this is a classical album or not.
169: *
170: * @return true if this album is classical, false if not
171: */
172: public boolean isClassical() {
173: return classical;
174: }
175:
176: /**
177: * Returns this album's composer - if any, for example "Richard Wagner".
178: * A composer is available if and only if this is a classical album.
179: *
180: * @return the composer of this album's music.
181: *
182: * @see #isClassical
183: */
184: public String getComposer() {
185: return composer;
186: }
187:
188: /**
189: * Sets this album's title and notifies observers
190: * if the title changed.
191: *
192: * @param title The title to set.
193: */
194: public void setTitle(String title) {
195: Object oldValue = getTitle();
196: this .title = title;
197: firePropertyChange(PROPERTYNAME_TITLE, oldValue, title);
198: }
199:
200: /**
201: * Sets a new artist and notifies observers if the artist changed.
202: *
203: * @param artist The artist to set.
204: */
205: public void setArtist(String artist) {
206: String oldValue = getArtist();
207: this .artist = artist;
208: firePropertyChange(PROPERTYNAME_ARTIST, oldValue, artist);
209: }
210:
211: /**
212: * Sets this album's classical property and notifies observers
213: * about changes. If not classical the composer is set to <code>null</code>.
214: *
215: * @param classical true to indicate that this album is classical
216: */
217: public void setClassical(boolean classical) {
218: boolean oldValue = isClassical();
219: this .classical = classical;
220: firePropertyChange(PROPERTYNAME_CLASSICAL, oldValue, classical);
221: if (!classical) {
222: setComposer(null);
223: }
224: }
225:
226: /**
227: * Sets this album's composer and notifies observers if it has changed.
228: * A composer shall be set only if this is a classical album.
229: *
230: * @param composer The composer to set.
231: *
232: * @see #isClassical
233: */
234: public void setComposer(String composer) {
235: Object oldValue = getComposer();
236: this .composer = composer;
237: firePropertyChange(PROPERTYNAME_COMPOSER, oldValue, composer);
238: }
239:
240: // Misc *******************************************************************
241:
242: /**
243: * Returns a string representation of this album
244: * that contains the property values in a single text line.
245: *
246: * @return a string representation of this album
247: */
248: @Override
249: public String toString() {
250: StringBuilder builder = new StringBuilder("Album");
251: builder.append(" [title=");
252: builder.append(getTitle());
253: builder.append("; artist=");
254: builder.append(getArtist());
255: builder.append("; classical=");
256: builder.append(isClassical());
257: builder.append("; composer=");
258: builder.append(getComposer());
259: builder.append("]");
260: return builder.toString();
261: }
262:
263: /**
264: * Returns a string representation of this album
265: * that contains the property values.
266: *
267: * @return a string representation of this album
268: */
269: public String toWrappedString() {
270: StringBuilder builder = new StringBuilder("Album");
271: builder.append("[\ntitle=");
272: builder.append(getTitle());
273: builder.append(";\nartist=");
274: builder.append(getArtist());
275: builder.append(";\nclassical=");
276: builder.append(isClassical());
277: builder.append(";\ncomposer=");
278: builder.append(getComposer());
279: builder.append("\n]");
280: return builder.toString();
281: }
282:
283: }
|