use of javax.swing.event.TreeExpansionEvent in project sonarlint-intellij by SonarSource.
the class FlowsTree method init.
private void init() {
setRootVisible(false);
setShowsRootHandles(false);
setCellRenderer(new TreeCellRenderer());
this.selectionModel.addTreeSelectionListener(e -> {
if (e.getSource() != null) {
TreePath newPath = e.getNewLeadSelectionPath();
if (newPath != null) {
Object o = newPath.getLastPathComponent();
if (!(o instanceof LocationNode)) {
FlowsTree.this.setSelectionPath(e.getOldLeadSelectionPath());
} else {
navigateToSelected();
}
}
}
});
TreeWillExpandListener l = new TreeWillExpandListener() {
@Override
public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
// nothing to do
}
@Override
public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException {
throw new ExpandVetoException(event);
}
};
addTreeWillExpandListener(l);
}
use of javax.swing.event.TreeExpansionEvent in project WorldPainter by Captain-Chaos.
the class MapExplorer method main.
public static void main(String[] args) throws IOException, ClassNotFoundException {
// Load or initialise configuration
File configDir = Configuration.getConfigDir();
if (!configDir.isDirectory()) {
configDir.mkdirs();
}
// This will migrate the configuration directory if necessary
Configuration config = Configuration.load();
if (config == null) {
if (!logger.isDebugEnabled()) {
// If debug logging is on, the Configuration constructor will
// already log this
logger.info("Creating new configuration");
}
config = new Configuration();
}
Configuration.setInstance(config);
logger.info("Installation ID: " + config.getUuid());
// Load and install trusted WorldPainter root certificate
X509Certificate trustedCert = null;
try {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
trustedCert = (X509Certificate) certificateFactory.generateCertificate(Main.class.getResourceAsStream("/wproot.pem"));
WPTrustManager trustManager = new WPTrustManager(trustedCert);
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, new TrustManager[] { trustManager }, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
} catch (CertificateException e) {
logger.error("Certificate exception while loading trusted root certificate", e);
} catch (NoSuchAlgorithmException e) {
logger.error("No such algorithm exception while loading trusted root certificate", e);
} catch (KeyManagementException e) {
logger.error("Key management exception while loading trusted root certificate", e);
}
// Load the plugins
if (trustedCert != null) {
PluginManager.loadPlugins(new File(configDir, "plugins"), trustedCert.getPublicKey());
} else {
logger.error("Trusted root certificate not available; not loading plugins");
}
WPPluginManager.initialise(config.getUuid());
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Minecraft Map Explorer");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
MapTreeModel treeModel = new MapTreeModel();
// File minecraftDir = MinecraftUtil.findMinecraftDir();
// File defaultDir;
// if (minecraftDir != null) {
// defaultDir = new File(minecraftDir, "saves");
// } else {
// defaultDir = new File(System.getProperty("user.home"));
// }
JTree tree = new JTree(treeModel);
tree.setRootVisible(false);
tree.setShowsRootHandles(true);
tree.setCellRenderer(new MapTreeCellRenderer());
JScrollPane scrollPane = new JScrollPane(tree);
// tree.expandPath(treeModel.getPath(defaultDir));
// tree.scrollPathToVisible(treeModel.getPath(defaultDir));
// Automatically expand any nodes if they only have one child
tree.addTreeExpansionListener(new TreeExpansionListener() {
@Override
public void treeExpanded(TreeExpansionEvent event) {
Object node = event.getPath().getLastPathComponent();
if ((!treeModel.isLeaf(node)) && (treeModel.getChildCount(node) == 1)) {
tree.expandPath(event.getPath().pathByAddingChild(treeModel.getChild(node, 0)));
}
}
@Override
public void treeCollapsed(TreeExpansionEvent event) {
}
});
tree.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
TreePath path = tree.getPathForLocation(e.getX(), e.getY());
if (path != null) {
((Node) path.getLastPathComponent()).doubleClicked();
}
}
}
});
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.setSize(1024, 768);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
use of javax.swing.event.TreeExpansionEvent in project jadx by skylot.
the class MainWindow method initUI.
private void initUI() {
setMinimumSize(new Dimension(200, 150));
mainPanel = new JPanel(new BorderLayout());
splitPane = new JSplitPane();
splitPane.setResizeWeight(SPLIT_PANE_RESIZE_WEIGHT);
mainPanel.add(splitPane);
DefaultMutableTreeNode treeRootNode = new DefaultMutableTreeNode(NLS.str("msg.open_file"));
treeModel = new DefaultTreeModel(treeRootNode);
tree = new JTree(treeModel);
ToolTipManager.sharedInstance().registerComponent(tree);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.setFocusable(false);
tree.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
tree.setFocusable(false);
}
});
tree.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
if (!nodeClickAction(getJNodeUnderMouse(e))) {
// click ignored -> switch to focusable mode
tree.setFocusable(true);
tree.requestFocus();
}
} else if (SwingUtilities.isRightMouseButton(e)) {
treeRightClickAction(e);
}
}
});
tree.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
nodeClickAction(tree.getLastSelectedPathComponent());
}
}
});
tree.setCellRenderer(new DefaultTreeCellRenderer() {
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean isLeaf, int row, boolean focused) {
Component c = super.getTreeCellRendererComponent(tree, value, selected, expanded, isLeaf, row, focused);
if (value instanceof JNode) {
JNode jNode = (JNode) value;
setText(jNode.makeStringHtml());
setIcon(jNode.getIcon());
setToolTipText(jNode.getTooltip());
} else {
setToolTipText(null);
}
if (value instanceof JPackage) {
setEnabled(((JPackage) value).isEnabled());
}
return c;
}
});
tree.addTreeWillExpandListener(new TreeWillExpandListener() {
@Override
public void treeWillExpand(TreeExpansionEvent event) {
TreePath path = event.getPath();
Object node = path.getLastPathComponent();
if (node instanceof JLoadableNode) {
((JLoadableNode) node).loadNode();
}
if (!treeReloading) {
project.addTreeExpansion(getPathExpansion(event.getPath()));
update();
}
}
@Override
public void treeWillCollapse(TreeExpansionEvent event) {
if (!treeReloading) {
project.removeTreeExpansion(getPathExpansion(event.getPath()));
update();
}
}
});
progressPane = new ProgressPanel(this, true);
IssuesPanel issuesPanel = new IssuesPanel(this);
JPanel leftPane = new JPanel(new BorderLayout());
JScrollPane treeScrollPane = new JScrollPane(tree);
treeScrollPane.setMinimumSize(new Dimension(100, 150));
JPanel bottomPane = new JPanel(new BorderLayout());
bottomPane.add(issuesPanel, BorderLayout.PAGE_START);
bottomPane.add(progressPane, BorderLayout.PAGE_END);
leftPane.add(treeScrollPane, BorderLayout.CENTER);
leftPane.add(bottomPane, BorderLayout.PAGE_END);
splitPane.setLeftComponent(leftPane);
tabbedPane = new TabbedPane(this);
tabbedPane.setMinimumSize(new Dimension(150, 150));
splitPane.setRightComponent(tabbedPane);
new DropTarget(this, DnDConstants.ACTION_COPY, new MainDropTarget(this));
heapUsageBar = new HeapUsageBar();
mainPanel.add(heapUsageBar, BorderLayout.SOUTH);
verticalSplitter = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
verticalSplitter.setTopComponent(splitPane);
verticalSplitter.setResizeWeight(SPLIT_PANE_RESIZE_WEIGHT);
mainPanel.add(verticalSplitter, BorderLayout.CENTER);
setContentPane(mainPanel);
setTitle(DEFAULT_TITLE);
}
use of javax.swing.event.TreeExpansionEvent in project java-swing-tips by aterai.
the class EmptyIcon method makeTree.
private static JTree makeTree() {
Icon emptyIcon = new EmptyIcon();
UIManager.put("Tree.expandedIcon", new IconUIResource(emptyIcon));
UIManager.put("Tree.collapsedIcon", new IconUIResource(emptyIcon));
JTree tree = new JTree();
tree.setEditable(true);
tree.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
int row = 0;
while (row < tree.getRowCount()) {
tree.expandRow(row++);
}
tree.addTreeWillExpandListener(new TreeWillExpandListener() {
@Override
public void treeWillExpand(TreeExpansionEvent e) {
// throws ExpandVetoException {
// throw new ExpandVetoException(e, "Tree expansion cancelled");
}
@Override
public void treeWillCollapse(TreeExpansionEvent e) throws ExpandVetoException {
throw new ExpandVetoException(e, "Tree collapse cancelled");
}
});
return tree;
}
use of javax.swing.event.TreeExpansionEvent in project java-swing-tips by aterai.
the class EmptyIcon method updateUI.
@Override
public void updateUI() {
setCellRenderer(null);
removeTreeWillExpandListener(listener);
super.updateUI();
setUI(new BasicTreeUI() {
@Override
public Rectangle getPathBounds(JTree tree, TreePath path) {
if (Objects.nonNull(tree) && Objects.nonNull(treeState)) {
return getTreePathBounds(path, tree.getInsets(), new Rectangle());
}
return null;
}
private Rectangle getTreePathBounds(TreePath path, Insets insets, Rectangle bounds) {
Rectangle rect = treeState.getBounds(path, bounds);
if (Objects.nonNull(rect)) {
rect.width = tree.getWidth();
rect.y += insets.top;
}
return rect;
}
});
UIManager.put("Tree.repaintWholeRow", Boolean.TRUE);
TreeCellRenderer r = getCellRenderer();
setCellRenderer((tree, value, selected, expanded, leaf, row, hasFocus) -> {
Component c = r.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
c.setBackground(selected ? SELECTED_COLOR : tree.getBackground());
if (c instanceof JComponent) {
((JComponent) c).setOpaque(true);
}
return c;
});
setOpaque(false);
setRootVisible(false);
// https://ateraimemo.com/Swing/TreeNodeCollapseVeto.html
listener = new TreeWillExpandListener() {
@Override
public void treeWillExpand(TreeExpansionEvent e) {
// throw new ExpandVetoException(e, "Tree expansion cancelled");
}
@Override
public void treeWillCollapse(TreeExpansionEvent e) throws ExpandVetoException {
throw new ExpandVetoException(e, "Tree collapse cancelled");
}
};
addTreeWillExpandListener(listener);
}
Aggregations