Main repository of MikuMikuStudio
Revisión | 13e366af9f8e8d87459618b6709f0e571d6f03bf (tree) |
---|---|
Tiempo | 2013-06-30 03:51:56 |
Autor | normen667 <normen667@75d0...> |
Commiter | normen667 |
SDK:
- commit disabled code to save scene from scratch
git-svn-id: http://jmonkeyengine.googlecode.com/svn/trunk@10685 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
@@ -0,0 +1,129 @@ | ||
1 | +/* | |
2 | + * Copyright (c) 2009-2010 jMonkeyEngine | |
3 | + * All rights reserved. | |
4 | + * | |
5 | + * Redistribution and use in source and binary forms, with or without | |
6 | + * modification, are permitted provided that the following conditions are | |
7 | + * met: | |
8 | + * | |
9 | + * * Redistributions of source code must retain the above copyright | |
10 | + * notice, this list of conditions and the following disclaimer. | |
11 | + * | |
12 | + * * Redistributions in binary form must reproduce the above copyright | |
13 | + * notice, this list of conditions and the following disclaimer in the | |
14 | + * documentation and/or other materials provided with the distribution. | |
15 | + * | |
16 | + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors | |
17 | + * may be used to endorse or promote products derived from this software | |
18 | + * without specific prior written permission. | |
19 | + * | |
20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 | + */ | |
32 | +package com.jme3.gde.core.appstates; | |
33 | + | |
34 | +import com.jme3.export.binary.BinaryExporter; | |
35 | +import com.jme3.gde.core.scene.SceneApplication; | |
36 | +import com.jme3.gde.core.scene.SceneRequest; | |
37 | +import com.jme3.scene.Spatial; | |
38 | +import java.io.File; | |
39 | +import java.io.IOException; | |
40 | +import java.util.concurrent.Callable; | |
41 | +import javax.swing.filechooser.FileFilter; | |
42 | +import org.netbeans.api.progress.ProgressHandle; | |
43 | +import org.netbeans.api.progress.ProgressHandleFactory; | |
44 | +import org.openide.DialogDisplayer; | |
45 | +import org.openide.NotifyDescriptor; | |
46 | +import org.openide.awt.StatusDisplayer; | |
47 | +import org.openide.cookies.SaveCookie; | |
48 | +import org.openide.filesystems.FileChooserBuilder; | |
49 | +import org.openide.filesystems.FileObject; | |
50 | +import org.openide.filesystems.FileUtil; | |
51 | +import org.openide.nodes.AbstractNode; | |
52 | +import org.openide.nodes.Children; | |
53 | + | |
54 | +/** | |
55 | + * Node for a not yet existing scene, can be saved as j3o | |
56 | + * @author normenhansen | |
57 | + */ | |
58 | +public class NewSceneSaveNode extends AbstractNode implements SaveCookie { | |
59 | + | |
60 | + private final SceneRequest request; | |
61 | + | |
62 | + public NewSceneSaveNode(SceneRequest nodeToSave) { | |
63 | + super(Children.LEAF); | |
64 | + request = nodeToSave; | |
65 | + getCookieSet().assign(SaveCookie.class, this); | |
66 | + | |
67 | + } | |
68 | + | |
69 | + @Override | |
70 | + public String toString() { | |
71 | + return "NewSceneSaveNode(" + getDisplayName() + ")"; | |
72 | + } | |
73 | + | |
74 | + public void save() throws IOException { | |
75 | + FileChooserBuilder builder = new FileChooserBuilder(NewSceneSaveNode.class); | |
76 | + builder.addFileFilter(new FileFilter() { | |
77 | + @Override | |
78 | + public boolean accept(File f) { | |
79 | + if (f.getName().toLowerCase().trim().endsWith(".j3o")) { | |
80 | + return true; | |
81 | + } | |
82 | + return false; | |
83 | + } | |
84 | + | |
85 | + @Override | |
86 | + public String getDescription() { | |
87 | + return "j3o Files"; | |
88 | + } | |
89 | + }); | |
90 | + | |
91 | + final File file = builder.showSaveDialog(); | |
92 | + if (file == null) { | |
93 | + return; | |
94 | + } | |
95 | + SceneApplication.getApplication().enqueue(new Callable<Void>() { | |
96 | + public Void call() throws Exception { | |
97 | + Spatial node = request.getRootNode(); | |
98 | + if (node == null) { | |
99 | + return null; | |
100 | + } | |
101 | + if (file.exists()) { | |
102 | + NotifyDescriptor.Confirmation mesg = new NotifyDescriptor.Confirmation("File exists, overwrite?", | |
103 | + "Not Saved", | |
104 | + NotifyDescriptor.YES_NO_OPTION, NotifyDescriptor.WARNING_MESSAGE); | |
105 | + DialogDisplayer.getDefault().notify(mesg); | |
106 | + if (mesg.getValue() != NotifyDescriptor.Confirmation.YES_OPTION) { | |
107 | + return null; | |
108 | + } | |
109 | + } | |
110 | + ProgressHandle progressHandle = ProgressHandleFactory.createHandle("Saving File.."); | |
111 | + progressHandle.start(); | |
112 | + try { | |
113 | + BinaryExporter exp = BinaryExporter.getInstance(); | |
114 | + exp.save(node, file); | |
115 | + } catch (Exception e) { | |
116 | + } finally { | |
117 | + FileObject fo = FileUtil.toFileObject(file); | |
118 | + if (fo != null) { | |
119 | + StatusDisplayer.getDefault().setStatusText(fo + " saved."); | |
120 | + fo.getParent().refresh(); | |
121 | + } | |
122 | + } | |
123 | + progressHandle.finish(); | |
124 | + return null; | |
125 | + } | |
126 | + }); | |
127 | + | |
128 | + } | |
129 | +} |
@@ -32,8 +32,11 @@ | ||
32 | 32 | package com.jme3.gde.core.appstates; |
33 | 33 | |
34 | 34 | import com.jme3.app.state.AppState; |
35 | +import com.jme3.gde.core.assets.ProjectAssetManager; | |
35 | 36 | import com.jme3.gde.core.scene.FakeApplication; |
37 | +import com.jme3.gde.core.scene.PreviewRequest; | |
36 | 38 | import com.jme3.gde.core.scene.SceneApplication; |
39 | +import com.jme3.gde.core.scene.SceneListener; | |
37 | 40 | import com.jme3.gde.core.scene.SceneRequest; |
38 | 41 | import com.sun.source.tree.ClassTree; |
39 | 42 | import com.sun.source.tree.CompilationUnitTree; |
@@ -50,6 +53,8 @@ import org.netbeans.api.java.source.CancellableTask; | ||
50 | 53 | import org.netbeans.api.java.source.CompilationController; |
51 | 54 | import org.netbeans.api.java.source.JavaSource; |
52 | 55 | import org.netbeans.api.java.source.SourceUtils; |
56 | +import org.netbeans.api.project.FileOwnerQuery; | |
57 | +import org.netbeans.api.project.Project; | |
53 | 58 | import org.openide.DialogDisplayer; |
54 | 59 | import org.openide.NotifyDescriptor; |
55 | 60 | import org.openide.awt.ActionID; |
@@ -69,7 +74,6 @@ id = "com.jme3.gde.core.appstates.RunAppStateAction") | ||
69 | 74 | @ActionRegistration( |
70 | 75 | displayName = "#CTL_RunAppState") |
71 | 76 | @ActionReferences({ |
72 | - @ActionReference(path = "Toolbars/Build", position = 325), | |
73 | 77 | @ActionReference(path = "Loaders/text/x-java/Actions", position = 1050), |
74 | 78 | @ActionReference(path = "Editors/text/x-java/Popup", position = 1740) |
75 | 79 | }) |
@@ -77,22 +81,19 @@ displayName = "#CTL_RunAppState") | ||
77 | 81 | public class RunAppStateAction implements ContextAwareAction { |
78 | 82 | |
79 | 83 | private static final Logger logger = Logger.getLogger(RunAppStateAction.class.getName()); |
80 | - private String className; | |
81 | - private JavaSource source; | |
84 | + private final ActionConfig config; | |
82 | 85 | |
83 | 86 | public RunAppStateAction() { |
84 | - className = null; | |
85 | - source = null; | |
87 | + config = null; | |
86 | 88 | } |
87 | 89 | |
88 | - public RunAppStateAction(String className, JavaSource src) { | |
89 | - this.className = className; | |
90 | - this.source = src; | |
90 | + private RunAppStateAction(ActionConfig config) { | |
91 | + this.config = config; | |
91 | 92 | } |
92 | 93 | |
93 | 94 | public Action createContextAwareInstance(Lookup actionContext) { |
94 | - checkData(actionContext, "com.jme3.app.state.AppState"); | |
95 | - return new RunAppStateAction(className, source); | |
95 | + ActionConfig config = checkData(actionContext, "com.jme3.app.state.AppState"); | |
96 | + return new RunAppStateAction(config); | |
96 | 97 | } |
97 | 98 | |
98 | 99 | public Object getValue(String key) { |
@@ -109,7 +110,7 @@ public class RunAppStateAction implements ContextAwareAction { | ||
109 | 110 | } |
110 | 111 | |
111 | 112 | public boolean isEnabled() { |
112 | - return className != null; | |
113 | + return config != null ? config.className != null : false; | |
113 | 114 | } |
114 | 115 | |
115 | 116 | public void addPropertyChangeListener(PropertyChangeListener listener) { |
@@ -120,47 +121,72 @@ public class RunAppStateAction implements ContextAwareAction { | ||
120 | 121 | |
121 | 122 | public void actionPerformed(ActionEvent e) { |
122 | 123 | //TODO: better way to access scene request |
124 | + if (config == null) { | |
125 | + logger.log(Level.SEVERE, "Performing unconfigured RunAppState action"); | |
126 | + return; | |
127 | + } | |
123 | 128 | SceneRequest req = SceneApplication.getApplication().getCurrentSceneRequest(); |
124 | 129 | if (req != null) { |
125 | 130 | FakeApplication app = req.getFakeApp(); |
126 | - try { | |
127 | - AppState state = (AppState) app.getClassByName(className).newInstance(); | |
128 | - app.getStateManager().attach(state); | |
129 | - AppStateExplorerTopComponent.openExplorer(); | |
130 | - } catch (InstantiationException ex) { | |
131 | - DialogDisplayer.getDefault().notifyLater(new NotifyDescriptor.Message("Error creating AppState, make sure it has an empty constructor!\n" + ex.getMessage())); | |
132 | - Exceptions.printStackTrace(ex); | |
133 | - } catch (IllegalAccessException ex) { | |
134 | - DialogDisplayer.getDefault().notifyLater(new NotifyDescriptor.Message("Error creating AppState, make sure it has an empty constructor!\n" + ex.getMessage())); | |
135 | - Exceptions.printStackTrace(ex); | |
136 | - } catch (Exception ex) { | |
137 | - DialogDisplayer.getDefault().notifyLater(new NotifyDescriptor.Message("Error creating AppState, exception when creating!\n" + ex.getMessage())); | |
138 | - Exceptions.printStackTrace(ex); | |
139 | - } | |
131 | + assert (app != null); | |
132 | + attachState(app); | |
140 | 133 | } else { |
141 | 134 | DialogDisplayer.getDefault().notifyLater(new NotifyDescriptor.Message("No Scene opened to attach to,\nopen a j3o scene.")); |
142 | - logger.log(Level.INFO, "No Scene Request available"); | |
135 | + if(5==5) | |
136 | + return; | |
137 | + if (config.manager != null) { | |
138 | + logger.log(Level.INFO, "Try request scene.."); | |
139 | + //TODO: rootNode is assigned in SceneApplication.. more elegant system (with scene lookup) | |
140 | + final SceneRequest sceneRequest = new SceneRequest(this, config.manager); | |
141 | + sceneRequest.setWindowTitle("New Scene"); | |
142 | + sceneRequest.setDataNode(new NewSceneSaveNode(sceneRequest)); | |
143 | + SceneApplication.getApplication().addSceneListener(new SceneListener() { | |
144 | + public void sceneOpened(SceneRequest request) { | |
145 | + if (request == sceneRequest) { | |
146 | + FakeApplication app = request.getFakeApp(); | |
147 | + attachState(app); | |
148 | + } | |
149 | + } | |
150 | + | |
151 | + public void sceneClosed(SceneRequest request) { | |
152 | + if (request == sceneRequest) { | |
153 | + SceneApplication.getApplication().removeSceneListener(this); | |
154 | + } | |
155 | + } | |
156 | + | |
157 | + public void previewCreated(PreviewRequest request) { | |
158 | + } | |
159 | + }); | |
160 | + SceneApplication.getApplication().openScene(sceneRequest); | |
161 | + } else { | |
162 | + DialogDisplayer.getDefault().notifyLater(new NotifyDescriptor.Message("Not a jME project!")); | |
163 | + } | |
143 | 164 | } |
144 | 165 | } |
145 | - | |
146 | - private void checkData(Lookup actionContext, final String name) { | |
147 | - source = null; | |
148 | - className = null; | |
166 | + | |
167 | + private ActionConfig checkData(Lookup actionContext, final String name) { | |
168 | + final ActionConfig ret = new ActionConfig(); | |
149 | 169 | try { |
150 | 170 | DataObject dObj = actionContext.lookup(DataObject.class); |
151 | 171 | if (dObj == null) { |
152 | 172 | logger.log(Level.FINE, "No DataObject"); |
153 | - return; | |
173 | + return null; | |
174 | + } | |
175 | + Project proj = FileOwnerQuery.getOwner(dObj.getPrimaryFile());; | |
176 | + if (proj != null) { | |
177 | + ret.manager = proj.getLookup().lookup(ProjectAssetManager.class); | |
178 | + } else { | |
179 | + logger.log(Level.INFO, "Project null"); | |
154 | 180 | } |
155 | 181 | FileObject fObj = dObj.getPrimaryFile(); |
156 | 182 | if (fObj == null) { |
157 | 183 | logger.log(Level.FINE, "No FileObject"); |
158 | - return; | |
184 | + return null; | |
159 | 185 | } |
160 | 186 | final JavaSource src = JavaSource.forFileObject(fObj); |
161 | 187 | if (src == null) { |
162 | 188 | logger.log(Level.FINE, "No JavaSource"); |
163 | - return; | |
189 | + return null; | |
164 | 190 | } |
165 | 191 | CancellableTask task = new CancellableTask<CompilationController>() { |
166 | 192 | public void run(CompilationController controller) throws IOException { |
@@ -185,8 +211,8 @@ public class RunAppStateAction implements ContextAwareAction { | ||
185 | 211 | TypeMirror elementType = myElement.asType(); |
186 | 212 | logger.log(Level.FINE, "Check {0} against {1}", new Object[]{elementType, appState}); |
187 | 213 | if (elementType != null && SourceUtils.checkTypesAssignable(controller, elementType, appState)) { |
188 | - source = src; | |
189 | - className = elementName; | |
214 | + ret.source = src; | |
215 | + ret.className = elementName; | |
190 | 216 | } |
191 | 217 | } |
192 | 218 | } |
@@ -197,8 +223,35 @@ public class RunAppStateAction implements ContextAwareAction { | ||
197 | 223 | } |
198 | 224 | }; |
199 | 225 | src.runUserActionTask(task, true); |
226 | + return ret; | |
200 | 227 | } catch (IOException ex) { |
201 | 228 | Exceptions.printStackTrace(ex); |
202 | 229 | } |
230 | + return null; | |
231 | + } | |
232 | + | |
233 | + private void attachState(FakeApplication app) { | |
234 | + try { | |
235 | + assert (config.className != null); | |
236 | + AppState state = (AppState) app.getClassByName(config.className).newInstance(); | |
237 | + app.getStateManager().attach(state); | |
238 | + AppStateExplorerTopComponent.openExplorer(); | |
239 | + } catch (InstantiationException ex) { | |
240 | + DialogDisplayer.getDefault().notifyLater(new NotifyDescriptor.Message("Error creating AppState, is the project compiled?\nAlso make sure it has an empty constructor!\n" + ex.getMessage())); | |
241 | + Exceptions.printStackTrace(ex); | |
242 | + } catch (IllegalAccessException ex) { | |
243 | + DialogDisplayer.getDefault().notifyLater(new NotifyDescriptor.Message("Error creating AppState, is the project compiled?\nAlso make sure it has an empty constructor!\n" + ex.getMessage())); | |
244 | + Exceptions.printStackTrace(ex); | |
245 | + } catch (Exception ex) { | |
246 | + DialogDisplayer.getDefault().notifyLater(new NotifyDescriptor.Message("Error creating AppState, exception when creating!\n" + ex.getMessage())); | |
247 | + Exceptions.printStackTrace(ex); | |
248 | + } | |
249 | + } | |
250 | + | |
251 | + private static class ActionConfig { | |
252 | + | |
253 | + public String className; | |
254 | + public JavaSource source; | |
255 | + public ProjectAssetManager manager; | |
203 | 256 | } |
204 | 257 | } |
\ No newline at end of file |