Subversion Repositories wimsdev

Rev

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

Rev Author Line No. Line
3662 schaersvoo 1
/*
7246 schaersvoo 2
    Sketch Elements: Chemistry molecular diagram drawing tool.
3662 schaersvoo 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
 
7246 schaersvoo 56
        for (int n=0;n<NUM_WIDGETS;n++) if (n<templ.numTemplates())
3662 schaersvoo 57
        {
58
            pics[n]=new EditorPane(MOL_WIDTH,MOL_HEIGHT);
7246 schaersvoo 59
            pics[n].setEditable(false);
3662 schaersvoo 60
            pics[n].setBackground(shade1);
7246 schaersvoo 61
            pics[n].replace(templ.getTemplate(n));
62
            pics[n].scaleToFit();
3662 schaersvoo 63
            content.add(pics[n]);
64
            pics[n].setLocation(FRAME_SIZE+MOL_WIDTH*(n%MOL_COL),FRAME_SIZE+MOL_HEIGHT*(n/MOL_COL));
7246 schaersvoo 65
            pics[n].setToolCursor();
66
            pics[n].setMolSelectListener(this);
3662 schaersvoo 67
        }
7246 schaersvoo 68
        numPages=(int)Math.ceil(templ.numTemplates()/(double)NUM_WIDGETS);
3662 schaersvoo 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
 
7246 schaersvoo 92
    public void molSelected(EditorPane source,int idx,boolean dblclick)
3662 schaersvoo 93
    {
94
        if (idx==0) return;
7246 schaersvoo 95
        selectListen.templSelected(source.molData().clone(),idx);
3662 schaersvoo 96
        dispose();
97
    }
7246 schaersvoo 98
    public void dirtyChanged(boolean isdirty) {}
3662 schaersvoo 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;
7246 schaersvoo 112
                pics[n].replace(i<templ.numTemplates() ? templ.getTemplate(i) : new Molecule());
113
                pics[n].scaleToFit();
3662 schaersvoo 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
}