X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fsymbollibrary%2Fui%2FDefaultFilterStrategy.java;h=3c9c58bc0ebc3c47822eb0be250d24260dc121be;hb=refs%2Fchanges%2F16%2F916%2F2;hp=480a9716c7a53b5057351a05a9a58d409e9ddb10;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/symbollibrary/ui/DefaultFilterStrategy.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/symbollibrary/ui/DefaultFilterStrategy.java index 480a9716c..3c9c58bc0 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/symbollibrary/ui/DefaultFilterStrategy.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/symbollibrary/ui/DefaultFilterStrategy.java @@ -1,167 +1,167 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.diagram.symbollibrary.ui; - -import java.nio.CharBuffer; - -/** - * Default implementation of IFilterStrategy. - * - *
- * It implements simple search semantics with only the special wildcard - * characters '*' ( 0 to n any characters) and '?' (any one character) - * recognized. In order to allow the filter to pass arbitrary prefixes, the - * client has to give a '*' prefix in the filter string. On the contrary, the - * client does not have to specify a '*' in order to pass arbitrary suffixes - - * arbitrary suffixes are allowed by default by this strategy. - * - *
- * This strategy forces the filter string to lowercase. - * - * TODO: this code is duplicated from org.simantics.browsing.ui.common.views since there was no good way of getting this same. Remove code duplication. - * - * @author Tuukka Lehtonen - */ -class DefaultFilterStrategy { - - private static final boolean DEBUG = false; - - private static StringBuilder addSearchWord(StringBuilder sb, String pattern) { - if (DEBUG) - System.out.println("addSearchWord(" + pattern + ") to '" + sb.toString() + "'"); - - if (pattern == null || pattern.isEmpty()) - return sb; - if (sb.length() > 0) - sb.append('|'); - sb.append('('); - sb.append(pattern); - sb.append(')'); - return sb; - } - - private static String toString(CharBuffer cb) { - cb.limit(cb.position()); - cb.reset(); - if (DEBUG) - System.out.println("toString(" + cb + ")"); - String result = cb.toString(); - cb.limit(cb.capacity()); - return result; - } - - public static String toSinglePatternString(String filter, boolean implicitPreAsterisk) { - if (!filter.isEmpty()) { - // Force searching in lowercase. - filter = filter.toLowerCase(); - - // Construct a regular expression from the specified text. - String regExFilter = filter - .replace("\\", "\\\\") // \ -> \\ - .replace(".", "\\.") // . -> \. - .replace("*", ".*") // * -> Any 0..n characters - .replace("?", ".") // ? -> Any single character - .replace("+", "\\+") // + -> \+ - .replace("(", "\\(") // ( -> \( - .replace(")", "\\)") // ) -> \) - .replace("[", "\\[") // [ -> \[ - .replace("]", "\\]") // ] -> \] - .replace("{", "\\{") // { -> \{ - .replace("}", "\\}") // } -> \} - .replace("^", "\\^") // ^ -> \^ - .replace("$", "\\$") // $ -> \$ - .replace("|", ".*|") // $ -> \$ - //.replace("|", "\\|") // | -> \| - .replace("&&", "\\&&") // && -> \&& - ; - - if (implicitPreAsterisk) - if (!regExFilter.startsWith(".*")) - regExFilter = ".*" + regExFilter ; - if (!regExFilter.endsWith(".*")) - regExFilter += ".*" ; - - return regExFilter; - } - return null; - } - - public static String defaultToPatternString(String filter, boolean implicitPreAsterisk) { - if (filter.isEmpty()) - return null; - - CharBuffer buf = CharBuffer.allocate(filter.length()*2); - buf.mark(); - StringBuilder sb = new StringBuilder(filter.length()*2); - boolean inQuote = false; - int len = filter.length(); - for (int i = 0; i < len;) { - char ch = filter.charAt(i); - if (DEBUG) - System.out.println("char[" + i + "]: '" + ch + "'"); - - if (ch == '"') { - if (!inQuote) { - if (DEBUG) - System.out.println("begin quoted text"); - inQuote = true; - } else { - if (DEBUG) - System.out.println("end quoted text"); - inQuote = false; - addSearchWord(sb, toSinglePatternString( toString(buf), implicitPreAsterisk )); - } - ++i; - continue; - } else if (ch == '\\') { - // Next character is escaped, i.e. taken as is. - ++i; - if (i >= len) - // Unexpected end-of-string - break; - - ch = filter.charAt(i); - if (DEBUG) - System.out.println("append escaped character '" + ch + "'"); - - buf.append(ch); - ++i; - break; - } else if (ch == ' ') { - if (inQuote) { - if (DEBUG) - System.out.println("append char '" + ch + "'"); - buf.append(ch); - ++i; - } else { - if (buf.position() > 0) { - addSearchWord(sb, toSinglePatternString( toString(buf), implicitPreAsterisk )); - } - ++i; - } - } else { - if (DEBUG) - System.out.println("append char '" + ch + "'"); - buf.append(ch); - ++i; - } - } - if (buf.position() > 0) { - addSearchWord(sb, toSinglePatternString( toString(buf), implicitPreAsterisk )); - } - - //sb.append(".*"); - - return sb.toString(); - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.diagram.symbollibrary.ui; + +import java.nio.CharBuffer; + +/** + * Default implementation of IFilterStrategy. + * + *
+ * It implements simple search semantics with only the special wildcard + * characters '*' ( 0 to n any characters) and '?' (any one character) + * recognized. In order to allow the filter to pass arbitrary prefixes, the + * client has to give a '*' prefix in the filter string. On the contrary, the + * client does not have to specify a '*' in order to pass arbitrary suffixes - + * arbitrary suffixes are allowed by default by this strategy. + * + *
+ * This strategy forces the filter string to lowercase. + * + * TODO: this code is duplicated from org.simantics.browsing.ui.common.views since there was no good way of getting this same. Remove code duplication. + * + * @author Tuukka Lehtonen + */ +class DefaultFilterStrategy { + + private static final boolean DEBUG = false; + + private static StringBuilder addSearchWord(StringBuilder sb, String pattern) { + if (DEBUG) + System.out.println("addSearchWord(" + pattern + ") to '" + sb.toString() + "'"); + + if (pattern == null || pattern.isEmpty()) + return sb; + if (sb.length() > 0) + sb.append('|'); + sb.append('('); + sb.append(pattern); + sb.append(')'); + return sb; + } + + private static String toString(CharBuffer cb) { + cb.limit(cb.position()); + cb.reset(); + if (DEBUG) + System.out.println("toString(" + cb + ")"); + String result = cb.toString(); + cb.limit(cb.capacity()); + return result; + } + + public static String toSinglePatternString(String filter, boolean implicitPreAsterisk) { + if (!filter.isEmpty()) { + // Force searching in lowercase. + filter = filter.toLowerCase(); + + // Construct a regular expression from the specified text. + String regExFilter = filter + .replace("\\", "\\\\") // \ -> \\ + .replace(".", "\\.") // . -> \. + .replace("*", ".*") // * -> Any 0..n characters + .replace("?", ".") // ? -> Any single character + .replace("+", "\\+") // + -> \+ + .replace("(", "\\(") // ( -> \( + .replace(")", "\\)") // ) -> \) + .replace("[", "\\[") // [ -> \[ + .replace("]", "\\]") // ] -> \] + .replace("{", "\\{") // { -> \{ + .replace("}", "\\}") // } -> \} + .replace("^", "\\^") // ^ -> \^ + .replace("$", "\\$") // $ -> \$ + .replace("|", ".*|") // $ -> \$ + //.replace("|", "\\|") // | -> \| + .replace("&&", "\\&&") // && -> \&& + ; + + if (implicitPreAsterisk) + if (!regExFilter.startsWith(".*")) + regExFilter = ".*" + regExFilter ; + if (!regExFilter.endsWith(".*")) + regExFilter += ".*" ; + + return regExFilter; + } + return null; + } + + public static String defaultToPatternString(String filter, boolean implicitPreAsterisk) { + if (filter.isEmpty()) + return null; + + CharBuffer buf = CharBuffer.allocate(filter.length()*2); + buf.mark(); + StringBuilder sb = new StringBuilder(filter.length()*2); + boolean inQuote = false; + int len = filter.length(); + for (int i = 0; i < len;) { + char ch = filter.charAt(i); + if (DEBUG) + System.out.println("char[" + i + "]: '" + ch + "'"); + + if (ch == '"') { + if (!inQuote) { + if (DEBUG) + System.out.println("begin quoted text"); + inQuote = true; + } else { + if (DEBUG) + System.out.println("end quoted text"); + inQuote = false; + addSearchWord(sb, toSinglePatternString( toString(buf), implicitPreAsterisk )); + } + ++i; + continue; + } else if (ch == '\\') { + // Next character is escaped, i.e. taken as is. + ++i; + if (i >= len) + // Unexpected end-of-string + break; + + ch = filter.charAt(i); + if (DEBUG) + System.out.println("append escaped character '" + ch + "'"); + + buf.append(ch); + ++i; + break; + } else if (ch == ' ') { + if (inQuote) { + if (DEBUG) + System.out.println("append char '" + ch + "'"); + buf.append(ch); + ++i; + } else { + if (buf.position() > 0) { + addSearchWord(sb, toSinglePatternString( toString(buf), implicitPreAsterisk )); + } + ++i; + } + } else { + if (DEBUG) + System.out.println("append char '" + ch + "'"); + buf.append(ch); + ++i; + } + } + if (buf.position() > 0) { + addSearchWord(sb, toSinglePatternString( toString(buf), implicitPreAsterisk )); + } + + //sb.append(".*"); + + return sb.toString(); + } + +}