📄 treeview.java
字号:
// cntrootAction = new ActionListener("Center - root") { public void actionPerformed(ActionEvent e) { // DbgOutput.println(1,e); center(); getTopLevelAncestor().repaint(); } }; final JDesktopPane fdesktop = desktop; globalAction = new AbstractAction("Global") { // globalAction = new ActionListner("Global") { public void actionPerformed(ActionEvent e) { // if (gv == null) { // gv = new GlobalView(td.tv); // } // DbgOutput.println(1,"GLOBAL"); if (!fdesktop.isAncestorOf(gv)) { fdesktop.add( gv, JLayeredPane.PALETTE_LAYER); } try { if (gv.isClosed()) { gv.setClosed( false ); } else if (gv.isIcon()) { gv.setIcon( false ); } else gv.setVisible( true ); } catch (Exception ex) { System.out.println("WHAT - global view: " + ex); } } }; JMenu navigatemenu = new JMenu("Navigate"); JMenu filemenu = new JMenu("Legend"); JMenu alignmenu = new JMenu("Align"); JMenuItem menuItem; // File menu ... menuItem = new JMenuItem("Choose colors"); menuItem.addActionListener(paletteAction); filemenu.add(menuItem); menuItem = new JMenuItem("Set Scale Max"); menuItem.addActionListener(maxAction); filemenu.add(menuItem); menuItem = new JMenuItem("Set Scale Min"); menuItem.addActionListener(minAction); filemenu.add(menuItem); leafMenuItem = new JCheckBoxMenuItem("Leaf Stats Only"); leafMenuItem.addActionListener(onlyleafAction); filemenu.add(leafMenuItem); menubar.add(filemenu); // Navigate menu ... menuItem = new JMenuItem("Path to node"); menuItem.addActionListener(pathAction); navigatemenu.add(menuItem); menuItem = new JMenuItem("Center on path"); menuItem.addActionListener(centerAction); navigatemenu.add(menuItem); menuItem = new JMenuItem("Center on root"); menuItem.addActionListener(cntrootAction); navigatemenu.add(menuItem); navigatemenu.addSeparator(); menuItem = new JMenuItem("Show Global View"); menuItem.addActionListener(globalAction); navigatemenu.add(menuItem); menubar.add(navigatemenu); // Align menu ... menuItem = new JMenuItem("Align Tree View"); menuItem.addActionListener(tfitAction); alignmenu.add(menuItem); menuItem = new JMenuItem("Align Global View"); menuItem.addActionListener(gfitAction); alignmenu.add(menuItem); alignmenu.addSeparator(); menuItem = new JMenuItem("Tile all"); menuItem.addActionListener(tileAction); alignmenu.add(menuItem); menubar.add(alignmenu); // toolbar.add(pathAction).setText("Path"); // toolbar.add(nodeAction).setText("Node"); // toolbar.add(centerAction).setText("Center"); // toolbar.add(statAction).setText("Stat"); // toolbar.add(paletteAction).setText("Pal"); // toolbar.add(globalAction).setText("Global"); // toolbar.add(tileAction).setText("Tile"); // toolbar.add(tfitAction).setText("Fit-TV"); // toolbar.add(gfitAction).setText("Fit-GV");// cButton = toolbar.add(cntrootAction);// cButton.setText("Center - root"); // toolbar.addActionListener(this); // This button and action is so that levels can be aligned // correctly. tButton = new Button("Dummy button"); tButton.addActionListener(this); // tButton.setText("Dummy button"); ae = new ActionEvent(tButton, ActionEvent.ACTION_PERFORMED, "fitlevels", 0); getRootPane().setJMenuBar(menubar); // toolbar.setFloatable(true); legend.setVisible(false); // getContentPane().add(toolbar, BorderLayout.NORTH); getContentPane().add(legend, BorderLayout.WEST); centeroot = true; setVisible(true);}public int getIndex(int num, Node[] nodes) { for(int i = 0; i < nodes.length; i++) if (nodes[i].nodenum == num) return i; return (-1);}public void showPath(int nodenum) throws LibgistException { try { refresh(); if ((pathleaf != nodenum) && (pathleaf >= 0)) undoPath(pathleaf); pathleaf = nodenum; centeroot = true; // must come before adjustPath ... adjustPath(); eq.postEvent(ae); // getTopLevelAncestor().repaint(0); // ActionEvent ae = new ActionEvent(tButton, //ActionEvent.ACTION_PERFORMED, "Center - root", 0); // DbgOutput.println(1,);// DbgOutput.println(1,ae);// EventQueue eqs = Toolkit.getDefaultToolkit().getSystemEventQueue();// eqs.postEvent(ae); //DbgOutput.println(1,eqs.peekEvent()); // DbgOutput.println(1,eqs);// DbgOutput.println(1,); // Thread.yield(); // notifyAll(); } catch (LibgistException e) { System.out.println(e); e.printStackTrace(); throw e; }}public void centerhitcomp(int oldloc) { JViewport view = scroll.getViewport(); Dimension size = scroll.getSize(); int newloc = ((SuperNode) hitcomp).ox; Point npt = view.getViewPosition(); npt.x += newloc - oldloc; // DbgOutput.println(1,"new: " + newloc + " " + "old: " + oldloc); // view.setBackingStoreEnabled( true ); view.setViewPosition(npt); // DbgOutput.println(1,view.isBackingStoreEnabled()); // view.validate(); centerhit = false;}public void center() { // Center on root ... JViewport view = scroll.getViewport(); Dimension size = scroll.getSize(); Insets in = scroll.getInsets(); size.width -= in.left + in.right; size.height -= in.top + in.bottom; Point rl = new Point(root.ox,root.oy); Dimension rs = root.getSize(); // DbgOutput.println(1,view.getViewPosition()); // DbgOutput.println(1,size + " " + rl); rl.x -= (size.width - rs.width) / 2; rl.y = 0; // DbgOutput.println(1,size + " " + rl); // view.setBackingStoreEnabled( true ); view.setViewPosition(rl); // view.validate(); centeroot = false; // view.validate(); // view.repaint(); // scroll.validate(); // scroll.repaint(); // validate(); // getTopLevelAncestor().repaint(); // DbgOutput.println(1,root.getLocation()); // DbgOutput.println(1,root.ox + ", " + root.oy); // DbgOutput.println(1,view.getViewPosition()); // DbgOutput.println(1,); }public void fitlevels() { Dimension size = scroll.getSize(); Insets in = scroll.getInsets(); size.width -= in.left + in.right; int i,j,num; for (i = 0; i < td.ui.levels.size(); i++) { Vector sibs = (Vector) td.ui.levels.elementAt(i); num = sibs.size(); DbgOutput.println(1,"level: " + i + " " + "size: " + num); SuperNode node; int leftover = 0; int istart = 0, iend, rwidth; for (j = 0; j < num; j++) { node = (SuperNode) sibs.elementAt(j); node.resizeRect.setBounds(node.getBounds()); iend = (int) Math.floor(((float)( j + 1 ) * (float) size.width) / (float) num); node.resizeRect.width = iend - istart - 4 /* border */ + leftover - td.ui.sibDist; rwidth = node.resizePanels(false, true); if (rwidth < node.resizeRect.width) leftover = node.resizeRect.width - rwidth; // else // DbgOutput.println(1,"Over: " + (rwidth - node.resizeRect.width)); istart = iend; }// lw += ((SuperNode) sibs.elementAt(j)).widthbelow;// if ( lw < size.width } }public void showStats(int count, float[] stats, String label) { statTitle = label; if (pallette == null) { JOptionPane.showMessageDialog(this, "Error no palette selected", "Error: No Palette", JOptionPane.ERROR_MESSAGE); return; } this.setTitle("Tree View: " + label); showStats(count, stats);}public void showStats(int count, float[] stats) { // DbgOutput.println(1,"count: " + count); this.stats = stats; minStat = stats[1]; maxStat = stats[1]; int level; for (int i=1; i < stats.length; i++) { level = Libgist.getLevel(i); if (level == -1) break; if (Float.isInfinite(stats[i])) continue; if (Float.isNaN(stats[i])) continue; if (leafMenuItem.getState() && (level != 1)) continue; if (minStat > stats[i]) minStat = stats[i]; else if (maxStat < stats[i]) maxStat = stats[i]; } if ((maxStat - minStat) <= 0) ratio = -1; else ratio = (double) 256.0 / (double)(maxStat - minStat /* + 1 */); realMinStat = minStat; realMaxStat = maxStat; showstats = true; legend.setVisible(true); Dimension sz = getSize(); if (sz.height < getMinimumSize().height) sz.height = getMinimumSize().height; setSize(sz); validate(); getTopLevelAncestor().repaint();}public void hideStats() { this.setTitle("Tree View"); legend.setVisible(false); showstats = false; validate();}private void undoPath(int nodenum) throws LibgistException { // assuming the nodes on the path exist. int i; int[] path = new int[100]; int pl = Libgist.getPath(nodenum, path); SuperNode sn = root; int nindex = 0; for (i=0; i < pl; i++) { nindex = getIndex(path[pl - i - 1],sn.nodes); sn.onpath = false; sn.pathchild = -1; sn.nodes[nindex].onpath = false; // sn.panels[nindex-sn.min].node.onpath = false; sn = sn.children[nindex]; }}public int[] path = new int[100];public int pl = 0;public void adjustPath() throws LibgistException { NodeView tView; int i; pl = Libgist.getPath(pathleaf, path); Node[] offspring; SuperNode sn = root; int nindex = 0; Node tempNode; for (i = 0; i < pl; i++) { nindex = getIndex(path[pl - i - 1],sn.nodes); if ((nindex < sn.min) || (nindex >= sn.min + sn.numPanels)) sn.adjustPanels(nindex); if (i+1 != pl) { sn.panels[nindex-sn.min].open = true; sn.panels[nindex-sn.min].node.open = true; } sn.open = true; sn.onpath = true; sn.panels[nindex-sn.min].node.onpath = true; // clean this up later ... tempNode = sn.panels[nindex-sn.min].node; for (int j=0; j < tempNode.topViews.length; j++) { tView = tempNode.topViews[j]; if (tView != null) { tView.pl = pl; tView.showPathChild(); tView.repaint(); } } for (int j = 0; j < sn.numPanels; j++) if ((sn.panels[j].node.onpath == false) || (i+1 == pl)) if (!sn.isVisible()) { sn.panels[j].open = false; if (sn.children[sn.panels[j].id] != null) sn.children[sn.panels[j].id].open = false; } sn.setVisible(true); if ((sn.children[nindex] == null) && (i+1 != pl)) { offspring = sn.nodes[nindex].getChildren(); sn.children[nindex] = new SuperNode(offspring,0,0,td,sn,nindex); sn.pathchild = nindex;// if (sn.pathchild == 0)// DbgOutput.println(1,"HERE: " +// sn.panels[0].node.nodenum + " " + sn.pathchild); sn = sn.children[nindex]; // new SuperNode(offspring,0,0,td); td.add(sn); } else { if (i+1 != pl) { sn.pathchild = nindex;// if (sn.pathchild == 0)// DbgOutput.println(1,"HERE2: " +// sn.panels[0].node.nodenum + " " + sn.pathchild +// " " + i + " " + pl + " " + path[pl - i - 1]); } sn = sn.children[nindex]; } } validate(); getTopLevelAncestor().repaint();}public void refresh() throws LibgistException { if (root.children[0] != null) refreshChildren(root.children[0], root.nodes[0]); else if (rootNode.nonce != Libgist.getNonce(rootNode.nodenum)) { rootNode = new Node(Libgist.getRoot(), Libgist.getNonce(Libgist.getRoot())); root.nodes[0] = rootNode; root.min = 0; if (rootNode.nDump != null) { rootNode.nDump.refresh(); DbgOutput.println(1,"Refresh: " + rootNode.nodenum); } } validate(); getTopLevelAncestor().repaint();}public void refreshChildren(SuperNode sn, Node parent) throws LibgistException { int i, j; int childnum = sn.nodes.length; // System.out.println("RefreshChildren: " + parent.nodenum); if (parent.nonce != Libgist.getNonce(parent.nodenum)) { parent.nonce = Libgist.getNonce(parent.nodenum); if (parent.nDump != null) { parent.nDump.refresh(); DbgOutput.println(1,"Refresh: " + parent.nodenum); } if (parent.nViews != null) { for (i=0; i < parent.nViews.size(); i++) ((NodeView) parent.nViews.elementAt(i)).repaint(); } int[] childnos = new int[Libgist.maxChildren()]; Node[] nodes = sn.nodes; SuperNode[] children = sn.children; childnum = Libgist.getChildren(parent.nodenum, childnos); Node[] newnodes = new Node[childnum]; SuperNode[] newchildren = new SuperNode[childnum]; for(i=0; i < childnum; i++) { if ( (j = getIndex(childnos[i], nodes)) > 0) { newnodes[i] = nodes[j]; newchildren[i] = children[j]; } else { newnodes[i] = new Node(childnos[i], Libgist.getNonce(childnos[i])); newchildren[i] = null; } } sn.nodes = newnodes; sn.children = children; if (sn.min > childnum - sn.numPanels) sn.min = childnum - sn.numPanels; sn.adjustPanels(sn.min); parent.childnum = childnum; } for(i=0; i < childnum; i++) if (sn.children[i] != null) refreshChildren(sn.children[i], sn.nodes[i]); else { // DbgOutput.println(1,"Checking leaf: " + sn.nodes[i].nodenum); if (sn.nodes[i].nonce != Libgist.getNonce(sn.nodes[i].nodenum)) { sn.nodes[i].nonce = Libgist.getNonce(sn.nodes[i].nodenum); if (sn.nodes[i].nDump != null) { sn.nodes[i].nDump.refresh(); DbgOutput.println(1,"Refresh: " + sn.nodes[i].nodenum); } if (sn.nodes[i].nViews != null) { for (i=0; i < sn.nodes[i].nViews.size(); i++) ((NodeView) sn.nodes[i].nViews.elementAt(i)).repaint(); } } // else {// if (sn.nodes[i].nodenum == 2) {// System.out.println("Not refresh: " + sn.nodes[i].nodenum);// System.out.println("Nonce: " + sn.nodes[i].nonce);// } // } }}public void dispose() { if (gv != null) { gv.setVisible(false); gv.dispose(); } int i; NodeView nView; NodeDump nDump; for (i = 0; i < nodeviews.size(); i++) { nView = (NodeView) nodeviews.elementAt(i); nView.setVisible(false); nView.dispose(); } for (i = 0; i < nodedumps.size(); i++) { nDump = (NodeDump) nodedumps.elementAt(i); nDump.setVisible(false); nDump.dispose(); } super.dispose();}public Dimension getMinimumSize() { if (showstats) { if (minsize.height < legend.getMinimumSize().height + 60) { minsize.height = legend.getMinimumSize().height + 60; } } else { minsize.width = minsize.height = 50; } return minsize;}public void actionPerformed(ActionEvent e) { //center(); //repaint(); centerhit = true; if (hitcomp == null) hitcomp = root; fitlevels(); validate(); repaint();}public DisplayPredInfo[] doublePredSize(DisplayPredInfo preds[]) { int size = preds.length; DisplayPredInfo[] newPreds = new DisplayPredInfo[2*size]; int i; // copy objects from original array ... for (i = 0; i < size; i++) newPreds[i] = preds[i]; // create a bunch of new entries ... for (i = size; i < 2*size; i++) newPreds[i] = new DisplayPredInfo(); return newPreds;}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -