001 package net.sourceforge.mp3dings.utils;
002
003 /* Mp3dings - manage mp3 meta-information
004 * Copyright (C) 2003 Moritz Ringler
005 * $Id: Bean.java,v 1.18 2007/01/30 15:58:36 ringler Exp $
006 *
007 * This program is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU General Public License
009 * as published by the Free Software Foundation; either version 2
010 * of the License, or (at your option) any later version.
011 *
012 * This program is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with this program; if not, write to the Free Software
019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020 */
021
022 import java.beans.BeanInfo;
023 import java.beans.IntrospectionException;
024 import java.beans.Introspector;
025 import java.beans.PropertyChangeEvent;
026 import java.beans.PropertyChangeListener;
027 import java.beans.PropertyChangeSupport;
028 import java.beans.PropertyDescriptor;
029 import java.beans.XMLDecoder;
030 import java.beans.XMLEncoder;
031 import java.io.BufferedInputStream;
032 import java.io.BufferedOutputStream;
033 import java.io.File;
034 import java.io.FileInputStream;
035 import java.io.FileOutputStream;
036 import java.io.IOException;
037 import java.io.OutputStream;
038 import java.io.Serializable;
039 import java.util.logging.Level;
040 import java.util.logging.Logger;
041 import java.util.prefs.Preferences;
042 import net.sourceforge.mp3dings.Globals;
043
044 /** A base class for beans.
045 * @author Moritz Ringler
046 * @version $Revision: 1.18 $ ($Date: 2007/01/30 15:58:36 $)
047 */
048 public class Bean implements Serializable{
049 private static final Logger logger = Logger.getLogger(Bean.class.getPackage().getName());
050 PropertyChangeSupport pcs;
051 static final long serialVersionUID = -1913598826606569984L;
052
053 public final static long NO_ID = Long.MAX_VALUE;
054 public final static Long L_NO_ID = new Long(NO_ID);
055
056 public Bean(){
057 pcs = new PropertyChangeSupport(this);
058 }
059
060 public static Bean load(Preferences pref, String key){
061 Object result = null;
062 XMLDecoder d = null;
063 String beanPath = pref.get(key, null);
064 return load((beanPath == null)?null:new File(beanPath));
065 }
066
067 public static Bean load(File f){
068 if(f == null){
069 return null;
070 }
071 Object result = null;
072 XMLDecoder d = null;
073 try{
074 d = new XMLDecoder(new BufferedInputStream(new FileInputStream(f)));
075 result = d.readObject();
076 } catch (Exception ex){
077 String type=(result == null)?"":" of type "+result.getClass();
078 logger.severe("Error loading Bean" + type +
079 " from " + f.toString());
080 logger.severe(ex.getMessage());
081 } finally {
082 if(d != null){
083 d.close();
084 }
085 }
086 return (result instanceof Bean)? (Bean) result : null;
087 }
088
089 public void save(Preferences pref, String key, String savePath){
090 if(!isSavable()){
091 return;
092 }
093 if (savePath == null){
094 savePath = Globals.getDir("HOME") + pref.absolutePath();
095 }
096 File f = new File(savePath);
097 if(!f.isAbsolute()){
098 f = new File(Globals.getDir("HOME"), f.getPath());
099 }
100 f.mkdirs();
101 f = new File(f,key+".xml");
102 savePath = f.getAbsolutePath();
103 OutputStream out = null;
104 XMLEncoder e = null;
105 try{
106 out = new BufferedOutputStream(new FileOutputStream(f));
107 e = new XMLEncoder(out);
108 e.writeObject(this);
109 e.close();
110 } catch (IOException ex){
111 logger.severe("Error saving bean " + getName() + " of type "
112 + getClass().getName());
113 Globals.logStackTrace(logger, Level.SEVERE, ex);
114 return;
115 } finally {
116 try{
117 if(e != null){
118 e.close();
119 }
120 if( out != null){
121 out.close();
122 }
123 //System.out.println(getClass()+":"+getName()+":"+key+":"+savePath);
124 pref.put(key, savePath);
125 } catch (IOException ex){
126 logger.severe("Error saving bean " + getName() + " of type "
127 + getClass().getName());
128 Globals.logStackTrace(logger, Level.SEVERE, ex);
129 }
130 }
131 }
132
133 /** @param key the key under which this bean is to be saved.
134 * This key must be a valid filename, so you should keep it
135 * as simple as possible. **/
136 public void save(Preferences pref, String key){
137 save(pref, key, null);
138 }
139
140 public Object[] getConstraints(String property){
141 return null;
142 }
143
144 /* name property */
145 private String name = "";
146 public final static String PROPERTY_NAME = "name";
147
148 public void setName(String s){
149 String oldname = name;
150 name = s;
151 if(!oldname.equals(name)){
152 firePropertyChange(PROPERTY_NAME, oldname, name);
153 }
154 }
155
156 public String getName(){
157 return name;
158 }
159
160 /* id property */
161 private Long id = L_NO_ID;
162 public final static String PROPERTY_ID = "id";
163
164 public void setId(long id){
165 Long oldid = this.id;
166 this.id = new Long(id);
167 firePropertyChange(PROPERTY_ID, oldid, this.id);
168 }
169
170 /* transient */
171 public boolean isSavable(){
172 return true;
173 }
174
175
176 public long getId(){
177 return id.longValue();
178 }
179
180 public void addPropertyChangeListener(PropertyChangeListener l){
181 pcs.addPropertyChangeListener(l);
182 }
183
184 public void removePropertyChangeListener(PropertyChangeListener l){
185 pcs.removePropertyChangeListener(l);
186 }
187
188 public PropertyChangeListener[] getPropertyChangeListeners(){
189 return pcs.getPropertyChangeListeners();
190 }
191
192
193 public void addPropertyChangeListener(String propertyName,
194 PropertyChangeListener listener) {
195 pcs.addPropertyChangeListener(propertyName, listener);
196 }
197
198 public void removePropertyChangeListener(
199 String propertyName,
200 PropertyChangeListener listener) {
201 pcs.removePropertyChangeListener(propertyName, listener);
202 }
203
204 public PropertyChangeListener[] getPropertyChangeListeners(String propertyName){
205 return pcs.getPropertyChangeListeners(propertyName);
206 }
207
208 public void firePropertyChange(String propertyName,
209 Object oldValue, Object newValue){
210 pcs.firePropertyChange(propertyName, oldValue, newValue);
211 }
212
213 public void firePropertyChange(String propertyName,
214 int oldValue, int newValue){
215 pcs.firePropertyChange(propertyName, oldValue, newValue);
216 }
217
218 public void firePropertyChange(String propertyName,
219 boolean oldValue, boolean newValue) {
220 pcs.firePropertyChange(propertyName, oldValue, newValue);
221 }
222
223 public void firePropertyChange(PropertyChangeEvent evt){
224 pcs.firePropertyChange(evt);
225 }
226
227 /*
228 public void fireIndexedPropertyChange(String propertyName, int index,
229 Object oldValue, Object newValue) {
230 pcs..fireIndexedPropertyChange(propertyName, index, oldValue, newValue);
231 }
232 */
233
234
235 /*
236 public void setXXX(type XXX){
237 this.xxx = xxx;
238 }
239
240 public type getXXX(){
241 return xxx;
242 } */
243
244 static {
245 try {
246 // Mark some properties transient
247 BeanInfo info = Introspector.getBeanInfo(Bean.class);
248 PropertyDescriptor[] propertyDescriptors = info.getPropertyDescriptors();
249 for (int i = 0; i < propertyDescriptors.length; ++i) {
250 PropertyDescriptor pd = propertyDescriptors[i];
251 if (pd.getName().equals("valid")) {
252 pd.setValue("transient", Boolean.TRUE);
253 }
254 }
255 } catch (IntrospectionException e) {
256 }
257 }
258 }