简体   繁体   English

如何在 Java 应用程序中创建上下文相关帮助

[英]How to create a context sensitive help in a Java application

I have a Java Desktop application which requires a context sensitive help.我有一个 Java 桌面应用程序,它需要上下文相关的帮助。 For example, my Validator application gets the input as SQL statements and based on what is typed it should show possible context sensitive help.例如,我的 Validator 应用程序将输入作为 SQL 语句,并且根据键入的内容,它应该显示可能的上下文相关帮助。

One use case could be: when the user types "update" and then presses Ctrl+Space it should show the list of table names as context sensitive help一个用例可能是:当用户键入“更新”然后按 Ctrl+Space 时,它应该将表名列表显示为上下文相关帮助

How do I go for this?我如何为此拨打 go?

Create an eclipse RCP application and extend the eclipse editor.创建一个 eclipse RCP 应用程序并扩展 eclipse 编辑器。

With eclipse is very simple create an editor and add a content assist to it.使用 eclipse 非常简单地创建一个编辑器并向其添加内容辅助。

Look at this FAQ .查看此常见问题解答

I don't know if I totally understand your question.我不知道我是否完全理解你的问题。 I'm guessing you're doing this in swing. Are you looking for one of those awt hover-like things (sorry, that's aweful, I just can't quite remember what it's called) like Google has when you're typing a search?我猜你是在 swing 中这样做的。你是在寻找那些类似 awt 悬停的东西之一吗(抱歉,那太棒了,我只是不太记得它叫什么)就像谷歌在你输入搜索?

I'm not sure whether I can help you with that, but here's a method I used to match objects against a search term based on any of their attributes' values using reflection.我不确定我是否可以帮助你,但这里有一种方法,我使用反射根据对象的任何属性值将对象与搜索词进行匹配。 This may be something you don't need at all, but I thought just in case I could give it to you.这可能是你根本不需要的东西,但我想以防万一我可以把它给你。 Hope it helps!希望能帮助到你!

  /**
  * Returns true if any attribute in the item matches the given constraints
  *
  * @param object the object you want to match
  * @param klass the class to get the fields from (in most cases you'll just call object.getClass())
  * @param iterations how many inherited levels you want to check fields for
  * @param match the String to match fields against
  * @param ignoreField fieldNames you wish to ignore, you can give as many as you like, you can also give an
  * array of strings
  * @return whether the given object contained fields which matched the given string
  */
  public static boolean matches(Object object, Class klass, int iterations, String match, String... ignoreField) {
    if (iterations < 0) {
      return false;
    }
    boolean checkMatch = false;
    try {
      checkMatch = matchFields(klass.getDeclaredFields(), object, match, ignoreField);
    } catch (IllegalArgumentException ex) {
      Logger.getLogger(OtherHelper.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
      Logger.getLogger(OtherHelper.class.getName()).log(Level.SEVERE, null, ex);
    }
    if (checkMatch) {
      return true;
    } else {
      Class<? extends Object> supersSuperclass = (Class<? extends Object>) klass.getSuperclass();
      if (supersSuperclass != Object.class) {
        boolean matches = matches(object, supersSuperclass, (iterations - 1), match, ignoreField);
        if (matches) {
          return true;
        } else {
          return false;
        }
      } else {
        return false;
      }
    }
  }

  /**
  * Calls matchField(field, bo, match) on each field in the given field array.
  *
  * @param fields the fields array to get the fields from
  * @param object the object to get the field values from
  * @param match the text to match fields to
  * @param ignoreField any number of fieldNames which are to be ignored.
  * @return true on first true field match
  * @throws IllegalArgumentException
  * @throws IllegalArgumentException
  * @throws IllegalAccessException
  */
  private static boolean matchFields(Field[] fields, Object object, String match, String... ignoreField) throws IllegalArgumentException, IllegalArgumentException, IllegalAccessException {
    List<String> ignoreFieldsList = Arrays.asList(ignoreField);
    for (Field field : fields) {
      if (!ignoreFieldsList.contains(field.getName())) {
        if (matchField(field, object, match)) {
          return true;
        }
      }
    }
    return false;
  }

  /**
  * Gets the value of the field and matches the string version of it with the given match
  *
  * @param field the field to match
  * @param object the object to get the field value from
  * @param match the match to match the field value to.
  * @return
  * @throws IllegalArgumentException
  * @throws IllegalArgumentException
  * @throws IllegalAccessException
  */
  private static boolean matchField(Field field, Object object, String match) throws IllegalArgumentException, IllegalArgumentException, IllegalAccessException {
    field.setAccessible(true);
    if (field.get(object) == null) {
      return false;
    }
    Class<?> type = field.getType();
    String value = null;
    if (type == Date.class) {
      SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy");
      Date date = (Date) field.get(object);
      value = sdf.format(date);
    } else if (type == String.class || isPrimitive(type)) {
      value = field.get(object).toString();
    }
    if (value != null
            && Pattern.compile(Pattern.quote(match), Pattern.CASE_INSENSITIVE).matcher(value).find()) {
      return true;
    } else {
      return false;
    }
  }

  /**
  * Checks first whether it is primitive and then whether it's wrapper is a primitive wrapper. Returns true
  * if either is true
  *
  * @param c
  * @return whether it's a primitive type itself or it's a wrapper for a primitive type
  */
  public static boolean isPrimitive(Class c) {
    if (c.isPrimitive()) {
      return true;
    } else if (c == Byte.class
            || c == Short.class
            || c == Integer.class
            || c == Long.class
            || c == Float.class
            || c == Double.class
            || c == Boolean.class
            || c == Character.class) {
      return true;
    } else {
      return false;
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM