我有一個需要上下文相關幫助的Java桌面應用程序。 例如,我的Validator應用程序以SQL語句的形式獲取輸入,並基於輸入內容顯示可能的上下文相關幫助。如何在Java應用程序中創建上下文相關幫助
一個用例可能是:當用戶鍵入「更新」,然後按下Ctrl +空格鍵它應該顯示錶名的列表,上下文敏感的幫助
我怎麼去嗎?
我有一個需要上下文相關幫助的Java桌面應用程序。 例如,我的Validator應用程序以SQL語句的形式獲取輸入,並基於輸入內容顯示可能的上下文相關幫助。如何在Java應用程序中創建上下文相關幫助
一個用例可能是:當用戶鍵入「更新」,然後按下Ctrl +空格鍵它應該顯示錶名的列表,上下文敏感的幫助
我怎麼去嗎?
創建一個eclipse RCP應用程序並擴展eclipse編輯器。
使用eclipse非常簡單,創建一個編輯器並添加一個內容輔助。請看FAQ。
我不知道我是否完全理解你的問題。我猜你正在揮揮手。您是否在尋找那些AWT盤旋狀的東西(抱歉,這是aweful,我不太記得它叫什麼),像谷歌,當你輸入一個搜索有一個?
我不知道我是否可以幫你,但這裏是我用來匹配基於任何使用反射他們的屬性的值的搜索詞對象的方法。這可能是一些你並不需要在所有的,但我想以防萬一,我可以給你。希望能幫助到你!
/**
* 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;
}
}
它被稱爲工具提示。 – alphazero 2012-04-22 17:22:37
@alphazero不,那不是我想到的。我正在考慮在您輸入時彈出並過濾的列表,例如Google即搜即得。 – kentcdodds 2012-04-22 19:11:40
您的意思是自動完成/輸入提示或上下文相關幫助嗎?後一種情況是,您可以根據您打開的界面的哪個對話框/部分訪問幫助文件的相關部分。 – 2012-04-17 16:25:17
如果這是一個基於Swing應用程序,窺視[玻璃面板(http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html)。 – alphazero 2012-04-22 19:27:34