📄 incrementalsearch.java
字号:
package TreeJuxtaposer;import AccordionTreeDrawer.*;import javax.swing.*;import java.awt.*;import java.util.*;import javax.swing.event.ListSelectionListener;import javax.swing.event.ListSelectionEvent;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;/** * @author jslack * * To change this generated comment edit the template variable "typecomment": * Window>Preferences>Java>Templates. * To enable and disable the creation of type comments go to * Window>Preferences>Java>Code Generation. */public class IncrementalSearch extends JFrame implements KeyListener, ListSelectionListener { private JScrollPane resultsPane; private JList searchResults; private JScrollPane statusPane; private JTextArea searchStatus; //private JComboBox results; private JTextField results; private Hashtable prefix; private TreeJuxtaposer tj; private int numSelected; private int numFound; private static final boolean boldTerminalNodes = false; private static final boolean boldNonTerminalNodes = true; // highlight search results found if under (or equal to) this threshold private static final int returnThreshold = 200; IncrementalSearch(TreeJuxtaposer tj) { this.tj = tj; doUI(); initializeList(null); results.setText(""); numSelected = 0; } IncrementalSearch(TreeJuxtaposer tj, ArrayList initialList) { this.tj = tj; doUI(); initializeList(initialList); results.setText(""); numSelected = 0; } private void doUI() { GridBagConstraints gbc; getContentPane().setLayout(new GridBagLayout()); gbc = new GridBagConstraints();// gbc.gridx = 0;// gbc.gridy = 0; results = new JTextField(); gbc = new GridBagConstraints(0,0,1,1,1.0,1.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0,0); results.setEditable(true); searchResults = new JList(); resultsPane = new JScrollPane(searchResults); resultsPane.setMinimumSize(new Dimension(250, 300)); resultsPane.setPreferredSize(new Dimension(250, 500)); resultsPane.setMaximumSize(new Dimension(1024, 1600)); searchStatus = new JTextArea(1, 23); searchStatus.setEditable(false); searchStatus.setBackground(this.getBackground()); searchStatus.setText("No nodes selected"); statusPane = new JScrollPane(searchStatus); getContentPane().add(resultsPane, gbc);// gbc.gridy = 1; gbc = new GridBagConstraints(0,1,1,1,1.0,0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0,0); results.setMinimumSize(new Dimension(250, 24)); results.setPreferredSize(new Dimension(250, 24)); results.setMaximumSize(new Dimension(1024, 24)); getContentPane().add(results, gbc);// gbc.gridy = 2; gbc = new GridBagConstraints(0,2,1,1,1.0,1.0, GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0,0); getContentPane().add(statusPane, gbc); results.addKeyListener(this);// this.setResizable(false); this.setResizable(true); resultsPane.revalidate(); initializeActions(); this.pack(); } private void initializeActions() { searchResults.addListSelectionListener(this); } /* * Initialize the nodes in the search list * nameList is an ArrayList of TreeNode */ public void initializeList(ArrayList nameList) { prefix = new Hashtable(); if (nameList == null) {// System.out.println("Remember to call IncrementalSearch.initializeList"); return; } else { prefix.put("", nameList); searchResults.setListData(nameList.toArray()); numFound = nameList.size(); } } private ArrayList recursiveSearch(String text) { if (text.length() == 0) { return (ArrayList)prefix.get(text); } String smaller = text.substring(0, text.length() - 1); ArrayList returnValue = (ArrayList)prefix.get(text); if (returnValue == null) { ArrayList temp = recursiveSearch(smaller); returnValue = new ArrayList(); int textLength = text.length(); for (int i = 0; i < temp.size(); i++) { String FQName = (String)temp.get(i); String tempString = tj.getLabelByFQName(FQName); boolean matchPrefixOnly = false; if (matchPrefixOnly) { if (tempString.length() >= textLength && tempString.toLowerCase().startsWith(text)) { returnValue.add(FQName); } } else { if (tempString == null) System.out.println("debug"); if (tempString.length() >= textLength && tempString.toLowerCase().lastIndexOf(text) != -1) { returnValue.add(FQName); // add TreeNode to returnValue } } } // add to the hashtable here //System.out.println(text + " @ " + returnValue); prefix.put(text, returnValue); } return returnValue; } private ArrayList partialResult() { ArrayList returnValue = null; String resultString = results.getText().toLowerCase(); returnValue = (ArrayList)prefix.get(resultString); if (returnValue == null) { returnValue = recursiveSearch(resultString); } return returnValue; } public void resetSearch() { searchResults.clearSelection(); Iterator tdIter = tj.treeDrawers.iterator(); while (tdIter.hasNext()) { AccordionTreeDrawerFinal atd = (AccordionTreeDrawerFinal)tdIter.next(); atd.changedMarks(); atd.requestRedraw(); } } public static void main(String[] args) { ArrayList testSet = new ArrayList(); testSet.add(new String("Nicotiana")); testSet.add(new String("Campanula")); testSet.add(new String("Scaevola")); testSet.add(new String("Dasyphyllum")); testSet.add(new String("Stokesia")); testSet.add(new String("Dimorphotheca")); testSet.add(new String("Senecio")); testSet.add(new String("Gerbera")); testSet.add(new String("Gazania")); testSet.add(new String("Echinops")); testSet.add(new String("Felicia")); testSet.add(new String("Tagetes")); testSet.add(new String("Chromolaena")); testSet.add(new String("Blennosperma")); testSet.add(new String("Coreopsis")); testSet.add(new String("Vernonia")); testSet.add(new String("Cacosmia")); testSet.add(new String("Cichorium")); testSet.add(new String("Achillea")); testSet.add(new String("Carthamnus")); testSet.add(new String("Flaveria")); testSet.add(new String("Piptocarpa")); testSet.add(new String("Helianthus")); testSet.add(new String("Tragopogon")); testSet.add(new String("Chrysanthemum")); testSet.add(new String("Eupatorium")); testSet.add(new String("Lactuca")); testSet.add(new String("Barnadesia")); IncrementalSearch is; is = new IncrementalSearch(null); is.show(); is.initializeList(testSet); } public void valueChanged(ListSelectionEvent evt) { int oldNumSelected = numSelected; Object selected[] = searchResults.getSelectedValues(); searchStatus.setText("Selected " + selected.length + " of " + numFound + " nodes"); if (tj != null && selected.length <= returnThreshold) { tj.clearGroup(TreeJuxtaposer.foundGroup); for (int i = 0; i < selected.length; i++) { Iterator tdIter = tj.treeDrawers.iterator(); while (tdIter.hasNext()) { AccordionTreeDrawerFinal atd = (AccordionTreeDrawerFinal)tdIter.next(); TreeNode n = atd.getNodeByName((String)selected[i]); if (n == null) continue; int key = n.getKey(); tj.addNodesToGroup(key, key, TreeJuxtaposer.foundGroup, atd); } } } if (selected.length > returnThreshold) tj.clearGroup(TreeJuxtaposer.foundGroup); RangeList foundGroupList = tj.getGroup(TreeJuxtaposer.foundGroup); tj.changedMarks(); tj.requestRedrawAll(); } public void keyReleased(KeyEvent evt) { int caretPos; caretPos = results.getCaretPosition(); ArrayList searchResultArray = partialResult(); String partialName = results.getText(); searchResults.setListData(searchResultArray.toArray()); searchStatus.setText("Search matched " + searchResultArray.size() + " nodes"); numFound = searchResultArray.size(); results.setText(partialName); results.setCaretPosition(caretPos); if (numFound < returnThreshold) { numSelected = numFound; searchResults.setSelectionInterval(0, numFound - 1); } else resetSearch(); } /* (non-Javadoc) * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent) */ public void keyTyped(KeyEvent e) {// int caretPos;// caretPos = results.getCaretPosition();// ArrayList searchResultArray = partialResult();// String partialName = results.getText();// searchResults.setListData(searchResultArray.toArray());// searchStatus.setText("Search matched " + searchResultArray.size() + " nodes");// numFound = searchResultArray.size();// results.setText(partialName);// results.setCaretPosition(caretPos);// // if (numFound < returnThreshold)// {// numSelected = numFound;// searchResults.setSelectionInterval(0, numFound - 1);// }// else// resetSearch(); } /* (non-Javadoc) * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent) */ public void keyPressed(KeyEvent e) { // ignore } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -