package ru.sakva.bsh;

 import java.awt.*;
 import java.awt.event.*;
 import java.awt.image.*;

/** Book Object to be displayed on Shelf Applet */

 public class Book
   implements ImageObserver
/* * */ { /* * */

 static final int TURNED    = 0x1;
 static final int UPWARD    = 0x2;
 static final int SELECTED  = 0x4;
 static final int BOX       = 0x8;
 static final int CHINESE   = 0x10;

 static int hmin=12;
 static int hmax=0;
 static int volume=16384;
 static public int width=25;
 static public int height=100;
 static public int thickness=16;
 static public Color defaultCoverColor = new Color(0x000080);
 static public Color defaultTitleColor  = new Color(0xFFC040);

/** Color of cover */
 public Color coverColor;
/** Color of title */
 public Color titleColor;
/** Picture on cover */
 public Image icon;
/** length of book */
 public int a;
/** height of book */
 public int b;
/** thickness of book */
 public int c;
/** x-coordinate of book */
 public int u;
/** y-coordinate of book */
 public int v;
/** title of book */


 String title;

 int flags;

 static final int fontsNumber=12;
 static int fontStep=2;
 static int fontSizeMax=30;
 static Font[] font = new Font[fontsNumber];
 static FontMetrics[] fontMetrics = new FontMetrics[fontsNumber];

 static void initFonts(int n)
{    // prepare Fonts
 fontStep=(n-8)/fontsNumber;
 fontSizeMax = (fontsNumber-1)*fontStep+8;
 int i=0; while(i < fontsNumber)
   {
    font[i]=new Font("serif",1,fontStep*i+8);
    fontMetrics[i]=Toolkit.getDefaultToolkit().getFontMetrics(font[i]);
    i++;
    }
 }

 static { initFonts(32); }

 public static void setMinimum(int a){ hmin = a; }

 public static void setMaximum(int a){ hmax = a; }

 public Book()
{
 a=width; b=height; c=thickness; title="";
 coverColor = defaultCoverColor;
 titleColor = defaultTitleColor;
 }

 public Book(String s){ this(); setTitle(s); }

 public Book(String s,int vol){ this(s); setVolume(vol); }

 public boolean selected(){ return (flags&SELECTED)==SELECTED; }

 public void select(boolean sel)
{ if(sel)flags|=SELECTED; else flags&=(~SELECTED); }

/** Book is turned 180 deg (in vertical mode only ) */
 public boolean turned(){ return (flags&TURNED)==TURNED; }

 public void turn(boolean trn)
{ if(trn)flags|=TURNED; else flags&=(~TURNED); }

 public boolean upward(){ return (flags&UPWARD)==UPWARD; }

 public void upward(boolean dw)
{ if(dw)flags|=UPWARD; else flags&=(~UPWARD); }

 public boolean chinese(){ return (flags&CHINESE)==CHINESE; }

 public void chinese(boolean dw)
{ if(dw)flags|=CHINESE; else flags&=(~CHINESE); }

 public boolean box(){ return (flags&BOX)==BOX; }

 public void box(boolean bx)
{ if(bx)flags|=BOX; else flags&=(~BOX); }

/** Sets volume of the book */
 public void setVolume(int vol)
{
 c=(int)((float)vol/volume*(hmin-2)+2);
 if(c < hmin)c=hmin;
 else { if(hmax != 0){ if(c > hmax)c=hmax; } else if(c > b)c=b; }
 }

 public void setTitle(String s){ title = new String(s); }
 public void setReference(String s){ }
 public void setIcon(Image im){ icon=im; }

 public String getAuthor(){ return ""; }
 public String getTitle(){ return title; }

/*
 public int getWidth(){ return w; }
 public int getHeight(){ return h; }
 public int getLength(){ return l; }
*/

 public void drawTitle(Graphics g,int u,int v,int icw)
{
 int i,k,n,f,fh,l;
 String s1,s2,s;
 n = title.length();
 if(n > 0)
   {
    if(chinese())
      {
       char[] chs = title.toCharArray();
       if((fh = (b-icw)/(n+1)) > fontSizeMax)fh=fontSizeMax;
       if((f=(fh-8)/fontStep) >= 0)
         {
          g.setFont(font[f]);
          i=0; while(i < n)
            {
             l=(c-fontMetrics[f].charWidth(chs[i]))/2;
             g.drawChars(chs,i,1,l,fh*(i+1)+fh/2);
             i++;
             }
          }
       }
    else
      {
       k = title.indexOf(';');
       if(k < 0){ s1=" "+title+" "; s2=null; }
       else
         {
          s1 = " "+title.substring(0,k);
          s2 = " "+title.substring(k+1,n)+" ";
          }
       fh = c; s=s1;
       if(s2 != null)
         {
          fh/=2;
          if(fontMetrics[0].stringWidth(s1) <
             fontMetrics[0].stringWidth(s2))s=s2;
          }
       if(fh > fontSizeMax)fh=fontSizeMax; f = (fh-8)/fontStep;  l=0;
       k=b-icw;
       while(f >= 0)
         { if((l=fontMetrics[f].stringWidth(s)) < k)break; f--; fh-=fontStep; }
       if(f >= 0)
         {
          g.setFont(font[f]);
          i=(k-l)/2+u;
          if(s2 == null)g.drawString(s1,i,v+(c+fh)/2-2);
          else
            {
             g.drawString(s1,i,v+c/2-2);
             g.drawString(s2,i,v+c/2+fh-3);
             }
          }
       }
    }
 }

// Draw vertical book
 void drawVertical(Graphics g)
{
 int i,j,k,l,f,fh;
 String s0;
 int[] p = new int[4];
 int[] q = new int[4];

 if(box()==false)
   {
    p[0]=u; p[1]=u+a; p[2]=p[1];   p[3]=u;
    q[0]=v; q[1]=v-a; q[2]=q[1]+b; q[3]=v+b;
    }
 else
   {
    p[0]=u; p[1]=u+c; p[2]=u+a+c;  p[3]=u+a;
    q[0]=q[1]=v;   q[2]=q[3]=v-a;
    }

 g.setColor(coverColor);   g.fillPolygon(p,q,4);
 g.setColor(Color.black);  g.drawPolygon(p,q,4);

 if(box()==false)
   {
    i=u+3; while(i < u+c-2)
      {
       g.setColor(Color.white);
       g.drawLine(i,v,i+a-3,v-a+3); i++;
       g.setColor(Color.black);
       g.drawLine(i,v,i+a-3,v-a+3); i++;
       }
    }

 if(icon != null)g.drawImage(icon,u,v,c,b,this);
 else { g.setColor(coverColor); g.fillRect(u,v,c,b); }

 g.setColor(Color.black); g.drawRect(u,v,c,b);

 p[0]=u+c; p[1]=u+c+a; p[2]=p[1];   p[3]=p[0];
 q[0]=v;   q[1]=v-a;   q[2]=q[1]+b; q[3]=v+b;
 g.setColor(coverColor); g.fillPolygon(p,q,4);
 g.setColor(Color.black);  g.drawPolygon(p,q,4);
 }

// Draw horisontal book
 void drawHorisontal(Graphics g)
{
 int i,j,k,l0,f,fh,iw,ih;
 String s0;
 int[] p = new int[4];
 int[] q = new int[4];

 if(box()==false)
   {
    p[0]=u; p[1]=u+b; p[2]=p[1]+a; p[3]=u+a;
    q[0]=q[1]=v+c; q[2]=q[3]=q[1]-a;
    }
 else
   {
    p[0]=p[1]=u+b; p[2]=p[3]=u+b+a;
    q[0]=v+c; q[1]=v; q[2]=v-a; q[3]=v+c-a;
    }
 g.setColor(coverColor);        g.fillPolygon(p,q,4);
 g.setColor(Color.black);  g.drawPolygon(p,q,4);

 if(box()==false)
   {
    i=v; while(i < v+c-2)
      {
       g.setColor(Color.white);
       g.drawLine(u+b,i,u+b+a-3,i-a+3); i++;
       g.setColor(Color.black);
       g.drawLine(u+b,i,u+b+a-3,i-a+3); i++;
       }
    }
 p[0]=u; p[1]=u+b; p[2]=p[1]+a; p[3]=u+a; q[0]=q[1]=v; q[2]=q[3]=v-a;

 g.setColor(coverColor); g.fillPolygon(p,q,4);

 g.setColor(coverColor); g.fillRect(u,v,b,c);
 k=0;
 if(icon != null)
   {
    iw=icon.getWidth(this); ih=icon.getHeight(this);
    k=(int)((float)iw/ih*c);
    g.setColor(Color.black);
    g.drawImage(icon,upward()?u+b-k:u,v,k,c,this);
    g.setColor(coverColor); g.drawPolygon(p,q,4);
    g.setColor(Color.black); g.drawRect(u,v,b,c);
    }
 g.setColor(Color.black); g.drawRect(u,v,b,c); g.drawPolygon(p,q,4);
 if(!chinese())
   {
    g.setColor(titleColor);
    drawTitle(g,upward()?u:u+k,v,k);
    }
 }

 public boolean imageUpdate(Image img,int s,int x,int y,int w,int h)
{
 if( (s & ALLBITS) == ALLBITS)
   {
    synchronized(this){ notify(); }
    return false;
    }
 else if( (s & (ABORT|ERROR))!=0)
   {
    synchronized(this){ notify();}
    return false;
    }
 else return true;
 }

/* * */ } /* * */


