alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  
*/ return MessageFormat.format( bundle.getString("TEXT_DETAIL_FMT_NAME1"), //NOI18N new Object[] {Integer.toString(line), Integer.toString(col)}); } else { /* position */ return MessageFormat.format( bundle.getString("TEXT_DETAIL_FMT_NAME2"), //NOI18N new Object[] {Integer.toString(line)}); } } /** * Returns short description of a visual representation of * a TextDetail. The description may be used e.g. * for a tooltip text of a node. * * @param det detailed information about location of a matching string * @return short description of a visual representation */ private static String getShortDesc(TextDetail det) { int line = det.getLine(); int col = det.getColumn(); if (col > 0) { /* line , column */ return MessageFormat.format( NbBundle.getBundle(FullTextType.class) .getString("TEXT_DETAIL_FMT_SHORT1"), //NOI18N new Object[] {Integer.toString(line), Integer.toString(col)}); } else { /* line */ return MessageFormat.format( NbBundle.getBundle(FullTextType.class) .getString("TEXT_DETAIL_FMT_SHORT2"), //NOI18N new Object[] {Integer.toString(line)}); } } /** * Returns full description of a visual representation of * a TextDetail. The description may be printed e.g. to * an OutputWindow. * * @param det detailed information about location of a matching string * @return full description of a visual representation */ private static String getFullDesc(TextDetail det) { String filename = det.getDataObject().getPrimaryFile().getNameExt(); String lineText = det.getLineText(); int line = det.getLine(); int col = det.getColumn(); if (col > 0) { /* [ at line , column ] */ return MessageFormat.format( NbBundle.getBundle(FullTextType.class) .getString("TEXT_DETAIL_FMT_FULL1"), //NOI18N new Object[] {lineText, filename, Integer.toString(line), Integer.toString(col)}); } else { /* [ line ] */ return MessageFormat.format( NbBundle.getBundle(FullTextType.class) .getString("TEXT_DETAIL_FMT_FULL2"), //NOI18N new Object[] {lineText, filename, Integer.toString(line)}); } } } // End of DetailNode class. /** * This action displays the matching string in a text editor. * This action is to be used in the window/dialog displaying a list of * found occurences of strings matching a search pattern. */ private static class GotoDetailAction extends NodeAction { /** */ public String getName() { return NbBundle.getBundle(FullTextType.class).getString("LBL_GotoDetailAction"); } /** */ public HelpCtx getHelpCtx() { return new HelpCtx(GotoDetailAction.class); } /** * @return true if at least one node is activated and * the first node is an instance of DetailNode * (or its subclass), false otherwise */ protected boolean enable(Node[] activatedNodes) { return activatedNodes != null && activatedNodes.length != 0 && activatedNodes[0] instanceof DetailNode; } /** * Displays the matching string in a text editor. * Works only if condition specified in method {@link #enable} is met, * otherwise does nothing. */ protected void performAction(Node[] activatedNodes) { if (enable(activatedNodes)) { ((DetailNode)activatedNodes[0]).gotoDetail(); } } /** */ protected boolean asynchronous() { return false; } } // End of GotoDetailAction class. /** Action on detail node which shows the text occurence. */ private static class ShowDetailAction extends NodeAction { /** Gets name of the action. Implements superclass abstract method. */ public String getName() { return NbBundle.getBundle(FullTextType.class).getString("LBL_ShowDetailAction"); } /** Gets help context fot this action. Implements superclass abstract method. */ public HelpCtx getHelpCtx() { return new HelpCtx(ShowDetailAction.class); } /** Tests wheter this action should be enables on specified activated nodes. * Implements superclass abstract method. */ protected boolean enable(Node[] activatedNodes) { if(activatedNodes == null || activatedNodes.length == 0) return false; if(activatedNodes[0] instanceof DetailNode) return true; return false; } /** Performs action on specified activated nodes. Implements superclass * abstract method. */ protected void performAction(Node[] activatedNodes) { if(activatedNodes == null || activatedNodes.length == 0) return; if(activatedNodes[0] instanceof DetailNode) ((DetailNode)activatedNodes[0]).showDetail(); } /** */ protected boolean asynchronous() { return false; } } // End of ShowDetailAction class. /** Gets help context for this search type. * Implements superclass abstract method. */ public HelpCtx getHelpCtx() { return new HelpCtx(FullTextType.class); } }

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */


package org.netbeans.modules.search.types;


import java.io.*;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.regex.Matcher;

import org.netbeans.modules.search.SearchDisplayer;

import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObject;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.actions.NodeAction;
import org.openide.util.actions.SystemAction;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.windows.OutputEvent;
import org.openide.windows.OutputListener;
import org.openide.xml.XMLUtil;


/**
 * Test DataObject primaryFile for line full-text match.
 *
 * @author  Petr Kuzel
 * @author  Marian Petras
 */
public class FullTextType extends TextType {

    private static final long serialVersionUID = 1L;
    //private static final long serialVersionUID = -8671182156199506714L;

    /**
     * Holds information about occurences of matching strings within individual
     * DataObjects.
     * 

* Contains mappings *

* (DataObject) -> (list of occurences of matchings strings * withing the DataObject) *
*

*/ private transient Map detailsMap; /** * Maximum number of matches reported for one line. * It eliminates huge memory consumption for single letter searches on long lines. */ private static final int MAX_REPORTED_OCCURENCES_ON_LINE = 5; /** * Maximum number of matches reported for one file. * It eliminates huge memory consumption for single letter searches in long files. */ private static final int MAX_REPORTED_OCCURENCES_IN_FILE = 200; /** */ public Object clone() { FullTextType clone = (FullTextType) super.clone(); clone.detailsMap = new HashMap(20); return clone; } /** */ public void destroy() { if (detailsMap != null) { detailsMap.clear(); } } /** Gets details map. */ private Map getDetailsMap() { if (detailsMap != null) { return detailsMap; } synchronized(this) { if (detailsMap == null) { detailsMap = new HashMap(20); } } return detailsMap; } /** */ public boolean testDataObject(DataObject dobj) { InputStream is = null; LineNumberReader reader = null; try { String line = ""; //NOI18N // primary file of the DataObject FileObject fo = dobj.getPrimaryFile(); if (fo == null) { return false; } // primary file content is = fo.getInputStream(); reader = new LineNumberReader(new InputStreamReader(is)); ArrayList txtDetails = new ArrayList(5); int fileCount = 0; while (fileCount < MAX_REPORTED_OCCURENCES_IN_FILE) { line = reader.readLine(); if (line == null) { break; } if (matchString != null) { int lineNum = reader.getLineNumber(); // current line + 1 int markLen = matchString.length(); String stringToSearch = caseSensitive ? line : line.toUpperCase(); int i = matchString(stringToSearch, 0); int lineCount = 0; while (i >= 0 && lineCount < MAX_REPORTED_OCCURENCES_ON_LINE) { TextDetail det = new TextDetail(dobj); det.setLine(lineNum); det.setColumn(i + 1); // current column + 1 det.setLineText(line); det.setMarkLength(markLen); txtDetails.add(det); i = matchString(stringToSearch, i + 1); lineCount ++; fileCount++; } } else if (matchRE(line)) { // TODO detect all occurences as in above code TextDetail det = new TextDetail(dobj); det.setLine(reader.getLineNumber()); det.setLineText(line); Matcher matcher = getMatcher(); int start = matcher.start(); int len = matcher.end() - start; det.setColumn(start +1); det.setMarkLength(len); txtDetails.add(det); fileCount++; } } // TODO somehow notify user if MAX_REPORTED_OCCURENCES_IN_FILE or MAX_REPORTED_OCCURENCES_ON_LINE if (txtDetails.isEmpty()) { return false; } txtDetails.trimToSize(); getDetailsMap().put(dobj, txtDetails); return true; } catch (FileNotFoundException fnfe) { return false; } catch (IOException ioe) { org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ioe); return false; } finally { if (reader != null) { try { reader.close(); reader = null; is = null; //marks that the InputStream is closed } catch (IOException ex) { org.openide.ErrorManager.getDefault().notify( org.openide.ErrorManager.INFORMATIONAL, ex); } } if (is != null) { try { is.close(); is = null; } catch (IOException ex) { org.openide.ErrorManager.getDefault().notify( org.openide.ErrorManager.INFORMATIONAL, ex); } } } } /** * @param resultObject DataObject to create the nodes for * @return DetailNodes representing the matches, * or null if no matching string is known for the * specified object * @see DetailNode */ public Node[] getDetails(Object resultObject) { List details = (List)getDetailsMap().get(resultObject); if(details == null) return null; List detailNodes = new ArrayList(details.size()); for(Iterator it = details.iterator(); it.hasNext();) { TextDetail txtDetail = (TextDetail)it.next(); Node detailNode = new DetailNode(txtDetail); detailNodes.add(detailNode); } return (Node[])detailNodes.toArray(new Node[detailNodes.size()]); } /** * @param node representing a DataObject with matches * @return DetailNodes representing the matches, * or null if the specified node does not represent * a DataObject or if no matching string is known for * the specified object */ public Node[] getDetails(Node node) { DataObject dataObject = (DataObject)node.getCookie(DataObject.class); if(dataObject == null) return null; return getDetails(dataObject); } /** * Node that represents information about one occurence of a matching * string. * * @see TextDetail */ private static class DetailNode extends AbstractNode implements OutputListener { /** Detail to represent. */ private TextDetail txtDetail; /** * Constructs a node representing the specified information about * a matching string. * * @param txtDetail information to be represented by this node */ public DetailNode(TextDetail txtDetail) { super(Children.LEAF); this.txtDetail = txtDetail; setDefaultAction(SystemAction.get(GotoDetailAction.class)); setShortDescription(DetailNode.getShortDesc(txtDetail)); setValue(SearchDisplayer.ATTR_OUTPUT_LINE, DetailNode.getFullDesc(txtDetail)); } /** */ protected SystemAction[] createActions() { return new SystemAction[] { SystemAction.get(GotoDetailAction.class), SystemAction.get(ShowDetailAction.class) }; } /** */ public String getName() { return txtDetail.getLineText() + " [" + DetailNode.getName(txtDetail) + "]"; // NOI18N } public String getHtmlDisplayName() { String colored; if (txtDetail.getMarkLength() > 0 && txtDetail.getColumn() > 0) { try { StringBuffer bold = new StringBuffer(); String plain = txtDetail.getLineText(); int col0 = txtDetail.getColumn() -1; // base 0 bold.append(XMLUtil.toElementContent(plain.substring(0, col0))); // NOI18N bold.append(""); // NOi18N int end = col0 + txtDetail.getMarkLength(); bold.append(XMLUtil.toElementContent(plain.substring(col0, end))); bold.append(""); // NOi18N if (txtDetail.getLineText().length() > end) { bold.append(XMLUtil.toElementContent(plain.substring(end))); } colored = bold.toString(); } catch (CharConversionException ex) { return null; } } else { try { colored = XMLUtil.toElementContent( txtDetail.getLineText()); } catch (CharConversionException e) { return null; } } try { return colored + " [" + XMLUtil.toElementContent(DetailNode.getName(txtDetail)) + "]"; // NOI18N } catch (CharConversionException e) { return null; } } /** Displays the matching string in a text editor. */ private void gotoDetail() { txtDetail.showDetail(TextDetail.DH_GOTO); } /** Show the text occurence. */ private void showDetail() { txtDetail.showDetail(TextDetail.DH_SHOW); } /** Implements OutputListener interface method. */ public void outputLineSelected (OutputEvent evt) { txtDetail.showDetail(TextDetail.DH_SHOW); } /** Implements OutputListener interface method. */ public void outputLineAction (OutputEvent evt) { txtDetail.showDetail(TextDetail.DH_GOTO); } /** Implements OutputListener interface method. */ public void outputLineCleared (OutputEvent evt) { txtDetail.showDetail(TextDetail.DH_HIDE); } /** * Returns name of a node representing a TextDetail. * * @param det detailed information about location of a matching string * @return name for the node */ private static String getName(TextDetail det) { int line = det.getLine(); int col = det.getColumn(); ResourceBundle bundle = NbBundle.getBundle(FullTextType.class); if (col > 0) { /* position :
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.