📄 treeview.java
字号:
// TreeView.java// Copyright (c) 1998, Regents of the University of California// $Id: TreeView.java,v 1.4 1999/12/14 06:08:13 marcel Exp $import javax.swing.*;import javax.swing.event.*;import java.awt.*;import java.awt.event.*;import java.io.*;import java.util.Vector;class FitAction extends AbstractAction {//class FitAction extends ActionListener {JDesktopPane desktop;JComponent c;public FitAction(JComponent c, JDesktopPane desktop) { this.c = c; this.desktop = desktop;}public void actionPerformed(ActionEvent e) { Insets i = desktop.getInsets(); Rectangle bounds = c.getBounds(); bounds.width = desktop.getSize().width - i.left - i.right; bounds.x = 0; c.setBounds(bounds); c.repaint();}}// This is no longer used ... 05/27/98 ...class TileAction extends AbstractAction {// class TileAction extends ActionListener {TreeView tv;public TileAction(TreeView tv) { this.tv = tv;}public void actionPerformed(ActionEvent e) { // tv.tfitAction.actionPerformed( null );// tv.globalAction.actionPerformed( null );// tv.gfitAction.actionPerformed( null );// Point location = tv.getLocation();// Rectangle bounds = tv.gv.getBounds();// location.y = bounds.y + bounds.height;// tv.setLocation(location);// tv.repaint(); tv.tile();}}public class TreeView extends JInternalFrame implements ActionListener {public static Dimension initInternalSize = new Dimension(300,300); // internal frame sizepublic JDesktopPane desktop;public TreeDisplay td;public SuperNode root;public Node rootNode;public int pathleaf;public boolean shownodes = false;public boolean showstats = false;public boolean showonlyleaf = false;public Color[] pallette;public float[] stats;public float realMinStat, realMaxStat;public float minStat, maxStat;public double ratio;public Component legend;public JScrollPane scroll;public final static String palettePath = "../treeview/palette/";public String statTitle = "Nodenum (mod) 256";public GlobalView gv;public Dimension minsize = new Dimension(50,50);// public JToolBar toolbar;public JMenuBar menubar;public EventQueue eq;public JButton cButton;public Button tButton;public ActionEvent ae;public JCheckBoxMenuItem leafMenuItem;public boolean centeroot = false;public JComponent hitcomp;public boolean centerhit = false;public Vector nodeviews;public Vector nodedumps;public DisplayPredInfo[] predsA;public DisplayPredInfo[] predsB;// public int predMutex = 0;AbstractAction nodeAction, pathAction, centerAction, statAction, paletteAction, globalAction, gfitAction, tfitAction, tileAction, cntrootAction, minAction, maxAction, onlyleafAction;// ActionListener nodeAction, pathAction, centerAction, statAction,// paletteAction, globalAction, gfitAction, tfitAction, tileAction,// cntrootAction;public void tile() { if (hitcomp == null) centeroot = true; else centerhit = true; tfitAction.actionPerformed( null ); // Point location = getLocation(); Rectangle tbounds = getBounds(); Rectangle dbounds = desktop.getBounds(); if (!gv.isClosed() && !gv.isIcon() && gv.isShowing()) { // globalAction.actionPerformed( null ); gfitAction.actionPerformed( null ); Rectangle bounds = gv.getBounds(); tbounds.y = bounds.y + bounds.height; // setLocation(location); setBounds(tbounds); } int i, j, k; Point tloc; NodeView nView; NodeDump nDump; j = 0; k = 0; for(i = 0; i < nodeviews.size(); i++) { nView = (NodeView) nodeviews.elementAt(i); if (!nView.isClosed() && !nView.isIcon() && nView.isShowing()) { tloc = nView.getLocation(); tloc.x = j * 75; if (tloc.x > dbounds.width - 10) { j = 0; tloc.x = j * 75; k++; } tloc.y = tbounds.y + tbounds.height + (k*20); j++; nView.setLocation(tloc); nView.repaint(); nView.toFront(); } } for(i = 0; i < nodedumps.size(); i++) { nDump = (NodeDump) nodedumps.elementAt(i); if (!nDump.isClosed() && !nDump.isIcon() && nDump.isShowing()) { tloc = nDump.getLocation(); tloc.x = j * 75; if (tloc.x > dbounds.width - 10) { j = 0; tloc.x = j * 75; k++; } tloc.y = tbounds.y + tbounds.height + (k*20); j++; nDump.setLocation(tloc); nDump.repaint(); nDump.toFront(); } } repaint();}public int ldColors(String filename) throws IOException { int[] r,g,b; int i; Color[] palette = new Color[256]; FileInputStream filein = new FileInputStream(filename); r = new int[256]; g = new int[256]; b = new int[256]; for (i=0; i < palette.length; i++) r[i] = filein.read(); for (i=0; i < palette.length; i++) g[i] = filein.read(); for (i=0; i < palette.length; i++) b[i] = filein.read(); for (i=0; i < palette.length; i++) { palette[i] = new Color(r[i],g[i],b[i]); // DbgOutput.println(1,palette[i]); } pallette = palette; return (256);}public int ldColors() { JFileChooser chooser = new JFileChooser(palettePath); chooser.setDialogTitle("Choose Palette"); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); boolean fileOk = false; int result = 0; while (!fileOk) { try { if (chooser.showDialog(this, "Ok") != 0) return result; result = ldColors(chooser.getSelectedFile().getPath()); fileOk = true; } catch (Exception e) { JOptionPane.showMessageDialog(this, "Error loading selected palette: " + e, "Error: Load Palette", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } } getTopLevelAncestor().repaint(); return (result);}public void rStats() { for (int i=0; i < stats.length; i++) stats[i] = i % 256;}public TreeView (JDesktopPane desktop) throws LibgistException { super("Tree View"); this.desktop = desktop; // initialize stats stats = new float[10000]; rStats(); // load pallette ... //try { // ldColors(palettePath + "earth.raw"); // set default pallette to grayscale for now ... pallette = new Color[256]; for (int i = 0; i < 256; i++) pallette[i] = new Color(i,i,i);// } catch (IOException e) {// DbgOutput.println(1,"Couldn't find earth.raw: " + e);// } // Initialize preds to size 256 .. predsA = new DisplayPredInfo[256]; predsB = new DisplayPredInfo[256]; for (int i = 0; i < 256; i++) predsA[i] = new DisplayPredInfo(); for (int i = 0; i < 256; i++) predsB[i] = new DisplayPredInfo(); eq = Toolkit.getDefaultToolkit().getSystemEventQueue(); legend = new JComponent() { public Dimension size = new Dimension(100,256 + 10); public Font font; public FontMetrics fm; public void paint(Graphics g) { if (font == null) { font = new Font("Dialog", Font.PLAIN, 10); g.setFont(font); fm = g.getFontMetrics(); } int w,j; // paint vert g.setFont(font); // i = PaintScale.vertical(g, fm, 14, 5, 260, minStat, maxStat, 10, 0); w = PaintScale.vertical(g, fm, 2, 5, 260, minStat, maxStat, 10, 1); for(j=0; j < pallette.length; j++) { g.setColor(pallette[j]); g.fillRect(w + 4, 260 - j, 10, 1); } if (size.width != w + 16) { DbgOutput.println(1,"RESIZING"); size.width = w + 16; setSize(size); // reshape( // invalidate(); // validate(); td.tv.validate(); //td.tv.repaint(); // validate(); } } public Dimension getPreferredSize() { return size; } public Dimension getMinimumSize() { return size; } }; pathleaf = -1; // Constructs internal frame for showing tree. setSize(initInternalSize); setResizable(true); setMaximizable(true); setIconifiable(true); // aFrame.setBorder(eBorder); // Testing TreeDisplay ... getContentPane().setLayout(new BorderLayout()); int rootNum; rootNode = new Node(/* rootNum = */ Libgist.getRoot(), Libgist.getNonce(Libgist.getRoot())); // DbgOutput.println(1,"RootNum" + rootNum); Node[] nodes = new Node[1]; nodes[0] = rootNode; td = new TreeDisplay(this); root = new SuperNode(nodes,0,0,td); td.setRoot(root); scroll = new JScrollPane(td); // scroll.getViewport().setBackingStoreEnabled(true); getContentPane().add(scroll, BorderLayout.CENTER); // toolbar = new JToolBar(); menubar = new JMenuBar(); statAction = new AbstractAction("Stat") { // statAction = new ActionListener("Stat") { public void actionPerformed(ActionEvent e) { if (showstats) hideStats(); else showStats(stats.length, stats, statTitle); } }; paletteAction = new AbstractAction("Pal") { // paletteAction = new ActionListener("Pal") { public void actionPerformed(ActionEvent e) { ldColors(); // for (int i = 0; i < 256; i++) // DbgOutput.println(1,pallette[i]); } }; gv = new GlobalView(td.tv); nodeviews = new Vector(); nodedumps = new Vector(); tfitAction = (AbstractAction) new FitAction(this, desktop); gfitAction = (AbstractAction) new FitAction(gv, desktop); tileAction = (AbstractAction) new TileAction(this); nodeAction = new AbstractAction("Node") { // nodeAction = new ActionListener("Node") { public void actionPerformed(ActionEvent e) { // DbgOutput.println(1,"NODE"); if (td.shownodes) td.shownodes = false; else td.shownodes = true; // td.root.panels[0].invalidate(); // td.root.invalidate(); // td.invalidate(); // getTopLevelAncestor().invalidate(); // getTopLevelAncestor().validate(); // getTopLevelAncestor().repaint(); // td.validate(); td.repaint(); } }; final JOptionPane pane = new JOptionPane("Node:", JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, null, null); final JOptionPane maxPalPane = new JOptionPane("Max Value: " + realMaxStat, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, null, null); final JOptionPane minPalPane = new JOptionPane("Min Value: " + realMinStat, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, null, null); pathAction = new AbstractAction("Path") { // pathAction = new ActionListener("Path") { public void actionPerformed(ActionEvent e) { String result; Container j = (Container) e.getSource(); // while (!(j instanceof TreeView)) // j = j.getParent(); // TreeView tv = (TreeView) j; TreeView tv = TreeView.this; int nodenum; if (pathleaf == -1) nodenum = 0; else nodenum = pathleaf; JDialog dialog = pane.createDialog(tv, "Show Path"); pane.setWantsInput(true); pane.setInitialSelectionValue(Integer.toString(nodenum)); pane.selectInitialValue(); dialog.show(); result = (String) pane.getInputValue(); int[] path = new int[100]; try { nodenum = Integer.valueOf(result).intValue(); } catch (NumberFormatException ne) { System.out.println("The node could not be located."); return; } try { Libgist.getPath(nodenum,path); } catch (LibgistException ex) { System.out.println("Could not find path to: " + nodenum); System.out.println(ex); ex.printStackTrace(); return; } try { showPath(nodenum); } catch (Exception ex) { System.out.println("Nope: " + ex); ex.printStackTrace(); return; } } }; maxAction = new AbstractAction("ScaleMaxValue") { public void actionPerformed(ActionEvent e) { String result; JDialog dialog = maxPalPane.createDialog(TreeView.this, "Set Scale Max"); maxPalPane.setMessage("Max Value: " + realMaxStat); maxPalPane.setWantsInput(true); maxPalPane.setInitialSelectionValue( Float.toString(maxStat)); maxPalPane.selectInitialValue(); dialog.show(); result = (String) maxPalPane.getInputValue(); try { float t = Float.valueOf(result).floatValue(); if (t >= minStat) { maxStat = t; if ((maxStat - minStat) <= 0) ratio = -1; else ratio = (double) 256.0 / (double)(maxStat - minStat /* + 1 */); } else System.out.println("Invalid entry"); getTopLevelAncestor().repaint(); } catch (NumberFormatException ne) { System.out.println("Invalid entry"); return; } } }; onlyleafAction = new AbstractAction("Shoe Only Leaf") { public void actionPerformed(ActionEvent e) { // if (showonlyleaf == false) // showonlyleaf = true; // else // showonlyleaf = false; showStats(stats.length, stats, statTitle); } }; minAction = new AbstractAction("ScaleMinValue") { public void actionPerformed(ActionEvent e) { String result; JDialog dialog = minPalPane.createDialog(TreeView.this, "Set Scale Min"); minPalPane.setMessage("Min Value: " + realMinStat); minPalPane.setWantsInput(true); minPalPane.setInitialSelectionValue( Float.toString(minStat)); minPalPane.selectInitialValue(); dialog.show(); result = (String) minPalPane.getInputValue(); try { float t = Float.valueOf(result).floatValue(); if (t < maxStat) { minStat = t; if ((maxStat - minStat) <= 0) ratio = -1; else ratio = (double) 256.0 / (double)(maxStat - minStat /* + 1 */); } else System.out.println("Invalid entry"); getTopLevelAncestor().repaint(); } catch (NumberFormatException ne) { System.out.println("Invalid entry"); return; } } }; // Center view on path ... centerAction = new AbstractAction("Center") { // centerAction = new ActionListener("Center") { public void actionPerformed(ActionEvent e) { Container j = (Container) e.getSource();// while (!(j instanceof TreeView))// j = j.getParent(); // TreeView tv = (TreeView) j; TreeView tv = TreeView.this; try { if (pathleaf != -1) showPath(tv.pathleaf); } catch (LibgistException ex) { return; } } }; cntrootAction = new AbstractAction("Center - root") {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -