Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Wednesday, 8 January 2014

Using JAXB to Parse a List of Items and References

I recently hit a problem using JAXB to parse a list of items in XML that contains both the actual items and references to items.
<examplelist>
  <item>A</item>
  <item>B</item>
  <item-ref uri="itemC.xml"/>
  <item-ref uri="itemD.xml"/>
  <item>E</item>
</examplelist>
I had implemented an adapter to transform an ItemRef instance to an Item, but the following code did not work as I had expected.
@XmlElements({
  @XmlElement(name = "item", type = Item.class),
  @XmlElement(name = "item-ref", type = ItemRef.class)
})
private List<ability> items = new LinkedList<item>();
Instead of applying the adapter to the item-ref elements, it applied the adapter to all elements mapped to the list (it took a while to work this one out). The fix was to create a new adapter class that mapped Item to Item and use this as the adapter for item-ref elements. If this adapter was passed an ItemRef, it adapted it to an Item, otherwise it returned it unmodified.

Tuesday, 20 April 2010

Android

So yesterday I decided that I would look into what is involved in writing an app for Android, Google's mobile phone OS.

I downloaded the various SDKs, eclipse plugins and emulators and spend about half a day setting them all up (Tip of the day, if using this in a Jazz client, change the Jazz client to use a Sun JVM instead of the default IBM one).

Once the set up was done, I jumped right in at the deep end and started to put together a UI for a very simple app designed to display a D&D character builder save file. I originally tried to build the UI using java, but nothing appeared. Puzzled, I looked through a the docs and tutorials and nearly everything used XML files to define the UI instead, so I tried that and quickly got something up and running. It wasn't pretty, but it did the job.

I'll reserve judgement until I see how hard it is to make it pretty (including the support of changing orientation to landscape).

Sunday, 26 October 2008

Geoff on GEF pt. IV

Wow! Things are now really falling into place.

Most of my effort recently has been spent trying to get my GEF ViewPart to display in a seperate window, as a nice touch I wanted it to display in fullscreen on a second monitor if one is available. Amazingly, I've done it.


@SuppressWarnings("restriction")
@Override
public void run() {
Display display = Display.getDefault();
Monitor[] monitors = display.getMonitors();

Monitor lastMon = monitors[monitors.length - 1];
Rectangle bounds = lastMon.getBounds();

try {
IWorkbenchWindow window = Workbench.getInstance().openWorkbenchWindow("org.gamingclubnetwork.tournament.perspective.display", null);

Shell shell = window.getShell();
shell.setMenuBar(null);
shell.setLocation(bounds.x, bounds.y);
shell.setFullScreen(monitors.length > 1);
}
catch (WorkbenchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


First of all we look for all available monitors and grab the last one (I assumed the secondary display will be used for projectors or large monitors).
With that done, the trick is to open a new workbench window specifying a new perspective that contains just my GEF view. I then grab the shell of this workbench, remove the menu bar, set it's location to the top corner of the monitor to show it on and then set to full screen if not on the primary display.

Friday, 24 October 2008

Geoff on GEF pt. III

I tracked down the reason for my figures not appearing to the layout used in my root figure. I was using the XYLayout from the example, changing it to the ToolbarLayout immediately produced the desired results. e.g.

@Override
protected IFigure createFigure() {
Figure f = new Figure();
f.setOpaque(true);
f.setLayoutManager(new ToolbarLayout());
return f;
}


I can now plough on and create edit parts and figures for the remaining components of my model.

One of the problems I encountered when using the ttolbar layout, is that shapes I have defined are not appearing as expected, is the short term I have replaced them with Labels containing just icons, but I think I will have to revisit this in the future.

Thursday, 23 October 2008

Geoff on GEF pt. II

So, carrying straight on from last night, I fixed the null pointer and now have an empty view displaying. The trouble is, as soon as I plugged in the listeners and updated the children of the EditPart when the model changed, I still get an empty view.

After some investigation I discovered that the following code can be used in a ViewPart to display a GEF diagram.


@Override
public void createPartControl(Composite parent) {
DefaultEditDomain defaultEditDomain = new DefaultEditDomain(null);

sgv = new ScrollingGraphicalViewer();

defaultEditDomain.addViewer(sgv);
sgv.createControl(parent);
ScalableRootEditPart sffrep = new ScalableRootEditPart();
sgv.setRootEditPart(sffrep);
sgv.setEditPartFactory(new TournamentEditPartFactory());

sgv.getControl().setBackground(ColorConstants.listBackground);

initView();
}

private void initView() {
sgv.setContents(TournamentManager.get());
}


However, my diagram is still not displaying as expected...

Wednesday, 22 October 2008

Geoff on GEF pt. I

It's been about 4 years since I last worked with the Graphical Editing Framework, or GEF, in eclipse. I have recently been working on a new application using eclipse RCP to manage tournaments (specifically wargaming events).
Anyway, I have a basic application up and running now and intend to add an additional feature to allow for real-time information to be displayed on a seperate screen (or projector). For this feature I've decided to use GEF once more.
As I said, it has been about 4 years since I last used GEF and I recall that it was a little tricky to get started with. This time around I have decided to document my experience as I develop this new functionality.

To get things started, I paid a visit to the GEF homepage and jumped straight into the tutorial referenced there. The tutorial starts by stating that an updated version was posted in March 2007, not a good sign if the homepage does not point to the more up-to-date documentation.
Skipping quickly over the first section, that explains the MVC architechture, I wrote a couple of EditParts as described, then decided to create a view to display them. Now, the tutorial only deals with creating an editor, not a view. So after some googling I found this post on creating a view for GEF.

One of the methods called is setEditPartFactory(), I know from experience that the Edit Part Factory is a core piece of functionality that creates controllers (EditParts) from your model. I was stunned to find that the Edit PartFactory was not described in the tutorial. I threw one together from memory (see below) to confirm that I could get a view to display.

On launching the app, the view displayed, but there was a null pointer when getting the children of my root object, not really a surprise as I had yet to populate it. Anyway, that was enough for one night. I shall continue to work on it tomorrow.

The content of my EditPartFactory (simply implements EditPartFactory):

public EditPart createEditPart(EditPart context, Object model) {
EditPart part = null;
if(model instanceof TournamentData)
{
part = new FloorplanEditPart();
}
else if(model instanceof Game)
{
part = new GameEditPart();
}

if(part != null)
{
part.setModel(model);
part.setParent(context);
}
return part;
}