2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.appletgui;
\r
21 import jalview.datamodel.*;
\r
24 import java.awt.event.*;
\r
27 public class FeatureSettings extends Panel implements ItemListener,
\r
28 MouseListener, MouseMotionListener, ActionListener, AdjustmentListener
\r
35 Panel featurePanel = new Panel();
\r
36 ScrollPane scrollPane;
\r
37 boolean alignmentHasFeatures = false;
\r
39 Scrollbar transparency ;
\r
41 public FeatureSettings(AlignViewport av, final AlignmentPanel ap)
\r
45 fr = ap.seqPanel.seqCanvas.getFeatureRenderer();
\r
47 transparency = new Scrollbar(Scrollbar.HORIZONTAL,
\r
48 100 - (int)(fr.transparency*100), 1, 1, 100);
\r
50 if(fr.transparencySetter!=null)
\r
52 transparency.addAdjustmentListener(this);
\r
55 transparency.setEnabled(false);
\r
57 java.net.URL url = getClass().getResource("/images/link.gif");
\r
60 linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);
\r
64 if(av.featuresDisplayed==null)
\r
65 fr.findAllFeatures();
\r
69 this.setLayout(new BorderLayout());
\r
70 scrollPane = new ScrollPane();
\r
71 scrollPane.add(featurePanel);
\r
72 if (alignmentHasFeatures)
\r
73 add(scrollPane, BorderLayout.CENTER);
\r
75 Button invert = new Button("Invert Selection");
\r
76 invert.addActionListener(this);
\r
78 Panel lowerPanel = new Panel(new GridLayout(2,1,5,10));
\r
79 lowerPanel.add(invert);
\r
81 Panel tPanel = new Panel(new BorderLayout());
\r
83 if(fr.transparencySetter!=null)
\r
85 tPanel.add(transparency, BorderLayout.CENTER);
\r
86 tPanel.add(new Label("Transparency"), BorderLayout.EAST);
\r
89 tPanel.add(new Label("Transparency not available in this web browser"), BorderLayout.CENTER);
\r
91 lowerPanel.add(tPanel, BorderLayout.SOUTH);
\r
93 add(lowerPanel, BorderLayout.SOUTH);
\r
97 if(groupPanel!=null)
\r
99 groupPanel.setLayout(
\r
100 new GridLayout( fr.featureGroups.size() / 4 + 1, 4));
\r
101 groupPanel.validate();
\r
103 add(groupPanel, BorderLayout.NORTH);
\r
105 frame = new Frame();
\r
107 int height = featurePanel.getComponentCount()*50 +30;
\r
109 height = Math.max(100, height);
\r
110 height = Math.min(400, height);
\r
112 jalview.bin.JalviewLite.addFrame(frame, "Feature Settings", 280,
\r
116 public void paint(Graphics g)
\r
118 g.setColor(Color.black);
\r
119 g.drawString("No Features added to this alignment!!", 10, 20);
\r
122 void setTableData()
\r
124 alignmentHasFeatures = false;
\r
126 if(fr.featureGroups==null)
\r
127 fr.featureGroups = new Hashtable();
\r
129 Vector allFeatures = new Vector();
\r
130 Vector allGroups = new Vector();
\r
131 SequenceFeature[] tmpfeatures;
\r
135 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
137 if (av.alignment.getSequenceAt(i).getSequenceFeatures() == null)
\r
140 alignmentHasFeatures = true;
\r
142 tmpfeatures = av.alignment.getSequenceAt(i).getSequenceFeatures();
\r
144 while (index < tmpfeatures.length)
\r
146 if(tmpfeatures[index].getFeatureGroup()!=null)
\r
148 group = tmpfeatures[index].featureGroup;
\r
149 if(!allGroups.contains(group))
\r
151 allGroups.addElement(group);
\r
153 boolean visible = true;
\r
154 if (fr.featureGroups.containsKey(group))
\r
156 visible = ( (Boolean) fr.featureGroups.get(group)).booleanValue();
\r
159 fr.featureGroups.put(group, new Boolean(visible));
\r
161 if (groupPanel == null)
\r
163 groupPanel = new Panel();
\r
166 Checkbox check = new MyCheckbox(
\r
169 (fr.featureLinks!=null && fr.featureLinks.containsKey(group))
\r
173 check.addMouseListener(this);
\r
174 check.setFont(new Font("Serif", Font.BOLD, 12));
\r
175 check.addItemListener(this);
\r
176 groupPanel.add(check);
\r
180 if (!allFeatures.contains(tmpfeatures[index].getType()))
\r
182 allFeatures.addElement(tmpfeatures[index].getType());
\r
191 //This routine adds and removes checkboxes depending on
\r
192 //Group selection states
\r
193 void resetTable(boolean groupsChanged)
\r
195 SequenceFeature [] tmpfeatures;
\r
196 String group=null, type;
\r
197 Vector visibleChecks = new Vector();
\r
199 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
201 if (av.alignment.getSequenceAt(i).getSequenceFeatures() == null)
\r
204 tmpfeatures = av.alignment.getSequenceAt(i).getSequenceFeatures();
\r
206 while (index < tmpfeatures.length)
\r
208 group = tmpfeatures[index].featureGroup;
\r
210 if (group==null || fr.featureGroups.get(group)==null ||
\r
211 ((Boolean) fr.featureGroups.get(group)).booleanValue())
\r
213 type = tmpfeatures[index].getType();
\r
214 if(!visibleChecks.contains(type) )
\r
216 visibleChecks.addElement(type);
\r
224 int cSize = featurePanel.getComponentCount();
\r
226 //This will remove any checkboxes which shouldn't be
\r
228 for (int i = 0; i < cSize; i++)
\r
230 comps = featurePanel.getComponents();
\r
231 check = (Checkbox) comps[i];
\r
232 if (!visibleChecks.contains(check.getLabel()))
\r
234 featurePanel.remove(i);
\r
240 if(fr.renderOrder!=null)
\r
242 //First add the checks in the previous render order,
\r
243 //in case the window has been closed and reopened
\r
244 for(int ro=fr.renderOrder.length-1; ro>-1; ro--)
\r
246 String item = fr.renderOrder[ro];
\r
248 if(!visibleChecks.contains(item))
\r
251 visibleChecks.removeElement(item);
\r
253 addCheck(false, item);
\r
257 // now add checkboxes which should be visible,
\r
258 // if they have not already been added
\r
259 Enumeration en = visibleChecks.elements();
\r
261 while(en.hasMoreElements())
\r
263 addCheck(groupsChanged, en.nextElement().toString());
\r
267 featurePanel.setLayout(new GridLayout(featurePanel.getComponentCount(), 1, 10,5));
\r
268 featurePanel.validate();
\r
271 if(scrollPane!=null)
\r
272 scrollPane.validate();
\r
274 itemStateChanged(null);
\r
277 void addCheck(boolean groupsChanged, String type)
\r
280 Component [] comps = featurePanel.getComponents();
\r
283 for (int i = 0; i < featurePanel.getComponentCount(); i++)
\r
285 check = (Checkbox) comps[i];
\r
286 if (check.getLabel().equals(type))
\r
295 boolean selected = false;
\r
296 if (groupsChanged || av.featuresDisplayed.containsKey(type))
\r
301 check = new MyCheckbox(type,
\r
303 (fr.featureLinks!=null && fr.featureLinks.containsKey(type))
\r
306 check.addMouseListener(this);
\r
307 check.addMouseMotionListener(this);
\r
308 check.setBackground(fr.getColour(type));
\r
309 check.addItemListener(this);
\r
310 featurePanel.add(check);
\r
314 public void actionPerformed(ActionEvent evt)
\r
316 for(int i=0; i<featurePanel.getComponentCount(); i++)
\r
318 Checkbox check = (Checkbox)featurePanel.getComponent(i);
\r
319 check.setState(!check.getState());
\r
321 selectionChanged();
\r
324 public void itemStateChanged(ItemEvent evt)
\r
328 //Is the source a top level featureGroup?
\r
329 Checkbox source = (Checkbox) evt.getSource();
\r
330 if (fr.featureGroups.containsKey(source.getLabel()))
\r
332 fr.featureGroups.put(source.getLabel(), new Boolean(source.getState()));
\r
333 ap.seqPanel.seqCanvas.repaint();
\r
334 if (ap.overviewPanel != null)
\r
335 ap.overviewPanel.updateOverviewImage();
\r
341 selectionChanged();
\r
344 void selectionChanged()
\r
346 Component[] comps = featurePanel.getComponents();
\r
347 int cSize = comps.length;
\r
349 Object[][] tmp = new Object[cSize][3];
\r
351 for (int i = 0; i < cSize; i++)
\r
353 Checkbox check = (Checkbox) comps[i];
\r
354 tmp[tmpSize][0] = check.getLabel();
\r
355 tmp[tmpSize][1] = fr.getColour(check.getLabel());
\r
356 tmp[tmpSize][2] = new Boolean(check.getState());
\r
360 Object[][]data = new Object[tmpSize][3];
\r
361 System.arraycopy(tmp, 0, data,0, tmpSize);
\r
363 fr.setFeaturePriority(data);
\r
364 ap.seqPanel.seqCanvas.repaint();
\r
365 if (ap.overviewPanel != null)
\r
366 ap.overviewPanel.updateOverviewImage();
\r
369 MyCheckbox selectedCheck;
\r
370 boolean dragging = false;
\r
372 public void mousePressed(MouseEvent evt)
\r
375 selectedCheck = (MyCheckbox)evt.getSource();
\r
377 if(fr.featureLinks!=null
\r
378 && fr.featureLinks.containsKey(selectedCheck.getLabel() )
\r
381 if(evt.getX()>selectedCheck.stringWidth+20)
\r
389 public void mouseDragged(MouseEvent evt)
\r
391 if(((Component)evt.getSource()).getParent()!=featurePanel)
\r
396 public void mouseReleased(MouseEvent evt)
\r
398 if(((Component)evt.getSource()).getParent()!=featurePanel)
\r
401 Component comp = null;
\r
402 Checkbox target = null;
\r
404 int height = evt.getY()+evt.getComponent().getLocation().y;
\r
406 if(height > this.getSize().height)
\r
408 comp = featurePanel.getComponent(featurePanel.getComponentCount()-1);
\r
410 else if(height < 0)
\r
412 comp = featurePanel.getComponent(0);
\r
416 comp = featurePanel.getComponentAt(evt.getX(),
\r
418 evt.getComponent().getLocation().y);
\r
421 if(comp!=null && comp instanceof Checkbox)
\r
422 target = (Checkbox)comp;
\r
424 if ( selectedCheck != null
\r
426 && selectedCheck != target)
\r
428 int targetIndex = -1;
\r
429 for(int i=0; i<featurePanel.getComponentCount(); i++)
\r
431 if(target==featurePanel.getComponent(i))
\r
432 { targetIndex = i; break; }
\r
435 featurePanel.remove(selectedCheck);
\r
436 featurePanel.add(selectedCheck, targetIndex);
\r
437 featurePanel.validate();
\r
438 itemStateChanged(null);
\r
442 public void setUserColour(String feature, Color col)
\r
444 fr.setColour(feature, col);
\r
445 featurePanel.removeAll();
\r
450 public void mouseEntered(MouseEvent evt){}
\r
451 public void mouseExited(MouseEvent evt){}
\r
452 public void mouseClicked(MouseEvent evt)
\r
454 MyCheckbox check = (MyCheckbox) evt.getSource();
\r
456 if (fr.featureLinks!=null
\r
457 && fr.featureLinks.containsKey(check.getLabel()))
\r
459 if (evt.getX() > check.stringWidth + 20)
\r
462 String link = fr.featureLinks.get(check.getLabel()).toString();
\r
463 ap.alignFrame.showURL(link.substring(link.indexOf("|") + 1),
\r
464 link.substring(0, link.indexOf("|")));
\r
468 if(check.getParent()!=featurePanel)
\r
471 if(evt.getClickCount()>1)
\r
473 new UserDefinedColours(this, check.getLabel(),
\r
474 fr.getColour(check.getLabel()));
\r
477 public void mouseMoved(MouseEvent evt){}
\r
479 public void adjustmentValueChanged(AdjustmentEvent evt)
\r
481 fr.transparency = ( (float) (100 - transparency.getValue()) / 100f);
\r
482 ap.seqPanel.seqCanvas.repaint();
\r
486 class MyCheckbox extends Checkbox
\r
488 public int stringWidth;
\r
490 public MyCheckbox(String label, boolean checked, boolean haslink)
\r
492 super(label, checked);
\r
494 FontMetrics fm = av.nullFrame.getFontMetrics(av.nullFrame.getFont());
\r
495 stringWidth = fm.stringWidth(label);
\r
496 this.hasLink = haslink;
\r
499 public void paint(Graphics g)
\r
502 g.drawImage(linkImage, stringWidth + 25,(
\r
503 getSize().height-linkImage.getHeight(this))/2,
\r