001    package net.sourceforge.mp3dings.utils;
002    
003    /* Mp3dings - manage mp3 meta-information
004     * Copyright (C) 2003 Moritz Ringler
005     * $Id: RenamingPattern.java,v 1.13 2007/01/17 17:47:31 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.io.Serializable;
023    import java.text.NumberFormat;
024    import java.lang.reflect.Method;
025    import java.util.Arrays;
026    import java.util.Iterator;
027    import java.util.ArrayList;
028    import java.util.List;
029    import javax.swing.AbstractListModel;
030    import javax.swing.ListModel;
031    import net.sourceforge.mp3dings.Field;
032    import net.sourceforge.mp3dings.filter.Filter;
033    import net.sourceforge.mp3dings.DataTableModel;
034    import net.sourceforge.mp3dings.utils.CaseChanger;
035    
036    /* TODO:
037     * - saving and loading
038     */
039    
040    /** A pattern that controls renaming of mp3 files. **/
041    public class RenamingPattern implements Serializable{
042        static final long serialVersionUID = -890886471037265670L;
043        private List blocks = new ArrayList();
044        private static class RenamingPatternListModel extends AbstractListModel{
045            protected final RenamingPattern pattern;
046    
047            public RenamingPatternListModel(RenamingPattern pattern){
048                this.pattern = pattern;
049            }
050    
051            public Object getElementAt(int index){
052                return pattern.getElementAt(index);
053            }
054    
055            public int getSize(){
056                return pattern.getSize();
057            }
058    
059            protected void fireIntervalRemoved(Object o, int i, int j){
060                super.fireIntervalRemoved(o, i, j);
061            }
062    
063            protected void fireIntervalAdded(Object o, int i, int j){
064                super.fireIntervalAdded(o, i, j);
065            }
066    
067            protected void fireContentsChanged(Object o, int i, int j){
068                super.fireContentsChanged(o, i, j);
069            }
070        }
071    
072        private final RenamingPatternListModel listmodel
073            = new RenamingPatternListModel(this);
074    
075        public RenamingPattern(){
076        }
077    
078        public static RenamingPattern newInstance(RenamingPattern pattern){
079            RenamingPattern copy = new RenamingPattern();
080            Iterator it = pattern.blocks.iterator();
081            while(it.hasNext()){
082                Block b = (Block) it.next();
083                Class bClass = b.getClass();
084                Block bcopy = null;
085                try{
086                    Method newInstance =
087                            bClass.getMethod("newInstance", new Class[]{bClass});
088                    bcopy = (Block) newInstance.invoke(null, new Object[]{b});
089                } catch (Exception ex){
090                    throw new RuntimeException(ex);
091                }
092                copy.blocks.add(bcopy);
093            }
094            return copy;
095        }
096    
097        public Object getElementAt(int index){
098            return blocks.get(index);
099        }
100    
101        public int getSize(){
102            return blocks.size();
103        }
104    
105        public ListModel asListModel(){
106            return listmodel;
107        }
108    
109        public Block remove(int index){
110            Object block = blocks.remove(index);
111            listmodel.fireIntervalRemoved(this, index, index);
112            return (Block) block;
113        }
114    
115        public void insert(int index, Block block){
116            blocks.add(index, block);
117            listmodel.fireIntervalAdded(this, index, index);
118        }
119    
120        public void move(int from, int to){
121            Object block = blocks.remove(from);
122            blocks.add(to, block);
123            listmodel.fireContentsChanged(this, from, to);
124        }
125    
126        public boolean equals(Object o){
127            boolean result = false;
128            if(o == this){
129                result = true;
130            } else if(o instanceof RenamingPattern){
131                RenamingPattern other = (RenamingPattern) o;
132                result = (other.blocks.equals(blocks));
133            }
134            return result;
135        }
136    
137        public int hashCode(){
138            int result = 59;
139            result = 37 * result + blocks.hashCode();
140            return result;
141        }
142    
143        public String toString(){
144            StringBuffer sb = new StringBuffer(256);
145            Iterator it = blocks.iterator();
146            while(it.hasNext()){
147                sb.append(it.next().toString());
148            }
149            return sb.toString();
150        }
151    
152        public void reset(){
153            blocks.clear();
154        }
155    
156        public String getExample(){
157            Iterator it = blocks.iterator();
158            StringBuffer sb = new StringBuffer(256);
159            while(it.hasNext()){
160                Block b = (Block) it.next();
161                sb.append(b.getExample());
162            }
163            return sb.toString();
164        }
165    
166        public String valueOf(DataTableModel data, int entry){
167            StringBuffer sb = new StringBuffer(256);
168            Iterator it = blocks.iterator();
169            while(it.hasNext()){
170                Block block = (Block) it.next();
171                sb.append(block.valueOf(data, entry));
172            }
173            return sb.toString();
174        }
175    
176    /*     public Vector getBlocks(){
177            return blocks;
178        } */
179    
180    
181        /** Every instantiable class XXXBlock implementing block must have a
182           <code>public static XXXBlock newInstance(XXXBlock b)<code>
183           method that returns a deep copy of its argument.**/
184        public static interface Block extends Serializable{
185            public Object getExample();
186    
187            public Object valueOf(DataTableModel data, int entry);
188        };
189    
190        public static final class ConstantBlock implements Block{
191            static final long serialVersionUID = -5047856580154329649L;
192            private String value;
193    
194            public ConstantBlock(String value){
195                this.value = value;
196            }
197    
198            public boolean equals(Object o){
199                boolean result = false;
200                if(o == this){
201                    result = true;
202                } else if(o instanceof ConstantBlock){
203                    ConstantBlock other = (ConstantBlock) o;
204                    result = other.value.equals(value);
205                }
206                return result;
207            }
208    
209            public void setValue(String s){
210                value = s;
211            }
212    
213            public String toString(){
214                return value;
215            }
216    
217            public Object valueOf(DataTableModel data, int entry){
218                return value;
219            }
220    
221            public int hashCode(){
222                int result = 59;
223                result = 37 * result + value.hashCode();
224                return result;
225            }
226    
227            public Object getExample(){
228                return toString();
229            }
230    
231            public static ConstantBlock newInstance(ConstantBlock b){
232                return new ConstantBlock(b.value);
233            }
234        }
235    
236        public static abstract class VarBlock implements Block{
237            static final long serialVersionUID = 8332320158072596098L;
238            protected final Field field;
239            protected List filters = new ArrayList();
240    
241            protected VarBlock(Field field){
242                this.field = field;
243            }
244    
245            public List getFilters(){
246                return filters;
247            }
248    
249            public void setFilters(Object[] filt){
250                if(filt != null){
251                    filters.clear();
252                    filters.addAll(Arrays.asList(filt));
253                }
254            }
255    
256            public Field getField(){
257                return field;
258            }
259    
260            public String toString(){
261                return field.longname();
262            }
263    
264            public static VarBlock getInstance(Field field){
265                VarBlock result = null;
266                switch(field.type()){
267                    case Field.TYPE_LONGTEXT:
268                        //falls through
269                    case Field.TYPE_STRING:
270                        result = new StringVarBlock(field);
271                        break;
272                    case Field.TYPE_NUMERIC:
273                        result = new NumberVarBlock(field);
274                        break;
275                }
276                return result;
277            }
278    
279            private Object filter(Object in){
280                Object result = in;
281                Iterator it = filters.iterator();
282                while(result != null && it.hasNext()){
283                    Object filter = it.next();
284                    if(filter instanceof Filter){
285                        result = ((Filter) filter).filter(result);
286                    }
287                }
288                return result;
289            }
290    
291            abstract protected Object process(Object in);
292    
293            public Object valueOf(DataTableModel data, int entry){
294                Object result = data.getFieldValue(entry, field);
295                return process(filter(result));
296            }
297    
298            public Object getExample(){
299                Object result = field.example();
300                return process(filter(result));
301            }
302    
303            public boolean isFiltered(){
304                return (filters != null && filters.size() != 0);
305            }
306    
307        }
308    
309        public static class StringVarBlock extends VarBlock{
310            static final long serialVersionUID = 1294435033533661184L;
311            private int ccmode = CaseChanger.NO_CHANGE;
312    
313            public StringVarBlock(Field field){
314                super(field);
315            }
316    
317            public static StringVarBlock newInstance(StringVarBlock b){
318                StringVarBlock copy = new StringVarBlock(b.field);
319                copy.filters.addAll(b.filters);
320                copy.ccmode = b.ccmode;
321                return copy;
322            }
323    
324            public void setCaseChangeMode(int mode){
325                ccmode = mode;
326            }
327    
328            public int getCaseChangeMode(){
329                return ccmode;
330            }
331    
332            public boolean equals(Object o){
333                boolean result = false;
334                if(o == this){
335                    result = true;
336                } else if(o instanceof StringVarBlock){
337                    StringVarBlock other = (StringVarBlock) o;
338                    result = (other.field == field);
339                    result = result && (other.ccmode == ccmode);
340                    /* filters is always non-null */
341                    result = result && other.filters.equals(filters);
342                }
343                return result;
344            }
345    
346            public int hashCode(){
347                int result = 59;
348                result = 37 * result + field.ordinal();
349                result = 37 * result + ccmode;
350                result = 37 * result + filters.hashCode();
351                return result;
352            }
353    
354            protected Object process(Object in){
355                Object result = in;
356                if(result != null){
357                    result = CaseChanger.changeCase(String.valueOf(result), ccmode);
358                }
359                return result;
360            }
361    
362            public boolean isFiltered(){
363                return (ccmode != CaseChanger.NO_CHANGE || super.isFiltered());
364            }
365        }
366    
367        public static class NumberVarBlock extends VarBlock{
368            static final long serialVersionUID = -792211010329196813L;
369            private NumberFormat format = null;
370    
371            public NumberVarBlock(Field field){
372                super(field);
373            }
374    
375            public static NumberVarBlock newInstance(NumberVarBlock b){
376                NumberVarBlock copy = new NumberVarBlock(b.field);
377                copy.filters.addAll(b.filters);
378                if(b.format != null){
379                    copy.format = (NumberFormat) b.format.clone();
380                }
381                return copy;
382            }
383    
384            public void setFormat(NumberFormat f){
385                format = f;
386            }
387    
388            public NumberFormat getFormat(){
389                return format;
390            }
391    
392            public boolean equals(Object o){
393                boolean result = false;
394                if(o == this){
395                    result = true;
396                } else if(o instanceof NumberVarBlock){
397                    NumberVarBlock other = (NumberVarBlock) o;
398                    result = (other.field == field);
399                    if(format == null){
400                        result = result && (other.format == null);
401                    } else {
402                        result = result && format.equals(other.format);
403                    }
404                    /* filters is always non-null */
405                    result = result && other.filters.equals(filters);
406                }
407                return result;
408            }
409    
410            public int hashCode(){
411                int result = 59;
412                result = 37 * result + field.ordinal();
413                result = 37 * result + ((format == null)? 0 : format.hashCode());
414                result = 37 * result + filters.hashCode();
415                return result;
416            }
417    
418            protected Object process(Object in){
419                Object result = in;
420                if(result instanceof Number){
421                    if(format == null){
422                        result = result.toString();
423                    } else {
424                        if(result instanceof Integer){
425                            result = format.format((long) ((Integer) result).intValue());
426                        } else if (result instanceof Long){
427                            result = format.format(((Long) result).longValue());
428                        } else if (result instanceof Float){
429                            result = format.format((double) ((Float) result).floatValue());
430                        } else if (result instanceof Double){
431                            result = format.format(((Double) result).doubleValue());
432                        }
433                    }
434                }
435                return result;
436            }
437        }
438    }