Java Code Examples for javax.swing.tree.TreeModel#addTreeModelListener()

The following examples show how to use javax.swing.tree.TreeModel#addTreeModelListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultOutlineModel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Creates a new instance of DefaultOutlineModel.  <strong><b>Note</b> 
 * Do not fire table structure changes from the wrapped TableModel (value
 * changes are okay).  Changes that affect the number of rows must come
 * from the TreeModel.
 * @param treeModel The tree model
 * @param tableModel The table model
 * @param largeModel <code>true</code> if it's a large model tree, <code>false</code> otherwise.
 * @param nodesColumnLabel Label of the node's column
 */
protected DefaultOutlineModel(TreeModel treeModel, TableModel tableModel, boolean largeModel, String nodesColumnLabel) {
    this.treeModel = treeModel;
    this.tableModel = tableModel;
    if (nodesColumnLabel != null) {
        this.nodesColumnLabel = nodesColumnLabel;
    }
    
    layout = largeModel ? (AbstractLayoutCache) new FixedHeightLayoutCache() 
        : (AbstractLayoutCache) new VariableHeightLayoutCache();
        
    broadcaster = new EventBroadcaster (this);
    
    layout.setRootVisible(true);
    layout.setModel(this);
    treePathSupport = new TreePathSupport(this, layout);
    treePathSupport.addTreeExpansionListener(broadcaster);
    treePathSupport.addTreeWillExpandListener(broadcaster);
    treeModel.addTreeModelListener(broadcaster);
    tableModel.addTableModelListener(broadcaster);
    if (tableModel instanceof ProxyTableModel) {
        ((ProxyTableModel) tableModel).setOutlineModel(this);
    }
}
 
Example 2
Source File: DebuggingViewComponent.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void createTreeView() {
    synchronized (lock) {
        releaseTreeView();
        treeView = new DebugTreeView();
        treeView.setRootVisible(false);
        treeView.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        treeView.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        treeView.getAccessibleContext().setAccessibleName(NbBundle.getMessage(DebuggingViewComponent.class, "DebuggingViewComponent.treeView.AccessibleContext.accessibleName")); // NOI18N
        treeView.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(DebuggingViewComponent.class, "DebuggingViewComponent.treeView.AccessibleContext.accessibleDescription")); // NOI18N
        treeView.getTree().addMouseWheelListener(this);
        treeView.addTreeExpansionListener(this);
        TreeModel model = treeView.getTree().getModel();
        model.addTreeModelListener(this);
        treeView.getViewport().addChangeListener(this);
        treeView.getTree().setScrollsOnExpand(false);
        mainPanel.add(treeView, BorderLayout.CENTER);
    }
}
 
Example 3
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
private MainPanel() {
  super(new BorderLayout());
  JTree tree = new JTree() {
    @Override public void updateUI() {
      setCellRenderer(null);
      setCellEditor(null);
      super.updateUI();
      // ???#1: JDK 1.6.0 bug??? Nimbus LnF
      setCellRenderer(new CheckBoxNodeRenderer());
      setCellEditor(new CheckBoxNodeEditor());
    }
  };
  TreeModel model = tree.getModel();
  DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
  // Java 9: Collections.list(root.breadthFirstEnumeration()).stream()
  Collections.list((Enumeration<?>) root.breadthFirstEnumeration()).stream()
    .filter(DefaultMutableTreeNode.class::isInstance)
    .map(DefaultMutableTreeNode.class::cast)
    .forEach(n -> n.setUserObject(new CheckBoxNode(Objects.toString(n.getUserObject(), ""), Status.DESELECTED)));

  model.addTreeModelListener(new CheckBoxStatusUpdateListener());

  tree.setEditable(true);
  tree.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));

  tree.expandRow(0);
  // tree.setToggleClickCount(1);

  setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  add(new JScrollPane(tree));
  setPreferredSize(new Dimension(320, 240));
}
 
Example 4
Source File: DefaultTreeCheckingModel.java    From importer-exporter with Apache License 2.0 5 votes vote down vote up
/**
        * Sets the specified tree model. The current cheking is cleared.
        */
   public void setTreeModel(TreeModel newModel) {
TreeModel oldModel = this.model;
if (oldModel != null) {
    oldModel.removeTreeModelListener(this.propagateCheckingListener);
}
this.model = newModel;
if (newModel != null) {
    newModel.addTreeModelListener(this.propagateCheckingListener);
}
clearChecking();
   }
 
Example 5
Source File: Preferences.java    From pdfxtk with Apache License 2.0 5 votes vote down vote up
synchronized void apply(JTree tree) {
     expanded = new boolean[expandedPaths.length];

     expandAll(tree);
     
     TreeModel model = tree.getModel();
     if(model != null)
model.addTreeModelListener(new JTreeExpander(this, tree));
   }
 
Example 6
Source File: MainWindow.java    From mzmine2 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void addRawDataTreeListener(TreeModelListener listener) {
  TreeModel model = getMainPanel().getRawDataTree().getModel();
  model.addTreeModelListener(listener);
}
 
Example 7
Source File: MainWindow.java    From mzmine2 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void addPeakListTreeListener(TreeModelListener listener) {
  TreeModel model = getMainPanel().getPeakListTree().getModel();
  model.addTreeModelListener(listener);
}
 
Example 8
Source File: FacadeInvalidator.java    From ganttproject with GNU General Public License v3.0 4 votes vote down vote up
public FacadeInvalidator(TreeModel treeModel, List<GanttProjectBase.RowHeightAligner> rowHeightAligners) {
  isValid = false;
  treeModel.addTreeModelListener(this);
  myRowAligners = Preconditions.checkNotNull(rowHeightAligners);
}
 
Example 9
Source File: MainPanel.java    From java-swing-tips with MIT License 4 votes vote down vote up
private MainPanel() {
  super(new BorderLayout());
  JTree tree = new JTree() {
    @Override public void updateUI() {
      setCellRenderer(null);
      setCellEditor(null);
      super.updateUI();
      // ???#1: JDK 1.6.0 bug??? Nimbus LnF
      setCellRenderer(new CheckBoxNodeRenderer());
      setCellEditor(new CheckBoxNodeEditor());
    }
  };
  TreeModel model = tree.getModel();
  DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
  // Java 9: Collections.list(root.breadthFirstEnumeration()).stream()
  Collections.list((Enumeration<?>) root.breadthFirstEnumeration()).stream()
    .filter(DefaultMutableTreeNode.class::isInstance)
    .map(DefaultMutableTreeNode.class::cast)
    .forEach(n -> n.setUserObject(new CheckBoxNode(Objects.toString(n.getUserObject(), ""), Status.DESELECTED)));

  model.addTreeModelListener(new CheckBoxStatusUpdateListener());

  tree.setEditable(true);
  tree.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));

  tree.expandRow(0);
  // tree.setToggleClickCount(1);

  JButton save = new JButton("save");
  save.addActionListener(e -> {
    try {
      File file = File.createTempFile("output", ".xml");
      String[] constructorPropertyNames = {"label", "status"};
      // try (XMLEncoder xe = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(file)))) {
      try (XMLEncoder xe = new XMLEncoder(new BufferedOutputStream(Files.newOutputStream(file.toPath())))) {
        xe.setPersistenceDelegate(CheckBoxNode.class, new DefaultPersistenceDelegate(constructorPropertyNames));
        xe.writeObject(tree.getModel());
      }
      try (Reader r = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
        textArea.read(r, "temp");
      }
    } catch (IOException ex) {
      ex.printStackTrace();
      textArea.setText(ex.getMessage());
    }
  });

  JButton load = new JButton("load");
  load.addActionListener(e -> {
    String text = textArea.getText();
    if (text.isEmpty()) {
      return;
    }
    // try (XMLDecoder xd = new XMLDecoder(new BufferedInputStream(new FileInputStream(new File("output.xml"))))) {
    byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
    try (XMLDecoder xd = new XMLDecoder(new BufferedInputStream(new ByteArrayInputStream(bytes)))) {
      DefaultTreeModel m = (DefaultTreeModel) xd.readObject();
      m.addTreeModelListener(new CheckBoxStatusUpdateListener());
      tree.setModel(m);
    }
  });

  Box box = Box.createHorizontalBox();
  box.add(Box.createHorizontalGlue());
  box.add(save);
  box.add(Box.createHorizontalStrut(4));
  box.add(load);

  JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
  sp.setResizeWeight(.5);
  sp.setTopComponent(new JScrollPane(tree));
  sp.setBottomComponent(new JScrollPane(textArea));

  add(sp);
  add(box, BorderLayout.SOUTH);
  setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  setPreferredSize(new Dimension(320, 240));
}