-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathOSPApplication.java
More file actions
188 lines (175 loc) · 6.57 KB
/
OSPApplication.java
File metadata and controls
188 lines (175 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Open Source Physics software is free software as described near the bottom of this code file.
*
* For additional information and documentation on Open Source Physics please see:
* <http://www.opensourcephysics.org/>
*/
package org.opensourcephysics.controls;
import java.util.Collection;
import java.util.Iterator;
import org.opensourcephysics.display.GUIUtils;
/**
* OSPApplication defines a model and a control.
*
* @author Douglas Brown
* @author Wolfgang Christian
* @version 1.1
*/
public class OSPApplication {
Control control;
Object model;
Class<?> loadedControlClass, loadedModelClass;
boolean compatibleModel = false;
/**
* Constructs an OSPApplication.
*
* @param control
* @param model
*/
public OSPApplication(Control control, Object model) {
this.control = control;
this.model = model;
}
/**
* Set the loader to import all xml data for a compatible model.
* @param b
*/
public void setCompatibleModel(boolean b) {
compatibleModel = b;
}
/**
* Gets the model that was used to load this class.
* @return
*/
public Class<?> getLoadedModelClass() {
return loadedModelClass;
}
/**
* Gets the control that was used to load this class.
* @return
*/
public Class<?> getLoadedControlClass() {
return loadedControlClass;
}
/**
* Returns an XML.ObjectLoader to save and load data for this object.
*
* @return the object loader
*/
public static XML.ObjectLoader getLoader() {
return new OSPAppLoader();
}
/**
* A class to save and load data for OSPControls.
*/
static class OSPAppLoader implements XML.ObjectLoader {
/**
* Saves object data to an XMLControl.
*
* @param xmlControl the xml control to save to
* @param obj the object to save
*/
public void saveObject(XMLControl xmlControl, Object obj) {
OSPApplication app = (OSPApplication) obj;
xmlControl.setValue("control", app.control); //$NON-NLS-1$
xmlControl.setValue("model", app.model); //$NON-NLS-1$
}
/**
* Creates an object using data from an XMLControl.
*
* @param xmlControl the xml control
* @return the newly created object
*/
public Object createObject(XMLControl xmlControl) {
Object model = xmlControl.getObject("model"); //$NON-NLS-1$
Control control = (Control) xmlControl.getObject("control"); //$NON-NLS-1$
return new OSPApplication(control, model);
}
/**
* Loads an object with data from an XMLControl.
*
* @param xmlControl the control
* @param obj the object
* @return the loaded object
*/
public Object loadObject(XMLControl xmlControl, Object obj) {
OSPApplication app = (OSPApplication) obj;
app.loadedControlClass = null;
app.loadedModelClass = null;
XMLControlElement cControl = (XMLControlElement) xmlControl.getChildControl("control"); //$NON-NLS-1$
XMLControlElement mControl = (XMLControlElement) xmlControl.getChildControl("model"); //$NON-NLS-1$
if((cControl==null)||(mControl==null)) {
OSPLog.fine("OSP Application not loaded. An OSP application must have a model and a control."); //$NON-NLS-1$
return app;
}
Class<?> modelClass = mControl.getObjectClass();
Class<?> controlClass = cControl.getObjectClass();
if((modelClass==null)||(controlClass==null)) {
if(controlClass==null) {
OSPLog.fine("Object not loaded. Cannot find class for control."); //$NON-NLS-1$
}
if(modelClass==null) {
OSPLog.fine("Object not loaded. Cannot find class for model."); //$NON-NLS-1$
}
return app;
}
boolean compatibleModels = app.compatibleModel;
if(app.model!=null) {
boolean loaderMatch = XML.getLoader(modelClass).getClass()==XML.getLoader(app.model.getClass()).getClass();
compatibleModels = compatibleModels||(modelClass==app.model.getClass())|| // identical classes are always compatible
(modelClass.isAssignableFrom(app.model.getClass())&&loaderMatch); // subclasses with identical loaders are assumed to be compatible
}
// load control data for compatible models
app.loadedControlClass = controlClass;
if((app.control!=null)&&(controlClass==app.control.getClass())) {
// matched control class: load normally
cControl.loadObject(app.control);
} else {
// auto-import compatible models
cControl.loadObject(app.control, true, compatibleModels);
}
Collection<String> appNames = app.control.getPropertyNames();
Iterator<String> it = cControl.getPropertyNames().iterator();
while(it.hasNext()) {
String name = it.next();
if(!appNames.contains(name)) { // remove names that are not currently in the app
app.control.setValue(name, null);
//System.out.println("removed: "+name);
}
}
app.loadedModelClass = modelClass;
if((app.model!=null)&&(modelClass==app.model.getClass())) {
// matched model class: load normally
mControl.loadObject(app.model);
} else {
// mismatched model class: auto-import with chooser
mControl.loadObject(app.model, true, false);
}
GUIUtils.repaintOSPFrames(); // make sure frames are up to date
return app;
}
}
}
/*
* Open Source Physics software is free software; you can redistribute
* it and/or modify it under the terms of the GNU General Public License (GPL) as
* published by the Free Software Foundation; either version 2 of the License,
* or(at your option) any later version.
*
* Code that uses any portion of the code in the org.opensourcephysics package
* or any subpackage (subdirectory) of this package must must also be be released
* under the GNU GPL license.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307 USA
* or view the license online at http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2007 The Open Source Physics project
* http://www.opensourcephysics.org
*/