How to reset mouse rotation in Java3D? - java-3d

I am writing a Java Applet sing Java3D and would like to reset the rotation of mouse in MouseRotate behavior when a button is clicked. The relevant codes are as follows:
BoundingSphere bound =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
MouseRotate mouseRotate = new MouseRotate();
TransformGroup modelGroup = new TransformGroup();
mouseRotate.setTransformGroup(modelGroup);
modelGroup.addChild(mouseRotate);
mouseRotate.setSchedulingBounds(bound);

It works. Thanks a lot! However while implementing your proposed method, accidentally I found a simpler. The code comes bellow.
This method seems to be working for my purpose and there is no need for the parent TransformGrop either. The method is obvious and I thought I tried it before. Perhaps I did try this but for other reasons it didn't work. Cheers, Hassan
TransformGroup modelGroup = new TransformGroup();
modelGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
modelGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
JButton resetButton = new JButton();
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
modelGroup.setTransform( new Transform3D());
}
});

Sorry, my mistake. It seems there is no way to reset MouseRotate without rewriting it.
Second try: Include another TransformGroup as parent of the modelGroup into the scene graph and set its transform to the invert transform of the modelGroup when resetting is denied.
TransformGroup modelGroupReset = new TransformGroup();
TransformGroup modelGroup = new TransformGroup();
modelGroupReset.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
modelGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
modelGroupReset.addChild(modelGroup);
JButton resetButton = new JButton();
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Transform3D t3d = new Transform3D();
modelGroup.getTransform(t3d);
t3d.invert();
modelGroupReset.setTransform(t3d);
}
});
;

Give this a try: Recall mouseRotate.setTransformGroup(modelGroup); followed by mouseRotate.initialize();. Not tested!

Related

Cocoa app without MainMenu.xib?

It's been asked before, but no clear (current) answer seems to be out there.. Is it actually possible to build a Cocoa app, WITH a Main Menu, exclusively from code (no MainMenu.xib, etc.) in such a way that it will still pass scrutiny when submitted to the app store? If so, what is the magic incantation(s) to get this to work?
Extra credit if someone can point me to a tutorial or document that's not from 2002 and works on mavericks..
Well, it certainly used to be possible.
I’d start by looking in main.m, and replace NSApplicationMain() with NSApplicationLoad(), so you get an NSApplication object.
Then you can create an NSMenu using standard NSMenu methods (-addItem:, etc), and then call
[NSApplication sharedApplication].mainMenu = myMenu;
Then, you know, make an NSWindow, show that, etc.
Of course, there are a lot of menu items that Apple sets up for you when you launch, that you might not get when you do this. You’d have to add them by hand if so.
This really isn’t something I recommend, but there you go.
http://blog.moostep.com/xamarin-mac-helloworld-application-without-xib/
http://blog.moostep.com/creating-menu-for-xamarin-mac-app-using-csharp/
I don't know enough about Mac App Store to know if this will pass muster. I assume you can translate it from C# to Objective-C. It doesn't use any special Mono or Xamarin features.
Read the blog articles for some background. Meanwhile, I'll paste my versions of main.cs and AppDelegate.cs here:
main.cs:
using System;
using MonoMac.AppKit;
namespace XibFreeCocoaApp
{
public class XibFreeCocoaApp
{
static void Main (string[] args)
{
NSApplication.Init ();
var application = NSApplication.SharedApplication;
application.Delegate = new AppDelegate();
application.Run ();
}
}
}
AppDelegate.cs:
using System;
using System.Drawing;
using MonoMac.AppKit;
using MonoMac.Foundation;
namespace XibFreeCocoaApp
{
public class AppDelegate : NSApplicationDelegate
{
public override void FinishedLaunching (MonoMac.Foundation.NSObject notification)
{
CreateMenu (NSProcessInfo.ProcessInfo.ProcessName);
var window = new NSWindow (
new RectangleF (0, 0, 400, 300),
NSWindowStyle.Titled,
NSBackingStore.Buffered,
false) {
Title = "Xib Free Cocoa App with Menu"
};
window.CascadeTopLeftFromPoint (new PointF (20, 20));
window.MakeKeyAndOrderFront(null);
}
void CreateMenu (string appName)
{
var mainMenu = new NSMenu();
var appMenuItem = new NSMenuItem ();
mainMenu.AddItem (appMenuItem);
var appMenu = new NSMenu ();
var quitMenuItem = new NSMenuItem (String.Format ("Quit {0}", appName), "q", delegate {
NSApplication.SharedApplication.Terminate(mainMenu);
});
appMenu.AddItem (quitMenuItem);
appMenuItem.Submenu = appMenu;
NSApplication.SharedApplication.MainMenu = mainMenu;
}
}
}
Be aware that the code above will not change the title of menu items that have a submenu - to do this you need to assign title to the Submenu as well like so:
NSMenuItem someMenuItem = new NSMenuItem("Title 1");
NSMenu someMenuItemSubMenu = new NSMenu();
someMenuItemSubMenu.Title = "Title 2";
someMenuItem.Submenu = someMenuItemSubMenu;
The displayed tile in this case will be "Title 2", not "Title 1" (actually "Title 1" will be completely ignored).

i want to delete edge (connector) from vertices which are seleted. i am using mxgraph

i want to delete edge which is actually connector between veritce of two shapes. i want to delete connector between the vertices which are selected by click a button.
i have used this code but it does not give me help.
graph.getModel().beginUpdate();
try {
graph.getModel().remove( edge);
} finally {
graph.getModel().endUpdate();
}
i have much study the answer of
http://forum.jgraph.com/questions/4744/delete-edge
this question but it does not give us any help.
thanks
JButton b1 = new JButton ("Clear");
b1.addActionListener(new ActionListener(){
// cell = component.getCellAt(e.getX(), e.getY());
#Override
public void actionPerformed(ActionEvent arg0){
graph.selectChildCell();
graph.removeCells();
}
});

Loading Xaml via XamlReader only for Preview

just got some Problems with Loading Xaml Files by Runtime.
For your Information my Code-Snippet to Load the File as Content of a Usercontrol:
public UserControl LoadXaml(FileInfo paramFile)
{
FileInfo _XamlFile = paramFile;
UIElement rootElement;
FileStream s = new FileStream(_XamlFile.FullName, FileMode.Open);
rootElement = (UIElement)XamlReader.Load(s);
s.Close();
UserControl uc = new UserControl();
if (rootElement.GetType() == typeof(Window))
{
uc.Content = (rootElement as Window).Content;
}
else
{
uc = rootElement as UserControl;
}
return uc;
}
private void lstPDFDokumente_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var _XamlFile = ((System.Windows.Controls.ListBox)sender).SelectedItem as FileInfo;
if (_XamlFile != null)
{
layoutGrid.Children.Clear();
System.Windows.Controls.UserControl rootElement;
rootElement = XamlController.LoadXaml(_XamlFile);
layoutGrid.Children.Add(rootElement);
}
}
This works fine while Events and x:Class="..." are deleted by hand.
The Problems I try to solve are:
If there is a x:Class="..." at the root element the XamlReader throws the first exception.
When the XamlReader reaches a Control which contains an event, for Example Click or TextChanged, it throws another Exception.
What i try to figure out is how to load a XamlFile, show it inside of a Control in the main Window and to show some of the attributes like Name,Height,Width and so on.
Just read dozens of Websites but never found a topic about to make a preview or things like that.
One of the solutions i tried is to read the Xaml File as XML and delete that code.
The Problem was to get a list of all possible Events in C#.
If there are some Questions to that Code, feel free to ask :)
Greetings
Daniel

Rss20FeedFormatter Ignores TextSyndicationContent type for SyndicationItem.Summary

While using the Rss20FeedFormatter class in a WCF project, I was trying to wrap the content of my description elements with a <![CDATA[ ]]> section. I found that no matter what I did, the HTML content of the description elements was always encoded and the CDATA section was never added. After peering into the source code of Rss20FeedFormatter, I found that when building the Summary node, it basically creates a new TextSyndicationContent instance which wipes out whatever settings were previously specified (I think).
My Code
public class CDataSyndicationContent : TextSyndicationContent
{
public CDataSyndicationContent(TextSyndicationContent content)
: base(content)
{
}
protected override void WriteContentsTo(System.Xml.XmlWriter writer)
{
writer.WriteCData(Text);
}
}
... (The following code should wrap the Summary with a CDATA section)
SyndicationItem item = new SyndicationItem();
item.Title = new TextSyndicationContent(name);
item.Summary = new CDataSyndicationContent(
new TextSyndicationContent(
"<div>This is a test</div>",
TextSyndicationContentKind.Html));
Rss20FeedFormatter Code
(AFAIK, the above code does not work because of this logic)
...
else if (reader.IsStartElement("description", ""))
result.Summary = new TextSyndicationContent(reader.ReadElementString());
...
As a workaround, I've resorted to using the RSS20FeedFormatter to build the RSS, and then patch the RSS manually. For example:
StringBuilder buffer = new StringBuilder();
XmlTextWriter writer = new XmlTextWriter(new StringWriter(buffer));
feedFormatter.WriteTo(writer ); // feedFormatter = RSS20FeedFormatter
PostProcessOutputBuffer(buffer);
WebOperationContext.Current.OutgoingResponse.ContentType =
"application/xml; charset=utf-8";
return new MemoryStream(Encoding.UTF8.GetBytes(buffer.ToString()));
...
public void PostProcessOutputBuffer(StringBuilder buffer)
{
var xmlDoc = XDocument.Parse(buffer.ToString());
foreach (var element in xmlDoc.Descendants("channel").First()
.Descendants("item")
.Descendants("description"))
{
VerifyCdataHtmlEncoding(buffer, element);
}
foreach (var element in xmlDoc.Descendants("channel").First()
.Descendants("description"))
{
VerifyCdataHtmlEncoding(buffer, element);
}
buffer.Replace(" xmlns:a10=\"http://www.w3.org/2005/Atom\"",
" xmlns:atom=\"http://www.w3.org/2005/Atom\"");
buffer.Replace("a10:", "atom:");
}
private static void VerifyCdataHtmlEncoding(StringBuilder buffer,
XElement element)
{
if (!element.Value.Contains("<") || !element.Value.Contains(">"))
{
return;
}
var cdataValue = string.Format("<{0}><![CDATA[{1}]]></{2}>",
element.Name,
element.Value,
element.Name);
buffer.Replace(element.ToString(), cdataValue);
}
The idea for this workaround came from the following location, I just adapted it to work with WCF instead of MVC. http://localhost:8732/Design_Time_Addresses/SyndicationServiceLibrary1/Feed1/
I'm just wondering if this is simply a bug in Rss20FeedFormatter or is it by design? Also, if anyone has a better solution, I'd love to hear it!
Well #Page Brooks, I see this more as a solution then as a question :). Thanks!!! And to answer your question ( ;) ), yes, I definitely think this is a bug in the Rss20FeedFormatter (though I did not chase it as far), because had encountered precisely the same issue that you described.
You have a 'localhost:8732' referral in your post, but it wasn't available on my localhost ;). I think you meant to credit the 'PostProcessOutputBuffer' workaround to this post:
http://damieng.com/blog/2010/04/26/creating-rss-feeds-in-asp-net-mvc
Or actually it is not in this post, but in a comment to it by David Whitney, which he later put in a seperate gist here:
https://gist.github.com/davidwhitney/1027181
Thank you for providing the adaption of this workaround more to my needs, because I had found the workaround too, but was still struggling to do the adaptation from MVC. Now I only needed to tweak your solution to put the RSS feed to the current Http request in the .ashx handler that I was using it in.
Basically I'm guessing that the fix you mentioned using the CDataSyndicationContent, is from feb 2011, assuming you got it from this post (at least I did):
SyndicationFeed: Content as CDATA?
This fix stopped working in some newer ASP.NET version or something, due to the code of the Rss20FeedFormatter changing to what you put in your post. This code change might as well have been an improvement for other stuff that IS in the MVC framework, but for those using the CDataSyndicationContent fix it definitely causes a bug!
string stylesheet = #"<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform""><xsl:output cdata-section-elements=""description"" method=""xml"" indent=""yes""/></xsl:stylesheet>";
XmlReader reader = XmlReader.Create(new StringReader(stylesheet));
XslCompiledTransform t = new XslCompiledTransform(true);
t.Load(reader);
using (MemoryStream ms = new MemoryStream())
{
XmlWriter writer = XmlWriter.Create(ms, t.OutputSettings);
rssFeed.WriteTo(writer); // rssFeed is Rss20FeedFormatter
writer.Flush();
ms.Position = 0;
string niko = Encoding.UTF8.GetString(ms.ToArray());
}
I'm sure someone pointed this out already but this a stupid workaround I used.
t.OutputSettings is of type XmlWriterSettings with cdataSections being populated with a single XmlQualifiedName "description".
Hope it helps someone else.
I found the code for Cdata elsewhere
public class CDataSyndicationContent : TextSyndicationContent
{
public CDataSyndicationContent(TextSyndicationContent content)
: base(content)
{
}
protected override void WriteContentsTo(System.Xml.XmlWriter writer)
{
writer.WriteCData(Text);
}
}
Code to call it something along the lines:
item.Content = new Helpers.CDataSyndicationContent(new TextSyndicationContent("<span>TEST2</span>", TextSyndicationContentKind.Html));
However the "WriteContentsTo" function wasn't being called.
Instead of Rss20FeedFormatter I tried Atom10FeedFormatter - and it worked!
Obviously this gives Atom feed rather than traditional RSS - but worth mentioning.
Output code is:
//var formatter = new Rss20FeedFormatter(feed);
Atom10FeedFormatter formatter = new Atom10FeedFormatter(feed);
using (var writer = XmlWriter.Create(response.Output, new XmlWriterSettings { Indent = true }))
{
formatter.WriteTo(writer);
}

Serialize an Activity to xaml

I have Googled a bit, and cannot seem to find any examples of Xaml-fying Activities - good, bad, or otherwise!
public static string ToXaml (this Activity activity)
{
// i would use ActivityXamlServices to go from Xaml
// to activity, but how to go other way? documentation
// is slim, and cannot infer proper usage of
// ActivityXamlServices from Xml remarks :S
string xaml = string.Empty;
return xaml;
}
Hints, tips, pointers would be welcome :)
NOTE: so found this. Will work through and update once working. Anyone wanna beat me to the punch, by all means. Better yet, if you can find a way to be rid of WorkflowDesigner, seems odd it is required.
Alright, so worked through this forum posting.
You may Xaml-fy [ie transform an instance to declarative Xaml] a well-known Activity via
public static string ToXaml (this Activity activity)
{
StringBuilder xaml = new StringBuilder ();
using (XmlWriter xmlWriter = XmlWriter.Create (
xaml,
new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true, }))
using (XamlWriter xamlWriter = new XamlXmlWriter (
xmlWriter,
new XamlSchemaContext ()))
using (XamlWriter xamlServicesWriter =
ActivityXamlServices.CreateBuilderWriter (xamlWriter))
{
ActivityBuilder activityBuilder = new ActivityBuilder
{
Implementation = activity
};
XamlServices.Save (xamlServicesWriter, activityBuilder);
}
return xaml.ToString ();
}
Your Xaml may contain certain artifacts, such as references to System.Activities.Presentation namespace appearing as xmlns:sap="...". If this presents an issue in your solution, read the source link above - there is a means to inject directives to ignore unrecognized namespaces.
Will leave this open for a while. If anyone can find a better solution, or improve upon this, please by all means :)
How about XamlServices.Save(filename, activity)?
Based on the other solution (for VS2010B2) and some Reflectoring, I found a solution for VS2010RC. Since XamlWriter is abstract in the RC, the new way to serialize an activity tree is this:
public static string ToXaml (this Activity activity)
{
var xamlBuilder = new StringBuilder();
var xmlWriter = XmlWriter.Create(xamlBuilder,
new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true });
using (xmlWriter)
{
var xamlXmlWriter =
new XamlXmlWriter(xmlWriter, new XamlSchemaContext());
using (xamlXmlWriter)
{
XamlWriter xamlWriter =
ActivityXamlServices.CreateBuilderWriter(xamlXmlWriter);
using (xamlWriter)
{
var activityBuilder =
new ActivityBuilder { Implementation = sequence };
XamlServices.Save(xamlWriter, activityBuilder);
}
}
}
return xamlBuilder.ToString();
}