From: amwaterhouse Date: Thu, 9 Feb 2006 14:42:18 +0000 (+0000) Subject: New implementation for holding search results X-Git-Tag: Root_VamJalview_2_07b+~157 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=df1c58f9669c477db082016aa919d6c6cc9da071;p=jalview.git New implementation for holding search results --- diff --git a/src/jalview/appletgui/AlignFrame.java b/src/jalview/appletgui/AlignFrame.java index 496a8bb..bd49198 100755 --- a/src/jalview/appletgui/AlignFrame.java +++ b/src/jalview/appletgui/AlignFrame.java @@ -817,6 +817,7 @@ public void itemStateChanged(ItemEvent evt) viewport.setSelectionGroup(null); viewport.getColumnSelection().clear(); viewport.setSelectionGroup(null); + alignPanel.idPanel.idCanvas.searchResults = null; alignPanel.seqPanel.seqCanvas.highlightSearchResults(null); alignPanel.repaint(); PaintRefresher.Refresh(null, viewport.alignment); diff --git a/src/jalview/appletgui/AlignmentPanel.java b/src/jalview/appletgui/AlignmentPanel.java index 0e9a0d8..0423cbb 100755 --- a/src/jalview/appletgui/AlignmentPanel.java +++ b/src/jalview/appletgui/AlignmentPanel.java @@ -185,49 +185,48 @@ public class AlignmentPanel extends Panel implements AdjustmentListener return new Dimension(idWidth, idPanel.idCanvas.getSize().height); } - public void highlightSearchResults(int[] results) + public void highlightSearchResults(SearchResults results) { seqPanel.seqCanvas.highlightSearchResults(results); - // do we need to scroll the panel? + // do we need to scroll the panel? if (results != null) { - SequenceI seq = av.alignment.getSequenceAt(results[0]); - int start = seq.findIndex(results[1]) - 1; - int end = seq.findIndex(results[2]) - 1; + SequenceI seq = results.getResultSequence(0); + int seqIndex = av.alignment.findIndex(seq); + int start = seq.findIndex(results.getResultStart(0)) - 1; + int end = seq.findIndex(results.getResultEnd(0)) - 1; - if(!av.wrapAlignment) - { - if ( (av.getStartRes() > start) || (av.getEndRes() < end) || - ( (av.getStartSeq() > results[0]) || - (av.getEndSeq() < results[0]))) + if(!av.wrapAlignment) { - int newy = results[0]; - if(start > av.alignment.getWidth() - hextent) - { - start = av.alignment.getWidth() - hextent; - if(start<0) - start = 0; - } - if(newy > av.alignment.getHeight() - vextent) + if ( (av.getStartRes() > end) || (av.getEndRes() < start) || + ( (av.getStartSeq() > seqIndex) || (av.getEndSeq() < seqIndex))) { - newy = av.alignment.getHeight() - vextent; - if(newy<0) - newy = 0; + if (start > av.alignment.getWidth() - hextent) + { + start = av.alignment.getWidth() - hextent; + if (start < 0) + start = 0; + } + if (seqIndex > av.alignment.getHeight() - vextent) + { + seqIndex = av.alignment.getHeight() - vextent; + if (seqIndex < 0) + seqIndex = 0; + } + setScrollValues(start, seqIndex); } - setScrollValues(start, newy); } - } - else - { - int cwidth = seqPanel.seqCanvas.getWrappedCanvasWidth(seqPanel.seqCanvas.getSize().width); - if( start(av.getStartRes()+cwidth) ) + else { - - vscroll.setValue(start / cwidth); - av.startRes = vscroll.getValue() * cwidth; + int cwidth = seqPanel.seqCanvas.getWrappedCanvasWidth(seqPanel. + seqCanvas.getSize().width); + if (start < av.getStartRes() || start > (av.getStartRes() + cwidth)) + { + vscroll.setValue(start / cwidth); + av.startRes = vscroll.getValue() * cwidth; + } } - } } repaint(); diff --git a/src/jalview/appletgui/Finder.java b/src/jalview/appletgui/Finder.java index f59216f..ac7bc2c 100755 --- a/src/jalview/appletgui/Finder.java +++ b/src/jalview/appletgui/Finder.java @@ -32,7 +32,8 @@ public class Finder extends Panel implements ActionListener AlignmentPanel ap; Frame frame; SuperGroup searchGroup; - Vector searchResults; + + SearchResults searchResults; int seqIndex = 0; int resIndex = 0; @@ -95,17 +96,14 @@ public class Finder extends Panel implements ActionListener searchGroup = new SuperGroup(searchString, ucs, true, true, false); - for (int i = 0; i < searchResults.size(); i += 3) + for (int i = 0; i < searchResults.getSize(); i ++ ) { // its possible edits may have occurred since search was performed // account for this here - SequenceI seq = av.alignment.getSequenceAt(Integer.parseInt(searchResults. - elementAt(i).toString())); - int startRes = seq.findIndex(Integer.parseInt(searchResults.elementAt(i + - 1).toString())) - 1; - int endRes = seq.findIndex(Integer.parseInt(searchResults.elementAt(i + 2). - toString())) - 1; + SequenceI seq = searchResults.getResultSequence(i); + int startRes = seq.findIndex(searchResults.getResultStart(i)) - 1; + int endRes = seq.findIndex(searchResults.getResultEnd(i)) - 1; SequenceGroup sg = new SequenceGroup(searchString, ucs, true, true, false, startRes, endRes); @@ -127,8 +125,7 @@ public class Finder extends Panel implements ActionListener com.stevesoft.pat.Regex regex = new com.stevesoft.pat.Regex(searchString); - searchResults = new Vector(); - int[] allResults = null; + searchResults = new SearchResults(); Sequence seq; String item = null; @@ -149,9 +146,8 @@ public class Finder extends Panel implements ActionListener seq = (Sequence) (av.getSelectionGroup().getSequenceAt(0)); } - searchResults.addElement(Integer.toString(av.getAlignment().findIndex(seq))); - searchResults.addElement(String.valueOf(res)); - searchResults.addElement(String.valueOf(res)); + + searchResults.addResult(seq, res, res); } catch (NumberFormatException ex) @@ -221,7 +217,7 @@ public class Finder extends Panel implements ActionListener continue; } - searchResults.addElement(Integer.toString(seqIndex)); + int sres = seq.findPosition(resIndex + Integer.parseInt(spaces. elementAt(resIndex).toString())); @@ -229,8 +225,8 @@ public class Finder extends Panel implements ActionListener Integer.parseInt( spaces.elementAt(regex.matchedTo() - 1). toString())); - searchResults.addElement(sres + ""); - searchResults.addElement(eres + ""); + + searchResults.addResult(seq, sres, eres); if (!findAll) { @@ -261,33 +257,28 @@ public class Finder extends Panel implements ActionListener } } - if (searchResults.size() == 0 && idMatch.size() > 0) + if (searchResults.getSize() == 0 && idMatch.size() > 0) { ap.idPanel.highlightSearchResults(idMatch); } - if (searchResults.size() > 0) + if (searchResults.getSize() > 0) { - allResults = new int[searchResults.size()]; - for (int i = 0; i < searchResults.size(); i++) - { - allResults[i] = Integer.parseInt(searchResults.elementAt(i).toString()); - } - createNewGroup.setEnabled(true); } else { + searchResults = null; resIndex = 0; seqIndex = 0; } // if allResults is null, this effectively switches displaySearch flag in seqCanvas - ap.highlightSearchResults(allResults); + ap.highlightSearchResults(searchResults); if (findAll) { - String message = (searchResults.size() / 3) + " matches found."; + String message = (searchResults.getSize()) + " matches found."; System.out.println(message); } diff --git a/src/jalview/appletgui/SeqCanvas.java b/src/jalview/appletgui/SeqCanvas.java index 8d95574..618ec8a 100755 --- a/src/jalview/appletgui/SeqCanvas.java +++ b/src/jalview/appletgui/SeqCanvas.java @@ -35,8 +35,7 @@ public class SeqCanvas AlignViewport av; - boolean displaySearch = false; - int[] searchResults = null; + SearchResults searchResults = null; int chunkHeight; int chunkWidth; @@ -454,6 +453,21 @@ public class SeqCanvas (i - starty) * av.charHeight, av.charWidth, av.charHeight); } + /// Highlight search Results once all sequences have been drawn + ////////////////////////////////////////////////////////// + if (searchResults != null) + { + int[] visibleResults = searchResults.getResults(nextSeq, x1, x2); + if (visibleResults != null) + for (int r = 0; r < visibleResults.length; r += 2) + { + sr.drawHighlightedText(nextSeq, visibleResults[r], + visibleResults[r + 1], + (visibleResults[r] - startx) * av.charWidth, + offset + ( (i - starty) * av.charHeight), + av.charWidth, av.charHeight); + } + } } ///////////////////////////////////// @@ -613,58 +627,10 @@ public class SeqCanvas } while (groupIndex < groups.size()); } - - /// Highlight search Results once all sequences have been drawn - ////////////////////////////////////////////////////////// - if (displaySearch) - { - for (int r = 0; r < searchResults.length; r += 3) - { - int searchSeq = searchResults[r]; - - if (searchSeq >= y1 && searchSeq < y2) - { - SequenceI seq = av.getAlignment().getSequenceAt(searchSeq); - - int searchStart = seq.findIndex(searchResults[r + 1]) - 1; - int searchEnd = seq.findIndex(searchResults[r + 2]) - 1; - - SequenceRenderer ssr = (SequenceRenderer) sr; - if (searchStart < x1) - { - searchStart = x1; - } - if (searchEnd > x2) - { - searchEnd = x2; - } - - ssr.drawHighlightedText(seq, - searchStart, - searchEnd, - (searchStart - startx) * av.charWidth, - offset + - (searchSeq-starty)*av.charHeight, - av.charWidth, - av.charHeight); - } - } - } - } - public void highlightSearchResults(int[] results) + public void highlightSearchResults(SearchResults results) { - // results are in the order sequence, startRes, endRes - if (results == null) - { - displaySearch = false; - } - else - { - displaySearch = true; - } - searchResults = results; repaint(); diff --git a/src/jalview/datamodel/SearchResults.java b/src/jalview/datamodel/SearchResults.java new file mode 100755 index 0000000..b0535b6 --- /dev/null +++ b/src/jalview/datamodel/SearchResults.java @@ -0,0 +1,130 @@ +/* + * 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.datamodel; + +public class SearchResults +{ + + Match [] matches; + + /** + * This method replaces the old search results which merely + * held an alignment index of search matches. This broke + * when sequences were moved around the alignment + * @param seq Sequence + * @param start int + * @param end int + */ + public void addResult(SequenceI seq, int start, int end) + { + if(matches == null) + { + matches = new Match[]{new Match(seq, start, end)}; + return; + } + + int mSize = matches.length; + + Match [] tmp = new Match[mSize+1]; + int m; + for(m=0; m=start) + { + if(matchStartend) + matchEnd = end; + + + if(result==null) + result = new int[]{matchStart, matchEnd}; + else + { + resultLength = result.length; + tmp = new int[resultLength+2]; + System.arraycopy(result,0,tmp,0,resultLength); + result = tmp; + result[resultLength] = matchStart; + result[resultLength+1] = matchEnd; + } + } + } + } + return result; + } + + public int getSize() + { + return matches==null ? 0 : matches.length; + } + + public SequenceI getResultSequence(int index) + { return matches[index].sequence; } + + public int getResultStart(int index) + { return matches[index].start; } + + public int getResultEnd(int index) + { return matches[index].end; } + + class Match + { + SequenceI sequence; + int start; + int end; + + public Match(SequenceI seq, int start, int end) + { + sequence = seq; + this.start = start; + this.end = end; + } + } +} diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index c193ab3..3319b33 100755 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -906,6 +906,7 @@ public class AlignFrame viewport.getColumnSelection().clear(); viewport.setSelectionGroup(null); alignPanel.seqPanel.seqCanvas.highlightSearchResults(null); + alignPanel.idPanel.idCanvas.searchResults = null; alignPanel.annotationPanel.activeRes = null; PaintRefresher.Refresh(null, viewport.alignment); } diff --git a/src/jalview/gui/AlignmentPanel.java b/src/jalview/gui/AlignmentPanel.java index fdd01d3..f81e471 100755 --- a/src/jalview/gui/AlignmentPanel.java +++ b/src/jalview/gui/AlignmentPanel.java @@ -240,24 +240,24 @@ public class AlignmentPanel extends GAlignmentPanel * * @param results DOCUMENT ME! */ - public void highlightSearchResults(int[] results) + public void highlightSearchResults(SearchResults results) { seqPanel.seqCanvas.highlightSearchResults(results); // do we need to scroll the panel? if (results != null) { - SequenceI seq = av.alignment.getSequenceAt(results[0]); - int start = seq.findIndex(results[1]) - 1; - int end = seq.findIndex(results[2]) - 1; + SequenceI seq = results.getResultSequence(0); + int seqIndex = av.alignment.findIndex(seq); + int start = seq.findIndex(results.getResultStart(0)) - 1; + int end = seq.findIndex(results.getResultEnd(0)) - 1; if(!av.wrapAlignment) { - if ( (av.getStartRes() > start) || (av.getEndRes() < end) || - ( (av.getStartSeq() > results[0]) || - (av.getEndSeq() < results[0]))) + if ( (av.getStartRes() > end) || (av.getEndRes() < start) || + ( (av.getStartSeq() > seqIndex) || (av.getEndSeq() < seqIndex))) { - setScrollValues(start, results[0]); + setScrollValues(start, seqIndex); } } else @@ -266,6 +266,7 @@ public class AlignmentPanel extends GAlignmentPanel if( start(av.getStartRes()+cwidth) ) { vscroll.setValue(start / cwidth); + av.startRes = vscroll.getValue() * cwidth; } } } diff --git a/src/jalview/gui/Finder.java b/src/jalview/gui/Finder.java index ff4fe60..5f215ed 100755 --- a/src/jalview/gui/Finder.java +++ b/src/jalview/gui/Finder.java @@ -43,10 +43,11 @@ public class Finder extends GFinder AlignmentPanel ap; JInternalFrame frame; SuperGroup searchGroup; - Vector searchResults; int seqIndex = 0; int resIndex = 0; + SearchResults searchResults; + /** * Creates a new Finder object. * @@ -74,16 +75,6 @@ public class Finder extends GFinder } }); } - - /** - * DOCUMENT ME! - * - * @param evt DOCUMENT ME! - */ - public void internalFrameClosing(InternalFrameEvent evt) - { - cancel_actionPerformed(null); - } }); } @@ -110,25 +101,6 @@ public class Finder extends GFinder doSearch(true); } - /** - * DOCUMENT ME! - * - * @param e DOCUMENT ME! - */ - public void cancel_actionPerformed(ActionEvent e) - { - try - { - // if allResults is null, this effectively switches displaySearch flag in seqCanvas - ap.highlightSearchResults(null); - ap.idPanel.highlightSearchResults(null); - - // frame.setClosed(true); - } - catch (Exception ex) - { - } - } /** * DOCUMENT ME! @@ -149,16 +121,13 @@ public class Finder extends GFinder searchGroup = new SuperGroup(searchString, ucs, true, true, false); - for (int i = 0; i < searchResults.size(); i += 3) + for (int i = 0; i < searchResults.getSize(); i ++ ) { // its possible edits may have occurred since search was performed // account for this here - SequenceI seq = av.alignment.getSequenceAt(Integer.parseInt( - searchResults.elementAt(i).toString())); - int startRes = seq.findIndex(Integer.parseInt( - searchResults.elementAt(i + 1).toString())) - 1; - int endRes = seq.findIndex(Integer.parseInt( - searchResults.elementAt(i + 2).toString())) - 1; + SequenceI seq = searchResults.getResultSequence(i); + int startRes = seq.findIndex(searchResults.getResultStart(i)) - 1; + int endRes = seq.findIndex(searchResults.getResultEnd(i)) - 1; SequenceGroup sg = new SequenceGroup(searchString, ucs, true, true, false, startRes, endRes); @@ -186,9 +155,7 @@ public class Finder extends GFinder com.stevesoft.pat.Regex regex = new com.stevesoft.pat.Regex(searchString); - searchResults = new Vector(); - - int[] allResults = null; + searchResults = new SearchResults(); Sequence seq; String item = null; @@ -208,9 +175,7 @@ public class Finder extends GFinder seq = (Sequence) (av.getSelectionGroup().getSequenceAt(0)); } - searchResults.add(Integer.toString(av.getAlignment().findIndex(seq))); - searchResults.add(res+""); - searchResults.add(res+""); + searchResults.addResult(seq, res, res); } catch (NumberFormatException ex) { @@ -256,7 +221,7 @@ public class Finder extends GFinder item = seq.getSequence().toUpperCase(); if ((selection != null) && - (selection.getEndRes() < av.alignment.getWidth())) + (selection.getEndRes() < av.alignment.getWidth()-1)) { item = item.substring(0, selection.getEndRes() + 1); } @@ -295,7 +260,6 @@ public class Finder extends GFinder continue; } - searchResults.add(Integer.toString(seqIndex)); int sres = seq.findPosition(resIndex + Integer.parseInt(spaces.elementAt(resIndex) @@ -304,8 +268,7 @@ public class Finder extends GFinder Integer.parseInt(spaces.elementAt(regex.matchedTo() - 1).toString())); - searchResults.addElement(sres + ""); - searchResults.addElement(eres + ""); + searchResults.addResult(seq, sres, eres); if (!findAll) { @@ -341,23 +304,23 @@ public class Finder extends GFinder } } - if ((searchResults.size() == 0) && (idMatch.size() > 0)) + if ((searchResults.getSize() == 0) && (idMatch.size() > 0)) { ap.idPanel.highlightSearchResults(idMatch); } - if (searchResults.size() > 0) - { - allResults = new int[searchResults.size()]; - for (int i = 0; i < searchResults.size(); i++) - { - allResults[i] = Integer.parseInt(searchResults.get(i).toString()); - } + int resultSize = searchResults.getSize(); - createNewGroup.setEnabled(true); - } + if (searchResults.getSize() > 0) + createNewGroup.setEnabled(true); else + searchResults = null; + + // if allResults is null, this effectively switches displaySearch flag in seqCanvas + ap.highlightSearchResults(searchResults); + + if(!findAll && resultSize==0) { JOptionPane.showInternalMessageDialog(this, "Finished searching", null, JOptionPane.INFORMATION_MESSAGE); @@ -365,14 +328,12 @@ public class Finder extends GFinder seqIndex = 0; } - // if allResults is null, this effectively switches displaySearch flag in seqCanvas - ap.highlightSearchResults(allResults); - if (findAll) { - String message = (searchResults.size() / 3) + " matches found."; - JOptionPane.showInternalMessageDialog(this, message, null, - JOptionPane.INFORMATION_MESSAGE); + String message = resultSize + " matches found."; + JOptionPane.showInternalMessageDialog(this, message, null, + JOptionPane.INFORMATION_MESSAGE); } + } } diff --git a/src/jalview/gui/SeqCanvas.java b/src/jalview/gui/SeqCanvas.java index 7dbb8d3..a9f2329 100755 --- a/src/jalview/gui/SeqCanvas.java +++ b/src/jalview/gui/SeqCanvas.java @@ -41,8 +41,7 @@ public class SeqCanvas extends JComponent int imgWidth; int imgHeight; AlignViewport av; - boolean displaySearch = false; - int[] searchResults = null; + SearchResults searchResults = null; boolean fastPaint = false; int LABEL_WEST; int LABEL_EAST; @@ -620,7 +619,23 @@ public class SeqCanvas extends JComponent offset + ((i - starty) * av.charHeight), av.charWidth, av.charHeight); } - } + + /// Highlight search Results once all sequences have been drawn + ////////////////////////////////////////////////////////// + if (searchResults != null) + { + int[] visibleResults = searchResults.getResults(nextSeq, x1, x2); + if (visibleResults != null) + for (int r = 0; r < visibleResults.length; r += 2) + { + sr.drawHighlightedText(nextSeq, visibleResults[r], + visibleResults[r + 1], + (visibleResults[r] - startx) * av.charWidth, + offset + ( (i - starty) * av.charHeight), + av.charWidth, av.charHeight); + } + } + } // ///////////////////////////////////// @@ -784,41 +799,6 @@ public class SeqCanvas extends JComponent } while (groupIndex < av.alignment.getGroups().size()); } - - /// Highlight search Results once all sequences have been drawn - ////////////////////////////////////////////////////////// - if (displaySearch) - { - for (int r = 0; r < searchResults.length; r += 3) - { - int searchSeq = searchResults[r]; - - if ((searchSeq >= y1) && (searchSeq < y2)) - { - SequenceI seq = av.getAlignment().getSequenceAt(searchSeq); - - int searchStart = seq.findIndex(searchResults[r + 1]) - 1; - int searchEnd = seq.findIndex(searchResults[r + 2]) - 1; - - SequenceRenderer ssr = (SequenceRenderer) sr; - - if (searchStart < x1) - { - searchStart = x1; - } - - if (searchEnd > x2) - { - searchEnd = x2; - } - - ssr.drawHighlightedText(seq, searchStart, searchEnd, - (searchStart - startx) * av.charWidth, - offset + ((searchSeq - starty) * av.charHeight), - av.charWidth, av.charHeight); - } - } - } } /** @@ -826,17 +806,9 @@ public class SeqCanvas extends JComponent * * @param results DOCUMENT ME! */ - public void highlightSearchResults(int[] results) + public void highlightSearchResults(SearchResults results) { - // results are in the order sequence, startRes, endRes - if (results == null) - { - displaySearch = false; - } - else - { - displaySearch = true; - } + img = null; searchResults = results;