1 /*******************************************************************************
\r
2 * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.
\r
3 * All rights reserved. This program and the accompanying materials
\r
4 * are made available under the terms of the Eclipse Public License v1.0
\r
5 * which accompanies this distribution, and is available at
\r
6 * http://www.eclipse.org/legal/epl-v10.html
\r
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.simantics.proconf.g3d.shapeeditor.actions;
\r
13 import java.io.File;
\r
14 import java.io.FilenameFilter;
\r
15 import java.util.Stack;
\r
17 import org.eclipse.jface.action.Action;
\r
18 import org.eclipse.swt.SWT;
\r
19 import org.eclipse.swt.widgets.DirectoryDialog;
\r
20 import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;
\r
21 import org.simantics.proconf.g3d.occ.geometry.OccTriangulator;
\r
23 import com.jme.bounding.BoundingBox;
\r
24 import com.jme.renderer.ColorRGBA;
\r
25 import com.jme.scene.Geometry;
\r
26 import com.jme.scene.state.MaterialState;
\r
29 public class LoadFolderAction extends Action {
\r
30 private ThreeDimensionalEditorBase editor;
\r
32 public LoadFolderAction(ThreeDimensionalEditorBase editor) {
\r
33 super("Load folder");
\r
34 this.editor = editor;
\r
40 DirectoryDialog loadDialog = new DirectoryDialog(editor.getRenderingComposite().getShell(), SWT.OPEN);
\r
41 loadDialog.setText("Select directory");
\r
43 String directory = loadDialog.open();
\r
44 if (directory != null) {
\r
45 File file = new File(directory);
\r
46 if (!file.isDirectory())
\r
48 Stack<File> directories = new Stack<File>();
\r
49 directories.push(file);
\r
50 boolean recursive = true;
\r
51 while (!directories.empty()) {
\r
52 File dir = directories.pop();
\r
54 String[] files = dir.list(new PLYFilter());
\r
55 for (String filename : files) {
\r
56 Geometry g = OccTriangulator.getGeometryFromFile(dir.getAbsolutePath() + "/" + filename)[0];
\r
57 MaterialState ms = editor.getRenderingComponent().getDisplaySystem().getRenderer().createMaterialState();
\r
58 ms.setAmbient(new ColorRGBA(0.f,0.f,0.f,0.f));
\r
59 ms.setEmissive(new ColorRGBA(0.f,0.f,0.f,0.f));
\r
60 ms.setShininess(128.f);
\r
61 ms.setDiffuse(new ColorRGBA(0.8f,0.8f,0.8f,0.f));
\r
62 ms.setSpecular(new ColorRGBA(1.f,1.f,1.f,0.f));
\r
63 ms.setMaterialFace(MaterialState.MF_FRONT_AND_BACK);
\r
64 if (g.getColorBuffer(0) != null) {
\r
65 ms.setColorMaterial(MaterialState.CM_DIFFUSE);
\r
67 g.setModelBound(new BoundingBox());
\r
68 g.updateModelBound();
\r
69 g.setRenderState(ms);
\r
70 editor.getRenderingComponent().getShadowRoot().attachChild(g);
\r
71 g.updateWorldBound();
\r
76 File[] newDirs = dir.listFiles(new DirectoryFilter());
\r
77 for (File d : newDirs)
\r
78 directories.push(d);
\r
87 protected class DirectoryFilter implements FilenameFilter {
\r
88 public boolean accept(File dir, String name) {
\r
89 File file = new File(dir.getAbsolutePath() + "/" + name);
\r
90 return file.isDirectory();
\r
94 protected class PLYFilter implements FilenameFilter {
\r
95 public boolean accept(File dir, String name) {
\r
96 return (name.endsWith("ply"));
\r