2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2007 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
24 import java.awt.event.*;
\r
26 import jalview.datamodel.*;
\r
28 public class FeatureSettings
\r
29 extends Panel implements ItemListener,
\r
30 MouseListener, MouseMotionListener, ActionListener, AdjustmentListener
\r
37 Panel featurePanel = new Panel();
\r
38 ScrollPane scrollPane;
\r
39 boolean alignmentHasFeatures = false;
\r
41 Scrollbar transparency;
\r
43 public FeatureSettings(final AlignmentPanel ap)
\r
47 fr = ap.seqPanel.seqCanvas.getFeatureRenderer();
\r
49 transparency = new Scrollbar(Scrollbar.HORIZONTAL,
\r
50 100 - (int) (fr.transparency * 100), 1, 1, 100);
\r
52 if (fr.transparencySetter != null)
\r
54 transparency.addAdjustmentListener(this);
\r
58 transparency.setEnabled(false);
\r
61 java.net.URL url = getClass().getResource("/images/link.gif");
\r
64 linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);
\r
67 if (av.featuresDisplayed == null)
\r
69 fr.findAllFeatures();
\r
74 this.setLayout(new BorderLayout());
\r
75 scrollPane = new ScrollPane();
\r
76 scrollPane.add(featurePanel);
\r
77 if (alignmentHasFeatures)
\r
79 add(scrollPane, BorderLayout.CENTER);
\r
82 Button invert = new Button("Invert Selection");
\r
83 invert.addActionListener(this);
\r
85 Panel lowerPanel = new Panel(new GridLayout(2, 1, 5, 10));
\r
86 lowerPanel.add(invert);
\r
88 Panel tPanel = new Panel(new BorderLayout());
\r
90 if (fr.transparencySetter != null)
\r
92 tPanel.add(transparency, BorderLayout.CENTER);
\r
93 tPanel.add(new Label("Transparency"), BorderLayout.EAST);
\r
97 tPanel.add(new Label("Transparency not available in this web browser"),
\r
98 BorderLayout.CENTER);
\r
101 lowerPanel.add(tPanel, BorderLayout.SOUTH);
\r
103 add(lowerPanel, BorderLayout.SOUTH);
\r
105 if (groupPanel != null)
\r
107 groupPanel.setLayout(
\r
108 new GridLayout(fr.featureGroups.size() / 4 + 1, 4));
\r
109 groupPanel.validate();
\r
111 add(groupPanel, BorderLayout.NORTH);
\r
113 frame = new Frame();
\r
115 int height = featurePanel.getComponentCount() * 50 + 60;
\r
117 height = Math.max(200, height);
\r
118 height = Math.min(400, height);
\r
120 jalview.bin.JalviewLite.addFrame(frame, "Feature Settings", 280,
\r
124 public void paint(Graphics g)
\r
126 g.setColor(Color.black);
\r
127 g.drawString("No Features added to this alignment!!", 10, 20);
\r
128 g.drawString("(Features can be added from searches or", 10, 40);
\r
129 g.drawString("from Jalview / GFF features files)", 10, 60);
\r
132 void setTableData()
\r
134 alignmentHasFeatures = false;
\r
136 if (fr.featureGroups == null)
\r
138 fr.featureGroups = new Hashtable();
\r
141 Vector allFeatures = new Vector();
\r
142 Vector allGroups = new Vector();
\r
143 SequenceFeature[] tmpfeatures;
\r
146 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
148 if (av.alignment.getSequenceAt(i).getSequenceFeatures() == null)
\r
153 alignmentHasFeatures = true;
\r
155 tmpfeatures = av.alignment.getSequenceAt(i).getSequenceFeatures();
\r
157 while (index < tmpfeatures.length)
\r
159 if (tmpfeatures[index].getFeatureGroup() != null)
\r
161 group = tmpfeatures[index].featureGroup;
\r
162 if (!allGroups.contains(group))
\r
164 allGroups.addElement(group);
\r
166 boolean visible = true;
\r
167 if (fr.featureGroups.containsKey(group))
\r
169 visible = ( (Boolean) fr.featureGroups.get(group)).booleanValue();
\r
172 fr.featureGroups.put(group, new Boolean(visible));
\r
174 if (groupPanel == null)
\r
176 groupPanel = new Panel();
\r
179 Checkbox check = new MyCheckbox(
\r
182 (fr.featureLinks != null && fr.featureLinks.containsKey(group))
\r
185 check.addMouseListener(this);
\r
186 check.setFont(new Font("Serif", Font.BOLD, 12));
\r
187 check.addItemListener(this);
\r
188 groupPanel.add(check);
\r
192 if (!allFeatures.contains(tmpfeatures[index].getType()))
\r
194 allFeatures.addElement(tmpfeatures[index].getType());
\r
203 //This routine adds and removes checkboxes depending on
\r
204 //Group selection states
\r
205 void resetTable(boolean groupsChanged)
\r
207 SequenceFeature[] tmpfeatures;
\r
208 String group = null, type;
\r
209 Vector visibleChecks = new Vector();
\r
211 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
213 if (av.alignment.getSequenceAt(i).getSequenceFeatures() == null)
\r
218 tmpfeatures = av.alignment.getSequenceAt(i).getSequenceFeatures();
\r
220 while (index < tmpfeatures.length)
\r
222 group = tmpfeatures[index].featureGroup;
\r
224 if (group == null || fr.featureGroups.get(group) == null ||
\r
225 ( (Boolean) fr.featureGroups.get(group)).booleanValue())
\r
227 type = tmpfeatures[index].getType();
\r
228 if (!visibleChecks.contains(type))
\r
230 visibleChecks.addElement(type);
\r
238 int cSize = featurePanel.getComponentCount();
\r
240 //This will remove any checkboxes which shouldn't be
\r
242 for (int i = 0; i < cSize; i++)
\r
244 comps = featurePanel.getComponents();
\r
245 check = (Checkbox) comps[i];
\r
246 if (!visibleChecks.contains(check.getLabel()))
\r
248 featurePanel.remove(i);
\r
254 if (fr.renderOrder != null)
\r
256 //First add the checks in the previous render order,
\r
257 //in case the window has been closed and reopened
\r
258 for (int ro = fr.renderOrder.length - 1; ro > -1; ro--)
\r
260 String item = fr.renderOrder[ro];
\r
262 if (!visibleChecks.contains(item))
\r
267 visibleChecks.removeElement(item);
\r
269 addCheck(false, item);
\r
273 // now add checkboxes which should be visible,
\r
274 // if they have not already been added
\r
275 Enumeration en = visibleChecks.elements();
\r
277 while (en.hasMoreElements())
\r
279 addCheck(groupsChanged, en.nextElement().toString());
\r
282 featurePanel.setLayout(new GridLayout(featurePanel.getComponentCount(), 1,
\r
284 featurePanel.validate();
\r
286 if (scrollPane != null)
\r
288 scrollPane.validate();
\r
291 itemStateChanged(null);
\r
294 void addCheck(boolean groupsChanged, String type)
\r
297 Component[] comps = featurePanel.getComponents();
\r
300 for (int i = 0; i < featurePanel.getComponentCount(); i++)
\r
302 check = (Checkbox) comps[i];
\r
303 if (check.getLabel().equals(type))
\r
312 boolean selected = false;
\r
313 if (groupsChanged || av.featuresDisplayed.containsKey(type))
\r
318 check = new MyCheckbox(type,
\r
320 (fr.featureLinks != null &&
\r
321 fr.featureLinks.containsKey(type))
\r
324 check.addMouseListener(this);
\r
325 check.addMouseMotionListener(this);
\r
326 check.setBackground(fr.getColour(type));
\r
327 check.addItemListener(this);
\r
328 featurePanel.add(check);
\r
332 public void actionPerformed(ActionEvent evt)
\r
334 for (int i = 0; i < featurePanel.getComponentCount(); i++)
\r
336 Checkbox check = (Checkbox) featurePanel.getComponent(i);
\r
337 check.setState(!check.getState());
\r
339 selectionChanged();
\r
342 public void itemStateChanged(ItemEvent evt)
\r
346 //Is the source a top level featureGroup?
\r
347 Checkbox source = (Checkbox) evt.getSource();
\r
348 if (fr.featureGroups.containsKey(source.getLabel()))
\r
350 fr.featureGroups.put(source.getLabel(), new Boolean(source.getState()));
\r
351 ap.seqPanel.seqCanvas.repaint();
\r
352 if (ap.overviewPanel != null)
\r
354 ap.overviewPanel.updateOverviewImage();
\r
361 selectionChanged();
\r
364 void selectionChanged()
\r
366 Component[] comps = featurePanel.getComponents();
\r
367 int cSize = comps.length;
\r
369 Object[][] tmp = new Object[cSize][3];
\r
371 for (int i = 0; i < cSize; i++)
\r
373 Checkbox check = (Checkbox) comps[i];
\r
374 tmp[tmpSize][0] = check.getLabel();
\r
375 tmp[tmpSize][1] = fr.getColour(check.getLabel());
\r
376 tmp[tmpSize][2] = new Boolean(check.getState());
\r
380 Object[][] data = new Object[tmpSize][3];
\r
381 System.arraycopy(tmp, 0, data, 0, tmpSize);
\r
383 fr.setFeaturePriority(data);
\r
385 ap.paintAlignment(true);
\r
388 MyCheckbox selectedCheck;
\r
389 boolean dragging = false;
\r
391 public void mousePressed(MouseEvent evt)
\r
394 selectedCheck = (MyCheckbox) evt.getSource();
\r
396 if (fr.featureLinks != null
\r
397 && fr.featureLinks.containsKey(selectedCheck.getLabel())
\r
400 if (evt.getX() > selectedCheck.stringWidth + 20)
\r
408 public void mouseDragged(MouseEvent evt)
\r
410 if ( ( (Component) evt.getSource()).getParent() != featurePanel)
\r
417 public void mouseReleased(MouseEvent evt)
\r
419 if ( ( (Component) evt.getSource()).getParent() != featurePanel)
\r
424 Component comp = null;
\r
425 Checkbox target = null;
\r
427 int height = evt.getY() + evt.getComponent().getLocation().y;
\r
429 if (height > featurePanel.getSize().height)
\r
432 comp = featurePanel.getComponent(featurePanel.getComponentCount() - 1);
\r
434 else if (height < 0)
\r
436 comp = featurePanel.getComponent(0);
\r
440 comp = featurePanel.getComponentAt(evt.getX(),
\r
442 evt.getComponent().getLocation().y);
\r
445 if (comp != null && comp instanceof Checkbox)
\r
447 target = (Checkbox) comp;
\r
450 if (selectedCheck != null
\r
452 && selectedCheck != target)
\r
454 int targetIndex = -1;
\r
455 for (int i = 0; i < featurePanel.getComponentCount(); i++)
\r
457 if (target == featurePanel.getComponent(i))
\r
464 featurePanel.remove(selectedCheck);
\r
465 featurePanel.add(selectedCheck, targetIndex);
\r
466 featurePanel.validate();
\r
467 itemStateChanged(null);
\r
471 public void setUserColour(String feature, Color col)
\r
473 fr.setColour(feature, col);
\r
474 featurePanel.removeAll();
\r
476 ap.paintAlignment(true);
\r
479 public void mouseEntered(MouseEvent evt)
\r
482 public void mouseExited(MouseEvent evt)
\r
485 public void mouseClicked(MouseEvent evt)
\r
487 MyCheckbox check = (MyCheckbox) evt.getSource();
\r
489 if (fr.featureLinks != null
\r
490 && fr.featureLinks.containsKey(check.getLabel()))
\r
492 if (evt.getX() > check.stringWidth + 20)
\r
495 String link = fr.featureLinks.get(check.getLabel()).toString();
\r
496 ap.alignFrame.showURL(link.substring(link.indexOf("|") + 1),
\r
497 link.substring(0, link.indexOf("|")));
\r
501 if (check.getParent() != featurePanel)
\r
506 if (evt.getClickCount() > 1)
\r
508 new UserDefinedColours(this, check.getLabel(),
\r
509 fr.getColour(check.getLabel()));
\r
513 public void mouseMoved(MouseEvent evt)
\r
516 public void adjustmentValueChanged(AdjustmentEvent evt)
\r
518 fr.transparency = ( (float) (100 - transparency.getValue()) / 100f);
\r
519 ap.seqPanel.seqCanvas.repaint();
\r
526 public int stringWidth;
\r
528 public MyCheckbox(String label, boolean checked, boolean haslink)
\r
530 super(label, checked);
\r
532 FontMetrics fm = av.nullFrame.getFontMetrics(av.nullFrame.getFont());
\r
533 stringWidth = fm.stringWidth(label);
\r
534 this.hasLink = haslink;
\r
537 public void paint(Graphics g)
\r
541 g.drawImage(linkImage, stringWidth + 25, (
\r
542 getSize().height - linkImage.getHeight(this)) / 2,
\r