import java.awt.*;
import java.awt.event.*;

class PString extends Dialog
    implements PObject, ActionListener, WindowListener
{
   final int maxLen=400;
   final int minLen=100;
   Font myFont=new Font("dialog", Font.PLAIN, 12);
   int myHeight;
   
   static public void main(String inArgs[]) {
      String exampleString="The Table Archive software will use the Sybase maintenance bihourly cronjob to run it.  It uses a new table ddb_archv_archive in the admin database to configure its behavior.  It supports 3 archive types:  File, Table, and Delete.  The Table type puts the archived data in a table in the same Sybase server.  The Delete type removes the older data from the specified table.  The File type puts the archived data in a gzipped, native BCP file.";
//      exampleString+=exampleString+exampleString;
//      exampleString+=exampleString+exampleString;
      PString example=new PString(exampleString);
      example.show();
   }
   
   public PString(String s) {
      super(new Frame(),true);
      myString=s;

      Button b=new Button("Close");
      add(b,"South");
      b.addActionListener(this);

      GridBagLayout layout=new GridBagLayout();
      GridBagConstraints constraints=new GridBagConstraints();
      constraints.anchor=constraints.NORTHWEST;
      constraints.fill=constraints.NONE;
      constraints.gridheight=1;
      constraints.gridwidth=1;
      constraints.weightx=1;
      constraints.weighty=1;
      constraints.ipadx=1;
      constraints.ipady=1;
      constraints.insets=new Insets(0, 1, 0, 1);
    
      Panel p=new Panel();
//    XYLayoutManager xy=new XYLayoutManager();
      p.setLayout(layout);
//    p.setLayout(xy);
      add(p,"Center");

      Label label;
      FontMetrics fm=fm=getFontMetrics(myFont);
      
      if(fm==null) {
         setSize(minLen, 100);
         return;
      }
      
      int bigLen=minLen;
      int y=1;
      String sSrc=myString;
      int tLen=0;
      int yPos=0;
      int lastPos, len, pos;
      myHeight=fm.getHeight()+fm.getLeading();

      while((tLen=fm.stringWidth(sSrc))>maxLen) {
         len=0;
         pos=0;

         // get largest string that fits
         do {
            lastPos=pos;
            pos=sSrc.indexOf(' ',pos+1);
            
            if(pos>-1) {
               len=fm.stringWidth(sSrc.substring(0,pos));
            }
            else {
               break;
            }
         } while(len<maxLen);

         if(lastPos>0) {
            s=sSrc.substring(0,lastPos);
            len=fm.stringWidth(s);
            sSrc=sSrc.substring(lastPos+1);

            //xy.setNext(5,yPos,0,0);
            constraints.gridx=1;
            constraints.gridy=y;
            label=getLabel(s);
            p.add(label, constraints);
            yPos+=myHeight;
            y++;

            if(bigLen<len)
               bigLen=len;
         }
      }

      label=getLabel(sSrc);
      constraints.gridx=1;
      constraints.gridy=y;
      p.add(label, constraints);

      if(bigLen<tLen)
         bigLen=tLen;

      Insets insets=getInsets();
//      setSize(bigLen+insets.left+insets.right+30,
//         insets.top+insets.bottom+yPos+b.getSize().height+myHeight*5);
pack();
   }

   private Label getLabel(String inString) {
      Label label=new Label(inString) {
         public Dimension getPreferredSize() {
            Dimension size=super.getPreferredSize();
            size.height=myHeight;
            return size;
         } 
      };
      
      label.setFont(myFont);
      return label;
   }
   
   public void show(Point mousePoint) {
      Dimension d=getSize();
      int x=mousePoint.x-d.width/2;
      int y=mousePoint.y-d.height/2;

      if(x<0)
         x=0;
      
      if(y<0)
         y=0;

      setLocation(x,y);
      super.show();
   } 

   public String toString() {
      return myString;
   }

   public void windowActivated(WindowEvent e) {
   }
   public void windowClosed(WindowEvent e) {
   }
   public void windowClosing(WindowEvent e) {
      hide();
   }
   public void windowDeactivated(WindowEvent e) {
   }
   public void windowDeiconified(WindowEvent e) {
   }
   public void windowIconified(WindowEvent e) {
   }
   public void windowOpened(WindowEvent e) {
   }

   public void actionPerformed(ActionEvent e) {
      String s=e.getActionCommand();
      if(s.equals("Close")) {
         windowClosing(null);
      }
      else
System.out.println(s+":"+e);
   }

   String myString;
}