1 /*
2  * Copyright (C) 2008  Genome Research Limited
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  *  @author: Tim Carver
19  */
20 
21 
22 package uk.ac.sanger.artemis.circular;
23 
24 import javax.swing.Box;
25 import javax.swing.ButtonGroup;
26 import javax.swing.JFrame;
27 import javax.swing.JLabel;
28 import javax.swing.JMenu;
29 import javax.swing.JMenuBar;
30 import javax.swing.JMenuItem;
31 import javax.swing.JPanel;
32 import javax.swing.JRadioButton;
33 import javax.swing.JSlider;
34 import javax.swing.KeyStroke;
35 import javax.swing.event.*;
36 import java.awt.event.*;
37 import java.awt.Dimension;
38 import java.util.Hashtable;
39 
40 public class LineAttribute extends JPanel
41 {
42   private static final long serialVersionUID = 1L;
43   private Hashtable lineAttr = new Hashtable();
44   private TextFieldInt start;
45   private TextFieldInt end;
46   private TextFieldInt lineSize;
47 
LineAttribute(final DNADraw draw)48   public LineAttribute(final DNADraw draw)
49   {
50     super();
51 
52     Dimension d = new Dimension(100,25);
53     Box bdown = Box.createVerticalBox();
54     bdown.add(Box.createVerticalStrut(4));
55     Box bacross = Box.createHorizontalBox();
56 
57     // circular or linear dna
58     ButtonGroup group = new ButtonGroup();
59     final JRadioButton jcirc = new JRadioButton("Circular");
60     jcirc.addActionListener(new ActionListener()
61     {
62       public void actionPerformed(ActionEvent e)
63       {
64         lineAttr.put("circular",new Boolean(jcirc.isSelected()));
65         if(draw != null)
66           draw.repaint();
67       }
68     });
69     group.add(jcirc);
70     JRadioButton jline = new JRadioButton("Linear");
71     jline.addActionListener(new ActionListener()
72     {
73       public void actionPerformed(ActionEvent e)
74       {
75         lineAttr.put("circular",new Boolean(jcirc.isSelected()));
76         if(draw != null)
77           draw.repaint();
78       }
79     });
80     group.add(jline);
81     jcirc.setSelected( draw.isCircular() );
82     jline.setSelected( !draw.isCircular() );
83     lineAttr.put("circular",new Boolean(draw.isCircular()));
84     bacross.add(jcirc);
85     bacross.add(jline);
86     bacross.add(Box.createHorizontalGlue());
87     bdown.add(bacross);
88 
89     // start position
90     bdown.add(Box.createVerticalStrut(4));
91     bacross = Box.createHorizontalBox();
92     start = new TextFieldInt();
93     start.addActionListener(new ActionListener()
94     {
95       public void actionPerformed(ActionEvent e)
96       {
97         int s = getStart();
98         lineAttr.put("start",new Integer(s));
99         draw.setStart(s);
100         if(draw != null)
101           draw.repaint();
102       }
103     });
104 
105     if(draw != null)
106       start.setValue(draw.getStart());
107     else
108       start.setValue(1);
109 
110     start.setPreferredSize(d);
111     start.setMaximumSize(d);
112     bacross.add(start);
113     bacross.add(new JLabel(" start"));
114     bacross.add(Box.createHorizontalGlue());
115     bdown.add(bacross);
116 
117     // end position
118     bacross = Box.createHorizontalBox();
119     end = new TextFieldInt();
120     end.addActionListener(new ActionListener()
121     {
122       public void actionPerformed(ActionEvent ev)
123       {
124         int e = getEnd();
125         lineAttr.put("end",new Integer(e));
126         draw.setEnd(e);
127         if(draw != null)
128           draw.repaint();
129       }
130     });
131     end.setPreferredSize(d);
132     end.setMaximumSize(d);
133 
134     if(draw != null)
135       end.setValue(draw.getEnd());
136     else
137       end.setValue(1000);
138 
139     bacross.add(end);
140     bacross.add(new JLabel(" stop"));
141     bacross.add(Box.createHorizontalGlue());
142     bdown.add(bacross);
143 
144     bacross = Box.createHorizontalBox();
145     lineSize = new TextFieldInt();
146 
147     // line size
148     int lsize = 5;
149     if(draw != null)
150       lsize = draw.getLineSize();
151     lineAttr.put("lsize",new Integer(lsize));
152 
153     final JSlider slider = new JSlider(1,20,lsize);
154     lineSize.setPreferredSize(d);
155     lineSize.setMaximumSize(d);
156     lineSize.setValue(lsize);
157     bacross.add(lineSize);
158     bacross.add(new JLabel(" line width"));
159     bacross.add(Box.createHorizontalGlue());
160     bdown.add(bacross);
161     // change line size on carriage return
162     lineSize.addActionListener(new ActionListener()
163     {
164       public void actionPerformed(ActionEvent e)
165       {
166         int size = lineSize.getValue();
167         slider.setValue(size);
168         lineAttr.put("lsize",new Integer(size));
169         if(draw != null)
170         {
171           draw.setLineSize(size);
172           draw.repaint();
173         }
174       }
175     });
176 
177     bacross = Box.createHorizontalBox();
178     slider.addChangeListener(new ChangeListener()
179     {
180       public void stateChanged(ChangeEvent e)
181       {
182         int size = slider.getValue();
183         lineSize.setValue(size);
184         lineAttr.put("lsize",new Integer(size));
185         if(draw != null)
186         {
187           draw.setLineSize(size);
188           draw.repaint();
189         }
190       }
191     });
192 
193     bacross.add(slider);
194     bacross.add(Box.createHorizontalGlue());
195     bdown.add(bacross);
196 
197     add(bdown);
198 
199   }
200 
createMenuBar(final JFrame f)201   protected JMenuBar createMenuBar(final JFrame f)
202   {
203     JMenuBar menuBar = new JMenuBar();
204 
205     JMenu fileMenu = new JMenu("File");
206     fileMenu.setMnemonic(KeyEvent.VK_F);
207     menuBar.add(fileMenu);
208 
209     JMenuItem closeMenu = new JMenuItem("Close");
210     closeMenu.setAccelerator(KeyStroke.getKeyStroke(
211               KeyEvent.VK_E, ActionEvent.CTRL_MASK));
212 
213     closeMenu.addActionListener(new ActionListener()
214     {
215       public void actionPerformed(ActionEvent e)
216       {
217         f.dispose();
218       }
219     });
220     fileMenu.add(closeMenu);
221     return menuBar;
222   }
223 
getLineAttr()224   protected Hashtable getLineAttr()
225   {
226     lineAttr.put("start",new Integer(start.getValue()));
227     lineAttr.put("end",new Integer(end.getValue()));
228     lineAttr.put("lsize",new Integer(lineSize.getValue()));
229 
230     return lineAttr;
231   }
232 
getStart()233   protected int getStart()
234   {
235     return start.getValue();
236   }
237 
238 
getEnd()239   protected int getEnd()
240   {
241     return end.getValue();
242   }
243 
244 }
245 
246