1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 14:42:11 +02:00

Extract utility class for next/prev navigation in trees.

This commit is contained in:
Markus Schorn 2006-07-05 12:58:04 +00:00
parent 193272f3ae
commit 074c73c018
7 changed files with 139 additions and 62 deletions

View file

@ -30,7 +30,7 @@ public class IBHistoryDropDownAction extends Action implements IMenuCreator {
private IBViewPart fView;
public ClearHistoryAction(IBViewPart view) {
super(IBMessages.HistoryDropDownAction_ClearHistory_label);
super(IBMessages.IBHistoryDropDownAction_ClearHistory_label);
fView= view;
}
@ -48,7 +48,7 @@ public class IBHistoryDropDownAction extends Action implements IMenuCreator {
public IBHistoryDropDownAction(IBViewPart view) {
fHierarchyView= view;
fMenu= null;
setToolTipText(IBMessages.HistoryDropDownAction_tooltip);
setToolTipText(IBMessages.IBHistoryDropDownAction_tooltip);
CPluginImages.setImageDescriptors(this, CPluginImages.T_LCL, "history_list.gif"); //$NON-NLS-1$
setMenuCreator(this);
}

View file

@ -48,9 +48,9 @@ public class IBHistoryListAction extends Action {
private HistoryListDialog(Shell shell, ITranslationUnit[] elements) {
super(shell);
setHelpAvailable(false);
setTitle(IBMessages.HistoryListAction_HistoryDialog_title);
setTitle(IBMessages.IBHistoryListAction_HistoryDialog_title);
String[] buttonLabels= new String[] {
IBMessages.HistoryListAction_Remove_label,
IBMessages.IBHistoryListAction_Remove_label,
};
IListAdapter adapter= new IListAdapter() {
@ -69,7 +69,7 @@ public class IBHistoryListAction extends Action {
CUILabelProvider labelProvider= new CUILabelProvider(CElementLabels.APPEND_ROOT_PATH, CElementImageProvider.OVERLAY_ICONS);
fHistoryList= new ListDialogField(adapter, buttonLabels, labelProvider);
fHistoryList.setLabelText(IBMessages.HistoryListAction_HistoryList_label);
fHistoryList.setLabelText(IBMessages.IBHistoryListAction_HistoryList_label);
fHistoryList.setElements(Arrays.asList(elements));
ISelection sel;
@ -163,7 +163,7 @@ public class IBHistoryListAction extends Action {
public IBHistoryListAction(IBViewPart view) {
fView= view;
setText(IBMessages.HistoryListAction_label);
setText(IBMessages.IBHistoryListAction_label);
}
/*

View file

@ -16,17 +16,17 @@ import org.eclipse.osgi.util.NLS;
public class IBMessages extends NLS {
private static final String BUNDLE_NAME = "org.eclipse.cdt.internal.ui.includebrowser.IBMessages"; //$NON-NLS-1$
public static String HistoryDropDownAction_ClearHistory_label;
public static String IBHistoryDropDownAction_ClearHistory_label;
public static String HistoryDropDownAction_tooltip;
public static String IBHistoryDropDownAction_tooltip;
public static String HistoryListAction_HistoryDialog_title;
public static String IBHistoryListAction_HistoryDialog_title;
public static String HistoryListAction_HistoryList_label;
public static String IBHistoryListAction_HistoryList_label;
public static String HistoryListAction_label;
public static String IBHistoryListAction_label;
public static String HistoryListAction_Remove_label;
public static String IBHistoryListAction_Remove_label;
public static String IBViewPart_falseInputMessage;

View file

@ -16,12 +16,12 @@ IBViewPart_showIncludesTo_label=Show Files Included
IBViewPart_showInclude_label=Open Include
IBViewPart_showFolders_label=Show Folders
IBViewPart_showFolders_tooltip=Show Folders
IBViewPart_previousMatch_label=Previous Match
IBViewPart_previousMatch_label=Previous Include
IBViewPart_showIncludesTo_tooltip=Show Files Included
IBViewPart_IncludedByContentDescription=Files including ''{0}'' - in {1}
IBViewPart_IncludesToContentDescription=Files included by ''{0}'' - in {1}
IBViewPart_hideInactive_tooltip=Hide Includes from Inactive Code
IBViewPart_previousMatch_tooltip=Show Previous Match
IBViewPart_previousMatch_tooltip=Show Previous Include
IBViewPart_OpenWithMenu_label=Open With
IBViewPart_hideInactive_label=Hide Inactive Includes
IBViewPart_hideSystem_tooltip=Hide System Includes
@ -30,13 +30,13 @@ IBViewPart_hideSystem_label=Hide System Includes
IBViewPart_workspaceScope=workspace
IBViewPart_refresh_label=Refresh
IBViewPart_FocusOn_label=Focus On ''{0}''
IBViewPart_nextMatch_label=Next Match
IBViewPart_nextMatch_label=Next Include
IBViewPart_refresh_tooltip=Refresh View Content
IBViewPart_falseInputMessage=Include Hierarchies can be shown for C or C++ files, only. They have to be part of the workspace.
IBViewPart_nextMatch_tooltip=Show Next Match
HistoryListAction_HistoryDialog_title=Include Browser History
HistoryDropDownAction_ClearHistory_label=Clear History
HistoryListAction_Remove_label=Remove
HistoryDropDownAction_tooltip=Show History List
HistoryListAction_HistoryList_label=Select a file to show in the Include Browser:
HistoryListAction_label=Open History...
IBViewPart_nextMatch_tooltip=Show Next Include
IBHistoryListAction_HistoryDialog_title=Include Browser History
IBHistoryDropDownAction_ClearHistory_label=Clear History
IBHistoryListAction_Remove_label=Remove
IBHistoryDropDownAction_tooltip=Show History List
IBHistoryListAction_HistoryList_label=Select a file to show in the Include Browser:
IBHistoryListAction_label=Open History...

View file

@ -120,7 +120,6 @@ public class IBNode implements IAdaptable {
/**
* Defines whether this is a system include (angle-brackets).
* @see FileInclusionNode#isSystemInclude().
*/
public void setIsSystemInclude(boolean isSystemInclude) {
fIsSystemInclude= isSystemInclude;
@ -137,7 +136,6 @@ public class IBNode implements IAdaptable {
/**
* Defines whether the inclusion appears in active code.
* @see FileInclusionNode#isInActiveCode().
*/
public void setIsActiveCode(boolean isActiveCode) {
fIsActive= isActiveCode;

View file

@ -48,7 +48,6 @@ import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorDescriptor;
@ -86,6 +85,7 @@ import org.eclipse.cdt.internal.ui.CPluginImages;
import org.eclipse.cdt.internal.ui.util.ExternalEditorInput;
import org.eclipse.cdt.internal.ui.util.Messages;
import org.eclipse.cdt.internal.ui.viewsupport.ExtendedTreeViewer;
import org.eclipse.cdt.internal.ui.viewsupport.TreeNavigator;
import org.eclipse.cdt.internal.ui.viewsupport.WorkingSetFilterUI;
/**
@ -481,47 +481,24 @@ public class IBViewPart extends ViewPart
mm.add(fFilterInactiveAction);
}
private TreeItem getNextTreeItem(boolean forward) {
Tree tree= fTreeViewer.getTree();
TreeItem[] selectedNodes= tree.getSelection();
TreeItem selectedItem= null;
for (int i = 0; i < selectedNodes.length; i++) {
TreeItem item = selectedNodes[i];
if (item.getData() instanceof IBNode) {
selectedItem= item;
break;
}
private IBNode getNextNode(boolean forward) {
TreeNavigator navigator= new TreeNavigator(fTreeViewer.getTree(), IBNode.class);
TreeItem selectedItem= navigator.getSelectedItemOrFirstOnLevel(1, forward);
if (selectedItem == null) {
return null;
}
if (selectedItem.getData().equals(fLastNavigationNode)) {
selectedItem= navigator.getNextSibbling(selectedItem, forward);
}
if (selectedItem == null) {
// try to obtain an include on level 1;
if (tree.getItemCount() > 0) {
selectedItem= tree.getItem(0);
int itemCount= selectedItem.getItemCount();
if (itemCount > 0) {
return selectedItem.getItem(forward ? 0 : itemCount-1);
}
}
return null;
}
IBNode selectedNode= (IBNode) selectedItem.getData();
if (!selectedNode.equals(fLastNavigationNode)) {
return selectedItem;
}
TreeItem parentItem= selectedItem.getParentItem();
int itemCount = parentItem.getItemCount();
if (parentItem != null && itemCount > 1) {
int index= parentItem.indexOf(selectedItem);
index = (index + (forward ? 1 : itemCount-1)) % itemCount;
return parentItem.getItem(index);
}
return null;
return selectedItem == null ? null : (IBNode) selectedItem.getData();
}
protected void onNextOrPrevious(boolean forward) {
TreeItem nextItem= getNextTreeItem(forward);
if (nextItem != null && nextItem.getData() instanceof IBNode) {
StructuredSelection sel= new StructuredSelection(nextItem.getData());
IBNode nextItem= getNextNode(forward);
if (nextItem != null) {
StructuredSelection sel= new StructuredSelection(nextItem);
fTreeViewer.setSelection(sel);
onShowInclude(sel);
}

View file

@ -0,0 +1,102 @@
/*******************************************************************************
* Copyright (c) 2006 Wind River Systems, Inc. and others.
* 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:
* Markus Schorn - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.ui.viewsupport;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
/**
* Utility to perform next/previous navigation on a tree.
* @author markus.schorn@windriver.com
*/
public class TreeNavigator {
private Tree fTree;
private Class fDataClass;
/**
* Creates a tree navigator for the given tree. It allows for finding tree items.
* In case you supply a dataClass only nodes with data of this class are considered.
* @param tree the tree to operate on
* @param dataClass the required class for the data of the tree nodes, or <code>null</code>.
*/
public TreeNavigator(Tree tree, Class dataClass) {
fTree= tree;
fDataClass= dataClass;
}
/**
* Find the first valid item of the selection.
* @return the first valid item in the selection or <code>null</code>
*/
public TreeItem getSelectedItem() {
return getItemOfClass(fTree.getSelection(), true);
}
private TreeItem getItemOfClass(TreeItem[] items, boolean fwd) {
for (int i = 0; i < items.length; i++) {
TreeItem item = items[i];
if (fDataClass==null || fDataClass.isInstance(item.getData())) {
return item;
}
}
return null;
}
/**
* Find the first valid item on the given level. All parents have to be valid also.
* @param level the level to search, use <code>0</code> for the root nodes of the tree.
* @param fwd if set to false the tree is searched reverse from the buttom.
* @return the first item on the given level, or <code>null</code>
*/
public TreeItem getFirstItemOnLevel(int level, boolean fwd) {
return getFirstOnLevel(fTree.getItems(), level, fwd);
}
private TreeItem getFirstOnLevel(TreeItem[] items, int level, boolean fwd) {
TreeItem item= getItemOfClass(items, fwd);
if (level <= 0 || item == null) {
return item;
}
return getFirstOnLevel(item.getItems(), level-1, fwd);
}
/**
* Combines the methods {@link TreeNavigator#getSelectedItem()} and
* {@link TreeNavigator#getFirstItemOnLevel(int, boolean)}.
* @param level the level to search, use <code>0</code> for the root nodes of the tree.
* @param fwd if set to false the tree is searched reverse from the buttom.
* @return the first valid item of the selection or the first item on the given level, or <code>null</code>
*/
public TreeItem getSelectedItemOrFirstOnLevel(int level, boolean fwd) {
TreeItem result= getSelectedItem();
if (result == null) {
result= getFirstItemOnLevel(level, fwd);
}
return result;
}
/**
* Searches for the next valid sibbling of the given item.
* @param current a tree item to start the search
* @param forward if false the previous sibbling is returned
* @return the next sibbling after the given one, or <code>null</code>
*/
public TreeItem getNextSibbling(TreeItem current, boolean forward) {
TreeItem parentItem= current.getParentItem();
int itemCount = parentItem.getItemCount();
if (parentItem != null && itemCount > 1) {
int index= parentItem.indexOf(current);
index = (index + (forward ? 1 : itemCount-1)) % itemCount;
return parentItem.getItem(index);
}
return null;
}
}