List of usage examples for javax.swing.tree TreePath getPathCount
public int getPathCount()
From source file:de.codesourcery.eve.skills.ui.components.impl.ItemChooserComponent.java
private void addButtonClicked() { final TreePath selectionPath = tree.getSelectionPath(); if (selectionPath == null || selectionPath.getPathCount() < 3) { return;/* ww w.ja va 2 s. co m*/ } final ITreeNode selected = (ITreeNode) selectionPath.getLastPathComponent(); if (selected.getValue() instanceof InventoryType) { selectedItemsModel.addItem((InventoryType) selected.getValue()); } }
From source file:com.openbravo.pos.admin.RolesViewTree.java
public String buildPermissionsStr() { TreePath[] treePaths = uTree.getCheckingPaths(); hasPermissions = false;//www. ja v a 2s. c o m sent = new StringBuilder(); sent.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); sent.append("\n<!-- File generated by new Admin console -->"); sent.append("\n\n <permissions>"); if (treePaths != null) { for (TreePath path : treePaths) { // we know any paths not 3 are not a leaf node if (path.getPathCount() == 3) { sent.append("\n\t<class name=\"" + classMap.get(path.toString()) + "\"/>"); hasPermissions = true; } } } sent.append("\n </permissions>"); return (sent.toString()); }
From source file:de.codesourcery.eve.skills.ui.components.impl.planning.CreateProductionTemplateComponent.java
@Override protected JPanel createPanel() { tree.setRootVisible(true);/* www .j ava 2s . co m*/ tree.setCellRenderer(renderer); tree.setModel(this.treeBuilder.getTreeModel()); final PopupMenuBuilder menuBuilder = new PopupMenuBuilder().addItem("Produce", new CreateSubPlanAction()) .addItem("Buy on market", new DeleteSubPlanAction()) .addItem("Buy on market", new DeleteInventionJobAction()) .addItem("Invent from T1 blueprint", new CreateInventionJobAction()); menuBuilder.attach(tree); tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() { @Override public void valueChanged(TreeSelectionEvent e) { final TreePath selectedPath = e.getPath(); if (selectedPath == null || selectedPath.getPathCount() == 0) { selectedNodeChanged(null); } else { selectedNodeChanged((ITreeNode) selectedPath.getLastPathComponent()); } } }); final JScrollPane treePane = new JScrollPane(tree); // add editor panel editorPanel.setLayout(new GridBagLayout()); editorPanel.setBorder(BorderFactory.createLineBorder(Color.black)); // cost statement panel final JPanel costPanel = costStatement.getPanel(); final JPanel combinedPanel = new JPanel(); combinedPanel.setLayout(new GridBagLayout()); combinedPanel.add(costPanel, constraints(0, 0).resizeBoth().useRemainingWidth().end()); combinedPanel.add(editorPanel, constraints(0, 1).resizeBoth().useRemainingSpace().end()); final JPanel result = new JPanel(); result.setLayout(new GridBagLayout()); final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treePane, combinedPanel); result.add(splitPane, constraints().resizeBoth().end()); costStatement.refresh(); return result; }
From source file:com.wwidesigner.gui.StudyView.java
@Override protected void initializeComponents() { // create file tree tree = new JTree() { @Override// w w w . j ava2 s . co m public String getToolTipText(MouseEvent e) { String tip = null; TreePath path = tree.getPathForLocation(e.getX(), e.getY()); if (path != null) { if (path.getPathCount() == 3) // it is a leaf { DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); if (node instanceof TreeNodeWithToolTips) { tip = ((TreeNodeWithToolTips) node).getToolTip(); } } } return tip == null ? getToolTipText() : tip; } }; // Show tooltips for the Study view, and let them persist for 8 seconds. ToolTipManager.sharedInstance().registerComponent(tree); ToolTipManager.sharedInstance().setDismissDelay(8000); // If a Study view node doesn't fit in the pane, expand it when hovering // over it. ExpandedTipUtils.install(tree); tree.setRootVisible(false); tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); tree.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { TreePath path = tree.getPathForLocation(e.getX(), e.getY()); if (path != null) { if (path.getPathCount() == 3) // it is a leaf { DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) node.getParent(); String category = (String) parentNode.getUserObject(); study.setCategorySelection(category, (String) node.getUserObject()); if (StudyModel.INSTRUMENT_CATEGORY_ID.equals(category) || StudyModel.TUNING_CATEGORY_ID.equals(category)) { try { study.validHoleCount(); } catch (Exception ex) { showException(ex); } } } updateView(); } } }); JScrollPane scrollPane = new JScrollPane(tree); scrollPane.setPreferredSize(new Dimension(225, 100)); add(scrollPane); Preferences myPreferences = getApplication().getPreferences(); String modelName = myPreferences.get(OptimizationPreferences.STUDY_MODEL_OPT, OptimizationPreferences.NAF_STUDY_NAME); setStudyModel(modelName); study.setPreferences(myPreferences); getApplication().getEventManager().subscribe(WIDesigner.FILE_OPENED_EVENT_ID, this); getApplication().getEventManager().subscribe(WIDesigner.FILE_CLOSED_EVENT_ID, this); getApplication().getEventManager().subscribe(WIDesigner.FILE_SAVED_EVENT_ID, this); getApplication().getEventManager().subscribe(WIDesigner.WINDOW_RENAMED_EVENT_ID, this); }
From source file:edu.mbl.jif.datasetconvert.CheckboxTreeDimensions.java
public void getSelectedIndices() { // channels = new int[]{0, 1, 2, 3, 4, 5, 6}; // slices = new int[]{0}; // frames = new int[]{0, 2}; // positions = new int[]{0}; List listChannels = new ArrayList(); List listSlices = new ArrayList(); List listFrames = new ArrayList(); List listPositions = new ArrayList(); // Check if root "All" selected, if so, not need to go further TreePath[] cr = CheckboxTreeDimensions.this.checkboxTree.getCheckingRoots(); for (TreePath path : cr) { System.out.println(path.getLastPathComponent()); }// w ww .j av a 2 s . c o m System.out.println("\nPaths: "); TreePath[] cp = CheckboxTreeDimensions.this.checkboxTree.getCheckingPaths(); for (TreePath path : cp) { //Object[] pathObjs = path.getPath(); System.out.println(path.toString()); if (path.getPathCount() == 3) { DefaultMutableTreeNode nodeDim = (DefaultMutableTreeNode) path.getPathComponent(1); String dimension = (String) nodeDim.getUserObject(); DefaultMutableTreeNode nodeIndex = (DefaultMutableTreeNode) path.getPathComponent(2); Object uo = nodeIndex.getUserObject(); if (dimension.equalsIgnoreCase("Channel")) { String chan = (String) nodeIndex.getUserObject(); listChannels.add(chan); } if (dimension.equalsIgnoreCase("Z-Section")) { int index = (Integer) nodeIndex.getUserObject(); listSlices.add(index); } if (dimension.equalsIgnoreCase("TimePoint")) { int index = (Integer) nodeIndex.getUserObject(); listFrames.add(index); } if (dimension.equalsIgnoreCase("Position")) { int index = (Integer) nodeIndex.getUserObject(); listPositions.add(index); } } } // sort the list Collections.sort(listChannels); Collections.sort(listSlices); Collections.sort(listFrames); Collections.sort(listPositions); System.out.println("\nChannels: "); Iterator<Object> it = listChannels.iterator(); while (it.hasNext()) { Object obj = it.next(); System.out.print(" " + obj); } // Change list of channel names to an array of channel indices System.out.println("\nChannel Indices: "); Iterator<Object> itc = listChannels.iterator(); channels = new int[listChannels.size()]; int c = 0; while (itc.hasNext()) { Object obj = itc.next(); String channelName = (String) obj; int index = indexOfChannel(channelNamesOriginal, channelName, 0); channels[c] = index; c++; System.out.print(" " + channelName + " ::: " + index); } // String[] strArray = {"abcd", "abdc", "bcda"}; //for (String s : strArray) // if (s.startsWith(searchTerm)) // System.out.println(s); // Swap startsWith for contains you wish to simply look for containment. System.out.println("\nSlices: "); it = listSlices.iterator(); slices = new int[listSlices.size()]; c = 0; while (it.hasNext()) { Object obj = it.next(); int index = (Integer) obj; slices[c] = index; c++; System.out.print(" " + obj); } System.out.println("\nFrames: "); it = listFrames.iterator(); frames = new int[listFrames.size()]; c = 0; while (it.hasNext()) { Object obj = it.next(); int index = (Integer) obj; frames[c] = index; c++; System.out.print(" " + obj); } System.out.println("\nPositions: "); it = listPositions.iterator(); positions = new int[listPositions.size()]; c = 0; while (it.hasNext()) { Object obj = it.next(); int index = (Integer) obj; positions[c] = index; c++; System.out.print(" " + obj); } if (channels.length < 1) { promptForNoChoice("Channel"); } System.out.println(Arrays.toString(channels)); System.out.println(Arrays.toString(slices)); System.out.println(Arrays.toString(frames)); System.out.println(Arrays.toString(positions)); //CheckboxTreeExample_1.this.checkboxTree.; }
From source file:eu.europa.ec.markt.dss.applet.view.validationpolicy.EditView.java
private void registerMouseListener(final JTree tree) { MouseListener mouseAdapter = new MouseAdapter() { public void mousePressed(MouseEvent mouseEvent) { if (mouseEvent.getButton() == MouseEvent.BUTTON3) { final int selectedRow = tree.getRowForLocation(mouseEvent.getX(), mouseEvent.getY()); final TreePath selectedPath = tree.getPathForLocation(mouseEvent.getX(), mouseEvent.getY()); if (selectedRow != -1) { final XmlDomAdapterNode clickedItem = (XmlDomAdapterNode) selectedPath .getLastPathComponent(); final boolean isLeaf = tree.getModel().isLeaf(selectedPath.getLastPathComponent()); // Do nothing on root element if (selectedPath.getPathCount() > 1) { // find the allowed actions, to know if a popup menu should be displayed and the content of the popup menu + action handlers if (clickedItem.node instanceof Element) { nodeActionAdd(mouseEvent, selectedRow, selectedPath, clickedItem, tree); } else if (isLeaf) { valueLeafActionEdit(mouseEvent, selectedPath, clickedItem, tree); }/* w w w . j a v a 2s .c o m*/ } } } } }; tree.addMouseListener(mouseAdapter); }
From source file:eu.europa.esig.dss.applet.view.validationpolicy.EditView.java
private void registerMouseListener(final JTree tree) { MouseListener mouseAdapter = new MouseAdapter() { @Override/*w w w . j av a2 s.com*/ public void mousePressed(MouseEvent mouseEvent) { if (mouseEvent.getButton() == MouseEvent.BUTTON3) { final int selectedRow = tree.getRowForLocation(mouseEvent.getX(), mouseEvent.getY()); final TreePath selectedPath = tree.getPathForLocation(mouseEvent.getX(), mouseEvent.getY()); if (selectedRow != -1) { final XmlDomAdapterNode clickedItem = (XmlDomAdapterNode) selectedPath .getLastPathComponent(); final boolean isLeaf = tree.getModel().isLeaf(selectedPath.getLastPathComponent()); // Do nothing on root element if (selectedPath.getPathCount() > 1) { // find the allowed actions, to know if a popup menu should be displayed and the content of the popup menu + action handlers if (clickedItem.node instanceof Element) { nodeActionAdd(mouseEvent, selectedRow, selectedPath, clickedItem, tree); } else if (isLeaf) { valueLeafActionEdit(mouseEvent, selectedPath, clickedItem, tree); } } } } } }; tree.addMouseListener(mouseAdapter); }
From source file:com.mirth.connect.client.ui.codetemplate.CodeTemplateLibrariesPanel.java
private void initComponents(Channel channel) { setBackground(UIConstants.BACKGROUND_COLOR); selectAllLabel = new JLabel("<html><u>Select All</u></html>"); selectAllLabel.setForeground(Color.BLUE); selectAllLabel.addMouseListener(new MouseAdapter() { @Override// w w w .jav a 2 s. c om public void mouseReleased(MouseEvent evt) { for (Enumeration<? extends MutableTreeTableNode> libraryNodes = ((MutableTreeTableNode) libraryTreeTable .getTreeTableModel().getRoot()).children(); libraryNodes.hasMoreElements();) { MutableTreeTableNode libraryNode = libraryNodes.nextElement(); Triple<String, String, Boolean> triple = (Triple<String, String, Boolean>) libraryNode .getUserObject(); libraryTreeTable.getTreeTableModel().setValueAt( new MutableTriple<String, String, Boolean>(triple.getLeft(), triple.getMiddle(), true), libraryNode, libraryTreeTable.getHierarchicalColumn()); } libraryTreeTable.updateUI(); } }); selectSeparatorLabel = new JLabel("|"); deselectAllLabel = new JLabel("<html><u>Deselect All</u></html>"); deselectAllLabel.setForeground(Color.BLUE); deselectAllLabel.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent evt) { for (Enumeration<? extends MutableTreeTableNode> libraryNodes = ((MutableTreeTableNode) libraryTreeTable .getTreeTableModel().getRoot()).children(); libraryNodes.hasMoreElements();) { MutableTreeTableNode libraryNode = libraryNodes.nextElement(); Triple<String, String, Boolean> triple = (Triple<String, String, Boolean>) libraryNode .getUserObject(); libraryTreeTable.getTreeTableModel().setValueAt( new MutableTriple<String, String, Boolean>(triple.getLeft(), triple.getMiddle(), false), libraryNode, libraryTreeTable.getHierarchicalColumn()); } libraryTreeTable.updateUI(); } }); expandAllLabel = new JLabel("<html><u>Expand All</u></html>"); expandAllLabel.setForeground(Color.BLUE); expandAllLabel.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent evt) { libraryTreeTable.expandAll(); } }); expandSeparatorLabel = new JLabel("|"); collapseAllLabel = new JLabel("<html><u>Collapse All</u></html>"); collapseAllLabel.setForeground(Color.BLUE); collapseAllLabel.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent evt) { libraryTreeTable.collapseAll(); } }); final TableCellEditor libraryCellEditor = new LibraryTreeCellEditor(); libraryTreeTable = new MirthTreeTable() { @Override public TableCellEditor getCellEditor(int row, int column) { if (isHierarchical(column)) { return libraryCellEditor; } else { return super.getCellEditor(row, column); } } }; DefaultTreeTableModel model = new SortableTreeTableModel(); DefaultMutableTreeTableNode rootNode = new DefaultMutableTreeTableNode(); model.setRoot(rootNode); libraryTreeTable.setLargeModel(true); libraryTreeTable.setTreeTableModel(model); libraryTreeTable.setOpenIcon(null); libraryTreeTable.setClosedIcon(null); libraryTreeTable.setLeafIcon(null); libraryTreeTable.setRootVisible(false); libraryTreeTable.setDoubleBuffered(true); libraryTreeTable.setDragEnabled(false); libraryTreeTable.setRowSelectionAllowed(true); libraryTreeTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); libraryTreeTable.setRowHeight(UIConstants.ROW_HEIGHT); libraryTreeTable.setFocusable(true); libraryTreeTable.setOpaque(true); libraryTreeTable.setEditable(true); libraryTreeTable.setSortable(false); libraryTreeTable.setAutoCreateColumnsFromModel(false); libraryTreeTable.setShowGrid(true, true); libraryTreeTable.setTableHeader(null); if (Preferences.userNodeForPackage(Mirth.class).getBoolean("highlightRows", true)) { libraryTreeTable.setHighlighters(HighlighterFactory .createAlternateStriping(UIConstants.HIGHLIGHTER_COLOR, UIConstants.BACKGROUND_COLOR)); } libraryTreeTable.setTreeCellRenderer(new LibraryTreeCellRenderer()); libraryTreeTable.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { checkSelection(evt); } @Override public void mouseReleased(MouseEvent evt) { checkSelection(evt); } private void checkSelection(MouseEvent evt) { if (libraryTreeTable.rowAtPoint(new Point(evt.getX(), evt.getY())) < 0) { libraryTreeTable.clearSelection(); } } }); libraryTreeTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent evt) { if (!evt.getValueIsAdjusting()) { boolean visible = false; int selectedRow = libraryTreeTable.getSelectedRow(); if (selectedRow >= 0) { TreePath selectedPath = libraryTreeTable.getPathForRow(selectedRow); if (selectedPath != null) { visible = true; Triple<String, String, Boolean> triple = (Triple<String, String, Boolean>) ((MutableTreeTableNode) selectedPath .getLastPathComponent()).getUserObject(); String description = ""; if (selectedPath.getPathCount() == 2) { description = libraryMap.get(triple.getLeft()).getDescription(); } else if (selectedPath.getPathCount() == 3) { description = PlatformUI.MIRTH_FRAME.codeTemplatePanel.getCachedCodeTemplates() .get(triple.getLeft()).getDescription(); } if (StringUtils.isBlank(description) || StringUtils.equals(description, CodeTemplateUtil .getDocumentation(CodeTemplate.DEFAULT_CODE).getDescription())) { descriptionTextPane.setText( "<html><body class=\"code-template-libraries-panel\"><i>No description.</i></body></html>"); } else { descriptionTextPane.setText("<html><body class=\"code-template-libraries-panel\">" + MirthXmlUtil.encode(description) + "</body></html>"); } } } descriptionScrollPane.setVisible(visible); updateUI(); } } }); libraryTreeTableScrollPane = new JScrollPane(libraryTreeTable); descriptionTextPane = new JTextPane(); descriptionTextPane.setContentType("text/html"); HTMLEditorKit editorKit = new HTMLEditorKit(); StyleSheet styleSheet = editorKit.getStyleSheet(); styleSheet.addRule(".code-template-libraries-panel {font-family:\"Tahoma\";font-size:11;text-align:top}"); descriptionTextPane.setEditorKit(editorKit); descriptionTextPane.setEditable(false); descriptionScrollPane = new JScrollPane(descriptionTextPane); descriptionScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); descriptionScrollPane.setVisible(false); }
From source file:com.mirth.connect.client.ui.dependencies.ChannelDependenciesPanel.java
private void initComponents() { setBackground(UIConstants.BACKGROUND_COLOR); dependencyLabel = new JLabel("This channel depends upon:"); dependencyTreeTable = new MirthTreeTable(); DefaultTreeTableModel dependencyModel = new SortableTreeTableModel(); DefaultMutableTreeTableNode dependencyRootNode = new DefaultMutableTreeTableNode(); dependencyModel.setRoot(dependencyRootNode); dependencyTreeTable.setTreeTableModel(dependencyModel); dependencyTreeTable.setRootVisible(false); dependencyTreeTable.setDoubleBuffered(true); dependencyTreeTable.setDragEnabled(false); dependencyTreeTable.setRowSelectionAllowed(true); dependencyTreeTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); dependencyTreeTable.setRowHeight(UIConstants.ROW_HEIGHT); dependencyTreeTable.setFocusable(true); dependencyTreeTable.setOpaque(true); dependencyTreeTable.setEditable(false); dependencyTreeTable.setSortable(true); dependencyTreeTable.putClientProperty("JTree.lineStyle", "Horizontal"); dependencyTreeTable.setAutoCreateColumnsFromModel(false); dependencyTreeTable.setShowGrid(true, true); dependencyTreeTable.setTableHeader(null); if (Preferences.userNodeForPackage(Mirth.class).getBoolean("highlightRows", true)) { dependencyTreeTable.setHighlighters(HighlighterFactory .createAlternateStriping(UIConstants.HIGHLIGHTER_COLOR, UIConstants.BACKGROUND_COLOR)); }/*w ww . jav a2 s. c o m*/ dependencyTreeTable.setTreeCellRenderer(new DependencyTreeCellRenderer(dependencyTreeTable)); dependencyTreeTable.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { checkSelection(evt); } @Override public void mouseReleased(MouseEvent evt) { checkSelection(evt); } private void checkSelection(MouseEvent evt) { if (dependencyTreeTable.rowAtPoint(new Point(evt.getX(), evt.getY())) < 0) { dependencyTreeTable.clearSelection(); } } }); dependencyTreeTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent evt) { if (!evt.getValueIsAdjusting()) { boolean removeEnabled = false; int[] rows = dependencyTreeTable.getSelectedModelRows(); if (rows.length == 1) { TreePath selectedPath = dependencyTreeTable.getPathForRow(rows[0]); if (selectedPath != null) { if (selectedPath.getPathCount() == 2) { removeEnabled = true; } } } dependencyRemoveButton.setEnabled(removeEnabled); } } }); dependencyScrollPane = new JScrollPane(dependencyTreeTable); dependencyExpandAllLabel = new JLabel("<html><u>Expand All</u></html>"); dependencyExpandAllLabel.setForeground(Color.BLUE); dependencyExpandAllLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); dependencyExpandAllLabel.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent evt) { dependencyTreeTable.expandAll(); } }); dependencyCollapseAllLabel = new JLabel("<html><u>Collapse All</u></html>"); dependencyCollapseAllLabel.setForeground(Color.BLUE); dependencyCollapseAllLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); dependencyCollapseAllLabel.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent evt) { dependencyTreeTable.collapseAll(); } }); dependencyAddButton = new JButton("Add"); dependencyAddButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { Map<String, String> allowedChannelsMap = getAllowedChannels(true); if (MapUtils.isNotEmpty(allowedChannelsMap)) { AddDialog dialog = new AddDialog(allowedChannelsMap, true); if (dialog.wasSaved()) { for (String dependencyId : dialog.getSelectedChannelIds()) { dependencies.add(new ChannelDependency(channel.getId(), dependencyId)); } updateTreeTable(true, dependencies); updateAddButton(); } } } }); dependencyRemoveButton = new JButton("Remove"); dependencyRemoveButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { int[] rows = dependencyTreeTable.getSelectedModelRows(); if (rows.length == 1) { TreePath selectedPath = dependencyTreeTable.getPathForRow(rows[0]); if (selectedPath != null && selectedPath.getPathCount() == 2) { Pair<String, String> pair = (Pair<String, String>) ((MutableTreeTableNode) selectedPath .getLastPathComponent()).getUserObject(); for (Iterator<ChannelDependency> it = dependencies.iterator(); it.hasNext();) { ChannelDependency dependency = it.next(); if (StringUtils.equals(channel.getId(), dependency.getDependentId()) && StringUtils.equals(pair.getLeft(), dependency.getDependencyId())) { it.remove(); } } updateTreeTable(true, dependencies); updateAddButton(); } } } }); dependencyRemoveButton.setEnabled(false); dependentLabel = new JLabel("This channel is depended upon by:"); dependentTreeTable = new MirthTreeTable(); DefaultTreeTableModel dependentModel = new SortableTreeTableModel(); DefaultMutableTreeTableNode dependentRootNode = new DefaultMutableTreeTableNode(); dependentModel.setRoot(dependentRootNode); dependentTreeTable.setTreeTableModel(dependentModel); dependentTreeTable.setRootVisible(false); dependentTreeTable.setDoubleBuffered(true); dependentTreeTable.setDragEnabled(false); dependentTreeTable.setRowSelectionAllowed(true); dependentTreeTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); dependentTreeTable.setRowHeight(UIConstants.ROW_HEIGHT); dependentTreeTable.setFocusable(true); dependentTreeTable.setOpaque(true); dependentTreeTable.setEditable(false); dependentTreeTable.setSortable(true); dependentTreeTable.putClientProperty("JTree.lineStyle", "Horizontal"); dependentTreeTable.setAutoCreateColumnsFromModel(false); dependentTreeTable.setShowGrid(true, true); dependentTreeTable.setTableHeader(null); if (Preferences.userNodeForPackage(Mirth.class).getBoolean("highlightRows", true)) { dependentTreeTable.setHighlighters(HighlighterFactory .createAlternateStriping(UIConstants.HIGHLIGHTER_COLOR, UIConstants.BACKGROUND_COLOR)); } dependentTreeTable.setTreeCellRenderer(new DependencyTreeCellRenderer(dependentTreeTable)); dependentTreeTable.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { checkSelection(evt); } @Override public void mouseReleased(MouseEvent evt) { checkSelection(evt); } private void checkSelection(MouseEvent evt) { if (dependentTreeTable.rowAtPoint(new Point(evt.getX(), evt.getY())) < 0) { dependentTreeTable.clearSelection(); } } }); dependentTreeTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent evt) { if (!evt.getValueIsAdjusting()) { boolean removeEnabled = false; int[] rows = dependentTreeTable.getSelectedModelRows(); if (rows.length == 1) { TreePath selectedPath = dependentTreeTable.getPathForRow(rows[0]); if (selectedPath != null) { if (selectedPath.getPathCount() == 2) { removeEnabled = true; } } } dependentRemoveButton.setEnabled(removeEnabled); } } }); dependentScrollPane = new JScrollPane(dependentTreeTable); dependentExpandAllLabel = new JLabel("<html><u>Expand All</u></html>"); dependentExpandAllLabel.setForeground(Color.BLUE); dependentExpandAllLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); dependentExpandAllLabel.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent evt) { dependentTreeTable.expandAll(); } }); dependentCollapseAllLabel = new JLabel("<html><u>Collapse All</u></html>"); dependentCollapseAllLabel.setForeground(Color.BLUE); dependentCollapseAllLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); dependentCollapseAllLabel.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent evt) { dependentTreeTable.collapseAll(); } }); dependentAddButton = new JButton("Add"); dependentAddButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { Map<String, String> allowedChannelsMap = getAllowedChannels(false); if (MapUtils.isNotEmpty(allowedChannelsMap)) { AddDialog dialog = new AddDialog(allowedChannelsMap, false); if (dialog.wasSaved()) { for (String dependentId : dialog.getSelectedChannelIds()) { dependencies.add(new ChannelDependency(dependentId, channel.getId())); } updateTreeTable(false, dependencies); updateAddButton(); } } } }); dependentRemoveButton = new JButton("Remove"); dependentRemoveButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { int[] rows = dependentTreeTable.getSelectedModelRows(); if (rows.length == 1) { TreePath selectedPath = dependentTreeTable.getPathForRow(rows[0]); if (selectedPath != null && selectedPath.getPathCount() == 2) { Pair<String, String> pair = (Pair<String, String>) ((MutableTreeTableNode) selectedPath .getLastPathComponent()).getUserObject(); for (Iterator<ChannelDependency> it = dependencies.iterator(); it.hasNext();) { ChannelDependency dependency = it.next(); if (StringUtils.equals(pair.getLeft(), dependency.getDependentId()) && StringUtils.equals(channel.getId(), dependency.getDependencyId())) { it.remove(); } } updateTreeTable(false, dependencies); updateAddButton(); } } } }); dependentRemoveButton.setEnabled(false); }
From source file:de.erdesignerng.visual.common.OutlineComponent.java
/** * Refresh the model tree as there were changes. * * @param aModel - model/*from www . j a v a 2s.c o m*/ */ public void refresh(Model aModel) { if (aModel != null) { TreePath theSelected = tree.getSelectionPath(); DefaultMutableTreeNode theGroup = null; DefaultMutableTreeNode theSelectedNode = theSelected != null ? (DefaultMutableTreeNode) theSelected.getLastPathComponent() : null; if (theSelected != null && theSelected.getPathCount() > 1) { theGroup = (DefaultMutableTreeNode) theSelected.getPath()[1]; } Set<Object> theExpandedUserObjects = expandedUserObjects; setModel(aModel); List<TreePath> thePathsToExpand = new ArrayList<>(); TreePath theNewSelection = null; for (int theRow = 0; theRow < tree.getRowCount(); theRow++) { TreePath thePath = tree.getPathForRow(theRow); DefaultMutableTreeNode theLastNew = (DefaultMutableTreeNode) thePath.getLastPathComponent(); if (theExpandedUserObjects.contains(theLastNew.getUserObject())) { thePathsToExpand.add(thePath); } if (theSelectedNode != null) { DefaultMutableTreeNode theLastGroup = null; if (thePath.getPathCount() > 1) { theLastGroup = (DefaultMutableTreeNode) thePath.getPath()[1]; } if (theLastGroup != null && theGroup != null) { if (!theLastGroup.getUserObject().equals(theGroup.getUserObject())) { continue; } } if (theLastNew.getUserObject().equals(theSelectedNode.getUserObject())) { theNewSelection = thePath; } } } thePathsToExpand.forEach(tree::expandPath); if (theNewSelection != null) { tree.setSelectionPath(theNewSelection); tree.scrollPathToVisible(theNewSelection); } } }