Back to index

wims  3.65+svn20090927
Viewer.java
Go to the documentation of this file.
00001 package rene.viewer;
00002 
00003 import java.awt.BorderLayout;
00004 import java.awt.Color;
00005 import java.awt.Dimension;
00006 import java.awt.Font;
00007 import java.awt.Frame;
00008 import java.awt.MenuItem;
00009 import java.awt.Panel;
00010 import java.awt.PopupMenu;
00011 import java.awt.Scrollbar;
00012 import java.awt.event.ActionEvent;
00013 import java.awt.event.ActionListener;
00014 import java.awt.event.AdjustmentEvent;
00015 import java.awt.event.AdjustmentListener;
00016 import java.awt.event.KeyEvent;
00017 import java.awt.event.KeyListener;
00018 import java.awt.event.MouseEvent;
00019 import java.awt.event.MouseListener;
00020 import java.awt.event.MouseMotionListener;
00021 import java.io.PrintWriter;
00022 
00023 import rene.gui.Global;
00024 import rene.gui.Panel3D;
00025 
00034 public class Viewer extends Panel 
00035        implements AdjustmentListener, MouseListener, MouseMotionListener,
00036               ActionListener, KeyListener, WheelListener
00037 {      TextDisplay TD;
00038        Scrollbar Vertical,Horizontal;
00039        TextPosition Start,End;
00040        PopupMenu PM;
00041        int X,Y;
00042        Panel P3D;
00043        
00044        public Viewer (boolean vs, boolean hs)
00045        {      TD=new TextDisplay(this);
00046               setLayout(new BorderLayout());
00047               add("Center",P3D=new Panel3D(TD));
00048               if (vs)
00049               {      add("East",Vertical=new Scrollbar(Scrollbar.VERTICAL,0,100,0,1100));
00050                      Vertical.addAdjustmentListener(this);
00051               }
00052               if (hs)
00053               {      add("South",Horizontal=new Scrollbar(Scrollbar.HORIZONTAL,0,100,0,1100));
00054                      Horizontal.addAdjustmentListener(this);
00055               }
00056               TD.addMouseListener(this);
00057               TD.addMouseMotionListener(this);
00058               Start=End=null;
00059               PM=new PopupMenu();
00060               MenuItem mi=new MenuItem(Global.name("block.copy","Copy"));
00061               mi.addActionListener(this);
00062               PM.add(mi);
00063               PM.addSeparator();
00064               mi=new MenuItem(Global.name("block.begin","Begin Block"));
00065               mi.addActionListener(this);
00066               PM.add(mi);
00067               mi=new MenuItem(Global.name("block.end","End Block"));
00068               mi.addActionListener(this);
00069               PM.add(mi);
00070               add(PM);
00071               Wheel W=new Wheel(this);
00072               addMouseWheelListener(W);
00073        }
00074        
00075        public Viewer ()
00076        {      this(true,true);
00077        }
00078        
00079        public Viewer (String dummy)
00080        {
00081        }
00082        
00083        public void setFont (Font f)
00084        {      TD.init(f);
00085        }
00086        
00087        public void appendLine (String s)
00088        {      TD.appendLine0(s);
00089        }
00090        
00091        public void appendLine (String s, Color c)
00092        {      TD.appendLine0(s,c);
00093        }
00094        
00095        public void append (String s)
00096        {      append(s,Color.black);
00097        }
00098        
00099        public void append (String s, Color c)
00100        {      TD.append(s,c);
00101        }
00102        
00103        public void doUpdate (boolean showlast)
00104        {      TD.doUpdate(showlast);
00105               setVerticalScrollbar();
00106        }
00107        
00108        public void adjustmentValueChanged (AdjustmentEvent e)
00109        {      if (e.getSource()==Vertical)
00110               {      switch (e.getAdjustmentType())
00111                      {      case AdjustmentEvent.UNIT_INCREMENT :
00112                                    TD.verticalUp(); break;
00113                             case AdjustmentEvent.UNIT_DECREMENT :
00114                                    TD.verticalDown(); break;
00115                             case AdjustmentEvent.BLOCK_INCREMENT :
00116                                    TD.verticalPageUp(); break;
00117                             case AdjustmentEvent.BLOCK_DECREMENT :
00118                                    TD.verticalPageDown(); break;
00119                             default :
00120                                    int v=Vertical.getValue();
00121                                    Vertical.setValue(v);
00122                                    TD.setVertical(v);
00123                                    return;
00124                      }
00125                      setVerticalScrollbar();
00126               }
00127               else if (e.getSource()==Horizontal)
00128               {      Horizontal.setValue(TD.setHorizontal(
00129                             Horizontal.getValue()));
00130               }
00131        }
00132        
00133        public void setVerticalScrollbar ()
00134        {      if (Vertical==null) return;
00135               int h=TD.computeVerticalSize();
00136               Vertical.setValues(TD.computeVertical(),h,0,1000+h);
00137        }
00138        
00139        public void setText (String S)
00140        {      TD.unmark(); Start=End=null;
00141               TD.setText(S);
00142               setVerticalScrollbar();
00143        }
00144        
00145        public void save (PrintWriter fo)
00146        {      TD.save(fo);
00147        }
00148        
00149        public void appendLine0 (String s)
00150        {      TD.appendLine0(s);
00151        }
00152        
00153        public void appendLine0 (String s, Color c)
00154        {      TD.appendLine0(s,c);
00155        }
00156        
00157        boolean Dragging=false;
00158        
00159        public void mouseClicked (MouseEvent e) {}
00160        
00161        public void mousePressed (MouseEvent e)
00162        {      if (e.isPopupTrigger() || e.isMetaDown())
00163               {      PM.show(e.getComponent(),e.getX(),e.getY());
00164                      X=e.getX(); Y=e.getY();
00165               }
00166               else
00167               {      TD.unmark(Start,End);
00168                      Start=TD.getposition(e.getX(),e.getY());
00169                      Start.oneleft();
00170                      End=null;
00171               }
00172        }
00173        
00174        public Dimension getPreferredSize ()
00175        {      return new Dimension(150,200);
00176        }
00177        
00178        public Dimension getMinimumSize ()
00179        {      return new Dimension(150,200);
00180        }
00181 
00182        public void mouseReleased (MouseEvent e)
00183        {      Dragging=false;
00184        }
00185        
00186        public void mouseEntered (MouseEvent e)
00187        {
00188        }
00189        
00190        public void mouseExited (MouseEvent e)
00191        {
00192        }
00193        
00194        public void mouseMoved (MouseEvent e) {}
00195        
00196        public void mouseDragged (MouseEvent e)
00197        {      TD.unmark(Start,End);
00198               TextPosition h=TD.getposition(e.getX(),e.getY());
00199               if (h!=null) End=h;
00200               TD.mark(Start,End);
00201        }
00202        
00203        public void actionPerformed (ActionEvent e)
00204        {      String o=e.getActionCommand();
00205               if (o.equals(Global.name("block.copy","Copy"))) TD.copy(Start,End);
00206               else if (o.equals(Global.name("block.begin","Begin Block")))
00207               {      TD.unmark(Start,End);
00208                      Start=TD.getposition(X,Y);
00209                      Start.oneleft();
00210                      if (End==null && TD.L.last()!=null)
00211                      {      End=TD.lastpos();
00212                      }
00213                      TD.mark(Start,End);
00214               }
00215               else if (o.equals(Global.name("block.end","End Block")))
00216               {      TD.unmark(Start,End);
00217                      End=TD.getposition(X,Y);
00218                      if (Start==null && TD.L.first()!=null)
00219                      {      Start=new TextPosition(TD.L.first(),0,0);
00220                      }
00221                      TD.mark(Start,End);
00222               }
00223        }
00224        
00225        public void keyPressed (KeyEvent e) {}
00226        
00227        public void keyReleased (KeyEvent e)
00228        {      if (e.isControlDown() && e.getKeyCode()==KeyEvent.VK_C
00229                      && Start!=null && End!=null)
00230               {      TD.copy(Start,End);
00231               }
00232        }
00233        
00234        public void keyTyped (KeyEvent e) {}
00235        
00236        public void setTabWidth (int t)
00237        {      TD.setTabWidth(t);
00238        }
00239        
00240        public void showFirst ()
00241        {      TD.showFirst();
00242               setVerticalScrollbar();
00243               TD.repaint();
00244        }
00245 
00246        public void showLast ()
00247        {      TD.showlast();
00248               setVerticalScrollbar();
00249               TD.repaint();
00250        }
00251 
00252        public boolean hasFocus () { return false; }
00253 
00254        public void setBackground (Color c)
00255        {      TD.setBackground(c);
00256               P3D.setBackground(c);
00257               super.setBackground(c);
00258        }      
00259 
00260        public void up (int n)
00261        {      for (int i=0; i<n; i++) TD.verticalUp();
00262               setVerticalScrollbar();
00263        }
00264 
00265        public void down (int n)
00266        {      for (int i=0; i<n; i++) TD.verticalDown();
00267               setVerticalScrollbar();
00268        }
00269 
00270        public void pageUp ()
00271        {      TD.verticalPageUp();
00272               setVerticalScrollbar();
00273        }
00274 
00275        public void pageDown ()
00276        {      TD.verticalPageDown();
00277               setVerticalScrollbar();
00278        }
00279        
00280        public void resized ()
00281        {      
00282        }
00283 
00284        public static void main (String args[])
00285        {      Frame f=new Frame();
00286               f.setLayout(new BorderLayout());
00287               Viewer v=new Viewer(true,false);
00288               f.add("Center",v);
00289               f.setSize(300,300);
00290               f.setVisible(true);
00291               v.append("test test test test test test test");
00292               v.appendLine("test test test test test test test");
00293               v.appendLine("test test test test test test test");
00294               v.appendLine("test test test test test test test");
00295        }
00296        
00297 }