Source code: com/virtuosotechnologies/asaph/standardmodel/StdChordSetKey.java
1 /*
2 ================================================================================
3
4 FILE: ChordSetKey.java
5
6 PROJECT:
7
8 Asaph
9
10 CONTENTS:
11
12 Standard implementation of ChordSetKey
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 javax.swing.undo.AbstractUndoableEdit;
47 import javax.swing.undo.CannotUndoException;
48 import javax.swing.undo.CannotRedoException;
49 import javax.swing.event.UndoableEditListener;
50 import org.xml.sax.SAXException;
51 import org.xml.sax.ErrorHandler;
52 import org.xml.sax.Locator;
53
54 import com.virtuosotechnologies.lib.xml.XMLUnparser;
55
56 import com.virtuosotechnologies.asaph.model.ChordSetKey;
57 import com.virtuosotechnologies.asaph.model.SimpleString;
58 import com.virtuosotechnologies.asaph.model.notation.Note;
59
60
61 /**
62 * Standard implementation of ChordSetKey
63 */
64 /*package*/ class StdChordSetKey
65 extends BaseSongMember
66 implements
67 ChordSetKey
68 {
69 private StdSimpleString infoString_;
70 private Note keyNote_;
71
72
73 /*package*/ StdChordSetKey(
74 StdChordSet parent,
75 Note keyNote,
76 String infoString)
77 {
78 super(parent);
79 infoString_ = new StdSimpleString(this, infoString);
80 keyNote_ = keyNote;
81 }
82
83
84 /*package*/ void unparse(
85 XMLUnparser unparser,
86 String element)
87 throws
88 IOException
89 {
90 unparser.startSingleLineElement(element);
91 unparser.addAttribute(XMLConstants.KEY_NOTE_ATTRIBUTE, keyNote_.generateString());
92 unparser.addString(infoString_.getString());
93 unparser.endElement(element);
94 }
95
96
97 /*package*/ class ParseHandler
98 extends ParseHandlerBase
99 {
100 /*package*/ ParseHandler(
101 ErrorHandler errorHandler,
102 Locator locator,
103 String localElement)
104 {
105 super(errorHandler, locator, localElement);
106 }
107
108 /*package*/ void localCharacters(
109 char[] chs,
110 int start,
111 int length)
112 throws
113 SAXException
114 {
115 infoString_.setString(infoString_.getString()+new String(chs, start, length), null);
116 }
117 }
118
119
120 //-------------------------------------------------------------------------
121 // Methods of ChordSetKey
122 //-------------------------------------------------------------------------
123
124 /**
125 * Get a SimpleString containing description info for the ChordSetKey.
126 * Every ChordSetKey has a description, even if it is the empty string.
127 *
128 * @return description info SimpleString
129 */
130 public SimpleString getInfoString()
131 {
132 return infoString_;
133 }
134
135
136 /**
137 * Get the note for key signature. The note should normally never be null,
138 * but this method may return null if the data is corrupted.
139 *
140 * @return key signature note
141 */
142 public Note getKeyNote()
143 {
144 return keyNote_;
145 }
146
147
148 /**
149 * Set the note for key signature
150 *
151 * @param note new note
152 * @param undoListener listener to notify if an undoable edit is generated,
153 * or null to suppress generation of undoable edits
154 * @exception NullPointerException note was null
155 */
156 public void setKeyNote(
157 final Note note,
158 UndoableEditListener undoListener)
159 {
160 if (note.equals(keyNote_))
161 {
162 return;
163 }
164 final Note oldNote = keyNote_;
165 keyNote_ = note;
166 if (undoListener != null)
167 {
168 internalReportUndoableEdit(undoListener,
169 new AbstractUndoableEdit()
170 {
171 public void undo()
172 throws CannotUndoException
173 {
174 super.undo();
175 keyNote_ = oldNote;
176 }
177
178 public void redo()
179 throws CannotRedoException
180 {
181 super.redo();
182 keyNote_ = note;
183 }
184 });
185 }
186 }
187 }