Back to index

wims  3.65+svn20090927
ExtendedViewer.java
Go to the documentation of this file.
00001 package rene.viewer;
00002 
00003 import java.awt.*;
00004 import java.awt.event.*;
00005 import java.io.PrintWriter;
00006 import java.util.Enumeration;
00007 
00008 import rene.gui.*;
00009 import rene.util.*;
00010 
00017 public class ExtendedViewer extends Viewer
00018        implements AdjustmentListener, MouseListener, MouseMotionListener,
00019               ActionListener, KeyListener, WheelListener
00020 {      TextDisplay TD;
00021        Scrollbar Vertical;
00022        TextPosition Start,End;
00023        PopupMenu PM;
00024        int X,Y;
00025        Panel P3D;
00026        MyVector V; // Vector of lines
00027        StringBuffer B; // Buffer for last line
00028        boolean Changed=false;
00029        
00030        public ExtendedViewer ()
00031        {      TD=new TextDisplay(this);
00032               setLayout(new BorderLayout());
00033               add("Center",P3D=new Panel3D(TD));
00034               add("East",Vertical=new Scrollbar(Scrollbar.VERTICAL,0,100,0,1100));
00035               Vertical.addAdjustmentListener(this);
00036               TD.addMouseListener(this);
00037               TD.addMouseMotionListener(this);
00038               Start=End=null;
00039               PM=new PopupMenu();
00040               MenuItem mi=new MenuItem(Global.name("block.copy","Copy"));
00041               mi.addActionListener(this);
00042               PM.add(mi);
00043               PM.addSeparator();
00044               mi=new MenuItem(Global.name("block.begin","Begin Block"));
00045               mi.addActionListener(this);
00046               PM.add(mi);
00047               mi=new MenuItem(Global.name("block.end","End Block"));
00048               mi.addActionListener(this);
00049               PM.add(mi);
00050               add(PM);
00051               Wheel W=new Wheel(this);
00052               addMouseWheelListener(W);
00053               V=new MyVector();
00054               B=new StringBuffer();
00055        }
00056 
00057        public void setFont (Font f)
00058        {      TD.init(f);
00059        }
00060 
00061        public void appendLine (String s)
00062        {      B.append(s);
00063               V.addElement(B.toString());
00064               B.setLength(0);
00065               Changed=true;
00066        }
00067        
00068        public void newLine ()
00069        {      V.addElement(B.toString());
00070               B.setLength(0);
00071               Changed=true;
00072        }      
00073 
00074        public void appendLine (String s, Color c) 
00075        {      appendLine(s);
00076        }
00077 
00078        public void append (String s)
00079        {      B.append(s);
00080        }
00081 
00082        public void append (String s, Color c) 
00083        {      append(s);
00084        }
00085 
00086        public void doUpdate (boolean showlast)
00087        {
00088        }
00089        
00090        public void update ()
00091        {      resized();
00092               showFirst();
00093        }
00094 
00095        public void adjustmentValueChanged (AdjustmentEvent e)
00096        {      if (e.getSource()==Vertical)
00097               {      switch (e.getAdjustmentType())
00098                      {      case AdjustmentEvent.UNIT_INCREMENT :
00099                                    TD.verticalUp(); break;
00100                             case AdjustmentEvent.UNIT_DECREMENT :
00101                                    TD.verticalDown(); break;
00102                             case AdjustmentEvent.BLOCK_INCREMENT :
00103                                    TD.verticalPageUp(); break;
00104                             case AdjustmentEvent.BLOCK_DECREMENT :
00105                                    TD.verticalPageDown(); break;
00106                             default :
00107                                    int v=Vertical.getValue();
00108                                    Vertical.setValue(v);
00109                                    TD.setVertical(v);
00110                                    return;
00111                      }
00112                      setVerticalScrollbar();
00113               }
00114        }
00115 
00116        public void setVerticalScrollbar ()
00117        {      if (Vertical==null) return;
00118               int h=TD.computeVerticalSize();
00119               Vertical.setValues(TD.computeVertical(),h,0,1000+h);
00120        }
00121 
00122        public void setText (String S)
00123        {      TD.unmark(); Start=End=null;
00124               TD.setText(S);
00125               V.removeAllElements();
00126               B.setLength(0);             
00127               setVerticalScrollbar();
00128        }
00129 
00130        public void save (PrintWriter fo)
00131        {      TD.save(fo);
00132        }
00133 
00134        public void appendLine0 (String s)
00135        {      appendLine(s);
00136        }
00137 
00138        public void appendLine0 (String s, Color c)
00139        {      appendLine(s);
00140        }
00141 
00142        boolean Dragging=false;
00143 
00144        public void mouseClicked (MouseEvent e) {}
00145        public void mousePressed (MouseEvent e)
00146        {      if (e.isPopupTrigger() || e.isMetaDown())
00147               {      PM.show(e.getComponent(),e.getX(),e.getY());
00148                      X=e.getX(); Y=e.getY();
00149               }
00150               else
00151               {      TD.unmark(Start,End);
00152                      Start=TD.getposition(e.getX(),e.getY());
00153                      Start.oneleft();
00154                      End=null;
00155               }
00156        }
00157        public Dimension getPreferredSize ()
00158        {      return new Dimension(150,200);
00159        }
00160        public Dimension getMinimumSize ()
00161        {      return new Dimension(150,200);
00162        }
00163 
00164        public void mouseReleased (MouseEvent e)
00165        {      Dragging=false;
00166        }
00167 
00168        public void mouseEntered (MouseEvent e)
00169        {
00170        }
00171 
00172        public void mouseExited (MouseEvent e)
00173        {
00174        }
00175 
00176        public void mouseMoved (MouseEvent e) {}
00177        public void mouseDragged (MouseEvent e)
00178        {      TD.unmark(Start,End);
00179               TextPosition h=TD.getposition(e.getX(),e.getY());
00180               if (h!=null) End=h;
00181               TD.mark(Start,End);
00182        }
00183 
00184        public void actionPerformed (ActionEvent e)
00185        {      String o=e.getActionCommand();
00186               if (o.equals(Global.name("block.copy","Copy"))) TD.copy(Start,End);
00187               else if (o.equals(Global.name("block.begin","Begin Block")))
00188               {      TD.unmark(Start,End);
00189                      Start=TD.getposition(X,Y);
00190                      Start.oneleft();
00191                      if (End==null && TD.L.last()!=null)
00192                      {      End=TD.lastpos();
00193                      }
00194                      TD.mark(Start,End);
00195               }
00196               else if (o.equals(Global.name("block.end","End Block")))
00197               {      TD.unmark(Start,End);
00198                      End=TD.getposition(X,Y);
00199                      if (Start==null && TD.L.first()!=null)
00200                      {      Start=new TextPosition(TD.L.first(),0,0);
00201                      }
00202                      TD.mark(Start,End);
00203               }
00204        }
00205 
00206        public void keyPressed (KeyEvent e) {}
00207 
00208        public void keyReleased (KeyEvent e)
00209        {      if (e.isControlDown() && e.getKeyCode()==KeyEvent.VK_C
00210                      && Start!=null && End!=null)
00211               {      TD.copy(Start,End);
00212               }
00213        }
00214 
00215        public void keyTyped (KeyEvent e) {}
00216        
00217        public void setTabWidth (int t)
00218        {      TD.setTabWidth(t);
00219        }
00220        
00221        public void showFirst ()
00222        {      TD.showFirst();
00223               setVerticalScrollbar();
00224               TD.repaint();
00225        }
00226 
00227        public void showLast ()
00228        {      TD.showlast();
00229               setVerticalScrollbar();
00230               TD.repaint();
00231        }
00232 
00233        public boolean hasFocus () { return false; }
00234 
00235        public void setBackground (Color c)
00236        {      TD.setBackground(c);
00237               P3D.setBackground(c);
00238               super.setBackground(c);
00239        }      
00240 
00241        public void up (int n)
00242        {      for (int i=0; i<n; i++) TD.verticalUp();
00243               setVerticalScrollbar();
00244        }
00245 
00246        public void down (int n)
00247        {      for (int i=0; i<n; i++) TD.verticalDown();
00248               setVerticalScrollbar();
00249        }
00250 
00251        public void pageUp ()
00252        {      TD.verticalPageUp();
00253               setVerticalScrollbar();
00254        }
00255 
00256        public void pageDown ()
00257        {      TD.verticalPageDown();
00258               setVerticalScrollbar();
00259        }
00260 
00261        public void paint (Graphics G)
00262        {      super.paint(G);
00263        }
00264        
00265        public void doAppend (String s)
00266        {      char a[]=s.toCharArray();
00267               int w[]=TD.getwidth(a);
00268               int start=0,end=0;
00269               int W=TD.getSize().width;
00270               int goodbreak;
00271               while (start<a.length && a[start]==' ') start++;
00272               if (start>=a.length)
00273               {      TD.appendLine("");
00274                      return;
00275               }
00276               int blanks=0;
00277               String sblanks="";
00278               int offset=0;
00279               if (start>0)
00280               {      blanks=start;
00281                      sblanks=new String(a,0,blanks);
00282                      offset=blanks+w[0];
00283               }
00284               while (start<a.length)
00285               {      int tw=TD.Offset+offset;
00286                      end=start;
00287                      goodbreak=start;
00288                      while (end<a.length && tw<W)
00289                      {      tw+=w[end]; 
00290                             if (a[end]==' ')
00291                                    goodbreak=end;
00292                             end++;
00293                      }
00294                      if (tw<W) goodbreak=end;
00295                      if (goodbreak==start) goodbreak=end; 
00296                      if (blanks>0)
00297                             TD.appendLine(sblanks+new String(a,start,goodbreak-start)); 
00298                      else
00299                             TD.appendLine(new String(a,start,goodbreak-start)); 
00300                      start=goodbreak;
00301                      while (start<a.length && a[start]==' ') start++;
00302               }
00303        }
00304        
00305        public synchronized void resized ()
00306        {      if (TD.getSize().width<=0) return;
00307               TD.setText("");
00308               Enumeration e=V.elements();
00309               while (e.hasMoreElements())
00310               {      String s=(String)e.nextElement();
00311                      doAppend(s);
00312               }
00313               TD.repaint();
00314        }
00315 
00316        public static void main (String args[])
00317        {      CloseFrame f=new CloseFrame();
00318               f.setLayout(new BorderLayout());
00319               ExtendedViewer v=new ExtendedViewer();
00320               f.add("Center",v);
00321               f.setSize(300,300);
00322               f.setVisible(true);
00323               v.append("test1 test test test test test test ");
00324               v.append("Donaudampfschifffahrtsgesellschaftskapitän ");
00325               v.append("test2 test test test test test test ");
00326               v.append("test3 test test test test test test ");
00327               v.append("test4 test test test test test test ");
00328               v.append("test5 test test test test test test ");
00329               v.append("test6 test test test test test test ");
00330               v.append("test7 test test test test test test ");
00331               v.append("test8 test test test test test test ");
00332               v.appendLine("");
00333               v.appendLine("");
00334               v.appendLine("  affe affe affe affe affe affe affe test test test test test last");
00335               v.appendLine("");
00336               v.appendLine("test affe affe affe test test test test test last ");
00337               v.appendLine("  ");
00338               v.appendLine("test test test test affe affe affe test test last");
00339               v.repaint();
00340               v.resized();
00341        }
00342 
00343        public void mouseWheelMoved(MouseWheelEvent arg0) {
00344        }
00345        
00346 }