Java tutorial
/* Copyright (C) 2006 NTT DATA Corporation This program is free software; you can redistribute it and/or Modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */ package com.clustercontrol.composite; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.LinkedList; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.viewers.CheckStateChangedEvent; import org.eclipse.jface.viewers.CheckboxTreeViewer; import org.eclipse.jface.viewers.ICheckStateListener; import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.SelectionChangedEvent; import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.jface.viewers.TreePath; import org.eclipse.jface.viewers.TreeSelection; import org.eclipse.jface.viewers.TreeViewer; import org.eclipse.swt.SWT; import org.eclipse.swt.events.DisposeEvent; import org.eclipse.swt.events.DisposeListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.Tree; import com.clustercontrol.repository.bean.FacilityConstant; import com.clustercontrol.repository.bean.FacilityTreeAttributeConstant; import com.clustercontrol.repository.composite.ScopeTreeSearchBarComposite; import com.clustercontrol.repository.util.RepositoryEndpointWrapper; import com.clustercontrol.repository.util.ScopePropertyUtil; import com.clustercontrol.util.FacilityTreeCache; import com.clustercontrol.util.FacilityTreeItemUtil; import com.clustercontrol.util.HinemosMessage; import com.clustercontrol.util.Messages; import com.clustercontrol.util.WidgetTestUtil; import com.clustercontrol.viewer.CommonTableViewer; import com.clustercontrol.viewer.FacilityTreeContentProvider; import com.clustercontrol.viewer.FacilityTreeLabelProvider; import com.clustercontrol.viewer.FacilityTreeViewerSorter; import com.clustercontrol.ws.repository.FacilityInfo; import com.clustercontrol.ws.repository.FacilityTreeItem; /** * ??<BR> * * @version 1.0.0 * @since 1.0.0 */ public class FacilityTreeComposite extends Composite { // private static Log m_log = LogFactory.getLog(FacilityTreeComposite.class); // ----- instance ----- // /** */ private CommonTableViewer tableViewer = null; /** */ //????protected protected TreeViewer treeViewer = null; /** rootFacilityId??? */ private String rootFacilityId = null; /** tableViewerItem */ private FacilityTreeItem treeItem = null; /** ?? */ private Date cacheDate = null; /** ? */ //????protected protected FacilityTreeItem selectItem = null; private List<?> selectionList; /** ? */ //????protected protected int subScopeNumber; /**???? */ private boolean scopeOnly = false; /**????? */ boolean selectNodeOnly = false; /**????**/ private boolean unregistered = true; /**????**/ private boolean internal = true; /** ???????? **/ //????protected protected boolean topicRefresh = true; /** ID */ private String ownerRoleId = null; // FIXME This variable is not used. Remove? /** ????????? */ private boolean checkflg = false; /** parent Composite */ private Composite parent = null; /** ??? */ private String managerName = null; /** ??ID */ private List<String> selectFacilityList = null; /** (#!#) */ private static final String SEPARATOR_HASH_EX_HASH = "#!#"; /** Enable key press on search bar */ private boolean enableKeyPress = false; // ----- ----- // public FacilityTreeComposite(Composite parent, int style, String managerName, String ownerRoleId, boolean selectNodeOnly) { super(parent, style); this.managerName = managerName; this.selectNodeOnly = selectNodeOnly; this.scopeOnly = false; this.unregistered = false; this.internal = false; this.parent = parent; this.ownerRoleId = ownerRoleId; this.createContents(); } public FacilityTreeComposite(Composite parent, int style, String managerName, String ownerRoleId, boolean scopeOnly, boolean unregistered, boolean internal) { super(parent, style); this.managerName = managerName; this.scopeOnly = scopeOnly; this.unregistered = unregistered; this.internal = internal; this.parent = parent; this.ownerRoleId = ownerRoleId; this.createContents(); } /** * ?????<br> * ver 4.1.0 ??VM???????? * * @param parent ?? * @param style * @param managerName ??? * @param ownerRoleId ID * @param scopeOnly ????????? * @param unregistered UNREGISTERED??????? * @param internal INTERNAL??????? * @param rootFacilityId ???ID */ public FacilityTreeComposite(Composite parent, int style, String managerName, String ownerRoleId, boolean scopeOnly, boolean unregistered, boolean internal, String rootFacilityId) { super(parent, style); this.managerName = managerName; this.scopeOnly = scopeOnly; this.unregistered = unregistered; this.internal = internal; this.rootFacilityId = rootFacilityId; this.parent = parent; this.ownerRoleId = ownerRoleId; this.createContents(); } /** * * * @param parent ?? * @param style * @param ownerRoleId ID * @param scopeOnly ????????? * @param unregistered UNREGISTERED??????? * @param internal INTERNAL??????? * @param topicRefresh ??????????? */ public FacilityTreeComposite(Composite parent, int style, String ownerRoleId, boolean scopeOnly, boolean unregistered, boolean internal, boolean topicRefresh) { super(parent, style); this.scopeOnly = scopeOnly; this.unregistered = unregistered; this.internal = internal; this.topicRefresh = topicRefresh; this.parent = parent; this.ownerRoleId = ownerRoleId; this.createContents(); } /** * * ????? * * @param parent ?? * @param style * @param ownerRoleId ID * @param scopeOnly ????????? * @param unregistered UNREGISTERED??????? * @param internal INTERNAL??????? * @param topicRefresh ??????????? * @param checkflg ???????? */ public FacilityTreeComposite(Composite parent, int style, String ownerRoleId, boolean scopeOnly, boolean unregistered, boolean internal, boolean topicRefresh, boolean checkflg) { super(parent, style); this.scopeOnly = scopeOnly; this.unregistered = unregistered; this.internal = internal; this.topicRefresh = topicRefresh; this.parent = parent; this.ownerRoleId = ownerRoleId; this.checkflg = checkflg; this.enableKeyPress = true; this.createContents(); } // ----- instance ----- // /** * ???????? * * @return */ public TreeViewer getTreeViewer() { return this.treeViewer; } /** * ???????? * * @return */ public Tree getTree() { return this.treeViewer.getTree(); } /** * ???????? * * @return */ public FacilityTreeItem getSelectItem() { return this.selectItem; } /* * ???? */ public void setSelectItem(FacilityTreeItem item) { selectItem = item; } /** ???????? * * @return */ public List<?> getSelectionList() { return this.selectionList; } /** /** * ????????? * * @return */ public int getSubScopeNumber() { return subScopeNumber; } /** * ????? * * ????protected */ protected void createContents() { // ?? GridLayout layout = new GridLayout(1, true); layout.marginHeight = 0; layout.marginWidth = 0; this.setLayout(layout); // ? GridData layoutData = new GridData(); layoutData.horizontalAlignment = GridData.FILL; layoutData.verticalAlignment = GridData.FILL; layoutData.grabExcessHorizontalSpace = true; layoutData.grabExcessVerticalSpace = true; // Add search bar Composite compSearch = new ScopeTreeSearchBarComposite(this, SWT.NONE, enableKeyPress); WidgetTestUtil.setTestId(this, "search", compSearch); compSearch.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); // ? Tree tree = null; // ? if (checkflg) { tree = new Tree(this, SWT.MULTI | SWT.BORDER | SWT.CHECK); this.treeViewer = new CheckboxTreeViewer(tree); } else { tree = new Tree(this, SWT.MULTI | SWT.BORDER); this.treeViewer = new TreeViewer(tree); } WidgetTestUtil.setTestId(this, null, tree); tree.setLayoutData(layoutData); // this.treeViewer.setContentProvider(new FacilityTreeContentProvider()); this.treeViewer.setLabelProvider(new FacilityTreeLabelProvider()); this.treeViewer.setSorter(new FacilityTreeViewerSorter()); // ?? this.treeViewer.addSelectionChangedListener(new ISelectionChangedListener() { @Override public void selectionChanged(SelectionChangedEvent event) { StructuredSelection selection = (StructuredSelection) event.getSelection(); selectItem = (FacilityTreeItem) selection.getFirstElement(); selectionList = selection.toList(); if (selectItem != null) { subScopeNumber = selectItem.getChildren().size(); } } }); if (checkflg) { // ?? CheckboxTreeViewer checkboxTreeViewer = (CheckboxTreeViewer) treeViewer; checkboxTreeViewer.addCheckStateListener(new ICheckStateListener() { public void checkStateChanged(CheckStateChangedEvent event) { CheckboxTreeViewer checkboxTreeViewer = (CheckboxTreeViewer) treeViewer; // ??????? if (event.getChecked()) { checkboxTreeViewer.setSubtreeChecked(event.getElement(), true); } if (!event.getChecked()) { checkboxTreeViewer.setSubtreeChecked(event.getElement(), false); Object object = event.getElement(); if (object instanceof FacilityTreeItem) { FacilityTreeItem item = (FacilityTreeItem) event.getElement(); setParentCheck(checkboxTreeViewer, item, false); } } // ???? selectFacilityList = getCheckedTreeInfo(); } }); } //??? final FacilityTreeComposite composite = this; if (topicRefresh) { FacilityTreeCache.addComposite(composite); } this.addDisposeListener(new DisposeListener() { @Override public void widgetDisposed(DisposeEvent e) { FacilityTreeCache.delComposite(composite); } }); // ??? this.update(); } private void setParentCheck(CheckboxTreeViewer checkboxTreeViewer, FacilityTreeItem item, boolean flag) { FacilityTreeItem parent = item.getParent(); if (parent == null) { return; } checkboxTreeViewer.setChecked(parent, flag); setParentCheck(checkboxTreeViewer, parent, flag); } /** * ???? */ @Override public void update() { // ????????? if (this.ownerRoleId != null) { try { if (this.selectNodeOnly) { // ??? m_log.debug("getNodeFacilityTree " + managerName); treeItem = addEmptyParent(RepositoryEndpointWrapper.getWrapper(managerName) .getNodeFacilityTree(this.ownerRoleId)); if (treeItem != null && treeItem.getChildren() != null && treeItem.getChildren().get(0) != null) { Collections.sort(treeItem.getChildren().get(0).getChildren(), new Comparator<FacilityTreeItem>() { @Override public int compare(FacilityTreeItem o1, FacilityTreeItem o2) { FacilityInfo info1 = ((FacilityTreeItem) o1).getData(); FacilityInfo info2 = ((FacilityTreeItem) o2).getData(); int order1 = info1.getDisplaySortOrder(); int order2 = info2.getDisplaySortOrder(); if (order1 == order2) { String object1 = info1.getFacilityId(); String object2 = info2.getFacilityId(); return object1.compareTo(object2); } else { return (order1 - order2); } } }); } } else { m_log.debug("getFacilityTree " + managerName); treeItem = addEmptyParent( RepositoryEndpointWrapper.getWrapper(managerName).getFacilityTree(this.ownerRoleId)); } } catch (Exception e) { m_log.warn("getTreeItem(), " + e.getMessage(), e); return; } } else { treeItem = FacilityTreeCache.getTreeItem(managerName); } Date cacheDate = null; if (managerName != null) { cacheDate = FacilityTreeCache.getCacheDate(managerName); } if (cacheDate != null && cacheDate.equals(this.cacheDate)) { return; } this.cacheDate = cacheDate; if (null == treeItem) { m_log.trace("treeItem is null. Skip."); } else { FacilityTreeItem scope = (treeItem.getChildren()).get(0); scope.getData().setFacilityName(HinemosMessage.replace(scope.getData().getFacilityName())); m_log.debug("internal=" + internal + ", unregistered=" + unregistered); //???? if (!internal) { if (managerName == null) { for (FacilityTreeItem managerScope : scope.getChildren()) { if (!FacilityTreeItemUtil.removeChild(managerScope, FacilityTreeAttributeConstant.INTERNAL_SCOPE)) { m_log.warn("failed removing " + FacilityTreeAttributeConstant.INTERNAL_SCOPE); } } } else { if (!FacilityTreeItemUtil.removeChild(scope, FacilityTreeAttributeConstant.INTERNAL_SCOPE)) { m_log.warn("failed removing " + FacilityTreeAttributeConstant.INTERNAL_SCOPE + ", managerName=" + managerName); } } } if (!unregistered) { if (managerName == null) { for (FacilityTreeItem managerScope : scope.getChildren()) { if (!FacilityTreeItemUtil.removeChild(managerScope, FacilityTreeAttributeConstant.UNREGISTERED_SCOPE)) { m_log.warn("failed removing " + FacilityTreeAttributeConstant.UNREGISTERED_SCOPE); } } } else { if (!FacilityTreeItemUtil.removeChild(scope, FacilityTreeAttributeConstant.UNREGISTERED_SCOPE)) { m_log.warn("failed removing " + FacilityTreeAttributeConstant.UNREGISTERED_SCOPE + ", managerName=" + managerName); } } } if (rootFacilityId != null) { // rootFacilityId?? FacilityTreeItemUtil.keepChild(scope, rootFacilityId); } if (scopeOnly) { FacilityTreeItemUtil.removeNode(scope); } // SWT????? checkAsyncExec(new Runnable() { @Override public void run() { m_log.trace("FacilityTreeComposite.checkAsyncExec() do runnnable"); Control control = treeViewer.getControl(); if (control == null || control.isDisposed()) { m_log.info("treeViewer is disposed. "); return; } FacilityTreeItem oldTreeItem = (FacilityTreeItem) treeViewer.getInput(); m_log.debug("run() oldTreeItem=" + oldTreeItem); if (null != oldTreeItem) { if (!oldTreeItem.equals(treeItem)) { ArrayList<String> expandIdList = new ArrayList<String>(); for (Object item : treeViewer.getExpandedElements()) { expandIdList.add(((FacilityTreeItem) item).getData().getFacilityId()); } m_log.debug("expandIdList.size=" + expandIdList.size()); treeViewer.setInput(treeItem); treeViewer.refresh(); expand(treeItem, expandIdList); } } else { treeViewer.setInput(treeItem); List<FacilityTreeItem> selectItem = treeItem.getChildren(); treeViewer.setSelection(new StructuredSelection(selectItem.get(0)), true); //??? treeViewer.expandToLevel(3); } if (checkflg) { // ??? setCheckedTreeInfo(selectFacilityList); } } private void expand(FacilityTreeItem item, List<String> expandIdList) { if (expandIdList.contains(item.getData().getFacilityId())) { treeViewer.expandToLevel(item, 1); } for (FacilityTreeItem child : item.getChildren()) { expand(child, expandIdList); } } }); } } /** * ?? * @param r * @return */ private boolean checkAsyncExec(Runnable r) { if (!this.isDisposed()) { m_log.trace("FacilityTreeComposite.checkAsyncExec() is true"); parent.getDisplay().asyncExec(r); return true; } else { m_log.trace("FacilityTreeComposite.checkAsyncExec() is false"); return false; } } /** * ??????? * */ public void setExpand(boolean isExpand) { if (isExpand) { this.treeViewer.setAutoExpandLevel(TreeViewer.ALL_LEVELS); } } /** * ???? * * @param treeItem */ public void setScopeTree(FacilityTreeItem treeItem) { try { this.treeItem = treeItem; this.treeViewer.setInput(treeItem); this.treeViewer.setAutoExpandLevel(TreeViewer.ALL_LEVELS); } catch (Exception e) { m_log.warn("setScopeTree(), " + e.getMessage(), e); } } /** * Tree???facilityID???????? * * @param treeItem * @param facilityID */ public void setScopeTreeWithSelection(FacilityTreeItem treeItem, String facilityID) { this.setScopeTree(treeItem); List<FacilityTreeItem> tmpItem = treeItem.getChildren(); //?FaiclityID??TreeItem?????? for (int i = 0; i < tmpItem.size(); i++) { setScopeTreeWithSelectionSub(tmpItem.get(i), facilityID); if (facilityID.equals(tmpItem.get(i).getData().getFacilityId())) { this.treeViewer.setSelection(new StructuredSelection(tmpItem.get(i)), true); } } } /** * setScopeTreeWithSelection???????????? * * @param treeItem * @param facilityID */ public void setScopeTreeWithSelectionSub(FacilityTreeItem treeItem, String facilityID) { List<FacilityTreeItem> tmpItem = treeItem.getChildren(); for (int i = 0; i < tmpItem.size(); i++) { setScopeTreeWithSelectionSub(tmpItem.get(i), facilityID); if (facilityID.equals(tmpItem.get(i).getData().getFacilityId())) { this.treeViewer.setSelection(new StructuredSelection(tmpItem.get(i)), true); } } } public CommonTableViewer getTableViewer() { return tableViewer; } public void setTableViewer(CommonTableViewer tableViewer) { this.tableViewer = tableViewer; } /** * ???????? * * @return */ public Table getTable() { return this.tableViewer.getTable(); } /** * ????????? * * @return */ public FacilityTreeItem getAllTreeItems() { return this.treeItem; } /** * ??String????????<br> * ?<br> * 2(COMPOSITE)??<br> * 0(SCOPE)???3(MANAGER)????? + "#!#" + ID + "#!#" + <br> * 1(NODE)????? + "#!#" + ?ID + "#!#" + ID + "#!#" + <br> * * @return */ public List<String> getCheckedTreeInfo() { Object[] treeItemList = ((CheckboxTreeViewer) getTreeViewer()).getCheckedElements(); List<String> selectFacilityStringList = new ArrayList<>(); m_log.debug("SIZE:" + treeItemList.length); for (Object objectItem : treeItemList) { if (objectItem instanceof FacilityTreeItem) { FacilityTreeItem facilityTreeItem = (FacilityTreeItem) objectItem; switch (facilityTreeItem.getData().getFacilityType()) { case FacilityConstant.TYPE_COMPOSITE: selectFacilityStringList.add(String.valueOf(facilityTreeItem.getData().getFacilityType())); break; case FacilityConstant.TYPE_SCOPE: case FacilityConstant.TYPE_MANAGER: // ???????? String managerName = ScopePropertyUtil.getManager(facilityTreeItem).getData().getFacilityId(); String facilityId = facilityTreeItem.getData().getFacilityId(); int facilityType = facilityTreeItem.getData().getFacilityType(); String param = managerName + SEPARATOR_HASH_EX_HASH + facilityId + SEPARATOR_HASH_EX_HASH + facilityType; m_log.debug(param); selectFacilityStringList.add(param); break; case FacilityConstant.TYPE_NODE: managerName = ScopePropertyUtil.getManager(facilityTreeItem).getData().getFacilityId(); facilityId = facilityTreeItem.getData().getFacilityId(); String parentFacilityId = facilityTreeItem.getParent().getData().getFacilityId(); facilityType = facilityTreeItem.getData().getFacilityType(); param = managerName + SEPARATOR_HASH_EX_HASH + parentFacilityId + SEPARATOR_HASH_EX_HASH + facilityId + SEPARATOR_HASH_EX_HASH + facilityType; m_log.debug(param); selectFacilityStringList.add(param); break; default: // ?? break; } } } return selectFacilityStringList; } /** * ???facilityItemList?????? * * @param treeItem */ private void setCheckedTreeInfo(List<String> facilityItemList) { List<FacilityTreeItem> treeItemList = new ArrayList<>(); List<FacilityTreeItem> parentItemList = new ArrayList<>(); checkTreeSelect(this.treeItem, facilityItemList, treeItemList, parentItemList); m_log.debug("setSelectItemSize:" + treeItemList.size() + ", parentItemList.size:" + parentItemList.size()); // ?? FacilityTreeItem facilityArr[] = treeItemList.toArray(new FacilityTreeItem[treeItemList.size()]); ((CheckboxTreeViewer) getTreeViewer()).setCheckedElements(facilityArr); // ????????????????? for (FacilityTreeItem item : parentItemList) { ((CheckboxTreeViewer) getTreeViewer()).setSubtreeChecked(item, true); } Object[] returns = ((CheckboxTreeViewer) getTreeViewer()).getCheckedElements(); m_log.debug("selectItemSize:" + returns.length); } /** * ???treeItem????????????????List???????List???? * * @param treeItem ????FacilityTreeItem * @param facilityStringList ??String?List * @param facilityList ???FacilityTreeItem?() * @param parentFacilityList ??????FacilityTreeItem?() */ public void checkTreeSelect(FacilityTreeItem treeItem, List<String> facilityStringList, List<FacilityTreeItem> facilityList, List<FacilityTreeItem> parentFacilityList) { if (facilityStringList == null || facilityStringList.size() == 0) { return; } List<FacilityTreeItem> treeItemList = treeItem.getChildren(); for (FacilityTreeItem childItem : treeItemList) { checkTreeSelect(childItem, facilityStringList, facilityList, parentFacilityList); } for (String detail : facilityStringList) { String facilityId = treeItem.getData().getFacilityId(); String managerName = ""; String parentFacilityId = ""; String details[] = detail.split(SEPARATOR_HASH_EX_HASH); String facilityType = details[details.length - 1]; if (treeItem.getData().getFacilityType() == FacilityConstant.TYPE_COMPOSITE) { // ????Detail?FacilityType????? if (details.length == 1) { m_log.debug("selected(have sub:composite) managerName:" + managerName + ", parentFacilityId:" + parentFacilityId + ", facilityId:" + facilityId); facilityList.add(treeItem); parentFacilityList.add(treeItem); } } else { managerName = ScopePropertyUtil.getManager(treeItem).getData().getFacilityId(); parentFacilityId = treeItem.getParent().getData().getFacilityId(); if (Integer.parseInt(facilityType) == FacilityConstant.TYPE_NODE && details[0].equals(managerName) && details[1].equals(parentFacilityId) && details[2].equals(facilityId)) { m_log.debug("selected tree managerName:" + managerName + ", parentFacilityId:" + parentFacilityId + ", facilityId:" + facilityId); facilityList.add(treeItem); } else if ((Integer.parseInt(facilityType) == FacilityConstant.TYPE_SCOPE || Integer.parseInt(facilityType) == FacilityConstant.TYPE_MANAGER) && details[0].equals(managerName) && details[1].equals(facilityId)) { // node???managerName?facilityId?parentFacilityId???? // scope???manager????managerName?facilityId???? m_log.debug("selected(have sub) managerName:" + managerName + ", parentFacilityId:" + parentFacilityId + ", facilityId:" + facilityId); facilityList.add(treeItem); parentFacilityList.add(treeItem); } } } } /** * ???List????? * * @param selectFacilityList ?? */ public void setSelectFacilityList(List<String> selectFacilityList) { this.selectFacilityList = selectFacilityList; } /** * * @param current ? * @param keyword ? * @return ? */ private FacilityTreeItem searchNeighbors(FacilityTreeItem current, String keyword) { FacilityTreeItem found; FacilityTreeItem parent = current.getParent(); if (null != parent) { do { int offset = parent.getChildren().indexOf(current) + 1; found = searchChildren(parent, keyword, offset); if (null != found) { return found; } current = parent; parent = current.getParent(); } while (null != parent); } return null; } /** * ? * @param parent * @param keyword ? * @param offset ?? * @return ? */ private FacilityTreeItem searchChildren(FacilityTreeItem parent, String keyword, int offset) { List<FacilityTreeItem> children = parent.getChildren(); Collections.sort(children, new Comparator<FacilityTreeItem>() { public int compare(FacilityTreeItem item1, FacilityTreeItem item2) { return item1.getData().getDisplaySortOrder() - item2.getData().getDisplaySortOrder(); } }); int len = children.size(); for (int i = offset; i < len; i++) { FacilityTreeItem child = children.get(i); if (child.getData().getFacilityId() != null && -1 != child.getData().getFacilityId().indexOf(keyword)) { return child; } else { FacilityTreeItem found = searchChildren(child, keyword, 0); if (null != found) { return found; } } } return null; } /** * * * @param item ? * @param keyword ? * @return ? */ private FacilityTreeItem searchItem(FacilityTreeItem item, String keyword) { FacilityTreeItem found; // 1. Search children found = searchChildren(item, keyword, 0); if (null != found) { return found; } // 2. If not found in children, search in neighbors found = searchNeighbors(item, keyword); if (null != found) { return found; } return null; } /** * * @param keyword ? */ public void doSearch(String keyword) { // Check and format keyword if (null == keyword) { return; } keyword = keyword.trim(); if (keyword.isEmpty()) { return; } StructuredSelection selection = (StructuredSelection) treeViewer.getSelection(); Object targetItem = selection.getFirstElement(); FacilityTreeItem result = searchItem( (FacilityTreeItem) (null != targetItem ? targetItem : treeViewer.getInput()), keyword); if (null != result) { FacilityTreeItem trace = result; LinkedList<FacilityTreeItem> pathList = new LinkedList<>(); do { pathList.addFirst(trace); trace = trace.getParent(); } while (null != trace); TreePath path = new TreePath(pathList.toArray(new FacilityTreeItem[] {})); treeViewer.setSelection(new TreeSelection(path), true); } else { MessageDialog.openInformation(this.getShell(), Messages.getString("message"), Messages.getString("search.not.found")); treeViewer.setSelection( new StructuredSelection(((FacilityTreeItem) treeViewer.getInput()).getChildren().get(0)), true); } } /** * ????FacilityTreeItem???Item? * * @param childTree FacilityTreeItem * @return ???FacilityTreeItem */ private FacilityTreeItem addEmptyParent(FacilityTreeItem childTree) { FacilityTreeItem rootTree = null; if (childTree != null) { // ??? rootTree = new FacilityTreeItem(); FacilityInfo rootInfo = new FacilityInfo(); rootInfo.setBuiltInFlg(true); rootInfo.setFacilityName(FacilityConstant.STRING_COMPOSITE); rootInfo.setFacilityType(FacilityConstant.TYPE_COMPOSITE); rootTree.setData(rootInfo); childTree.setParent(rootTree); rootTree.getChildren().add(childTree); } return rootTree; } }