1 /*
2  * aTunes
3  * Copyright (C) Alex Aranda, Sylvain Gaudard and contributors
4  *
5  * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors
6  *
7  * http://www.atunes.org
8  * http://sourceforge.net/projects/atunes
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  */
20 
21 package net.sourceforge.atunes.kernel.modules.ui;
22 
23 import net.sourceforge.atunes.model.IFrame;
24 import net.sourceforge.atunes.model.IFrameFactory;
25 import net.sourceforge.atunes.model.IStateUI;
26 import net.sourceforge.atunes.utils.Logger;
27 
28 import org.springframework.context.ApplicationContext;
29 import org.springframework.context.ApplicationContextAware;
30 
31 /**
32  * Creates frame
33  *
34  * @author alex
35  *
36  */
37 public class FrameFactory implements IFrameFactory, ApplicationContextAware {
38 
39 	private IStateUI stateUI;
40 
41 	private String defaultFrameClass;
42 
43 	private ApplicationContext context;
44 
45 	/**
46 	 * @param stateUI
47 	 */
setStateUI(final IStateUI stateUI)48 	public void setStateUI(final IStateUI stateUI) {
49 		this.stateUI = stateUI;
50 	}
51 
52 	@Override
setApplicationContext( final ApplicationContext applicationContext)53 	public void setApplicationContext(
54 			final ApplicationContext applicationContext) {
55 		this.context = applicationContext;
56 	}
57 
58 	@Override
setDefaultFrameClass(final String defaultFrameClass)59 	public void setDefaultFrameClass(final String defaultFrameClass) {
60 		this.defaultFrameClass = defaultFrameClass;
61 	}
62 
63 	@Override
create()64 	public IFrame create() {
65 		IFrame frame = null;
66 		Class<? extends IFrame> clazz = stateUI.getFrameClass();
67 		if (clazz != null) {
68 			try {
69 				frame = clazz.newInstance();
70 			} catch (InstantiationException e) {
71 				Logger.error(e);
72 			} catch (IllegalAccessException e) {
73 				Logger.error(e);
74 			}
75 		}
76 
77 		if (frame == null) {
78 			frame = constructDefaultFrame();
79 			if (frame != null) {
80 				stateUI.setFrameClass(frame.getClass());
81 			}
82 		}
83 
84 		if (frame == null) {
85 			throw new IllegalArgumentException("Could not create main frame");
86 		}
87 
88 		frame.setApplicationContext(context);
89 
90 		return frame;
91 	}
92 
93 	/**
94 	 * Creates default frame
95 	 *
96 	 * @throws ClassNotFoundException
97 	 * @throws IllegalAccessException
98 	 * @throws InstantiationException
99 	 */
constructDefaultFrame()100 	private IFrame constructDefaultFrame() {
101 		IFrame frame = null;
102 		try {
103 			frame = (IFrame) Class.forName(defaultFrameClass).newInstance();
104 		} catch (InstantiationException e) {
105 			Logger.error(e);
106 		} catch (IllegalAccessException e) {
107 			Logger.error(e);
108 		} catch (ClassNotFoundException e) {
109 			Logger.error(e);
110 		}
111 		return frame;
112 	}
113 
114 }
115