experimental
relu91 10 years ago
parent ba848391dd
commit 81ba23a2e2
  1. 18
      sdk/jme3-gui/nbproject/project.properties
  2. BIN
      sdk/jme3-gui/release/modules/ext/Nifty-Editor0.5.9.jar
  3. 402
      sdk/jme3-gui/src/com/jme3/gde/gui/Navigator.java
  4. 669
      sdk/jme3-gui/src/com/jme3/gde/gui/NiftyGuiVisualElement.java
  5. 245
      sdk/jme3-gui/src/com/jme3/gde/gui/nodes/GElementNode.java

@ -1,9 +1,9 @@
file.reference.swingtonifty.jar=release/modules/ext/swingtonifty.jar
#Thu, 25 Aug 2011 20:26:49 +0200
javac.source=1.6
javac.compilerargs=-Xlint -Xlint:-serial
license.file=../license-jme.txt
nbm.homepage=http://www.jmonkeyengine.com
nbm.module.author=Normen Hansen,Cristiano Aguzzi
nbm.needs.restart=true
spec.version.base=3.1.5
file.reference.swingtonifty.jar=release/modules/ext/swingtonifty.jar
#Thu, 25 Aug 2011 20:26:49 +0200
javac.source=1.6
javac.compilerargs=-Xlint -Xlint:-serial
license.file=../license-jme.txt
nbm.homepage=http://www.jmonkeyengine.com
nbm.module.author=Normen Hansen,Cristiano Aguzzi
nbm.needs.restart=true
spec.version.base=3.2

@ -1,202 +1,200 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.jme3.gde.gui;
import com.jme3.gde.gui.nodes.GElementNode;
import com.jme3.gde.gui.nodes.GUINode;
import com.jme3.gde.gui.nodes.ScreenChildFactory;
import jada.ngeditor.controller.CommandProcessor;
import jada.ngeditor.controller.GUIEditor;
import jada.ngeditor.controller.commands.SelectCommand;
import jada.ngeditor.listeners.events.ReloadGuiEvent;
import jada.ngeditor.listeners.events.SelectionChanged;
import jada.ngeditor.model.GUI;
import jada.ngeditor.model.GuiEditorModel;
import jada.ngeditor.model.elements.GElement;
import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JComponent;
import org.netbeans.spi.navigator.NavigatorPanel;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.BeanTreeView;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.util.Utilities;
import org.openide.util.lookup.ProxyLookup;
import org.openide.windows.TopComponent;
/**
*
* @author cris
*/
@NavigatorPanel.Registration(mimeType = "text/x-niftygui+xml", displayName="Gui View")
public class Navigator extends javax.swing.JPanel implements NavigatorPanel,ExplorerManager.Provider , Observer, PropertyChangeListener{
private Lookup lookup;
private ExplorerManager mgr = new ExplorerManager();
private final BeanTreeView beanTreeView;
private GUIEditor editor;
private Lookup context;
private boolean lock;
/**
* Creates new form Navigator
*/
public Navigator() {
initComponents();
setLayout(new BorderLayout());
beanTreeView = new BeanTreeView();
add(beanTreeView, BorderLayout.CENTER);
CommandProcessor.getInstance().getObservable().addObserver(this);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 244, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 274, Short.MAX_VALUE)
);
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
@Override
public String getDisplayName() {
return "GuiView";
}
@Override
public String getDisplayHint() {
return "Easy view for your gui";
}
@Override
public JComponent getComponent() {
return this;
}
@Override
public void panelActivated(Lookup context) {
}
@Override
public void panelDeactivated() {
ExplorerUtils.activateActions(mgr, false);
}
@Override
public Lookup getLookup() {
return lookup;
}
@Override
public ExplorerManager getExplorerManager() {
return mgr;
}
@Override
public void update(Observable o, Object arg) {
if(o instanceof GuiEditorModel){
try {
GuiEditorModel model = (GuiEditorModel) o;
model.getCurrent().addObserver(this);
model.getCurrent().getSelection().addObserver(this);
this.intNavigator(model.getCurrent());
} catch (PropertyVetoException ex) {
Exceptions.printStackTrace(ex);
}
}
if(arg instanceof SelectionChanged){
SelectionChanged event = (SelectionChanged) arg;
if(event.getNewSelection().isEmpty()){
return;
}
ArrayList<String> path = new ArrayList<String>();
GElement parent = ((SelectionChanged)arg).getElement();
while(parent!=null){
path.add(parent.getID());
parent = parent.getParent();
}
Node result = mgr.getRootContext();
for(int i=path.size()-1;i>=0;i--){
result = result.getChildren().findChild(path.get(i));
}
try {
mgr.setSelectedNodes(new Node[]{result});
} catch (PropertyVetoException ex) {
Exceptions.printStackTrace(ex);
}
}
beanTreeView.updateUI();
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(ExplorerManager.PROP_SELECTED_NODES)) {
Node[] newValue = (Node[]) evt.getNewValue();
if (newValue.length > 0) {
AbstractNode firstSelected = (AbstractNode) newValue[0];
if (firstSelected instanceof GElementNode) {
GElement element = ((GElementNode) firstSelected).getGelement();
GUI gui = ((GUINode)mgr.getRootContext()).getGui();
gui.getSelection().deleteObserver(this); // I don't wont to get notified about this selection change
SelectCommand command = CommandProcessor.getInstance().getCommand(SelectCommand.class);
command.setElement(element);
try {
lock = true;
CommandProcessor.getInstance().excuteCommand(command);
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
}
gui.getSelection().addObserver(this);
}
}
}
}
private void intNavigator(GUI gui) throws PropertyVetoException {
ExplorerUtils.activateActions(mgr, true);
AbstractNode guiRoot = new GUINode(gui);
guiRoot.setName("Gui");
this.mgr.setRootContext(guiRoot);
this.beanTreeView.updateUI();
mgr.addPropertyChangeListener(this);
this.mgr.setSelectedNodes(new Node[]{guiRoot});
Lookup lookup1 = ExplorerUtils.createLookup(mgr, getActionMap());
lookup = new ProxyLookup(lookup1);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.jme3.gde.gui;
import com.jme3.gde.gui.nodes.GElementNode;
import com.jme3.gde.gui.nodes.GUINode;
import jada.ngeditor.controller.CommandProcessor;
import jada.ngeditor.controller.GUIEditor;
import jada.ngeditor.controller.commands.SelectCommand;
import jada.ngeditor.listeners.events.SelectionChanged;
import jada.ngeditor.model.GUI;
import jada.ngeditor.model.GuiEditorModel;
import jada.ngeditor.model.elements.GElement;
import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JComponent;
import org.netbeans.spi.navigator.NavigatorPanel;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.BeanTreeView;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.lookup.ProxyLookup;
/**
*
* @author cris
*/
@NavigatorPanel.Registration(mimeType = "text/x-niftygui+xml", displayName="Gui View")
public class Navigator extends javax.swing.JPanel implements NavigatorPanel,ExplorerManager.Provider , Observer, PropertyChangeListener{
private Lookup lookup;
private ExplorerManager mgr = new ExplorerManager();
private final BeanTreeView beanTreeView;
/**
* Creates new form Navigator
*/
public Navigator() {
initComponents();
setLayout(new BorderLayout());
beanTreeView = new BeanTreeView();
add(beanTreeView, BorderLayout.CENTER);
final GuiEditorModel model = (GuiEditorModel) CommandProcessor.getInstance().getObservable();
model.addObserver(this);
if(model.getCurrent() != null){
try {
this.intNavigator(model.getCurrent());
} catch (PropertyVetoException ex) {
Exceptions.printStackTrace(ex);
}
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 244, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 274, Short.MAX_VALUE)
);
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
@Override
public String getDisplayName() {
return "GuiView";
}
@Override
public String getDisplayHint() {
return "Easy view for your gui";
}
@Override
public JComponent getComponent() {
return this;
}
@Override
public void panelActivated(Lookup context) {
}
@Override
public void panelDeactivated() {
ExplorerUtils.activateActions(mgr, false);
}
@Override
public Lookup getLookup() {
return lookup;
}
@Override
public ExplorerManager getExplorerManager() {
return mgr;
}
@Override
public void update(Observable o, Object arg) {
if(o instanceof GuiEditorModel){
try {
GuiEditorModel model = (GuiEditorModel) o;
model.getCurrent().addObserver(this);
model.getCurrent().getSelection().addObserver(this);
this.intNavigator(model.getCurrent());
} catch (PropertyVetoException ex) {
Exceptions.printStackTrace(ex);
}
}
if(arg instanceof SelectionChanged){
SelectionChanged event = (SelectionChanged) arg;
if(event.getNewSelection().isEmpty()){
return;
}
ArrayList<String> path = new ArrayList<String>();
GElement parent = ((SelectionChanged)arg).getElement();
while(parent!=null){
path.add(parent.getID());
parent = parent.getParent();
}
Node result = mgr.getRootContext();
for(int i=path.size()-1;i>=0;i--){
result = result.getChildren().findChild(path.get(i));
}
try {
mgr.setSelectedNodes(new Node[]{result});
} catch (PropertyVetoException ex) {
Exceptions.printStackTrace(ex);
}
}
beanTreeView.updateUI();
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(ExplorerManager.PROP_SELECTED_NODES)) {
Node[] newValue = (Node[]) evt.getNewValue();
if (newValue.length > 0) {
AbstractNode firstSelected = (AbstractNode) newValue[0];
if (firstSelected instanceof GElementNode) {
GElement element = ((GElementNode) firstSelected).getGelement();
GUI gui = ((GUINode)mgr.getRootContext()).getGui();
gui.getSelection().deleteObserver(this); // I don't wont to get notified about this selection change
SelectCommand command = CommandProcessor.getInstance().getCommand(SelectCommand.class);
command.setElement(element);
try {
CommandProcessor.getInstance().excuteCommand(command);
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
}
gui.getSelection().addObserver(this);
}
}
}
}
private void intNavigator(GUI gui) throws PropertyVetoException {
ExplorerUtils.activateActions(mgr, true);
AbstractNode guiRoot = new GUINode(gui);
guiRoot.setName("Gui");
this.mgr.setRootContext(guiRoot);
this.beanTreeView.updateUI();
mgr.addPropertyChangeListener(this);
this.mgr.setSelectedNodes(new Node[]{guiRoot});
Lookup lookup1 = ExplorerUtils.createLookup(mgr, getActionMap());
lookup = new ProxyLookup(lookup1);
}
}

@ -1,335 +1,334 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.jme3.gde.gui;
import com.jme3.gde.core.assets.ProjectAssetManager;
import com.jme3.gde.gui.nodes.GUINode;
import de.lessvoid.nifty.Nifty;
import jada.ngeditor.controller.CommandProcessor;
import jada.ngeditor.controller.GUIEditor;
import jada.ngeditor.guiviews.DND.PaletteDropTarget;
import jada.ngeditor.guiviews.DND.TrasferHandling;
import jada.ngeditor.guiviews.J2DNiftyView;
import jada.ngeditor.listeners.events.SelectionChanged;
import jada.ngeditor.model.GuiEditorModel;
import jada.ngeditor.model.elements.GElement;
import jada.ngeditor.model.elements.GLayer;
import jada.ngeditor.model.exception.NoProductException;
import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyVetoException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Observable;
import java.util.Observer;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.api.progress.ProgressHandleFactory;
import org.netbeans.core.spi.multiview.CloseOperationState;
import org.netbeans.core.spi.multiview.MultiViewElement;
import org.netbeans.core.spi.multiview.MultiViewElementCallback;
import org.openide.awt.UndoRedo;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.loaders.DataObject;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.NbBundle.Messages;
import org.openide.windows.TopComponent;
import org.xml.sax.SAXException;
import sun.rmi.runtime.Log;
@MultiViewElement.Registration(
displayName = "#LBL_NiftyGui_VISUAL",
iconBase = "com/jme3/gde/gui/Computer_File_043.gif",
mimeType = "text/x-niftygui+xml",
persistenceType = TopComponent.PERSISTENCE_NEVER,
preferredID = "NiftyGuiVisual",
position = 2000)
@Messages("LBL_NiftyGui_VISUAL=Visual")
public final class NiftyGuiVisualElement extends JPanel implements MultiViewElement , ExplorerManager.Provider,Observer, PropertyChangeListener {
private NiftyGuiDataObject obj;
private JToolBar toolbar = new JToolBar();
private transient MultiViewElementCallback callback;
private GUIEditor editor;
private final Nifty nifty;
private final J2DNiftyView view;
private final JComboBox layers = new JComboBox();
private final ExplorerManager nodesManager;
private final UndoRedo.Manager undoSupport;
private int guiID;
public NiftyGuiVisualElement(Lookup lkp) {
obj = lkp.lookup(NiftyGuiDataObject.class);
assert obj != null;
initComponents();
view = new J2DNiftyView(800, 600);
view.init();
this.scrollArea.getViewport().addChangeListener(view);
this.scrollArea.setViewportView(view);
TrasferHandling tranf = new TrasferHandling();
PaletteDropTarget tmp = new PaletteDropTarget();
editor = obj.getLookup().lookup(GUIEditor.class);
nodesManager = new ExplorerManager();
nifty = view.getNifty();
view.setTransferHandler(tranf);
view.setDropTarget(tmp);
// editor.addObserver(view);
// editor.addObserver(tranf);
this.obj.addPropertyChangeListener(this);
this.createToolbar();
this.undoSupport = new UndoRedo.Manager();
CommandProcessor.getInstance().setUndoManager(undoSupport);
}
/**
* Old code
* @author normenhansen
*/
private void createToolbar() {
toolbar.setPreferredSize(new Dimension(10000, 24));
toolbar.setMaximumSize(new Dimension(10000, 24));
toolbar.setFloatable(false);
toolbar.add(new JLabel("Change Resolution"));
JComboBox comboBox = new JComboBox(new String[]{"640x480", "480x800", "800x480", "800x600", "1024x768", "1280x720"});
comboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
String string = (String) e.getItem();
if ("640x480".equals(string)) {
view.setResoltion(640, 480);
} else if ("1024x768".equals(string)) {
view.setResoltion(1024, 768);
} else if ("1280x720".equals(string)) {
view.setResoltion(1280, 720);
} else if ("800x600".equals(string)) {
view.setResoltion(800, 600);
} else if ("800x480".equals(string)) {
view.setResoltion(800, 480);
} else if ("480x800".equals(string)) {
view.setResoltion(480, 800);
} else {
view.setResoltion(800, 600);
}
}
});
toolbar.add(comboBox);
comboBox.setSelectedItem("800x600");
toolbar.add(new JLabel("Current Layer"));
layers.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
GLayer item = (GLayer) e.getItem();
editor.selectElement(item);
}
});
toolbar.add(layers);
}
@Override
public String getName() {
return "NiftyGuiVisualElement";
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
scrollArea = new javax.swing.JScrollPane();
setLayout(new java.awt.BorderLayout());
add(scrollArea, java.awt.BorderLayout.CENTER);
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JScrollPane scrollArea;
// End of variables declaration//GEN-END:variables
@Override
public JComponent getVisualRepresentation() {
return this;
}
@Override
public JComponent getToolbarRepresentation() {
return toolbar;
}
@Override
public Action[] getActions() {
return new Action[0];
}
@Override
public Lookup getLookup() {
return ExplorerUtils.createLookup(nodesManager, new ActionMap());
}
/**
* Raw implementation , just to prototype the editor
*/
@Override
public void componentOpened() {
loadGui();
}
@Override
public void componentClosed() {
}
/**
* Raw implementation , just to prototype the editor
*/
@Override
public void componentShowing() {
if(!this.obj.isModified()){
return;
}
loadGui();
}
/**
* Raw implementation , just to prototype the editor
*/
@Override
public void componentHidden() {
String path = this.obj.getPrimaryFile().getPath();
try {
this.editor.saveGui(path);
} catch (FileNotFoundException ex) {
Exceptions.printStackTrace(ex);
} catch (JAXBException ex) {
Exceptions.printStackTrace(ex);
} catch (NullPointerException ex){
Exceptions.printStackTrace(ex);
}
}
@Override
public void componentActivated() {
GuiEditorModel model = (GuiEditorModel) CommandProcessor.getInstance().getObservable();
model.setCurrentGUI(guiID);
CommandProcessor.getInstance().setUndoManager(undoSupport);
}
@Override
public void componentDeactivated() {
}
@Override
public UndoRedo getUndoRedo() {
return this.undoSupport;
}
@Override
public void setMultiViewCallback(MultiViewElementCallback callback) {
this.callback = callback;
}
@Override
public CloseOperationState canCloseElement() {
return CloseOperationState.STATE_OK;
}
@Override
public ExplorerManager getExplorerManager() {
return nodesManager;
}
@Override
public void update(Observable o, Object arg) {
if(arg instanceof SelectionChanged ){
SelectionChanged event = (SelectionChanged) arg;
if(event.getNewSelection().isEmpty()){
return;
}
ArrayList<String> path = new ArrayList<String>();
GElement parent = event.getElement();
while(parent!=null){
path.add(parent.getID());
parent = parent.getParent();
}
Node result = nodesManager.getRootContext();
for(int i=path.size()-1;i>=0 && result!=null;i--){
result = result.getChildren().findChild(path.get(i));
}
try {
if(result!=null){
nodesManager.setSelectedNodes(new Node[]{result});
}
} catch (PropertyVetoException ex) {
Exceptions.printStackTrace(ex);
}
}
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if(evt.getPropertyName().equals(DataObject.PROP_MODIFIED)){
boolean old = (Boolean)evt.getOldValue();
boolean nev = (Boolean)evt.getNewValue();
if(old && !nev){
this.loadGui();
}
}
}
private void loadGui() {
try {
ProgressHandle handle = ProgressHandleFactory.createHandle("Loading the gui file");
InputStream is = this.obj.getPrimaryFile().getInputStream();
ProjectAssetManager mgr = this.obj.getLookup().lookup(ProjectAssetManager.class);
String assetPath = mgr.getAssetFolder().getPath();
this.editor.createNewGui(nifty,is,new File(assetPath));
this.view.newGui(this.editor.getGui());
nodesManager.setRootContext(new GUINode(this.editor.getGui()));
this.editor.getGui().getSelection().addObserver(this);
Collection<GLayer> layers1 = this.editor.getGui().getLayers();
guiID = this.editor.getGui().getGUIid();
DefaultComboBoxModel<GLayer> model = new DefaultComboBoxModel<GLayer>(layers1.toArray(new GLayer[0]));
layers.setModel(model);
layers.setSelectedItem(this.editor.getCurrentLayer());
handle.finish();
} catch (ParserConfigurationException ex) {
Exceptions.printStackTrace(ex);
} catch (JAXBException ex) {
Exceptions.printStackTrace(ex);
} catch (ClassNotFoundException ex) {
Exceptions.printStackTrace(ex);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
} catch (NoProductException ex) {
Exceptions.printStackTrace(ex);
} catch (SAXException ex) {
Exceptions.printStackTrace(ex);
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.jme3.gde.gui;
import com.jme3.app.Application;
import com.jme3.gde.core.assets.ProjectAssetManager;
import com.jme3.gde.gui.nodes.GElementNode;
import com.jme3.gde.gui.nodes.GUINode;
import de.lessvoid.nifty.Nifty;
import jada.ngeditor.controller.CommandProcessor;
import jada.ngeditor.controller.GUIEditor;
import jada.ngeditor.guiviews.DND.PaletteDropTarget;
import jada.ngeditor.guiviews.DND.TrasferHandling;
import jada.ngeditor.guiviews.J2DNiftyView;
import jada.ngeditor.listeners.events.SelectionChanged;
import jada.ngeditor.model.GuiEditorModel;
import jada.ngeditor.model.elements.GElement;
import jada.ngeditor.model.elements.GLayer;
import jada.ngeditor.model.exception.NoProductException;
import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyVetoException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.api.progress.ProgressHandleFactory;
import org.netbeans.core.spi.multiview.CloseOperationState;
import org.netbeans.core.spi.multiview.MultiViewElement;
import org.netbeans.core.spi.multiview.MultiViewElementCallback;
import org.openide.awt.UndoRedo;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.loaders.DataObject;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.NbBundle.Messages;
import org.openide.util.lookup.AbstractLookup;
import org.openide.util.lookup.InstanceContent;
import org.openide.util.lookup.ProxyLookup;
import org.openide.windows.TopComponent;
import org.xml.sax.SAXException;
import sun.rmi.runtime.Log;
@MultiViewElement.Registration(
displayName = "#LBL_NiftyGui_VISUAL",
iconBase = "com/jme3/gde/gui/Computer_File_043.gif",
mimeType = "text/x-niftygui+xml",
persistenceType = TopComponent.PERSISTENCE_NEVER,
preferredID = "NiftyGuiVisual",
position = 2000)
@Messages("LBL_NiftyGui_VISUAL=Visual")
public final class NiftyGuiVisualElement extends JPanel implements MultiViewElement , ExplorerManager.Provider,Observer, PropertyChangeListener {
private static final Logger logger = Logger.getLogger(NiftyGuiVisualElement.class.getName());
private NiftyGuiDataObject obj;
private JToolBar toolbar = new JToolBar();
private transient MultiViewElementCallback callback;
private GUIEditor editor;
private final Nifty nifty;
private final J2DNiftyView view;
private final JComboBox layers = new JComboBox();
private final ExplorerManager nodesManager;
private final UndoRedo.Manager undoSupport;
private int guiID;
private final InstanceContent content = new InstanceContent();
private Lookup lookup;
public NiftyGuiVisualElement(Lookup lkp) {
obj = lkp.lookup(NiftyGuiDataObject.class);
assert obj != null;
initComponents();
view = new J2DNiftyView(800, 600);
view.init();
this.scrollArea.getViewport().addChangeListener(view);
this.scrollArea.setViewportView(view);
TrasferHandling tranf = new TrasferHandling();
PaletteDropTarget tmp = new PaletteDropTarget();
editor = obj.getLookup().lookup(GUIEditor.class);
nodesManager = new ExplorerManager();
nifty = view.getNifty();
view.setTransferHandler(tranf);
view.setDropTarget(tmp);
// editor.addObserver(view);
// editor.addObserver(tranf);
this.obj.addPropertyChangeListener(this);
this.createToolbar();
this.undoSupport = new UndoRedo.Manager();
CommandProcessor.getInstance().setUndoManager(undoSupport);
this.content.set(Collections.singleton(obj.getNodeDelegate()), null);
lookup = new AbstractLookup(content);
}
/**
* Old code
* @author normenhansen
*/
private void createToolbar() {
toolbar.setPreferredSize(new Dimension(10000, 24));
toolbar.setMaximumSize(new Dimension(10000, 24));
toolbar.setFloatable(false);
toolbar.add(new JLabel("Change Resolution"));
JComboBox comboBox = new JComboBox(new String[]{"640x480", "480x800", "800x480", "800x600", "1024x768", "1280x720"});
comboBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
String string = (String) e.getItem();
if ("640x480".equals(string)) {
view.setResoltion(640, 480);
} else if ("1024x768".equals(string)) {
view.setResoltion(1024, 768);
} else if ("1280x720".equals(string)) {
view.setResoltion(1280, 720);
} else if ("800x600".equals(string)) {
view.setResoltion(800, 600);
} else if ("800x480".equals(string)) {
view.setResoltion(800, 480);
} else if ("480x800".equals(string)) {
view.setResoltion(480, 800);
} else {
view.setResoltion(800, 600);
}
}
});
toolbar.add(comboBox);
comboBox.setSelectedItem("800x600");
toolbar.add(new JLabel("Current Layer"));
layers.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
GLayer item = (GLayer) e.getItem();
editor.selectElement(item);
}
});
toolbar.add(layers);
}
@Override
public String getName() {
return "NiftyGuiVisualElement";
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
scrollArea = new javax.swing.JScrollPane();
setLayout(new java.awt.BorderLayout());
add(scrollArea, java.awt.BorderLayout.CENTER);
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JScrollPane scrollArea;
// End of variables declaration//GEN-END:variables
@Override
public JComponent getVisualRepresentation() {
return this;
}
@Override
public JComponent getToolbarRepresentation() {
return toolbar;
}
@Override
public Action[] getActions() {
return new Action[0];
}
@Override
public Lookup getLookup() {
return this.lookup ;
}
/**
* Raw implementation , just to prototype the editor
*/
@Override
public void componentOpened() {
loadGui();
}
@Override
public void componentClosed() {
}
/**
* Raw implementation , just to prototype the editor
*/
@Override
public void componentShowing() {
if(!this.obj.isModified()){
return;
}
loadGui();
}
/**
* Raw implementation , just to prototype the editor
*/
@Override
public void componentHidden() {
String path = this.obj.getPrimaryFile().getPath();
try {
this.editor.saveGui(path);
} catch (FileNotFoundException ex) {
Exceptions.printStackTrace(ex);
} catch (JAXBException ex) {
Exceptions.printStackTrace(ex);
} catch (NullPointerException ex){
Exceptions.printStackTrace(ex);
}
}
@Override
public void componentActivated() {
try{
GuiEditorModel model = (GuiEditorModel) CommandProcessor.getInstance().getObservable();
model.setCurrentGUI(guiID);
CommandProcessor.getInstance().setUndoManager(undoSupport);
}catch(java.lang.IllegalArgumentException ex){
logger.log(Level.SEVERE,"Can't load your gui", ex);
}
}
@Override
public void componentDeactivated() {
}
@Override
public UndoRedo getUndoRedo() {
return this.undoSupport;
}
@Override
public void setMultiViewCallback(MultiViewElementCallback callback) {
this.callback = callback;
}
@Override
public CloseOperationState canCloseElement() {
return CloseOperationState.STATE_OK;
}
@Override
public ExplorerManager getExplorerManager() {
return nodesManager;
}
@Override
public void update(Observable o, Object arg) {
if(arg instanceof SelectionChanged ){
SelectionChanged event = (SelectionChanged) arg;
if(event.getNewSelection().isEmpty()){
return;
}
GElement parent = event.getElement();
GElementNode node = new GElementNode(parent);
this.content.set(Collections.singleton(node), null);
}
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if(evt.getPropertyName().equals(DataObject.PROP_MODIFIED)){
boolean old = (Boolean)evt.getOldValue();
boolean nev = (Boolean)evt.getNewValue();
if(old && !nev){
this.loadGui();
}
}
}
private void loadGui() {
try {
ProgressHandle handle = ProgressHandleFactory.createHandle("Loading the gui file");
InputStream is = this.obj.getPrimaryFile().getInputStream();
ProjectAssetManager mgr = this.obj.getLookup().lookup(ProjectAssetManager.class);
String assetPath = mgr.getAssetFolder().getPath();
this.editor.createNewGui(nifty,is,new File(assetPath));
this.view.newGui(this.editor.getGui());
nodesManager.setRootContext(new GUINode(this.editor.getGui()));
this.editor.getGui().getSelection().addObserver(this);
Collection<GLayer> layers1 = this.editor.getGui().getLayers();
guiID = this.editor.getGui().getGUIid();
DefaultComboBoxModel<GLayer> model = new DefaultComboBoxModel<GLayer>(layers1.toArray(new GLayer[0]));
layers.setModel(model);
layers.setSelectedItem(this.editor.getCurrentLayer());
handle.finish();
} catch (ParserConfigurationException ex) {
Exceptions.printStackTrace(ex);
} catch (JAXBException ex) {
Exceptions.printStackTrace(ex);
} catch (ClassNotFoundException ex) {
Exceptions.printStackTrace(ex);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
} catch (NoProductException ex) {
Exceptions.printStackTrace(ex);
} catch (SAXException ex) {
Exceptions.printStackTrace(ex);
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
}
}
}

@ -1,117 +1,128 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.jme3.gde.gui.nodes;
import com.jme3.gde.gui.propertyeditors.ResourceEditor;
import com.jme3.gde.gui.propertyeditors.SizeEditor;
import jada.ngeditor.model.elements.GElement;
import java.beans.PropertyEditor;
import java.lang.reflect.InvocationTargetException;
import java.util.Map.Entry;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.nodes.Sheet;
import sun.beans.editors.BoolEditor;
import sun.beans.editors.BooleanEditor;
/**
*
* @author cris
*/
public class GElementNode extends AbstractNode{
private final GElement element;
public GElementNode(GElement element) {
super(Children.create(new GElementChildFactory(element), false));
this.element = element;
this.setName(element.getID());
}
public GElement getGelement(){
return element;
}
public void updateChildren(){
}
@Override
protected Sheet createSheet() {
Sheet s = super.createSheet();
Sheet.Set set = s.get(Sheet.PROPERTIES);
if (set == null) {
set = Sheet.createPropertiesSet();
s.put(set);
}
set.setName("Element Properties");
set.setShortDescription("You can set element properties");
for(Entry<String,String> pair : this.element.listAttributes().entrySet()){
final ElementAttributeProperty elementAttributeProperty = new ElementAttributeProperty(element,pair.getKey());
pickEditor(pair, elementAttributeProperty);
set.put(elementAttributeProperty);
}
s.put(set);
return s;
}
private void pickEditor(Entry<String, String> pair, final ElementAttributeProperty elementAttributeProperty) {
if(pair.getKey().equals("width")||pair.getKey().equals("height") || pair.getKey().equals("x") || pair.getKey().equals("y") ){
elementAttributeProperty.setPropertyEditor(new SizeEditor());
}else if(pair.getKey().equals("filename")){
elementAttributeProperty.setPropertyEditor(new ResourceEditor());
}
}
public class ElementAttributeProperty extends Node.Property {
private String attributeName;
private GElement element;
private PropertyEditor editor;
public ElementAttributeProperty(GElement element, String attributeName) {
super(String.class);
this.element = element;
this.attributeName = attributeName;
this.setName(attributeName);
this.setDisplayName(attributeName);
}
@Override
public boolean canRead() {
return true;
}
@Override
public String getValue() throws IllegalAccessException, InvocationTargetException{
return element.getAttribute(attributeName);
}
@Override
public boolean canWrite() {
return true;
}
public void setPropertyEditor(PropertyEditor editor){
this.editor = editor;
}
@Override
public PropertyEditor getPropertyEditor() {
return this.editor; //To change body of generated methods, choose Tools | Templates.
}
@Override
public void setValue(Object val) throws IllegalAccessException, IllegalArgumentException {
element.addAttribute(attributeName, val.toString());
element.refresh();
}
};
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.jme3.gde.gui.nodes;
import com.jme3.gde.gui.propertyeditors.ResourceEditor;
import com.jme3.gde.gui.propertyeditors.SizeEditor;
import jada.ngeditor.controller.CommandProcessor;
import jada.ngeditor.controller.commands.EditAttributeCommand;
import jada.ngeditor.model.elements.GElement;
import java.beans.PropertyEditor;
import java.lang.reflect.InvocationTargetException;
import java.util.Map.Entry;
import javax.swing.JOptionPane;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.nodes.Sheet;
import org.openide.util.Exceptions;
import sun.beans.editors.BoolEditor;
import sun.beans.editors.BooleanEditor;
/**
*
* @author cris
*/
public class GElementNode extends AbstractNode{
private final GElement element;
public GElementNode(GElement element) {
super(Children.create(new GElementChildFactory(element), false));
this.element = element;
this.setName(element.getID());
}
public GElement getGelement(){
return element;
}
public void updateChildren(){
}
@Override
protected Sheet createSheet() {
Sheet s = super.createSheet();
Sheet.Set set = s.get(Sheet.PROPERTIES);
if (set == null) {
set = Sheet.createPropertiesSet();
s.put(set);
}
set.setName("Element Properties");
set.setShortDescription("You can set element properties");
for(Entry<String,String> pair : this.element.listAttributes().entrySet()){
final ElementAttributeProperty elementAttributeProperty = new ElementAttributeProperty(element,pair.getKey());
pickEditor(pair, elementAttributeProperty);
set.put(elementAttributeProperty);
}
s.put(set);
return s;
}
private void pickEditor(Entry<String, String> pair, final ElementAttributeProperty elementAttributeProperty) {
if(pair.getKey().equals("width")||pair.getKey().equals("height") || pair.getKey().equals("x") || pair.getKey().equals("y") ){
elementAttributeProperty.setPropertyEditor(new SizeEditor());
}else if(pair.getKey().equals("filename")){
elementAttributeProperty.setPropertyEditor(new ResourceEditor());
}
}
public class ElementAttributeProperty extends Node.Property {
private String attributeName;
private GElement element;
private PropertyEditor editor;
public ElementAttributeProperty(GElement element, String attributeName) {
super(String.class);
this.element = element;
this.attributeName = attributeName;
this.setName(attributeName);
this.setDisplayName(attributeName);
}
@Override
public boolean canRead() {
return true;
}
@Override
public String getValue() throws IllegalAccessException, InvocationTargetException{
return element.getAttribute(attributeName);
}
@Override
public boolean canWrite() {
return true;
}
public void setPropertyEditor(PropertyEditor editor){
this.editor = editor;
}
@Override
public PropertyEditor getPropertyEditor() {
return this.editor; //To change body of generated methods, choose Tools | Templates.
}
@Override
public void setValue(Object val) throws IllegalAccessException, IllegalArgumentException {
try {
EditAttributeCommand command = CommandProcessor.getInstance().getCommand(EditAttributeCommand.class);
command.setAttribute(attributeName);
command.setValue(val.toString());
CommandProcessor.getInstance().excuteCommand(command);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
};
}

Loading…
Cancel
Save