Subversion Repositories wimsdev

Rev

Rev 3662 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.     Sketch Elements: Chemistry molecular diagram drawing tool.
  3.    
  4.     (c) 2005 Dr. Alex M. Clark
  5.    
  6.     Released as GNUware, under the Gnu Public License (GPL)
  7.    
  8.     See www.gnu.org for details.
  9. */
  10.  
  11. package WIMSchem;
  12.  
  13. import java.awt.*;
  14. import java.awt.event.*;
  15. import javax.swing.*;
  16. import javax.swing.plaf.basic.*;
  17.  
  18. /*
  19.     A popup window which displays available templates within a grid of molecule widgets, adding page navigation and reporting of the
  20.     selection of individual templates.
  21. */
  22.  
  23. public class TemplateSelector extends JFrame implements WindowFocusListener, MolSelectListener, ActionListener
  24. {
  25.     Templates templ;
  26.     TemplSelectListener selectListen;
  27.    
  28.     static final int MOL_COL=5,MOL_ROW=4,MOL_WIDTH=100,MOL_HEIGHT=75;
  29.     static final int FRAME_SIZE=1;
  30.     static final int ARROW_WIDTH=30,ARROW_HEIGHT=15;
  31.     static final int WIDTH=MOL_COL*MOL_WIDTH+2*FRAME_SIZE;
  32.     static final int HEIGHT=MOL_ROW*MOL_HEIGHT+2*FRAME_SIZE+ARROW_HEIGHT;
  33.     static final int NUM_WIDGETS=MOL_COL*MOL_ROW;
  34.    
  35.     EditorPane[] pics=new EditorPane[NUM_WIDGETS];
  36.     BasicArrowButton prev,next;
  37.     int curPage=0,numPages;
  38.  
  39.     public TemplateSelector(Templates templ,TemplSelectListener listen,int HeightFudge)
  40.     {
  41.         this.templ=templ;
  42.         selectListen=listen;
  43.        
  44.         setUndecorated(true);
  45.         getRootPane().setWindowDecorationStyle(JRootPane.NONE);
  46.         setSize(WIDTH,HEIGHT+HeightFudge);
  47.        
  48.         TemplateBorder content=new TemplateBorder();
  49.         setContentPane(content);
  50.  
  51.         Color bckgr=getBackground();
  52.         Color shade1=new Color(Math.max(bckgr.getRed()-8,0),Math.max(bckgr.getGreen()-8,0),bckgr.getBlue());
  53.         Color shade2=new Color(Math.max(bckgr.getRed()-16,0),Math.max(bckgr.getGreen()-16,0),bckgr.getBlue());
  54.         content.setBackground(shade1);
  55.        
  56.         for (int n=0;n<NUM_WIDGETS;n++) if (n<templ.numTemplates())
  57.         {
  58.             pics[n]=new EditorPane(MOL_WIDTH,MOL_HEIGHT);
  59.             pics[n].setEditable(false);
  60.             pics[n].setBackground(shade1);
  61.             pics[n].replace(templ.getTemplate(n));
  62.             pics[n].scaleToFit();
  63.             content.add(pics[n]);
  64.             pics[n].setLocation(FRAME_SIZE+MOL_WIDTH*(n%MOL_COL),FRAME_SIZE+MOL_HEIGHT*(n/MOL_COL));
  65.             pics[n].setToolCursor();
  66.             pics[n].setMolSelectListener(this);
  67.         }
  68.         numPages=(int)Math.ceil(templ.numTemplates()/(double)NUM_WIDGETS);
  69.        
  70.         prev=new BasicArrowButton(SwingConstants.WEST);
  71.         next=new BasicArrowButton(SwingConstants.EAST);
  72.         content.add(prev);
  73.         content.add(next);
  74.         prev.setLocation(WIDTH-FRAME_SIZE-2*ARROW_WIDTH,HEIGHT-FRAME_SIZE-ARROW_HEIGHT);
  75.         prev.setSize(ARROW_WIDTH,ARROW_HEIGHT);
  76.         next.setLocation(WIDTH-FRAME_SIZE-ARROW_WIDTH,HEIGHT-FRAME_SIZE-ARROW_HEIGHT);
  77.         next.setSize(ARROW_WIDTH,ARROW_HEIGHT);
  78.         prev.addActionListener(this);
  79.         next.addActionListener(this);
  80.        
  81.         addWindowFocusListener(this);
  82.     }
  83.    
  84.     // ------------------ event functions --------------------
  85.    
  86.     public void windowGainedFocus(WindowEvent e) {}
  87.     public void windowLostFocus(WindowEvent e)
  88.     {
  89.         dispose();
  90.     }
  91.    
  92.     public void molSelected(EditorPane source,int idx,boolean dblclick)
  93.     {
  94.         if (idx==0) return;
  95.         selectListen.templSelected(source.molData().clone(),idx);
  96.         dispose();
  97.     }
  98.     public void dirtyChanged(boolean isdirty) {}
  99.    
  100.     public void actionPerformed(ActionEvent e)
  101.     {
  102.         int newPage=curPage;
  103.         if (e.getSource()==prev) newPage=curPage>0 ? curPage-1 : numPages-1;
  104.         if (e.getSource()==next) newPage=curPage<numPages-1 ? curPage+1 : 0;
  105.         if (newPage!=curPage)
  106.         {
  107.             curPage=newPage;
  108.            
  109.             for (int n=0;n<NUM_WIDGETS;n++)
  110.             {
  111.                 int i=curPage*NUM_WIDGETS+n;
  112.                 pics[n].replace(i<templ.numTemplates() ? templ.getTemplate(i) : new Molecule());
  113.                 pics[n].scaleToFit();
  114.             }
  115.         }
  116.     }
  117. }
  118.  
  119. class TemplateBorder extends JComponent
  120. {
  121.     public TemplateBorder()
  122.     {
  123.         setOpaque(true);
  124.     }
  125.  
  126.     protected void paintComponent(Graphics gr)
  127.     {
  128.         Graphics2D g=(Graphics2D)gr;
  129.  
  130.         g.setColor(getBackground());
  131.         g.fillRect(0,0,getWidth(),getHeight());
  132.  
  133.         g.setColor(Color.BLACK);
  134.         g.drawRect(0,0,getWidth()-1,getHeight()-1);
  135.     }
  136.  
  137. }
  138.