use of javax.swing.event.TreeExpansionListener in project omegat by omegat-org.
the class PreferencesWindowController method show.
@SuppressWarnings("serial")
public void show(Window parent, Class<? extends IPreferencesController> initialSelection) {
dialog = new JDialog();
dialog.setTitle(OStrings.getString("PREFERENCES_TITLE_NO_SELECTION"));
dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
dialog.setModal(true);
StaticUIUtils.setWindowIcon(dialog);
outerPanel = new PreferencePanel();
innerPanel = new PreferenceViewSelectorPanel();
outerPanel.prefsViewPanel.add(innerPanel, BorderLayout.CENTER);
dialog.getContentPane().add(outerPanel);
overlay = new HighlightablePanel(dialog.getRootPane(), innerPanel.selectedPrefsScrollPane);
// Prevent ugly white viewport background with GTK LAF
innerPanel.selectedPrefsScrollPane.getViewport().setOpaque(false);
innerPanel.selectedPrefsScrollPane.setBackground(innerPanel.getBackground());
innerPanel.searchTextField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
SwingUtilities.invokeLater(PreferencesWindowController.this::searchAndFilterTree);
}
@Override
public void insertUpdate(DocumentEvent e) {
SwingUtilities.invokeLater(PreferencesWindowController.this::searchAndFilterTree);
}
@Override
public void changedUpdate(DocumentEvent e) {
SwingUtilities.invokeLater(PreferencesWindowController.this::searchAndFilterTree);
}
});
innerPanel.searchTextField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_UP) {
innerPanel.availablePrefsTree.getActionForKeyStroke(KeyStroke.getKeyStrokeForEvent(e)).actionPerformed(new ActionEvent(innerPanel.availablePrefsTree, 0, null));
innerPanel.availablePrefsTree.requestFocusInWindow();
e.consume();
}
}
});
innerPanel.searchTextField.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
innerPanel.searchTextField.selectAll();
searchCurrentView();
preloadGuis();
}
});
innerPanel.clearButton.addActionListener(e -> {
innerPanel.searchTextField.clear();
});
innerPanel.availablePrefsTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
DefaultMutableTreeNode root = createNodeTree();
walkTree(root, node -> {
IPreferencesController view = (IPreferencesController) node.getUserObject();
if (view != null) {
view.addFurtherActionListener(this);
}
});
innerPanel.availablePrefsTree.setModel(new DefaultTreeModel(root));
innerPanel.availablePrefsTree.addTreeSelectionListener(e -> {
handleViewSelection(e);
updateTitle();
});
innerPanel.availablePrefsTree.addTreeExpansionListener(new TreeExpansionListener() {
@Override
public void treeExpanded(TreeExpansionEvent event) {
SwingUtilities.invokeLater(PreferencesWindowController.this::adjustTreeSize);
}
@Override
public void treeCollapsed(TreeExpansionEvent event) {
}
});
innerPanel.selectedPrefsScrollPane.getViewport().setBackground(innerPanel.getBackground());
DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) innerPanel.availablePrefsTree.getCellRenderer();
renderer.setIcon(null);
renderer.setLeafIcon(null);
renderer.setOpenIcon(null);
renderer.setClosedIcon(null);
renderer.setDisabledIcon(null);
outerPanel.okButton.addActionListener(e -> {
if (currentView == null || currentView.validate()) {
new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
doSave();
return null;
}
@Override
protected void done() {
if (getIsReloadRequired()) {
SwingUtilities.invokeLater(ProjectUICommands::promptReload);
}
}
}.execute();
StaticUIUtils.closeWindowByEvent(dialog);
}
});
outerPanel.cancelButton.addActionListener(e -> StaticUIUtils.closeWindowByEvent(dialog));
// Hide undo, reset buttons on outer panel
outerPanel.undoButton.setVisible(false);
outerPanel.resetButton.setVisible(false);
// Use ones on inner panel to indicate that actions are view-specific
innerPanel.undoButton.addActionListener(e -> currentView.undoChanges());
innerPanel.resetButton.addActionListener(e -> currentView.restoreDefaults());
dialog.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
walkTree(root, node -> {
// Start with tree fully expanded
if (node.getChildCount() > 0) {
innerPanel.availablePrefsTree.expandPath(new TreePath(node.getPath()));
}
});
SwingUtilities.invokeLater(() -> {
if (initialSelection != null) {
selectView(initialSelection);
}
});
}
});
innerPanel.availablePrefsScrollPane.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
SwingUtilities.invokeLater(PreferencesWindowController.this::adjustTreeSize);
}
});
ActionMap actionMap = innerPanel.getActionMap();
InputMap inputMap = innerPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
actionMap.put(ACTION_KEY_NEW_SEARCH, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
innerPanel.searchTextField.requestFocusInWindow();
innerPanel.searchTextField.selectAll();
}
});
KeyStroke searchKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_F, Java8Compat.getMenuShortcutKeyMaskEx());
inputMap.put(searchKeyStroke, ACTION_KEY_NEW_SEARCH);
actionMap.put(ACTION_KEY_CLEAR_OR_CLOSE, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if (!innerPanel.searchTextField.isEmpty()) {
// Move focus away from search field
innerPanel.availablePrefsTree.requestFocusInWindow();
innerPanel.clearButton.doClick();
} else {
StaticUIUtils.closeWindowByEvent(dialog);
}
}
});
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), ACTION_KEY_CLEAR_OR_CLOSE);
// Don't let Enter close the dialog
innerPanel.searchTextField.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), ACTION_KEY_DO_SEARCH);
innerPanel.searchTextField.getActionMap().put(ACTION_KEY_DO_SEARCH, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
searchCurrentView();
}
});
String searchKeyText = StaticUIUtils.getKeyStrokeText(searchKeyStroke);
innerPanel.searchTextField.setHintText(OStrings.getString("PREFERENCES_SEARCH_HINT", searchKeyText));
// Set initial state
searchAndFilterTree();
adjustTreeSize();
dialog.getRootPane().setDefaultButton(outerPanel.okButton);
dialog.setPreferredSize(new Dimension(800, 500));
dialog.pack();
// Prevent search field from getting initial focus
innerPanel.availablePrefsTree.requestFocusInWindow();
dialog.setLocationRelativeTo(parent);
dialog.setVisible(true);
}
use of javax.swing.event.TreeExpansionListener in project android by JetBrains.
the class MemoryInstanceView method ensureTreeInitialized.
private void ensureTreeInitialized(@NotNull JPanel parentPanel) {
if (myTree != null) {
assert myTreeModel != null && myTreeRoot != null;
return;
}
myTreeRoot = new MemoryObjectTreeNode<>(new InstanceObject() {
@NotNull
@Override
public String getName() {
return "";
}
});
myTreeModel = new DefaultTreeModel(myTreeRoot);
JTree tree = new Tree(myTreeModel);
tree.setRootVisible(false);
tree.setShowsRootHandles(true);
tree.addTreeSelectionListener(e -> {
TreePath path = e.getPath();
if (!e.isAddedPath()) {
return;
}
assert path.getLastPathComponent() instanceof MemoryObjectTreeNode;
MemoryObjectTreeNode instanceObject = (MemoryObjectTreeNode) path.getLastPathComponent();
assert instanceObject.getAdapter() instanceof InstanceObject;
InstanceObject selectedInstanceObject = (InstanceObject) instanceObject.getAdapter();
myStage.selectInstance(selectedInstanceObject);
});
// Not all nodes have been populated during buildTree. Here we capture the TreeExpansionEvent to check whether any children
// under the expanded node need to be populated.
tree.addTreeExpansionListener(new TreeExpansionListener() {
@Override
public void treeExpanded(TreeExpansionEvent event) {
TreePath path = event.getPath();
assert path.getLastPathComponent() instanceof MemoryObjectTreeNode;
MemoryObjectTreeNode treeNode = (MemoryObjectTreeNode) path.getLastPathComponent();
for (int i = 0; i < treeNode.getChildCount(); i++) {
assert treeNode.getChildAt(i) instanceof MemoryObjectTreeNode;
MemoryObjectTreeNode childNode = (MemoryObjectTreeNode) treeNode.getChildAt(i);
// Anything below the top level should be FieldObjects
assert childNode.getAdapter() instanceof FieldObject;
FieldObject childObject = (FieldObject) childNode.getAdapter();
if (childObject.getValueType() == ValueType.OBJECT && childNode.getChildCount() == 0) {
populateFields(childNode);
myTreeModel.nodeStructureChanged(childNode);
}
}
}
@Override
public void treeCollapsed(TreeExpansionEvent event) {
// No-op. TODO remove unseen children?
}
});
assert myClassObject != null;
List<InstanceAttribute> attributes = myClassObject.getInstanceAttributes();
ColumnTreeBuilder builder = new ColumnTreeBuilder(tree);
for (InstanceAttribute attribute : attributes) {
builder.addColumn(myAttributeColumns.get(attribute).getBuilder());
}
builder.setTreeSorter((Comparator<MemoryObjectTreeNode<InstanceObject>> comparator, SortOrder sortOrder) -> {
myTreeRoot.sort(comparator);
myTreeModel.nodeStructureChanged(myTreeRoot);
});
builder.setBackground(ProfilerColors.MONITOR_BACKGROUND);
myTree = builder.build();
parentPanel.add(myTree, BorderLayout.CENTER);
}
use of javax.swing.event.TreeExpansionListener in project intellij-community by JetBrains.
the class ModulesDependenciesPanel method installLeftTreeListeners.
private void installLeftTreeListeners() {
myLeftTree.addTreeExpansionListener(new TreeExpansionListener() {
@Override
public void treeCollapsed(TreeExpansionEvent event) {
}
@Override
public void treeExpanded(TreeExpansionEvent event) {
DefaultMutableTreeNode expandedNode = (DefaultMutableTreeNode) event.getPath().getLastPathComponent();
for (int i = 0; i < expandedNode.getChildCount(); i++) {
DefaultMutableTreeNode child = (DefaultMutableTreeNode) expandedNode.getChildAt(i);
if (child.getChildCount() == 0 && !isLooped(event.getPath(), child)) {
Module module = ((MyUserObject) child.getUserObject()).myModule;
for (Module dependency : getModuleDependencies(module)) {
child.add(new DefaultMutableTreeNode(new MyUserObject(isInCycle(dependency), dependency)));
}
TreeUtil.sortRecursively(child, NODE_COMPARATOR);
}
}
}
private boolean isLooped(TreePath path, DefaultMutableTreeNode child) {
for (Object o : path.getPath()) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) o;
if (node != child && Comparing.equal(node.getUserObject(), child.getUserObject())) {
return true;
}
}
return false;
}
});
myLeftTree.addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
TreePath selectionPath = myLeftTree.getSelectionPath();
if (selectionPath != null) {
myPathField.setText(StringUtil.join(selectionPath.getPath(), o -> {
Object userObject = ((DefaultMutableTreeNode) o).getUserObject();
return userObject instanceof MyUserObject ? ((MyUserObject) userObject).myModule.getName() : "";
}, " : "));
DefaultMutableTreeNode selection = (DefaultMutableTreeNode) selectionPath.getLastPathComponent();
if (selection != null) {
updateRightTree(((MyUserObject) selection.getUserObject()).myModule);
}
}
}
});
}
use of javax.swing.event.TreeExpansionListener in project intellij-community by JetBrains.
the class SmartExpander method installOn.
public static void installOn(final JTree tree) {
tree.addTreeWillExpandListener(new TreeWillExpandListener() {
@Override
public void treeWillCollapse(TreeExpansionEvent event) {
TreePath path = event.getPath();
Enumeration children = ((TreeNode) path.getLastPathComponent()).children();
while (children.hasMoreElements()) {
TreePath childPath = path.pathByAddingChild(children.nextElement());
if (tree.isExpanded(childPath)) {
tree.collapsePath(childPath);
}
}
}
@Override
public void treeWillExpand(TreeExpansionEvent event) {
}
});
tree.addTreeExpansionListener(new TreeExpansionListener() {
@Override
public void treeCollapsed(TreeExpansionEvent event) {
}
@Override
public void treeExpanded(TreeExpansionEvent event) {
TreePath path = event.getPath();
TreeNode lastPathComponent = (TreeNode) path.getLastPathComponent();
if (lastPathComponent.getChildCount() == 1) {
TreePath firstChildPath = path.pathByAddingChild(lastPathComponent.getChildAt(0));
tree.expandPath(firstChildPath);
}
}
});
}
use of javax.swing.event.TreeExpansionListener in project intellij-community by JetBrains.
the class UsageViewImpl method initTree.
private void initTree() {
ApplicationManager.getApplication().assertIsDispatchThread();
myTree.setShowsRootHandles(true);
SmartExpander.installOn(myTree);
TreeUtil.installActions(myTree);
EditSourceOnDoubleClickHandler.install(myTree);
myTree.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (KeyEvent.VK_ENTER == e.getKeyCode()) {
TreePath leadSelectionPath = myTree.getLeadSelectionPath();
if (leadSelectionPath == null)
return;
DefaultMutableTreeNode node = (DefaultMutableTreeNode) leadSelectionPath.getLastPathComponent();
if (node instanceof UsageNode) {
final Usage usage = ((UsageNode) node).getUsage();
usage.navigate(false);
usage.highlightInEditor();
} else if (node.isLeaf()) {
Navigatable navigatable = getNavigatableForNode(node);
if (navigatable != null && navigatable.canNavigate()) {
navigatable.navigate(false);
}
}
}
}
});
TreeUtil.selectFirstNode(myTree);
PopupHandler.installPopupHandler(myTree, IdeActions.GROUP_USAGE_VIEW_POPUP, ActionPlaces.USAGE_VIEW_POPUP);
myTree.addTreeExpansionListener(new TreeExpansionListener() {
@Override
public void treeExpanded(TreeExpansionEvent event) {
clearRendererCache();
TreePath path = event.getPath();
Object component = path.getLastPathComponent();
if (component instanceof Node) {
Node node = (Node) component;
if (!expandingAll && node.needsUpdate()) {
checkNodeValidity(node, path);
}
}
}
@Override
public void treeCollapsed(TreeExpansionEvent event) {
clearRendererCache();
}
});
TreeUIHelper.getInstance().installTreeSpeedSearch(myTree, o -> {
Object value = o.getLastPathComponent();
TreeCellRenderer renderer = myTree.getCellRenderer();
if (renderer instanceof UsageViewTreeCellRenderer) {
UsageViewTreeCellRenderer coloredRenderer = (UsageViewTreeCellRenderer) renderer;
return coloredRenderer.getPlainTextForNode(value);
}
return value == null ? null : value.toString();
}, true);
}
Aggregations