/* PPDemo.java * * Copyright (c) 2003, Matti Katila * * You may use and distribute under the terms of either the GNU Lesser * General Public License, either version 2 of the license or, * at your choice, any later version. Alternatively, you may use and * distribute under the terms of the XPL. * * See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of * the licenses. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the README * file for more details. * */ /* * Written by Matti Katila */ package gzz.modules.pp; import gzz.*; import gzz.view.AbstractViewContext; import gzz.modules.pp.*; import gzz.client.AbstractUpdateManager; import gzz.client.gl.GLUpdateManager; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.util.Random; /** PPDemo's meaning is to show the pp's zzstructure with motion. * So let's talk about it like demo or robustness test. */ public class PPDemo implements ActionListener { public static final String rcsid = "$Id: PPDemo.java,v 1.1 2003/01/27 15:58:22 mudyc Exp $"; private static boolean dbg = false; private void pa(String s) { System.out.println(s); } private PPView2 ppv; private AbstractViewContext avc; public PPDemo(PPView2 p, GLUpdateManager g, AbstractViewContext a) { this.ppv = p; this.avc = a; g.addActionListener(this); } private Cell start; private boolean justCameToPaper = true; public void actionPerformed(ActionEvent e) { if (dbg) pa("Event "+e); PPDims d = ppv.d; Cell c = avc.getAccursed(); if (dbg) pa("In Cell: "+c); start = c.h(d.clone); // if just cam to paper - jump to different note if (justCameToPaper) { ppv.clearInterps(); if (dbg) pa("Just came to paper.."); if (c.s(d.contains, 1) != null) { if (dbg) pa(" Go rigth"); c = randomMoves(d.contains, 1); } else if (c.s(d.contains, -1) != null) { if (dbg) pa(" Go left"); c = randomMoves(d.contains, -1); } else { // only one note and that is link.. we go back.. if (c.h(d.clone).s(d.clone) != null) { c = c.h(d.clone).s(d.clone).s(d.association); c = c.h(d.clone); avc.setAccursed(c); setAnim(start,c); AbstractUpdateManager.chg(); return; } else throw new Error("No links, how do I get in here??"); } justCameToPaper = false; } // we can now go to next link because we have jumped around.. else { pa("Already in paper.."); if (c.h(d.clone).s(d.clone) != null) { pa(" Jump to link"); if (c.h(d.clone).s(d.clone).s(d.association, 1) != null) c = c.h(d.clone).s(d.clone).s(d.association, 1); else c = c.h(d.clone).s(d.clone).s(d.association, -1); c = c.h(d.clone); setAnim(start,c); } justCameToPaper = true; } if (dbg) pa("To Cell: "+c); c = c.h(d.clone); avc.setCursorOffset(0); avc.setAccursed(c); AbstractUpdateManager.chg(); } private Cell randomMoves(Dim d, int dir) { Cell c = avc.getAccursed(); // count how many can we go int jumps = 0; for (; c != null; c = c.s(d, dir)) jumps++; if (jumps == 0) return null; Random r = new Random(); int go = 1 + r.nextInt(jumps-1); c = avc.getAccursed(); for (int i=0; i