2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.appletgui;
21 import jalview.datamodel.*;
24 import java.awt.event.*;
27 public class FeatureSettings extends Panel implements ItemListener,
28 MouseListener, MouseMotionListener, ActionListener, AdjustmentListener
35 Panel featurePanel = new Panel();
36 ScrollPane scrollPane;
37 boolean alignmentHasFeatures = false;
39 Scrollbar transparency ;
41 public FeatureSettings(AlignViewport av, final AlignmentPanel ap)
45 fr = ap.seqPanel.seqCanvas.getFeatureRenderer();
47 transparency = new Scrollbar(Scrollbar.HORIZONTAL,
48 100 - (int)(fr.transparency*100), 1, 1, 100);
50 if(fr.transparencySetter!=null)
52 transparency.addAdjustmentListener(this);
55 transparency.setEnabled(false);
57 java.net.URL url = getClass().getResource("/images/link.gif");
60 linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);
64 if(av.featuresDisplayed==null)
69 this.setLayout(new BorderLayout());
70 scrollPane = new ScrollPane();
71 scrollPane.add(featurePanel);
72 if (alignmentHasFeatures)
73 add(scrollPane, BorderLayout.CENTER);
75 Button invert = new Button("Invert Selection");
76 invert.addActionListener(this);
78 Panel lowerPanel = new Panel(new GridLayout(2,1,5,10));
79 lowerPanel.add(invert);
81 Panel tPanel = new Panel(new BorderLayout());
83 if(fr.transparencySetter!=null)
85 tPanel.add(transparency, BorderLayout.CENTER);
86 tPanel.add(new Label("Transparency"), BorderLayout.EAST);
89 tPanel.add(new Label("Transparency not available in this web browser"), BorderLayout.CENTER);
91 lowerPanel.add(tPanel, BorderLayout.SOUTH);
93 add(lowerPanel, BorderLayout.SOUTH);
100 new GridLayout( fr.featureGroups.size() / 4 + 1, 4));
101 groupPanel.validate();
103 add(groupPanel, BorderLayout.NORTH);
107 int height = featurePanel.getComponentCount()*50 +60;
110 height = Math.max(200, height);
111 height = Math.min(400, height);
113 jalview.bin.JalviewLite.addFrame(frame, "Feature Settings", 280,
117 public void paint(Graphics g)
119 g.setColor(Color.black);
120 g.drawString("No Features added to this alignment!!", 10, 20);
121 g.drawString("(Features can be added from searches or", 10, 40);
122 g.drawString("from Jalview / GFF features files)", 10, 60);
127 alignmentHasFeatures = false;
129 if(fr.featureGroups==null)
130 fr.featureGroups = new Hashtable();
132 Vector allFeatures = new Vector();
133 Vector allGroups = new Vector();
134 SequenceFeature[] tmpfeatures;
138 for (int i = 0; i < av.alignment.getHeight(); i++)
140 if (av.alignment.getSequenceAt(i).getSequenceFeatures() == null)
143 alignmentHasFeatures = true;
145 tmpfeatures = av.alignment.getSequenceAt(i).getSequenceFeatures();
147 while (index < tmpfeatures.length)
149 if(tmpfeatures[index].getFeatureGroup()!=null)
151 group = tmpfeatures[index].featureGroup;
152 if(!allGroups.contains(group))
154 allGroups.addElement(group);
156 boolean visible = true;
157 if (fr.featureGroups.containsKey(group))
159 visible = ( (Boolean) fr.featureGroups.get(group)).booleanValue();
162 fr.featureGroups.put(group, new Boolean(visible));
164 if (groupPanel == null)
166 groupPanel = new Panel();
169 Checkbox check = new MyCheckbox(
172 (fr.featureLinks!=null && fr.featureLinks.containsKey(group))
176 check.addMouseListener(this);
177 check.setFont(new Font("Serif", Font.BOLD, 12));
178 check.addItemListener(this);
179 groupPanel.add(check);
183 if (!allFeatures.contains(tmpfeatures[index].getType()))
185 allFeatures.addElement(tmpfeatures[index].getType());
194 //This routine adds and removes checkboxes depending on
195 //Group selection states
196 void resetTable(boolean groupsChanged)
198 SequenceFeature [] tmpfeatures;
199 String group=null, type;
200 Vector visibleChecks = new Vector();
202 for (int i = 0; i < av.alignment.getHeight(); i++)
204 if (av.alignment.getSequenceAt(i).getSequenceFeatures() == null)
207 tmpfeatures = av.alignment.getSequenceAt(i).getSequenceFeatures();
209 while (index < tmpfeatures.length)
211 group = tmpfeatures[index].featureGroup;
213 if (group==null || fr.featureGroups.get(group)==null ||
214 ((Boolean) fr.featureGroups.get(group)).booleanValue())
216 type = tmpfeatures[index].getType();
217 if(!visibleChecks.contains(type) )
219 visibleChecks.addElement(type);
227 int cSize = featurePanel.getComponentCount();
229 //This will remove any checkboxes which shouldn't be
231 for (int i = 0; i < cSize; i++)
233 comps = featurePanel.getComponents();
234 check = (Checkbox) comps[i];
235 if (!visibleChecks.contains(check.getLabel()))
237 featurePanel.remove(i);
243 if(fr.renderOrder!=null)
245 //First add the checks in the previous render order,
246 //in case the window has been closed and reopened
247 for(int ro=fr.renderOrder.length-1; ro>-1; ro--)
249 String item = fr.renderOrder[ro];
251 if(!visibleChecks.contains(item))
254 visibleChecks.removeElement(item);
256 addCheck(false, item);
260 // now add checkboxes which should be visible,
261 // if they have not already been added
262 Enumeration en = visibleChecks.elements();
264 while(en.hasMoreElements())
266 addCheck(groupsChanged, en.nextElement().toString());
270 featurePanel.setLayout(new GridLayout(featurePanel.getComponentCount(), 1, 10,5));
271 featurePanel.validate();
275 scrollPane.validate();
277 itemStateChanged(null);
280 void addCheck(boolean groupsChanged, String type)
283 Component [] comps = featurePanel.getComponents();
286 for (int i = 0; i < featurePanel.getComponentCount(); i++)
288 check = (Checkbox) comps[i];
289 if (check.getLabel().equals(type))
298 boolean selected = false;
299 if (groupsChanged || av.featuresDisplayed.containsKey(type))
304 check = new MyCheckbox(type,
306 (fr.featureLinks!=null && fr.featureLinks.containsKey(type))
309 check.addMouseListener(this);
310 check.addMouseMotionListener(this);
311 check.setBackground(fr.getColour(type));
312 check.addItemListener(this);
313 featurePanel.add(check);
317 public void actionPerformed(ActionEvent evt)
319 for(int i=0; i<featurePanel.getComponentCount(); i++)
321 Checkbox check = (Checkbox)featurePanel.getComponent(i);
322 check.setState(!check.getState());
327 public void itemStateChanged(ItemEvent evt)
331 //Is the source a top level featureGroup?
332 Checkbox source = (Checkbox) evt.getSource();
333 if (fr.featureGroups.containsKey(source.getLabel()))
335 fr.featureGroups.put(source.getLabel(), new Boolean(source.getState()));
336 ap.seqPanel.seqCanvas.repaint();
337 if (ap.overviewPanel != null)
338 ap.overviewPanel.updateOverviewImage();
347 void selectionChanged()
349 Component[] comps = featurePanel.getComponents();
350 int cSize = comps.length;
352 Object[][] tmp = new Object[cSize][3];
354 for (int i = 0; i < cSize; i++)
356 Checkbox check = (Checkbox) comps[i];
357 tmp[tmpSize][0] = check.getLabel();
358 tmp[tmpSize][1] = fr.getColour(check.getLabel());
359 tmp[tmpSize][2] = new Boolean(check.getState());
363 Object[][]data = new Object[tmpSize][3];
364 System.arraycopy(tmp, 0, data,0, tmpSize);
366 fr.setFeaturePriority(data);
367 ap.seqPanel.seqCanvas.repaint();
368 if (ap.overviewPanel != null)
369 ap.overviewPanel.updateOverviewImage();
372 MyCheckbox selectedCheck;
373 boolean dragging = false;
375 public void mousePressed(MouseEvent evt)
378 selectedCheck = (MyCheckbox)evt.getSource();
380 if(fr.featureLinks!=null
381 && fr.featureLinks.containsKey(selectedCheck.getLabel() )
384 if(evt.getX()>selectedCheck.stringWidth+20)
392 public void mouseDragged(MouseEvent evt)
394 if(((Component)evt.getSource()).getParent()!=featurePanel)
399 public void mouseReleased(MouseEvent evt)
401 if(((Component)evt.getSource()).getParent()!=featurePanel)
404 Component comp = null;
405 Checkbox target = null;
407 int height = evt.getY()+evt.getComponent().getLocation().y;
409 if(height > featurePanel.getSize().height)
412 comp = featurePanel.getComponent(featurePanel.getComponentCount()-1);
416 comp = featurePanel.getComponent(0);
420 comp = featurePanel.getComponentAt(evt.getX(),
422 evt.getComponent().getLocation().y);
425 if(comp!=null && comp instanceof Checkbox)
426 target = (Checkbox)comp;
428 if ( selectedCheck != null
430 && selectedCheck != target)
432 int targetIndex = -1;
433 for(int i=0; i<featurePanel.getComponentCount(); i++)
435 if(target==featurePanel.getComponent(i))
436 { targetIndex = i; break; }
439 featurePanel.remove(selectedCheck);
440 featurePanel.add(selectedCheck, targetIndex);
441 featurePanel.validate();
442 itemStateChanged(null);
446 public void setUserColour(String feature, Color col)
448 fr.setColour(feature, col);
449 featurePanel.removeAll();
454 public void mouseEntered(MouseEvent evt){}
455 public void mouseExited(MouseEvent evt){}
456 public void mouseClicked(MouseEvent evt)
458 MyCheckbox check = (MyCheckbox) evt.getSource();
460 if (fr.featureLinks!=null
461 && fr.featureLinks.containsKey(check.getLabel()))
463 if (evt.getX() > check.stringWidth + 20)
466 String link = fr.featureLinks.get(check.getLabel()).toString();
467 ap.alignFrame.showURL(link.substring(link.indexOf("|") + 1),
468 link.substring(0, link.indexOf("|")));
472 if(check.getParent()!=featurePanel)
475 if(evt.getClickCount()>1)
477 new UserDefinedColours(this, check.getLabel(),
478 fr.getColour(check.getLabel()));
481 public void mouseMoved(MouseEvent evt){}
483 public void adjustmentValueChanged(AdjustmentEvent evt)
485 fr.transparency = ( (float) (100 - transparency.getValue()) / 100f);
486 ap.seqPanel.seqCanvas.repaint();
490 class MyCheckbox extends Checkbox
492 public int stringWidth;
494 public MyCheckbox(String label, boolean checked, boolean haslink)
496 super(label, checked);
498 FontMetrics fm = av.nullFrame.getFontMetrics(av.nullFrame.getFont());
499 stringWidth = fm.stringWidth(label);
500 this.hasLink = haslink;
503 public void paint(Graphics g)
506 g.drawImage(linkImage, stringWidth + 25,(
507 getSize().height-linkImage.getHeight(this))/2,