Back to index

wims  3.65+svn20090927
TemplateSelector.java
Go to the documentation of this file.
00001 /*
00002     WIMSchem Elements: Chemistry molecular diagram drawing tool.
00003     
00004     (c) 2005 Dr. Alex M. Clark
00005     
00006     Released as GNUware, under the Gnu Public License (GPL)
00007     
00008     See www.gnu.org for details.
00009 */
00010 
00011 package WIMSchem;
00012 
00013 import java.awt.*;
00014 import java.awt.event.*;
00015 import javax.swing.*;
00016 import javax.swing.plaf.basic.*;
00017 
00018 /*
00019     A popup window which displays available templates within a grid of molecule widgets, adding page navigation and reporting of the
00020     selection of individual templates.
00021 */
00022 
00023 public class TemplateSelector extends JFrame implements WindowFocusListener, MolSelectListener, ActionListener
00024 {
00025     Templates templ;
00026     TemplSelectListener selectListen;
00027     
00028     static final int MOL_COL=5,MOL_ROW=4,MOL_WIDTH=100,MOL_HEIGHT=75;
00029     static final int FRAME_SIZE=1;
00030     static final int ARROW_WIDTH=30,ARROW_HEIGHT=15;
00031     static final int WIDTH=MOL_COL*MOL_WIDTH+2*FRAME_SIZE;
00032     static final int HEIGHT=MOL_ROW*MOL_HEIGHT+2*FRAME_SIZE+ARROW_HEIGHT;
00033     static final int NUM_WIDGETS=MOL_COL*MOL_ROW;
00034     
00035     EditorPane[] pics=new EditorPane[NUM_WIDGETS];
00036     BasicArrowButton prev,next;
00037     int curPage=0,numPages;
00038 
00039     public TemplateSelector(Templates templ,TemplSelectListener listen,int HeightFudge)
00040     {
00041        this.templ=templ;
00042        selectListen=listen;
00043        
00044        setUndecorated(true);
00045        getRootPane().setWindowDecorationStyle(JRootPane.NONE);
00046        setSize(WIDTH,HEIGHT+HeightFudge);
00047        
00048        TemplateBorder content=new TemplateBorder();
00049        setContentPane(content);
00050 
00051        Color bckgr=getBackground();
00052        Color shade1=new Color(Math.max(bckgr.getRed()-8,0),Math.max(bckgr.getGreen()-8,0),bckgr.getBlue());
00053        Color shade2=new Color(Math.max(bckgr.getRed()-16,0),Math.max(bckgr.getGreen()-16,0),bckgr.getBlue());
00054        content.setBackground(shade1);
00055        
00056        for (int n=0;n<NUM_WIDGETS;n++) if (n<templ.NumTemplates())
00057        {
00058            pics[n]=new EditorPane(MOL_WIDTH,MOL_HEIGHT);
00059            pics[n].SetEditable(false);
00060            pics[n].setBackground(shade1);
00061            pics[n].Replace(templ.GetTemplate(n));
00062            pics[n].ScaleToFit();
00063            content.add(pics[n]);
00064            pics[n].setLocation(FRAME_SIZE+MOL_WIDTH*(n%MOL_COL),FRAME_SIZE+MOL_HEIGHT*(n/MOL_COL));
00065            pics[n].SetToolCursor();
00066            pics[n].SetMolSelectListener(this);
00067        }
00068        numPages=(int)Math.ceil(templ.NumTemplates()/(double)NUM_WIDGETS);
00069        
00070        prev=new BasicArrowButton(SwingConstants.WEST);
00071        next=new BasicArrowButton(SwingConstants.EAST);
00072        content.add(prev);
00073        content.add(next);
00074        prev.setLocation(WIDTH-FRAME_SIZE-2*ARROW_WIDTH,HEIGHT-FRAME_SIZE-ARROW_HEIGHT);
00075        prev.setSize(ARROW_WIDTH,ARROW_HEIGHT);
00076        next.setLocation(WIDTH-FRAME_SIZE-ARROW_WIDTH,HEIGHT-FRAME_SIZE-ARROW_HEIGHT);
00077        next.setSize(ARROW_WIDTH,ARROW_HEIGHT);
00078        prev.addActionListener(this);
00079        next.addActionListener(this);
00080        
00081        addWindowFocusListener(this);
00082     }
00083     
00084     // ------------------ event functions --------------------
00085     
00086     public void windowGainedFocus(WindowEvent e) {}
00087     public void windowLostFocus(WindowEvent e) 
00088     {
00089        dispose();
00090     }
00091     
00092     public void MolSelected(EditorPane source,int idx,boolean dblclick)
00093     {
00094        if (idx==0) return;
00095        selectListen.TemplSelected(source.MolData().Clone(),idx);
00096        dispose();
00097     }
00098     public void DirtyChanged(boolean isdirty) {}
00099     
00100     public void actionPerformed(ActionEvent e)
00101     {
00102        int newPage=curPage;
00103        if (e.getSource()==prev) newPage=curPage>0 ? curPage-1 : numPages-1;
00104        if (e.getSource()==next) newPage=curPage<numPages-1 ? curPage+1 : 0;
00105        if (newPage!=curPage)
00106        {
00107            curPage=newPage;
00108            
00109            for (int n=0;n<NUM_WIDGETS;n++)
00110            {
00111               int i=curPage*NUM_WIDGETS+n;
00112               pics[n].Replace(i<templ.NumTemplates() ? templ.GetTemplate(i) : new Molecule());
00113               pics[n].ScaleToFit();
00114            }
00115        }
00116     }
00117 }
00118 
00119 class TemplateBorder extends JComponent
00120 {
00121     public TemplateBorder() 
00122     {
00123        setOpaque(true);
00124     }
00125 
00126     protected void paintComponent(Graphics gr) 
00127     {
00128        Graphics2D g=(Graphics2D)gr;
00129 
00130        g.setColor(getBackground());
00131        g.fillRect(0,0,getWidth(),getHeight());
00132 
00133        g.setColor(Color.BLACK);
00134        g.drawRect(0,0,getWidth()-1,getHeight()-1);
00135     }
00136 
00137 }