Browse Source

alphafine 异步搜索优化 中断

master
XiaXiang 8 years ago
parent
commit
1efe1eefca
  1. 10
      designer/src/com/fr/design/mainframe/alphafine/AlphaFineHelper.java
  2. 33
      designer/src/com/fr/design/mainframe/alphafine/component/AlphaFineDialog.java
  3. 69
      designer/src/com/fr/design/mainframe/alphafine/search/manager/AlphaSearchManager.java
  4. 13
      designer/src/com/fr/design/mainframe/alphafine/search/manager/DocumentSearchManager.java
  5. 4
      designer/src/com/fr/design/mainframe/alphafine/search/manager/PluginSearchManager.java
  6. 4
      designer/src/com/fr/design/mainframe/alphafine/search/manager/RecentSearchManager.java
  7. 2
      designer/src/com/fr/design/mainframe/alphafine/search/manager/RecommendSearchManager.java

10
designer/src/com/fr/design/mainframe/alphafine/AlphaFineHelper.java

@ -5,6 +5,7 @@ import com.fr.design.mainframe.alphafine.cell.model.NoResultModel;
import com.fr.design.mainframe.alphafine.component.AlphaFineDialog;
import com.fr.design.mainframe.alphafine.model.SearchResult;
import com.fr.general.Inter;
import com.fr.general.ProcessCanceledException;
import com.fr.stable.StringUtils;
/**
@ -57,6 +58,15 @@ public class AlphaFineHelper {
return getSplitText(text, 1);
}
/**
* 中断当前线程的搜索
*/
public static void checkCancel() {
if (Thread.interrupted()) {
throw new ProcessCanceledException();
}
}

33
designer/src/com/fr/design/mainframe/alphafine/component/AlphaFineDialog.java

@ -2,7 +2,6 @@ package com.fr.design.mainframe.alphafine.component;
import com.fr.base.FRContext;
import com.fr.design.DesignerEnvManager;
import com.fr.design.actions.help.alphafine.AlphafineContext;
import com.fr.design.dialog.UIDialog;
import com.fr.design.gui.ibutton.UIButton;
import com.fr.design.gui.icontainer.UIScrollPane;
@ -93,6 +92,7 @@ public class AlphaFineDialog extends UIDialog {
if (waitingForSearch) {
long currentTime = System.currentTimeMillis();
if (currentTime - lastUpdateTime > 1000) {
System.out.print(searchTextField.getText() + "............\n");
doSearch(searchTextField.getText());
waitingForSearch = false;
}
@ -144,6 +144,8 @@ public class AlphaFineDialog extends UIDialog {
searchTextField.getDocument().addDocumentListener(new DocumentAdapter() {
@Override
protected void textChanged(DocumentEvent e) {
// lastUpdateTime = System.currentTimeMillis();
// waitingForSearch = true;
doSearch(searchTextField.getText());
}
});
@ -275,8 +277,11 @@ public class AlphaFineDialog extends UIDialog {
this.searchWorker.execute();
}
private synchronized void rebuildList(String searchText) {
System.out.print("background===" + searchText + "\n");
/**
* 重新构建搜索结果列表
* @param searchText
*/
private void rebuildList(String searchText) {
searchListModel.removeAllElements();
getRecentList(searchText);
getRecommendList(searchText);
@ -286,20 +291,15 @@ public class AlphaFineDialog extends UIDialog {
getPluginList(searchText);
}
private void checkCancel() {
if (Thread.interrupted()) {
throw new ProcessCanceledException();
}
}
private synchronized void getDocumentList(final String searchText) {
SearchResult documentModelList = DocumentSearchManager.getDocumentSearchManager().getLessSearchResult(searchText);
checkCancel();
for (Object object : documentModelList) {
AlphaFineHelper.checkCancel();
searchListModel.addElement(object);
}
System.out.print("document" + "-----" + searchText + "\n");
@ -309,41 +309,42 @@ public class AlphaFineDialog extends UIDialog {
private synchronized void getFileList(final String searchText) {
SearchResult fileModelList = FileSearchManager.getFileSearchManager().getLessSearchResult(searchText);
for (Object object : fileModelList) {
AlphaFineHelper.checkCancel();
searchListModel.addElement(object);
}
System.out.print("file" + "-----" + searchText + "\n");
}
private synchronized void getActionList(final String searchText) {
SearchResult actionModelList = ActionSearchManager.getActionSearchManager().getLessSearchResult(searchText);
for (Object object : actionModelList) {
AlphaFineHelper.checkCancel();
searchListModel.addElement(object);
}
System.out.print("action" + "-----" + searchText + "\n");
}
private synchronized void getPluginList(final String searchText) {
SearchResult pluginModelList = PluginSearchManager.getPluginSearchManager().getLessSearchResult(searchText);
for (Object object : pluginModelList) {
AlphaFineHelper.checkCancel();
searchListModel.addElement(object);
}
System.out.print("plugin" + "-----" + searchText + "\n");
}
private synchronized void getRecommendList(final String searchText) {
SearchResult recommendModelList = RecommendSearchManager.getRecommendSearchManager().getLessSearchResult(searchText);
for (Object object : recommendModelList) {
AlphaFineHelper.checkCancel();
searchListModel.addElement(object);
}
System.out.print("recommend" + "-----" + searchText + "\n");
}
private synchronized void getRecentList(final String searchText) {
SearchResult recentModelList = RecentSearchManager.getRecentSearchManger().getLessSearchResult(searchText);
for (Object object : recentModelList) {
AlphaFineHelper.checkCancel();
searchListModel.addElement(object);
}
System.out.print("recent" + "-----" + searchText + "\n");
}

69
designer/src/com/fr/design/mainframe/alphafine/search/manager/AlphaSearchManager.java

@ -1,69 +0,0 @@
package com.fr.design.mainframe.alphafine.search.manager;
import com.fr.design.mainframe.alphafine.cell.model.MoreModel;
import com.fr.design.mainframe.alphafine.model.SearchResult;
import com.fr.general.Inter;
/**
* Created by XiaXiang on 2017/3/28.
*/
public class AlphaSearchManager implements AlphaFineSearchProcessor {
private static AlphaSearchManager searchManager;
private static PluginSearchManager pluginSearchManager;
private static DocumentSearchManager documentSearchManager;
private static FileSearchManager fileSearchManager;
private static ActionSearchManager actionSearchManager;
private static RecommendSearchManager recommendSearchManager;
private static RecentSearchManager recentSearchManager;
public synchronized static AlphaSearchManager getSearchManager() {
init();
return searchManager;
}
private synchronized static void init() {
if (searchManager == null) {
searchManager = new AlphaSearchManager();
pluginSearchManager = PluginSearchManager.getPluginSearchManager();
documentSearchManager = DocumentSearchManager.getDocumentSearchManager();
fileSearchManager = FileSearchManager.getFileSearchManager();
actionSearchManager = ActionSearchManager.getActionSearchManager();
recommendSearchManager = RecommendSearchManager.getRecommendSearchManager();
recentSearchManager = RecentSearchManager.getRecentSearchManger();
}
}
@Override
public synchronized SearchResult getLessSearchResult(String searchText) {
SearchResult recentModelList = recentSearchManager.getLessSearchResult(searchText);
SearchResult recommendModelList = recommendSearchManager.getLessSearchResult(searchText);
SearchResult actionModelList = actionSearchManager.getLessSearchResult(searchText);
SearchResult fileModelList = fileSearchManager.getLessSearchResult(searchText);
SearchResult documentModelList = documentSearchManager.getLessSearchResult(searchText);
SearchResult pluginModelList = pluginSearchManager.getLessSearchResult(searchText);
recentModelList.addAll(recommendModelList);
recentModelList.addAll(actionModelList);
recentModelList.addAll(fileModelList);
recentModelList.addAll(documentModelList);
recentModelList.addAll(pluginModelList);
return recentModelList;
}
public SearchResult showDefaultSearchResult() {
SearchResult searchResult = new SearchResult();
searchResult.add(new MoreModel(Inter.getLocText("FR-Designer_AlphaFine_Latest")));
searchResult.add(new MoreModel(Inter.getLocText("FR-Designer_AlphaFine_Conclude")));
searchResult.add(new MoreModel(Inter.getLocText("FR-Designer_Set")));
searchResult.add(new MoreModel(Inter.getLocText("FR-Designer_Templates")));
searchResult.add(new MoreModel(Inter.getLocText("FR-Designer_COMMUNITY_HELP")));
searchResult.add(new MoreModel(Inter.getLocText("FR-Designer-Plugin_Addon")));
return searchResult;
}
@Override
public SearchResult getMoreSearchResult() {
return null;
}
}

13
designer/src/com/fr/design/mainframe/alphafine/search/manager/DocumentSearchManager.java

@ -47,27 +47,20 @@ public class DocumentSearchManager implements AlphaFineSearchProcessor {
if (!httpClient.isServerAlive()) {
return getNoConnectList();
}
if (Thread.interrupted()) {
System.out.print(searchText + "--------崩了1111\n");
throw new ProcessCanceledException();
}
result = httpClient.getResponseText();
if (Thread.interrupted()) {
System.out.print(searchText + "--------崩了22222\n");
throw new ProcessCanceledException();
}
AlphaFineHelper.checkCancel();
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.optJSONArray("docdata");
if (jsonArray != null) {
final int length = Math.min(AlphaFineConstants.SHOW_SIZE, jsonArray.length());
for (int i = 0; i < length; i++) {
AlphaFineHelper.checkCancel();
DocumentModel cellModel = getModelFromCloud(jsonArray.optJSONObject(i));
this.lessModelList.add(cellModel);
}
for (int i = length; i < jsonArray.length(); i++) {
AlphaFineHelper.checkCancel();
DocumentModel cellModel = getModelFromCloud(jsonArray.optJSONObject(i));
this.moreModelList.add(cellModel);
}

4
designer/src/com/fr/design/mainframe/alphafine/search/manager/PluginSearchManager.java

@ -13,6 +13,7 @@ import com.fr.design.mainframe.alphafine.model.SearchResult;
import com.fr.general.ComparatorUtils;
import com.fr.general.FRLogger;
import com.fr.general.Inter;
import com.fr.general.ProcessCanceledException;
import com.fr.general.http.HttpClient;
import com.fr.json.JSONArray;
import com.fr.json.JSONObject;
@ -57,15 +58,18 @@ public class PluginSearchManager implements AlphaFineSearchProcessor {
return getNoConnectList();
}
result = httpClient.getResponseText();
AlphaFineHelper.checkCancel();
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject.optJSONArray("result");
if (jsonArray != null) {
int length = Math.min(AlphaFineConstants.SHOW_SIZE, jsonArray.length());
for (int i = 0; i < length; i++) {
AlphaFineHelper.checkCancel();
PluginModel cellModel = getPluginModel(jsonArray.optJSONObject(i), false);
this.lessModelList.add(cellModel);
}
for (int i = length; i < jsonArray.length(); i++) {
AlphaFineHelper.checkCancel();
PluginModel cellModel = getPluginModel(jsonArray.optJSONObject(i), false);
this.moreModelList.add(cellModel);
}

4
designer/src/com/fr/design/mainframe/alphafine/search/manager/RecentSearchManager.java

@ -3,6 +3,7 @@ package com.fr.design.mainframe.alphafine.search.manager;
import com.fr.base.FRContext;
import com.fr.base.Utils;
import com.fr.design.mainframe.alphafine.AlphaFineConstants;
import com.fr.design.mainframe.alphafine.AlphaFineHelper;
import com.fr.design.mainframe.alphafine.CellType;
import com.fr.design.mainframe.alphafine.cell.model.AlphaCellModel;
import com.fr.design.mainframe.alphafine.cell.model.MoreModel;
@ -273,9 +274,10 @@ public class RecentSearchManager extends XMLFileManager implements AlphaFineSear
* @param searchText
* @return
*/
public List<AlphaCellModel> getRecentModelList(String searchText) {
public synchronized List<AlphaCellModel> getRecentModelList(String searchText) {
recentModelList = new ArrayList<>();
for (String key : recentKVModelMap.keySet()) {
AlphaFineHelper.checkCancel();
if (ComparatorUtils.equals(key, searchText)) {
recentModelList = recentKVModelMap.get(searchText);
int size = recentModelList.size();

2
designer/src/com/fr/design/mainframe/alphafine/search/manager/RecommendSearchManager.java

@ -41,12 +41,14 @@ public class RecommendSearchManager implements AlphaFineSearchProcessor {
return getNoConnectList();
}
result = httpClient.getResponseText();
AlphaFineHelper.checkCancel();
try {
JSONObject jsonObject = new JSONObject(result);
if (jsonObject.optString("status").equals("success")) {
JSONArray jsonArray = jsonObject.optJSONArray("result");
if (jsonArray != null && jsonArray.length() > 0) {
for (int i = 0; i < jsonArray.length(); i++) {
AlphaFineHelper.checkCancel();
AlphaCellModel alphaCellModel = CellModelHelper.getModelFromJson((JSONObject) jsonArray.get(i));
if (alphaCellModel != null && !RecentSearchManager.getRecentSearchManger().getRecentModelList().contains(alphaCellModel)) {
this.modelList.add(alphaCellModel);

Loading…
Cancel
Save