Source code: com/virtuosotechnologies/asaph/standardmodel/StdStandardSongBlock.java
1 /*
2 ================================================================================
3
4 FILE: StdStandardSongBlock.java
5
6 PROJECT:
7
8 Asaph
9
10 CONTENTS:
11
12 Standard implementation of StandardSongBlock
13
14 PROGRAMMERS:
15
16 Daniel Azuma (DA) <dazuma@kagi.com>
17
18 COPYRIGHT:
19
20 Copyright (C) 2003 Daniel Azuma (dazuma@kagi.com)
21
22 This program is free software; you can redistribute it and/or
23 modify it under the terms of the GNU General Public License as
24 published by the Free Software Foundation; either version 2
25 of the License, or (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public
33 License along with this program; if not, write to
34 Free Software Foundation, Inc.
35 59 Temple Place, Suite 330
36 Boston, MA 02111-1307 USA
37
38 ================================================================================
39 */
40
41
42 package com.virtuosotechnologies.asaph.standardmodel;
43
44
45 import java.io.IOException;
46 import java.util.Set;
47 import java.util.HashSet;
48 import java.util.Collections;
49 import java.util.Iterator;
50 import javax.swing.undo.AbstractUndoableEdit;
51 import javax.swing.undo.CannotUndoException;
52 import javax.swing.undo.CannotRedoException;
53 import javax.swing.event.UndoableEditListener;
54
55 import com.virtuosotechnologies.lib.util.StringID;
56 import com.virtuosotechnologies.lib.xml.XMLUnparser;
57
58 import com.virtuosotechnologies.asaph.model.StandardSongBlock;
59 import com.virtuosotechnologies.asaph.model.Variation;
60
61
62 /**
63 * Standard implementation of StandardSongBlock
64 */
65 /*package*/ class StdStandardSongBlock
66 extends BaseSongBlock
67 implements
68 StandardSongBlock
69 {
70 private Set omittingVariations_;
71
72
73 /**
74 * Constructor
75 */
76 /*package*/ StdStandardSongBlock(
77 StdSong parent,
78 StringID id)
79 {
80 this(parent, id, 0);
81 }
82
83
84 /**
85 * Constructor
86 */
87 /*package*/ StdStandardSongBlock(
88 StdSong parent,
89 StringID id,
90 int indentLevel)
91 {
92 super(parent, id, indentLevel);
93 omittingVariations_ = new HashSet();
94 }
95
96
97 /*package*/ void unparseVariationAttribute(
98 XMLUnparser unparser)
99 throws
100 IOException
101 {
102 if (!omittingVariations_.isEmpty())
103 {
104 StringBuffer buf = new StringBuffer();
105 for (Iterator iter = omittingVariations_.iterator(); iter.hasNext(); )
106 {
107 StdVariation variation = (StdVariation)iter.next();
108 buf.append(variation.getSerializableID());
109 if (iter.hasNext())
110 {
111 buf.append(' ');
112 }
113 }
114 unparser.addAttribute(XMLConstants.BLOCK_OMITVARIATIONS_ATTRIBUTE,
115 new String(buf));
116 }
117 }
118
119
120 //-------------------------------------------------------------------------
121 // Methods of StandardSongBlock
122 //-------------------------------------------------------------------------
123
124 /**
125 * Get a set of Variations in which this block is omitted.
126 *
127 * @return Set of Variations
128 */
129 public Set getOmittingVariations()
130 {
131 return Collections.unmodifiableSet(omittingVariations_);
132 }
133
134
135 /**
136 * Add a variation to the set of omitting variations.
137 *
138 * @param variation Variation under which to omit this block.
139 * @param undoListener listener to notify if an undoable edit is generated,
140 * or null to suppress generation of undoable edits
141 * @exception IllegalArgumentException variation is not present
142 * @exception NullPointerException variation was null
143 */
144 public void omitVariation(
145 final Variation variation,
146 UndoableEditListener undoListener)
147 {
148 if (variation.getSong() != getSong() || variation.isDefunct())
149 {
150 throw new IllegalArgumentException();
151 }
152 if (!omittingVariations_.contains(variation))
153 {
154 omittingVariations_.add(variation);
155 if (undoListener != null)
156 {
157 internalReportUndoableEdit(undoListener,
158 new AbstractUndoableEdit()
159 {
160 public void undo()
161 throws CannotUndoException
162 {
163 super.undo();
164 omittingVariations_.remove(variation);
165 }
166
167 public void redo()
168 throws CannotRedoException
169 {
170 super.redo();
171 omittingVariations_.add(variation);
172 }
173 });
174 }
175 }
176 }
177
178
179 /**
180 * Remove a variation from the set of omitting variations.
181 *
182 * @param variation Variation under which to no longer omit this block.
183 * @param undoListener listener to notify if an undoable edit is generated,
184 * or null to suppress generation of undoable edits
185 * @exception IllegalArgumentException variation is not present
186 * @exception NullPointerException variation was null
187 */
188 public void unOmitVariation(
189 final Variation variation,
190 UndoableEditListener undoListener)
191 {
192 if (variation.getSong() != getSong() || variation.isDefunct())
193 {
194 throw new IllegalArgumentException();
195 }
196 if (omittingVariations_.contains(variation))
197 {
198 omittingVariations_.remove(variation);
199 if (undoListener != null)
200 {
201 internalReportUndoableEdit(undoListener,
202 new AbstractUndoableEdit()
203 {
204 public void undo()
205 throws CannotUndoException
206 {
207 super.undo();
208 omittingVariations_.add(variation);
209 }
210
211 public void redo()
212 throws CannotRedoException
213 {
214 super.redo();
215 omittingVariations_.remove(variation);
216 }
217 });
218 }
219 }
220 }
221 }