/**
   * Sets the time in milliseconds of the last activity related to this <tt>Conference</tt> to the
   * current system time.
   */
  public void touch() {
    long now = System.currentTimeMillis();

    synchronized (this) {
      if (getLastActivityTime() < now) lastActivityTime = now;
    }
  }
Esempio n. 2
0
  public void mapdata(Message msg) {
    long now = System.currentTimeMillis();
    int pktid = msg.int32();
    int off = msg.uint16();
    int len = msg.uint16();
    Defrag fragbuf;
    synchronized (fragbufs) {
      if ((fragbuf = fragbufs.get(pktid)) == null) {
        fragbuf = new Defrag(len);
        fragbufs.put(pktid, fragbuf);
      }
      fragbuf.add(msg.blob, 8, msg.blob.length - 8, off);
      fragbuf.last = now;
      if (fragbuf.done()) {
        mapdata2(fragbuf.msg());
        fragbufs.remove(pktid);
      }

      /* Clean up old buffers */
      for (Iterator<Map.Entry<Integer, Defrag>> i = fragbufs.entrySet().iterator(); i.hasNext(); ) {
        Map.Entry<Integer, Defrag> e = i.next();
        Defrag old = e.getValue();
        if (now - old.last > 10000) i.remove();
      }
    }
  }
Esempio n. 3
0
 public static void main(String[] args) throws Exception {
   // 创建一个字符串对象
   String str = new String("Struts2权威指南");
   // 创建一个引用队列
   ReferenceQueue rq = new ReferenceQueue();
   // 创建一个虚引用,让此虚引用引用到"Struts2权威指南"字符串
   PhantomReference pr = new PhantomReference(str, rq);
   // 切断str引用和"Struts2权威指南"字符串之间的引用
   str = null;
   // 取出虚引用所引用的对象,并不能通过虚引用访问被引用的对象,所以此处输出null
   System.out.println(pr.get());
   // 强制垃圾回收
   System.gc();
   System.runFinalization();
   // 取出引用队列中最先进入队列中引用与pr进行比较
   System.out.println(rq.poll() == pr);
 }
Esempio n. 4
0
  /**
   * Runs in {@link #sendKeepAliveMessageThread} and sends STUN keep-alive <tt>Message</tt>s to the
   * STUN server associated with the <tt>StunCandidateHarvester</tt> of this instance.
   *
   * @return <tt>true</tt> if the method is to be invoked again; otherwise, <tt>false</tt>
   */
  private boolean runInSendKeepAliveMessageThread() {
    synchronized (sendKeepAliveMessageSyncRoot) {
      // Since we're going to #wait, make sure we're not canceled yet.
      if (sendKeepAliveMessageThread != Thread.currentThread()) return false;
      if (sendKeepAliveMessageInterval == SEND_KEEP_ALIVE_MESSAGE_INTERVAL_NOT_SPECIFIED) {
        return false;
      }

      // Determine the amount of milliseconds that we'll have to #wait.
      long timeout;

      if (sendKeepAliveMessageTime == -1) {
        /*
         * If we're just starting, don't just go and send a new STUN
         * keep-alive message but rather wait for the whole interval.
         */
        timeout = sendKeepAliveMessageInterval;
      } else {
        timeout =
            sendKeepAliveMessageTime + sendKeepAliveMessageInterval - System.currentTimeMillis();
      }
      // At long last, #wait if necessary.
      if (timeout > 0) {
        try {
          sendKeepAliveMessageSyncRoot.wait(timeout);
        } catch (InterruptedException iex) {
        }
        /*
         * Apart from being the time to send the STUN keep-alive
         * message, it could be that we've experienced a spurious
         * wake-up or that we've been canceled.
         */
        return true;
      }
    }

    sendKeepAliveMessageTime = System.currentTimeMillis();
    try {
      sendKeepAliveMessage();
    } catch (StunException sex) {
      logger.log(Level.INFO, "Failed to send STUN keep-alive message.", sex);
    }
    return true;
  }
Esempio n. 5
0
 public void ctick() {
   long now = System.currentTimeMillis();
   int dt = (int) (now - lastctick);
   synchronized (grids) {
     for (Grid g : grids.values()) {
       g.tick(dt);
     }
   }
   lastctick = now;
 }
  /* We look for System property sun.jvm.hotspot.jdi.<vm version>.
   * This property should have the value of JDK HOME directory for
   * the given <vm version>.
   */
  private static String getSAClassPathForVM(String vmVersion) {
    final String prefix = "sun.jvm.hotspot.jdi.";
    // look for exact match of VM version
    String jvmHome = System.getProperty(prefix + vmVersion);
    if (DEBUG) {
      System.out.println("looking for System property " + prefix + vmVersion);
    }

    if (jvmHome == null) {
      // omit chars after first '-' in VM version and try
      // for example, in '1.5.0-b55' we take '1.5.0'
      int index = vmVersion.indexOf('-');
      if (index != -1) {
        vmVersion = vmVersion.substring(0, index);
        if (DEBUG) {
          System.out.println("looking for System property " + prefix + vmVersion);
        }
        jvmHome = System.getProperty(prefix + vmVersion);
      }

      if (jvmHome == null) {
        // System property is not set
        if (DEBUG) {
          System.out.println("can't locate JDK home for " + vmVersion);
        }
        return null;
      }
    }

    if (DEBUG) {
      System.out.println("JDK home for " + vmVersion + " is " + jvmHome);
    }

    // sa-jdi is in $JDK_HOME/lib directory
    StringBuffer buf = new StringBuffer();
    buf.append(jvmHome);
    buf.append(File.separatorChar);
    buf.append("lib");
    buf.append(File.separatorChar);
    buf.append("sa-jdi.jar");
    return buf.toString();
  }
 public Object[] toArray() {
   cleanupGarbageCollectedValues();
   Object[] result = new Object[elementSize];
   int resultSize = 0;
   for (int i = 0; i < values.length; i++) {
     if (values[i] == null) continue;
     Object tmp = values[i].get();
     if (tmp != null) result[resultSize++] = tmp;
   }
   if (result.length == resultSize) return result;
   Object[] finalResult = new Object[resultSize];
   System.arraycopy(result, 0, finalResult, 0, resultSize);
   return finalResult;
 }
Esempio n. 8
0
 public void sendreqs() {
   long now = System.currentTimeMillis();
   synchronized (req) {
     for (Iterator<Map.Entry<Coord, Request>> i = req.entrySet().iterator(); i.hasNext(); ) {
       Map.Entry<Coord, Request> e = i.next();
       Coord c = e.getKey();
       Request r = e.getValue();
       if (now - r.lastreq > 1000) {
         r.lastreq = now;
         if (++r.reqs >= 5) {
           i.remove();
         } else {
           Message msg = new Message(Session.MSG_MAPREQ);
           msg.addcoord(c);
           sess.sendmsg(msg);
         }
       }
     }
   }
 }
Esempio n. 9
0
public class MCache {
  public static final Coord tilesz = new Coord(11, 11);
  public static final Coord cmaps = new Coord(100, 100);
  public static final Coord cutsz = new Coord(25, 25);
  public static final Coord cutn = cmaps.div(cutsz);
  private final Resource.Spec[] nsets = new Resource.Spec[256];

  @SuppressWarnings("unchecked")
  private final Reference<Resource>[] sets = new Reference[256];

  @SuppressWarnings("unchecked")
  private final Reference<Tileset>[] csets = new Reference[256];

  @SuppressWarnings("unchecked")
  private final Reference<Tiler>[] tiles = new Reference[256];

  Map<Coord, Request> req = new HashMap<Coord, Request>();
  Map<Coord, Grid> grids = new HashMap<Coord, Grid>();
  Session sess;
  Set<Overlay> ols = new HashSet<Overlay>();
  int olseq = 0;
  Random gen = new Random();
  Map<Integer, Defrag> fragbufs = new TreeMap<Integer, Defrag>();
  long lastctick = System.currentTimeMillis();

  public static class LoadingMap extends Loading {
    public LoadingMap() {}

    public LoadingMap(Throwable cause) {
      super(cause);
    }
  }

  private static class Request {
    private long lastreq = 0;
    private int reqs = 0;
  }

  public class Overlay {
    private Coord c1, c2;
    private int mask;

    public Overlay(Coord c1, Coord c2, int mask) {
      this.c1 = c1;
      this.c2 = c2;
      this.mask = mask;
      ols.add(this);
      olseq++;
    }

    public void destroy() {
      ols.remove(this);
    }

    public void update(Coord c1, Coord c2) {
      if (!c1.equals(this.c1) || !c2.equals(this.c2)) {
        olseq++;
        this.c1 = c1;
        this.c2 = c2;
      }
    }
  }

  public class Grid {
    public final int tiles[] = new int[cmaps.x * cmaps.y];
    public final int z[] = new int[cmaps.x * cmaps.y];
    public final int ol[] = new int[cmaps.x * cmaps.y];
    private final Cut cuts[];
    int olseq = -1;
    private Collection<Gob>[] fo = null;
    public final Coord gc, ul;
    public long id;
    String mnm;

    private class Cut {
      MapMesh mesh;
      Defer.Future<MapMesh> dmesh;
      Rendered[] ols;
      int deftag;
    }

    private class Flavobj extends Gob {
      private Flavobj(Coord c, double a) {
        super(sess.glob, c);
        this.a = a;
      }

      public Random mkrandoom() {
        Random r = new Random(Grid.this.id);
        r.setSeed(r.nextInt() ^ rc.x);
        r.setSeed(r.nextInt() ^ rc.y);
        return (r);
      }
    }

    public Grid(Coord gc) {
      this.gc = gc;
      this.ul = gc.mul(cmaps);
      cuts = new Cut[cutn.x * cutn.y];
      for (int i = 0; i < cuts.length; i++) cuts[i] = new Cut();
    }

    public int gettile(Coord tc) {
      return (tiles[tc.x + (tc.y * cmaps.x)]);
    }

    public int getz(Coord tc) {
      return (z[tc.x + (tc.y * cmaps.x)]);
    }

    public int getol(Coord tc) {
      return (ol[tc.x + (tc.y * cmaps.x)]);
    }

    private void makeflavor() {
      @SuppressWarnings("unchecked")
      Collection<Gob>[] fo = (Collection<Gob>[]) new Collection[cutn.x * cutn.y];
      for (int i = 0; i < fo.length; i++) fo[i] = new LinkedList<Gob>();
      Coord c = new Coord(0, 0);
      Coord tc = gc.mul(cmaps);
      int i = 0;
      Random rnd = new Random(id);
      for (c.y = 0; c.y < cmaps.x; c.y++) {
        for (c.x = 0; c.x < cmaps.y; c.x++, i++) {
          Tileset set = tileset(tiles[i]);
          if (set.flavobjs.size() > 0) {
            if (rnd.nextInt(set.flavprob) == 0) {
              Resource r = set.flavobjs.pick(rnd);
              double a = rnd.nextDouble() * 2 * Math.PI;
              Gob g = new Flavobj(c.add(tc).mul(tilesz).add(tilesz.div(2)), a);
              g.setattr(new ResDrawable(g, r));
              Coord cc = c.div(cutsz);
              fo[cc.x + (cc.y * cutn.x)].add(g);
            }
          }
        }
      }
      this.fo = fo;
    }

    public Collection<Gob> getfo(Coord cc) {
      if (fo == null) makeflavor();
      return (fo[cc.x + (cc.y * cutn.x)]);
    }

    private Cut geticut(Coord cc) {
      return (cuts[cc.x + (cc.y * cutn.x)]);
    }

    public MapMesh getcut(Coord cc) {
      Cut cut = geticut(cc);
      if (cut.dmesh != null) {
        if (cut.dmesh.done() || (cut.mesh == null)) {
          cut.mesh = cut.dmesh.get();
          cut.dmesh = null;
        }
      }
      return (cut.mesh);
    }

    public Rendered getolcut(int ol, Coord cc) {
      int nseq = MCache.this.olseq;
      if (this.olseq != nseq) {
        for (int i = 0; i < cutn.x * cutn.y; i++) cuts[i].ols = null;
        this.olseq = nseq;
      }
      Cut cut = geticut(cc);
      if (cut.ols == null) cut.ols = getcut(cc).makeols();
      return (cut.ols[ol]);
    }

    private void buildcut(final Coord cc) {
      final Cut cut = geticut(cc);
      final int deftag = ++cut.deftag;
      cut.dmesh =
          Defer.later(
              new Defer.Callable<MapMesh>() {
                public MapMesh call() {
                  Random rnd = new Random(id);
                  rnd.setSeed(rnd.nextInt() ^ cc.x);
                  rnd.setSeed(rnd.nextInt() ^ cc.y);
                  return (MapMesh.build(MCache.this, rnd, ul.add(cc.mul(cutsz)), cutsz));
                }
              });
    }

    public void ivneigh(Coord nc) {
      Coord cc = new Coord();
      for (cc.y = 0; cc.y < cutn.y; cc.y++) {
        for (cc.x = 0; cc.x < cutn.x; cc.x++) {
          if ((((nc.x < 0) && (cc.x == 0)) || ((nc.x > 0) && (cc.x == cutn.x - 1)) || (nc.x == 0))
              && (((nc.y < 0) && (cc.y == 0))
                  || ((nc.y > 0) && (cc.y == cutn.y - 1))
                  || (nc.y == 0))) {
            buildcut(new Coord(cc));
          }
        }
      }
    }

    public void tick(int dt) {
      if (fo != null) {
        for (Collection<Gob> fol : fo) {
          for (Gob fo : fol) fo.ctick(dt);
        }
      }
    }

    private void invalidate() {
      for (int y = 0; y < cutn.y; y++) {
        for (int x = 0; x < cutn.x; x++) buildcut(new Coord(x, y));
      }
      fo = null;
      for (Coord ic :
          new Coord[] {
            new Coord(-1, -1),
            new Coord(0, -1),
            new Coord(1, -1),
            new Coord(-1, 0),
            new Coord(1, 0),
            new Coord(-1, 1),
            new Coord(0, 1),
            new Coord(1, 1)
          }) {
        Grid ng = grids.get(gc.add(ic));
        if (ng != null) ng.ivneigh(ic.inv());
      }
      loaded = true;
    }

    public void fill(Message msg) {
      String mmname = msg.string().intern();
      if (mmname.equals("")) mnm = null;
      else mnm = mmname;
      int[] pfl = new int[256];
      while (true) {
        int pidx = msg.uint8();
        if (pidx == 255) break;
        pfl[pidx] = msg.uint8();
      }
      Message blob = msg.inflate();
      id = blob.int64();
      for (int i = 0; i < tiles.length; i++) tiles[i] = blob.uint8();
      for (int i = 0; i < z.length; i++) z[i] = blob.int16();
      for (int i = 0; i < ol.length; i++) ol[i] = 0;
      while (true) {
        int pidx = blob.uint8();
        if (pidx == 255) break;
        int fl = pfl[pidx];
        int type = blob.uint8();
        Coord c1 = new Coord(blob.uint8(), blob.uint8());
        Coord c2 = new Coord(blob.uint8(), blob.uint8());
        int ol;
        if (type == 0) {
          if ((fl & 1) == 1) ol = 2;
          else ol = 1;
        } else if (type == 1) {
          if ((fl & 1) == 1) ol = 8;
          else ol = 4;
        } else {
          throw (new RuntimeException("Unknown plot type " + type));
        }
        for (int y = c1.y; y <= c2.y; y++) {
          for (int x = c1.x; x <= c2.x; x++) {
            this.ol[x + (y * cmaps.x)] |= ol;
          }
        }
      }
      invalidate();
    }

    /* ===== BD Code Start ===== */
    private BufferedImage gridImage;
    private boolean loaded = false;
    private boolean rendered = false;

    private int getz(int x, int y) {
      return (z[x + (y * cmaps.x)]);
    }

    public BufferedImage getGridImage() {
      if (rendered) return gridImage;

      if (!loaded) throw new Loading();

      gridImage = TexI.mkbuf(cmaps);

      BufferedImage[] texes = new BufferedImage[256];

      Coord c = new Coord();
      for (c.y = 0; c.y < cmaps.y; c.y++) {
        for (c.x = 0; c.x < cmaps.x; c.x++) {
          int t = gettile(c);
          BufferedImage tex = tileimg(t, texes);
          if (tex != null)
            gridImage.setRGB(
                c.x,
                c.y,
                tex.getRGB(
                    Utils.floormod(c.x, tex.getWidth()), Utils.floormod(c.y, tex.getHeight())));
        }
      }
      for (c.y = 1; c.y < cmaps.y - 1; c.y++) {
        for (c.x = 1; c.x < cmaps.x - 1; c.x++) {
          int t = gettile(c);
          if ((gettile(c.add(-1, 0)) > t)
              || (gettile(c.add(1, 0)) > t)
              || (gettile(c.add(0, -1)) > t)
              || (gettile(c.add(0, 1)) > t)) gridImage.setRGB(c.x, c.y, Color.BLACK.getRGB());
        }
      }
      rendered = true;
      return gridImage;
    }
    /* ===== BD Code End ===== */
  }

  public MCache(Session sess) {
    this.sess = sess;
  }

  public void ctick() {
    long now = System.currentTimeMillis();
    int dt = (int) (now - lastctick);
    synchronized (grids) {
      for (Grid g : grids.values()) {
        g.tick(dt);
      }
    }
    lastctick = now;
  }

  public void invalidate(Coord cc) {
    synchronized (req) {
      if (req.get(cc) == null) req.put(cc, new Request());
    }
  }

  public void invalblob(Message msg) {
    int type = msg.uint8();
    if (type == 0) {
      invalidate(msg.coord());
    } else if (type == 1) {
      Coord ul = msg.coord();
      Coord lr = msg.coord();
      trim(ul, lr);
    } else if (type == 2) {
      trimall();
    }
  }

  private Grid cached = null;

  public Grid getgrid(Coord gc) {
    synchronized (grids) {
      if ((cached == null) || !cached.gc.equals(cached)) {
        cached = grids.get(gc);
        if (cached == null) {
          request(gc);
          throw (new LoadingMap());
        }
      }
      return (cached);
    }
  }

  public Grid getgridt(Coord tc) {
    return (getgrid(tc.div(cmaps)));
  }

  public int gettile(Coord tc) {
    Grid g = getgridt(tc);
    return (g.gettile(tc.sub(g.ul)));
  }

  public int getz(Coord tc) {
    Grid g = getgridt(tc);
    return (g.getz(tc.sub(g.ul)));
  }

  public float getcz(float px, float py) {
    float tw = tilesz.x, th = tilesz.y;
    Coord ul = new Coord(Utils.floordiv(px, tw), Utils.floordiv(py, th));
    float sx = Utils.floormod(px, tw) / tw;
    float sy = Utils.floormod(py, th) / th;
    return (((1.0f - sy) * (((1.0f - sx) * getz(ul)) + (sx * getz(ul.add(1, 0)))))
        + (sy * (((1.0f - sx) * getz(ul.add(0, 1))) + (sx * getz(ul.add(1, 1))))));
  }

  public float getcz(Coord pc) {
    return (getcz(pc.x, pc.y));
  }

  public int getol(Coord tc) {
    Grid g = getgridt(tc);
    int ol = g.getol(tc.sub(g.ul));
    for (Overlay lol : ols) {
      if (tc.isect(lol.c1, lol.c2.add(lol.c1.inv()).add(new Coord(1, 1)))) ol |= lol.mask;
    }
    return (ol);
  }

  public MapMesh getcut(Coord cc) {
    return (getgrid(cc.div(cutn)).getcut(cc.mod(cutn)));
  }

  public Collection<Gob> getfo(Coord cc) {
    return (getgrid(cc.div(cutn)).getfo(cc.mod(cutn)));
  }

  public Rendered getolcut(int ol, Coord cc) {
    return (getgrid(cc.div(cutn)).getolcut(ol, cc.mod(cutn)));
  }

  public void mapdata2(Message msg) {
    Coord c = msg.coord();
    synchronized (grids) {
      synchronized (req) {
        if (req.containsKey(c)) {
          Grid g = grids.get(c);
          if (g == null) grids.put(c, g = new Grid(c));
          g.fill(msg);
          req.remove(c);
          olseq++;
        }
      }
    }
  }

  public void mapdata(Message msg) {
    long now = System.currentTimeMillis();
    int pktid = msg.int32();
    int off = msg.uint16();
    int len = msg.uint16();
    Defrag fragbuf;
    synchronized (fragbufs) {
      if ((fragbuf = fragbufs.get(pktid)) == null) {
        fragbuf = new Defrag(len);
        fragbufs.put(pktid, fragbuf);
      }
      fragbuf.add(msg.blob, 8, msg.blob.length - 8, off);
      fragbuf.last = now;
      if (fragbuf.done()) {
        mapdata2(fragbuf.msg());
        fragbufs.remove(pktid);
      }

      /* Clean up old buffers */
      for (Iterator<Map.Entry<Integer, Defrag>> i = fragbufs.entrySet().iterator(); i.hasNext(); ) {
        Map.Entry<Integer, Defrag> e = i.next();
        Defrag old = e.getValue();
        if (now - old.last > 10000) i.remove();
      }
    }
  }

  public Resource tilesetr(int i) {
    synchronized (sets) {
      Resource res = (sets[i] == null) ? null : (sets[i].get());
      if (res == null) {
        if (nsets[i] == null) return (null);
        res = nsets[i].get();
        sets[i] = new SoftReference<Resource>(res);
      }
      return (res);
    }
  }

  public Tileset tileset(int i) {
    synchronized (csets) {
      Tileset cset = (csets[i] == null) ? null : (csets[i].get());
      if (cset == null) {
        Resource res = tilesetr(i);
        if (res == null) return (null);
        try {
          cset = res.layer(Resource.tileset);
        } catch (Loading e) {
          throw (new LoadingMap(e));
        }
        csets[i] = new SoftReference<Tileset>(cset);
      }
      return (cset);
    }
  }

  public Tiler tiler(int i) {
    synchronized (tiles) {
      Tiler tile = (tiles[i] == null) ? null : (tiles[i].get());
      if (tile == null) {
        Tileset set = tileset(i);
        if (set == null) return (null);
        tile = set.tfac().create(i, set);
        tiles[i] = new SoftReference<Tiler>(tile);
      }
      return (tile);
    }
  }

  public void tilemap(Message msg) {
    while (!msg.eom()) {
      int id = msg.uint8();
      String resnm = msg.string();
      int resver = msg.uint16();
      nsets[id] = new Resource.Spec(resnm, resver);
    }
  }

  public void trimall() {
    synchronized (grids) {
      synchronized (req) {
        grids.clear();
        req.clear();
      }
    }
  }

  public void trim(Coord ul, Coord lr) {
    synchronized (grids) {
      synchronized (req) {
        for (Iterator<Map.Entry<Coord, Grid>> i = grids.entrySet().iterator(); i.hasNext(); ) {
          Map.Entry<Coord, Grid> e = i.next();
          Coord gc = e.getKey();
          Grid g = e.getValue();
          if ((gc.x < ul.x) || (gc.y < ul.y) || (gc.x > lr.x) || (gc.y > lr.y)) i.remove();
        }
        for (Iterator<Coord> i = req.keySet().iterator(); i.hasNext(); ) {
          Coord gc = i.next();
          if ((gc.x < ul.x) || (gc.y < ul.y) || (gc.x > lr.x) || (gc.y > lr.y)) i.remove();
        }
      }
    }
  }

  public void request(Coord gc) {
    synchronized (req) {
      if (!req.containsKey(gc)) req.put(gc, new Request());
    }
  }

  public void reqarea(Coord ul, Coord br) {
    ul = ul.div(cutsz);
    br = br.div(cutsz);
    Coord rc = new Coord();
    for (rc.y = ul.y; rc.y <= br.y; rc.y++) {
      for (rc.x = ul.x; rc.x <= br.x; rc.x++) {
        try {
          getcut(new Coord(rc));
        } catch (Loading e) {
        }
      }
    }
  }

  public void sendreqs() {
    long now = System.currentTimeMillis();
    synchronized (req) {
      for (Iterator<Map.Entry<Coord, Request>> i = req.entrySet().iterator(); i.hasNext(); ) {
        Map.Entry<Coord, Request> e = i.next();
        Coord c = e.getKey();
        Request r = e.getValue();
        if (now - r.lastreq > 1000) {
          r.lastreq = now;
          if (++r.reqs >= 5) {
            i.remove();
          } else {
            Message msg = new Message(Session.MSG_MAPREQ);
            msg.addcoord(c);
            sess.sendmsg(msg);
          }
        }
      }
    }
  }

  /* ===== BD Code Start ===== */
  private BufferedImage tileimg(int t, BufferedImage[] texes) {
    BufferedImage img = texes[t];
    if (img == null) {
      Resource r = tilesetr(t);
      if (r == null) return (null);
      Resource.Image ir = r.layer(Resource.imgc);
      if (ir == null) return (null);
      img = ir.img;
      texes[t] = img;
    }
    return (img);
  }
  /* ===== BD Code End ===== */
}
  /**
   * Returns the main control panel.
   *
   * @param mainFrame The main test frame.
   * @param mainTabbedPane The main tabbed pane.
   * @param mainTabPreviewPainter The preview painter of the main tabbed pane.
   * @param toolbar The toolbar of the main test frame.
   * @return The main control panel.
   */
  public static JPanel getMainControlPanel(
      final JFrame mainFrame,
      final JTabbedPane mainTabbedPane,
      final MyMainTabPreviewPainter mainTabPreviewPainter,
      final JToolBar toolbar) {
    FormLayout lm = new FormLayout("right:pref, 4dlu, fill:pref:grow", "");
    DefaultFormBuilder builder = new DefaultFormBuilder(lm);
    // builder.setDefaultDialogBorder();

    builder.appendSeparator("Title pane settings");
    final JCheckBox markAsModified = new JCheckBox("Marked modified");
    markAsModified.setOpaque(false);
    markAsModified.setSelected(false);
    markAsModified.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            mainFrame
                .getRootPane()
                .putClientProperty(
                    SubstanceLookAndFeel.WINDOW_MODIFIED,
                    (markAsModified.isSelected() ? Boolean.TRUE : false));
          }
        });
    builder.append("Modified", markAsModified);

    final JCheckBox heapPanel = new JCheckBox("Has heap panel");
    heapPanel.setOpaque(false);
    heapPanel.setSelected(false);
    heapPanel.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            SubstanceLookAndFeel.setWidgetVisible(
                mainFrame.getRootPane(),
                heapPanel.isSelected(),
                SubstanceWidgetType.TITLE_PANE_HEAP_STATUS);
          }
        });
    builder.append("Heap panel", heapPanel);

    JButton changeTitleButton = new JButton("Change");
    changeTitleButton.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            String random = "abcdefghijklmnopqrstuvwxyz ";
            int length = 60 + (int) (150 * Math.random());
            String title = "";
            while (length > 0) {
              title += random.charAt((int) (random.length() * Math.random()));
              length--;
            }
            mainFrame.setTitle(title);
          }
        });
    builder.append("Title string", changeTitleButton);

    builder.appendSeparator("Miscellaneous");

    final JCheckBox defaultRoundedCorners = new JCheckBox("by default");
    defaultRoundedCorners.setOpaque(false);
    boolean roundable =
        Boolean.valueOf(
            System.getProperty(SubstanceLookAndFeel.WINDOW_ROUNDED_CORNERS_PROPERTY, "True"));
    defaultRoundedCorners.setSelected(roundable);
    defaultRoundedCorners.setEnabled(roundable);
    defaultRoundedCorners.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            UIManager.put(
                SubstanceLookAndFeel.WINDOW_ROUNDED_CORNERS, defaultRoundedCorners.isSelected());
          }
        });
    builder.append("Rounded Windows", defaultRoundedCorners);

    final JCheckBox useThemedDefaultIconsCheckBox = new JCheckBox("use themed icons");
    useThemedDefaultIconsCheckBox.setOpaque(false);
    useThemedDefaultIconsCheckBox.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            SwingUtilities.invokeLater(
                new Runnable() {
                  @Override
                  public void run() {
                    UIManager.put(
                        SubstanceLookAndFeel.USE_THEMED_DEFAULT_ICONS,
                        useThemedDefaultIconsCheckBox.isSelected() ? Boolean.TRUE : null);
                    mainFrame.repaint();
                  }
                });
          }
        });
    builder.append("Themed icons", useThemedDefaultIconsCheckBox);

    final JCheckBox useConstantThemesOnDialogs = new JCheckBox("use constant themes");
    useConstantThemesOnDialogs.setOpaque(false);
    useConstantThemesOnDialogs.setSelected(SubstanceLookAndFeel.isToUseConstantThemesOnDialogs());
    useConstantThemesOnDialogs.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            SwingUtilities.invokeLater(
                new Runnable() {
                  @Override
                  public void run() {
                    SubstanceLookAndFeel.setToUseConstantThemesOnDialogs(
                        useConstantThemesOnDialogs.isSelected());
                    SubstanceLookAndFeel.setSkin(
                        SubstanceLookAndFeel.getCurrentSkin(mainFrame.getRootPane()));
                  }
                });
          }
        });
    builder.append("Option pane icons", useConstantThemesOnDialogs);

    final JComboBox placementCombo = new JComboBox(new Object[] {"top", "bottom", "left", "right"});
    placementCombo.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            String selected = (String) placementCombo.getSelectedItem();
            if ("top".equals(selected)) mainTabbedPane.setTabPlacement(JTabbedPane.TOP);
            if ("bottom".equals(selected)) mainTabbedPane.setTabPlacement(JTabbedPane.BOTTOM);
            if ("left".equals(selected)) mainTabbedPane.setTabPlacement(JTabbedPane.LEFT);
            if ("right".equals(selected)) mainTabbedPane.setTabPlacement(JTabbedPane.RIGHT);
          }
        });
    builder.append("Placement", placementCombo);

    try {
      final JComboBox overviewKindCombo =
          new FlexiComboBox<TabOverviewKind>(
              TabOverviewKind.GRID, TabOverviewKind.MENU_CAROUSEL, TabOverviewKind.ROUND_CAROUSEL) {
            @Override
            public String getCaption(TabOverviewKind item) {
              return item.getName();
            }
          };
      overviewKindCombo.setSelectedItem(
          LafWidgetUtilities2.getTabPreviewPainter(mainTabbedPane).getOverviewKind(mainTabbedPane));
      overviewKindCombo.addActionListener(
          new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
              mainTabPreviewPainter.setTabOverviewKind(
                  (TabOverviewKind) overviewKindCombo.getSelectedItem());
            }
          });

      builder.append("Overview kind", overviewKindCombo);
    } catch (NoClassDefFoundError ignored) {
    }

    final JComboBox menuGutterFillCombo =
        new FlexiComboBox<MenuGutterFillKind>(
            MenuGutterFillKind.NONE,
            MenuGutterFillKind.SOFT,
            MenuGutterFillKind.HARD,
            MenuGutterFillKind.SOFT_FILL,
            MenuGutterFillKind.HARD_FILL) {
          @Override
          public String getCaption(MenuGutterFillKind item) {
            return item.name();
          }
        };
    menuGutterFillCombo.setSelectedItem(MenuGutterFillKind.HARD);
    menuGutterFillCombo.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            UIManager.put(
                SubstanceLookAndFeel.MENU_GUTTER_FILL_KIND, menuGutterFillCombo.getSelectedItem());
          }
        });
    builder.append("Menu fill", menuGutterFillCombo);

    final JComboBox focusKindCombo =
        new FlexiComboBox<FocusKind>(FocusKind.values()) {
          @Override
          public String getCaption(FocusKind item) {
            return item.name();
          }
        };
    focusKindCombo.setSelectedItem(FocusKind.ALL_INNER);
    focusKindCombo.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            UIManager.put(SubstanceLookAndFeel.FOCUS_KIND, focusKindCombo.getSelectedItem());
          }
        });
    builder.append("Focus kind", focusKindCombo);

    JButton buttonGlassPane = new JButton("Show");
    buttonGlassPane.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            final JPanel glassPane =
                new JPanel() {
                  @Override
                  public void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    Graphics2D graphics = (Graphics2D) g;
                    int height = getHeight();
                    int width = getWidth();
                    Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) 0.4);
                    graphics.setComposite(c);
                    for (int i = 0; i < height; i++) {
                      Color color =
                          (i % 2 == 0) ? new Color(200, 200, 255) : new Color(230, 230, 255);
                      graphics.setColor(color);
                      graphics.drawLine(0, i, width, i);
                    }
                    Composite c2 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) 1.0);
                    graphics.setComposite(c2);
                  }
                };
            glassPane.setOpaque(false);
            glassPane.addMouseListener(new MouseAdapter() {});
            glassPane.addKeyListener(new KeyAdapter() {});
            mainFrame.setGlassPane(glassPane);
            new Thread() {
              @Override
              public void run() {
                glassPane.setVisible(true);
                try {
                  Thread.sleep(5000);
                } catch (InterruptedException ie) {
                  ie.printStackTrace();
                }
                glassPane.setVisible(false);
              }
            }.start();
          }
        });
    builder.append("Glass pane", buttonGlassPane);

    builder.appendSeparator("Custom animations");
    final JCheckBox allowFocusLoopAnimations = new JCheckBox("Allow animation");
    allowFocusLoopAnimations.setOpaque(false);
    allowFocusLoopAnimations.setSelected(
        AnimationConfigurationManager.getInstance()
            .isAnimationAllowed(AnimationFacet.FOCUS_LOOP_ANIMATION, null));
    allowFocusLoopAnimations.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            if (allowFocusLoopAnimations.isSelected()) {
              AnimationConfigurationManager.getInstance()
                  .allowAnimations(AnimationFacet.FOCUS_LOOP_ANIMATION);
            } else {
              AnimationConfigurationManager.getInstance()
                  .disallowAnimations(AnimationFacet.FOCUS_LOOP_ANIMATION);
            }
          }
        });
    builder.append("Focus loop", allowFocusLoopAnimations);

    final JCheckBox allowGlowIconAnimations = new JCheckBox("Allow animation");
    allowGlowIconAnimations.setOpaque(false);
    allowGlowIconAnimations.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            if (allowGlowIconAnimations.isSelected()) {
              AnimationConfigurationManager.getInstance().allowAnimations(AnimationFacet.ICON_GLOW);
            } else {
              AnimationConfigurationManager.getInstance()
                  .disallowAnimations(AnimationFacet.ICON_GLOW);
            }
          }
        });
    builder.append("Icon glow", allowGlowIconAnimations);

    final JCheckBox allowGhostIconAnimations = new JCheckBox("Allow animation");
    allowGhostIconAnimations.setOpaque(false);
    allowGhostIconAnimations.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            if (allowGhostIconAnimations.isSelected()) {
              AnimationConfigurationManager.getInstance()
                  .allowAnimations(AnimationFacet.GHOSTING_ICON_ROLLOVER);
            } else {
              AnimationConfigurationManager.getInstance()
                  .disallowAnimations(AnimationFacet.GHOSTING_ICON_ROLLOVER);
            }
          }
        });
    builder.append("Icon rollover", allowGhostIconAnimations);

    final JCheckBox allowGhostPressAnimations = new JCheckBox("Allow animation");
    allowGhostPressAnimations.setOpaque(false);
    allowGhostPressAnimations.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            if (allowGhostPressAnimations.isSelected()) {
              AnimationConfigurationManager.getInstance()
                  .allowAnimations(AnimationFacet.GHOSTING_BUTTON_PRESS);
            } else {
              AnimationConfigurationManager.getInstance()
                  .disallowAnimations(AnimationFacet.GHOSTING_BUTTON_PRESS);
            }
          }
        });
    builder.append("Button press", allowGhostPressAnimations);

    builder.appendSeparator("Toolbar");
    final JCheckBox isToolbarFlat = new JCheckBox("Is flat");
    isToolbarFlat.setOpaque(false);
    isToolbarFlat.setSelected(true);
    isToolbarFlat.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            toolbar.putClientProperty(
                SubstanceLookAndFeel.FLAT_PROPERTY, isToolbarFlat.isSelected());
            toolbar.repaint();
          }
        });
    builder.append("Flat", isToolbarFlat);

    builder.appendSeparator("Menu bar");
    final JCheckBox menuSearch = new JCheckBox("Has menu search");
    menuSearch.setOpaque(false);
    menuSearch.setSelected(false);
    menuSearch.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            SubstanceLookAndFeel.setWidgetVisible(
                mainFrame.getRootPane(), menuSearch.isSelected(), SubstanceWidgetType.MENU_SEARCH);
          }
        });
    builder.append("Menu search", menuSearch);

    final JCheckBox menuLocale = new JCheckBox("Has custom locale");
    menuLocale.setOpaque(false);
    menuLocale.setSelected(false);
    menuLocale.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            if (menuLocale.isSelected()) {
              mainFrame.getJMenuBar().setLocale(Locale.FRENCH);
              mainFrame
                  .getJMenuBar()
                  .putClientProperty(LafWidget.IGNORE_GLOBAL_LOCALE, Boolean.TRUE);
            } else {
              mainFrame.getJMenuBar().putClientProperty(LafWidget.IGNORE_GLOBAL_LOCALE, null);
            }
          }
        });
    builder.append("Menu locale", menuLocale);

    JPanel result = builder.getPanel();
    result.setName("Main control panel");
    return result;
  }
Esempio n. 11
0
 static {
   myLoader = ConnectorImpl.class.getClassLoader();
   freeVMClasses = new ArrayList(0);
   DEBUG = System.getProperty("sun.jvm.hotspot.jdi.ConnectorImpl.DEBUG") != null;
 }