/* * Jalview - A Sequence Alignment Editor and Viewer * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ package jalview.appletgui; import jalview.datamodel.*; import java.awt.*; import java.util.*; import java.awt.event.*; public class FeatureSettings extends Panel implements ItemListener, MouseListener, MouseMotionListener { final FeatureRenderer fr; final AlignmentPanel ap; final AlignViewport av; final Frame frame; Panel groupPanel; Panel featurePanel = new Panel(); ScrollPane scrollPane; public FeatureSettings(AlignViewport av, final AlignmentPanel ap) { this.ap = ap; this.av = av; fr = ap.seqPanel.seqCanvas.getFeatureRenderer(); setTableData(); this.setLayout(new BorderLayout()); scrollPane = new ScrollPane(); scrollPane.add(featurePanel); add(scrollPane, BorderLayout.CENTER); if(groupPanel!=null) { groupPanel.setLayout( new GridLayout(groupPanel.getComponentCount() / 4, 4)); groupPanel.validate(); add(groupPanel, BorderLayout.NORTH); } frame = new Frame(); frame.add(this); int height = featurePanel.getComponentCount()*50 ; if (height>400) height = 400; jalview.bin.JalviewLite.addFrame(frame, "Feature Settings", 250, height); } void setTableData() { if(fr.featureGroups==null) fr.featureGroups = new Hashtable(); else fr.featureGroups.clear(); Vector allFeatures = new Vector(); SequenceFeature[] tmpfeatures; for (int i = 0; i < av.alignment.getHeight(); i++) { if (av.alignment.getSequenceAt(i).getSequenceFeatures() == null) continue; tmpfeatures = av.alignment.getSequenceAt(i).getSequenceFeatures(); int index = 0; while (index < tmpfeatures.length) { if(tmpfeatures[index].getFeatureGroup()!=null && tmpfeatures[index].getFeatureGroup().length()>0 && !fr.featureGroups.containsKey(tmpfeatures[index].getFeatureGroup())) { fr.featureGroups.put(tmpfeatures[index].getFeatureGroup(), new Boolean(true)); if(groupPanel==null) { groupPanel = new Panel(); } Checkbox check = new Checkbox(tmpfeatures[index].getFeatureGroup(), true); check.setFont(new Font("Serif", Font.BOLD, 12)); check.addItemListener(this); groupPanel.add(check); } if (!allFeatures.contains(tmpfeatures[index].getType())) { allFeatures.addElement(tmpfeatures[index].getType()); } index ++; } } resetTable(); featurePanel.setLayout(new GridLayout(allFeatures.size(), 2, 10,5)); featurePanel.validate(); } void resetTable() { featurePanel.removeAll(); Enumeration groups = fr.featureGroups.keys(); SequenceFeature [] tmpfeatures; String group, type; Vector checksAdded = new Vector(); while(groups.hasMoreElements()) { group = groups.nextElement().toString(); if ( !( (Boolean) fr.featureGroups.get(group)).booleanValue()) continue; for (int i = 0; i < av.alignment.getHeight(); i++) { if (av.alignment.getSequenceAt(i).getSequenceFeatures() == null) continue; tmpfeatures = av.alignment.getSequenceAt(i).getSequenceFeatures(); int index = 0; while (index < tmpfeatures.length) { if (tmpfeatures[index].getFeatureGroup() != null && tmpfeatures[index].getFeatureGroup().equals(group)) { type = tmpfeatures[index].getType(); if(!checksAdded.contains(type)) { checksAdded.addElement(type); Checkbox check = new Checkbox(type, true); check.addMouseListener(this); check.addMouseMotionListener(this); check.setBackground(fr.getColour(type)); check.addItemListener(this); featurePanel.add(check); } } index++; } } } featurePanel.validate(); if(scrollPane!=null) scrollPane.validate(); } public void itemStateChanged(ItemEvent evt) { if (evt != null) { //Is the source a top level featureGroup? Checkbox source = (Checkbox) evt.getSource(); if (fr.featureGroups.containsKey(source.getLabel())) { fr.featureGroups.put(source.getLabel(), new Boolean(source.getState())); ap.seqPanel.seqCanvas.repaint(); if (ap.overviewPanel != null) ap.overviewPanel.updateOverviewImage(); resetTable(); return; } } Component[] comps = featurePanel.getComponents(); int cSize = comps.length; Object[][] tmp = new Object[cSize][3]; int tmpSize = 0; for (int i = 0; i < cSize; i++) { Checkbox check = (Checkbox) comps[i]; if(!check.getState()) continue; tmp[tmpSize][0] = check.getLabel(); tmp[tmpSize][1] = fr.getColour(check.getLabel()); tmp[tmpSize][2] = new Boolean(check.getState()); tmpSize++; } Object[][]data = new Object[tmpSize][3]; System.arraycopy(tmp, 0, data,0, tmpSize); fr.setFeaturePriority(data); ap.seqPanel.seqCanvas.repaint(); if (ap.overviewPanel != null) ap.overviewPanel.updateOverviewImage(); } Checkbox selectedCheck; boolean dragging = false; public void mousePressed(MouseEvent evt) { selectedCheck = (Checkbox)evt.getSource(); } public void mouseDragged(MouseEvent evt) { dragging = true; } public void mouseReleased(MouseEvent evt) { Component comp = null; Checkbox target = null; int height = evt.getY()+evt.getComponent().getLocation().y; if(height > this.getSize().height) { comp = featurePanel.getComponent(featurePanel.getComponentCount()-1); } else if(height < 0) { comp = featurePanel.getComponent(0); } else { comp = featurePanel.getComponentAt(evt.getX(), evt.getY() + evt.getComponent().getLocation().y); } if(comp!=null && comp instanceof Checkbox) target = (Checkbox)comp; if (selectedCheck != null && target != null && selectedCheck != target) { int targetIndex = -1; for(int i=0; i