1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2010 Grigor Iliev <grigor@grigoriliev.com> |
5 |
* |
6 |
* This file is part of JSampler. |
7 |
* |
8 |
* JSampler is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License version 2 |
10 |
* as published by the Free Software Foundation. |
11 |
* |
12 |
* JSampler is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with JSampler; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
20 |
* MA 02111-1307 USA |
21 |
*/ |
22 |
|
23 |
|
24 |
package org.jsampler.view; |
25 |
|
26 |
import java.util.StringTokenizer; |
27 |
import java.util.Vector; |
28 |
|
29 |
import java.util.jar.Attributes; |
30 |
import java.util.jar.Manifest; |
31 |
|
32 |
import java.util.logging.Level; |
33 |
|
34 |
import javax.swing.SwingUtilities; |
35 |
|
36 |
import org.jsampler.CC; |
37 |
import org.jsampler.HF; |
38 |
|
39 |
/** |
40 |
* This class provides information about the available views in the current distribution. |
41 |
* @author Grigor Iliev |
42 |
*/ |
43 |
public class JSViews { |
44 |
private static Vector<ViewEntry> viewEntries = new Vector<ViewEntry>(); |
45 |
private static String currentView = null; |
46 |
private static String defaultView = null; |
47 |
|
48 |
/** Forbits instantiation of JSViews */ |
49 |
private |
50 |
JSViews() { } |
51 |
|
52 |
/** |
53 |
* Gets a list of all available views. |
54 |
* @return A list of all available views. |
55 |
*/ |
56 |
public static String[] |
57 |
getAvailableViews() { |
58 |
String[] views = new String[viewEntries.size()]; |
59 |
for(int i = 0; i < viewEntries.size(); i++) views[i] = viewEntries.get(i).name; |
60 |
return views; |
61 |
} |
62 |
|
63 |
/** |
64 |
* Gets the current view name of this distribution. |
65 |
* @return The current view name of this distribution. |
66 |
*/ |
67 |
public static String |
68 |
getCurrentView() { return currentView; } |
69 |
|
70 |
/** |
71 |
* Gets the default view name of this distribution. |
72 |
* @return The default view name of this distribution. |
73 |
*/ |
74 |
public static String |
75 |
getDefaultView() { return defaultView; } |
76 |
|
77 |
/** |
78 |
* Changes the current view. |
79 |
* This method should be invoked only from the event-dispatching thread. |
80 |
* @param viewName The new view to be used. |
81 |
*/ |
82 |
public static void |
83 |
setView(String viewName) { |
84 |
if(viewName == null) { |
85 |
CC.getLogger().info("viewName is null!"); |
86 |
return; |
87 |
} |
88 |
|
89 |
ViewEntry entry = null; |
90 |
|
91 |
for(ViewEntry ve : viewEntries) { |
92 |
if(viewName.equals(ve.name)) { |
93 |
entry = ve; |
94 |
break; |
95 |
} |
96 |
} |
97 |
|
98 |
if(entry == null) { |
99 |
CC.getLogger().info("Missing view: " + viewName); |
100 |
return; |
101 |
} |
102 |
if(CC.getMainFrame() != null) { |
103 |
SwingUtilities.invokeLater(new Runnable() { |
104 |
public void |
105 |
run() { CC.getMainFrame().setVisible(false); } |
106 |
}); |
107 |
} |
108 |
|
109 |
try { |
110 |
CC.setViewConfig ( |
111 |
(JSViewConfig)Class.forName(entry.viewConfig).newInstance() |
112 |
); |
113 |
} catch(Exception e) { |
114 |
CC.getLogger().info(HF.getErrorMessage(e)); |
115 |
return; |
116 |
} |
117 |
|
118 |
setView0(entry); |
119 |
} |
120 |
|
121 |
private static void |
122 |
setView0(ViewEntry entry) { |
123 |
try { |
124 |
CC.setMainFrame((JSMainFrame)Class.forName(entry.mainFrame).newInstance()); |
125 |
CC.setProgressIndicator ( |
126 |
(JSProgress)Class.forName(entry.progressIndicator).newInstance() |
127 |
); |
128 |
CC.getMainFrame().setVisible(true); |
129 |
currentView = entry.name; |
130 |
} catch(Exception e) { |
131 |
CC.getLogger().log(Level.INFO, HF.getErrorMessage(e), e); |
132 |
return; |
133 |
} |
134 |
} |
135 |
|
136 |
/** |
137 |
* Parses the manifest file <code>views.mf</code> to gather information |
138 |
* about the available views in this JSampler distribution. |
139 |
*/ |
140 |
public static void |
141 |
parseManifest() { |
142 |
viewEntries.removeAllElements(); |
143 |
|
144 |
try { |
145 |
Manifest m = new Manifest(JSViews.class.getResourceAsStream("views.mf")); |
146 |
String s = m.getMainAttributes().getValue("JS-Views"); |
147 |
if(s == null) { |
148 |
CC.getLogger().warning("Missing manifest attribute: JS-Views"); |
149 |
return; |
150 |
} |
151 |
|
152 |
StringTokenizer st = new StringTokenizer(s); |
153 |
while(st.hasMoreTokens()) { |
154 |
s = st.nextToken(); |
155 |
Attributes a = m.getAttributes(s); |
156 |
|
157 |
if(a == null) { |
158 |
CC.getLogger().warning("Missing manifest entry: " + s); |
159 |
continue; |
160 |
} |
161 |
|
162 |
ViewEntry ve = new ViewEntry(); |
163 |
ve.name = a.getValue("View-Name"); |
164 |
ve.viewConfig = a.getValue("View-Config"); |
165 |
ve.mainFrame = a.getValue("Main-Frame"); |
166 |
ve.progressIndicator = a.getValue("Progress-Indicator"); |
167 |
|
168 |
viewEntries.addElement(ve); |
169 |
} |
170 |
|
171 |
s = m.getMainAttributes().getValue("JS-Default-View"); |
172 |
Attributes a = m.getAttributes(s); |
173 |
if(a != null) defaultView = a.getValue("View-Name"); |
174 |
} catch(Exception x) { |
175 |
CC.getLogger().log(Level.INFO, HF.getErrorMessage(x), x); |
176 |
} |
177 |
} |
178 |
|
179 |
private static class ViewEntry { |
180 |
String name; |
181 |
String viewConfig; |
182 |
String mainFrame; |
183 |
String progressIndicator; |
184 |
} |
185 |
} |