home *** CD-ROM | disk | FTP | other *** search
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.Event;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.Panel;
- import java.awt.Scrollbar;
- import java.util.Vector;
-
- public class Outline extends Panel {
- public static final int FIRST_CHILD = 0;
- public static final int LAST_CHILD = 1;
- public static final int OLDER_SIBLING = 2;
- public static final int YOUNGER_SIBLING = 3;
- public static final int EXPAND_BOX = 0;
- public static final int ICON_BOX = 1;
- public static final int TEXT_BOX = 2;
- public static String DEFAULT_FONT = new String("Courier");
- public static int DEFAULT_FONT_SIZE = 12;
- public static Color DEFAULT_BK_COLOR;
- public static Color DEFAULT_FR_COLOR;
- public static Color DEFAULT_SC_COLOR;
- public static Color DEFAULT_SL_BK_COLOR;
- public static Color DEFAULT_SL_FR_COLOR;
- private static boolean IS_BROKEN_SCROLL_BAR;
- OutlineOwner Owner;
- OutlineNode RootNode;
- int NumNodes;
- Vector VisibleNodes;
- OutlineNode CurrentNode;
- boolean DoUpdate;
- int NodeHeight;
- int NodesInCanvas;
- int TopNode;
- int HorzShift;
- private OutlineCanvas OutlinePane;
- private Scrollbar VertScroll;
- private Scrollbar HorzScroll;
- private Thread mainThread;
-
- public Outline(OutlineOwner owner) {
- IS_BROKEN_SCROLL_BAR = System.getProperty("os.name").indexOf("indows") != -1 && System.getProperty("java.version").compareTo("1.0.2") > 0;
- this.Owner = owner;
- this.RootNode = new OutlineNode(this);
- this.NumNodes = 0;
- this.TopNode = 0;
- this.HorzShift = 0;
- this.DoUpdate = true;
- this.VisibleNodes = new Vector();
- this.CurrentNode = null;
- ((Container)this).setLayout(new BorderLayout());
- this.OutlinePane = new OutlineCanvas(this);
- this.VertScroll = new Scrollbar(1);
- this.HorzScroll = new Scrollbar(0);
- ((Container)this).add("East", this.VertScroll);
- ((Container)this).add("South", this.HorzScroll);
- ((Container)this).add("Center", this.OutlinePane);
- this.setFont(DEFAULT_FONT, DEFAULT_FONT_SIZE);
- this.setColors(DEFAULT_BK_COLOR, DEFAULT_FR_COLOR, DEFAULT_SL_BK_COLOR, DEFAULT_SL_FR_COLOR, DEFAULT_SC_COLOR);
- }
-
- public int getNodes() {
- return this.NumNodes;
- }
-
- public int getCurrentNode() {
- return this.CurrentNode == null ? -1 : this.CurrentNode.getIndex();
- }
-
- public int getTopmostNode() {
- return this.TopNode;
- }
-
- public int getPageSize() {
- return this.NodesInCanvas;
- }
-
- public boolean setUpdating(boolean update) {
- boolean temp = this.DoUpdate;
- this.DoUpdate = update;
- if (this.DoUpdate && !temp) {
- this.OutlinePane.repaint();
- }
-
- return temp;
- }
-
- public void expandWholeOutline() {
- ((Component)this).disable();
- boolean updating = this.setUpdating(false);
-
- for(int i = 0; i < this.NumNodes; ++i) {
- this.expandNode(i, true);
- }
-
- ((Component)this).enable();
- this.setUpdating(updating);
- }
-
- public void clearOutline() {
- for(int i = this.getFirstChild(-1); i != -1; i = this.getFirstChild(-1)) {
- this.deleteNode(i);
- }
-
- }
-
- public void addNode(int RelativeIdx, int relation, OutlineNodeData Data) {
- if (RelativeIdx >= -1 && RelativeIdx < this.NumNodes) {
- if (RelativeIdx != -1 || relation <= 1) {
- OutlineNode relative = this.getNodeByIndex(RelativeIdx);
- OutlineNode node = new OutlineNode(this, Data);
- node.attach(relative, relation);
- this.reindex();
- this.createVisibleList();
- this.updateOutline();
- }
- }
- }
-
- public void deleteNode(int idx) {
- if (idx >= 0 && idx < this.NumNodes) {
- OutlineNode nd = this.getNodeByIndex(idx);
- nd.detach();
- nd.delete();
- this.reindex();
- this.createVisibleList();
- this.updateOutline();
- }
- }
-
- public void expandNode(int idx, boolean expand) {
- OutlineNode nd = this.getNodeByIndex(idx);
- if (nd != null) {
- boolean exp = nd.expanded();
- if (exp != expand) {
- nd.expand(expand);
- if (exp != nd.expanded()) {
- if (nd.expanded()) {
- this.Owner.nodeExpanded(this, idx);
- } else {
- this.Owner.nodeCollapsed(this, idx);
- }
-
- this.createVisibleList();
- this.updateOutline();
- }
- }
- }
-
- }
-
- public boolean setCurrentNode(int idx) {
- OutlineNode nd = this.getNodeByIndex(idx);
- if (nd == null) {
- return false;
- } else if (this.CurrentNode != null && this.CurrentNode.getIndex() == idx) {
- return false;
- } else {
- this.Owner.nodeSelected(this, idx);
- int visIdx = this.VisibleNodes.indexOf(nd);
- if (visIdx == -1) {
- this.CurrentNode = nd;
- boolean updating = this.setUpdating(false);
-
- for(OutlineNode var6 = this.CurrentNode.ParentNode; var6.ParentNode != null; var6 = var6.ParentNode) {
- var6.expand(true);
- }
-
- this.createVisibleList();
- this.adjustScrolling();
- this.setUpdating(updating);
- } else {
- if (!this.OutlinePane.FontSet) {
- this.CurrentNode = nd;
- return true;
- }
-
- Graphics g = this.OutlinePane.getGraphics();
- if (g == null) {
- return true;
- }
-
- this.OutlinePane.selectColorsFonts(g);
- if (this.CurrentNode != null) {
- this.drawCurrent(g, this.CurrentNode.getIndex(), false);
- }
-
- this.CurrentNode = nd;
- this.drawCurrent(g, this.CurrentNode.getIndex(), true);
- g.dispose();
- }
-
- this.getCurrentNodeInCanvas();
- return true;
- }
- }
-
- public void getCurrentNodeInCanvas() {
- int visIdx = this.VisibleNodes.indexOf(this.CurrentNode);
- if (visIdx < this.TopNode) {
- this.TopNode = visIdx;
- } else {
- if (visIdx < this.TopNode + this.NodesInCanvas) {
- return;
- }
-
- this.TopNode = visIdx - this.NodesInCanvas + 1;
- if (this.TopNode > this.VisibleNodes.size() - this.NodesInCanvas + 1) {
- this.TopNode = this.VisibleNodes.size() - this.NodesInCanvas + 1;
- }
- }
-
- this.updateOutline();
- }
-
- public boolean expanded(int idx) {
- return this.getNodeByIndex(idx).expanded();
- }
-
- public int getParent(int idx) {
- return this.getNodeByIndex(idx).getParent();
- }
-
- public int getFirstChild(int idx) {
- return this.getNodeByIndex(idx).getFirstChild();
- }
-
- public int getOlderSibling(int idx) {
- return this.getNodeByIndex(idx).getOlderSibling();
- }
-
- public int getYoungerSibling(int idx) {
- return this.getNodeByIndex(idx).getYoungerSibling();
- }
-
- public int getNodeDepth(int idx) {
- return this.getNodeByIndex(idx).getDepth();
- }
-
- public OutlineNodeData getNodeData(int idx) {
- return this.getNodeByIndex(idx).getData();
- }
-
- public void setFont(String name, int size) {
- this.OutlinePane.FontName = new String(name);
- this.OutlinePane.FontSize = size;
- this.OutlinePane.FontSet = false;
- }
-
- public void setColors(Color bk, Color fr, Color bksel, Color frsel, Color frsc) {
- this.OutlinePane.setBackground(bk);
- this.OutlinePane.backColor = new Color(bk.getRGB());
- this.OutlinePane.foreColor = new Color(fr.getRGB());
- this.OutlinePane.foreScColor = new Color(frsc.getRGB());
- this.OutlinePane.backSelColor = new Color(bksel.getRGB());
- this.OutlinePane.foreSelColor = new Color(frsel.getRGB());
- }
-
- public void PrintOutline() {
- for(int i = 0; i < this.NumNodes; ++i) {
- OutlineNode node = this.getNodeByIndex(i);
- System.out.println("" + node.NodeDepth + " " + node.NodeData.NodeTitle());
- }
-
- }
-
- public boolean handleEvent(Event evt) {
- return evt.id != 605 && evt.id != 602 && evt.id != 601 && evt.id != 604 && evt.id != 603 ? super.handleEvent(evt) : this.handleScroll(evt);
- }
-
- private boolean handleScroll(Event evt) {
- if (evt.target == this.VertScroll) {
- this.TopNode = this.VertScroll.getValue();
- this.repaintOutline();
- }
-
- if (evt.target == this.HorzScroll) {
- this.HorzShift = this.HorzScroll.getValue();
- this.repaintOutline();
- }
-
- return true;
- }
-
- public void resize(int width, int height) {
- super.resize(width, height);
- this.updateOutline();
- }
-
- public void resize(Dimension d) {
- super.resize(d);
- ((Container)this).layout();
- this.repaintOutline();
- }
-
- public void reshape(int x, int y, int width, int height) {
- super.reshape(x, y, width, height);
- ((Container)this).layout();
- this.updateOutline();
- }
-
- public boolean keyDown(Event evt, int key) {
- if (this.CurrentNode == null) {
- return true;
- } else {
- int visIdx = this.VisibleNodes.indexOf(this.CurrentNode);
- if (key == 1005) {
- if (visIdx == this.VisibleNodes.size() - 1) {
- return true;
- } else {
- int idx = ((OutlineNode)this.VisibleNodes.elementAt(visIdx + 1)).getIndex();
- this.setCurrentNode(idx);
- return true;
- }
- } else if (key == 1004) {
- if (visIdx == 0) {
- return true;
- } else {
- int idx = ((OutlineNode)this.VisibleNodes.elementAt(visIdx - 1)).getIndex();
- this.setCurrentNode(idx);
- return true;
- }
- } else if (key != 1003 && key != 32) {
- if (key != 1002 && key != 8) {
- if (key == 1007) {
- if (this.HorzScroll.getValue() >= this.HorzScroll.getMaximum() - this.HorzScroll.getVisible()) {
- return true;
- } else {
- this.HorzScroll.setValue(this.HorzScroll.getValue() + 1);
- this.HorzShift = this.HorzScroll.getValue();
- this.repaintOutline();
- return true;
- }
- } else if (key == 1006) {
- this.HorzScroll.setValue(this.HorzScroll.getValue() - 1);
- this.HorzShift = this.HorzScroll.getValue();
- this.repaintOutline();
- return true;
- } else if (key == 43) {
- this.expandNode(this.CurrentNode.getIndex(), true);
- return true;
- } else if (key == 45) {
- this.expandNode(this.CurrentNode.getIndex(), false);
- return true;
- } else if (key == 10) {
- this.Owner.nodeDoubleClicked(this, this.CurrentNode.getIndex(), 2);
- return true;
- } else {
- return super.keyDown(evt, key);
- }
- } else if (visIdx < this.NodesInCanvas) {
- return true;
- } else {
- int idx = ((OutlineNode)this.VisibleNodes.elementAt(visIdx - this.NodesInCanvas)).getIndex();
- this.setCurrentNode(idx);
- return true;
- }
- } else if (visIdx >= this.VisibleNodes.size() - this.NodesInCanvas) {
- return true;
- } else {
- int idx = ((OutlineNode)this.VisibleNodes.elementAt(visIdx + this.NodesInCanvas)).getIndex();
- this.setCurrentNode(idx);
- return true;
- }
- }
- }
-
- void outlineClick(int x, int y, int numClicks) {
- int idx = this.getNodeAtPos(x, y);
- if (idx != -1) {
- System.out.println("Click event. " + numClicks + " clicks");
- if (this.setCurrentNode(idx)) {
- numClicks = 1;
- }
-
- int clickPos = x / this.NodeHeight;
- clickPos += this.HorzShift;
- if (clickPos == this.getNodeDepth(idx)) {
- if (numClicks % 2 == 1) {
- this.expandNode(idx, !this.expanded(idx));
- return;
- }
- } else if (clickPos == this.getNodeDepth(idx) + 1) {
- if (this.getNodeData(idx).NodeIcon() != null) {
- if (numClicks > 1) {
- this.Owner.nodeDoubleClicked(this, idx, 1);
- return;
- }
-
- return;
- }
-
- if (numClicks > 1) {
- this.Owner.nodeDoubleClicked(this, idx, 2);
- return;
- }
- } else if (clickPos > this.getNodeDepth(idx) + 1 && numClicks > 1) {
- this.Owner.nodeDoubleClicked(this, idx, 2);
- }
-
- }
- }
-
- void drawOutline(Graphics g) {
- if (this.DoUpdate) {
- int selIdx;
- if (this.CurrentNode != null) {
- selIdx = this.VisibleNodes.indexOf(this.CurrentNode);
- } else {
- selIdx = -1;
- }
-
- int topIdx = this.NodesInCanvas + this.TopNode;
-
- for(int i = this.TopNode; i < topIdx && i < this.VisibleNodes.size(); ++i) {
- this.drawNode(g, i, i == selIdx);
- }
-
- }
- }
-
- private void updateOutline() {
- this.adjustScrolling();
- this.repaintOutline();
- }
-
- private void repaintOutline() {
- if (this.DoUpdate) {
- this.OutlinePane.repaint();
- }
-
- }
-
- private synchronized void drawNode(Graphics g, int idx, boolean select) {
- if (this.DoUpdate) {
- if (idx >= this.TopNode && idx < this.TopNode + this.NodesInCanvas) {
- OutlineNode nd = (OutlineNode)this.VisibleNodes.elementAt(idx);
- Dimension d = new Dimension(this.OutlinePane.bounds().width, this.NodeHeight);
- if (select || !nd.getData().IsSecondary()) {
- nd.drawNode(g, idx - this.TopNode, d, select, this.OutlinePane.foreSelColor, this.OutlinePane.backSelColor);
- return;
- }
-
- nd.drawNode(g, idx - this.TopNode, d, true, this.OutlinePane.foreScColor, this.OutlinePane.backColor);
- }
-
- }
- }
-
- private void drawCurrent(Graphics g, int idx, boolean select) {
- if (this.DoUpdate) {
- OutlineNode nd = this.getNodeByIndex(idx);
- int visIdx = this.VisibleNodes.indexOf(nd);
- if (visIdx >= this.TopNode && visIdx < this.TopNode + this.NodesInCanvas) {
- Dimension d = new Dimension(this.OutlinePane.bounds().width, this.NodeHeight);
- if (select) {
- nd.drawNode(g, visIdx - this.TopNode, d, true, this.OutlinePane.foreSelColor, this.OutlinePane.backSelColor);
- } else if (nd.getData().IsSecondary()) {
- nd.drawNode(g, visIdx - this.TopNode, d, true, this.OutlinePane.foreScColor, this.OutlinePane.backColor);
- } else {
- nd.drawNode(g, visIdx - this.TopNode, d, true, this.OutlinePane.foreColor, this.OutlinePane.backColor);
- }
- }
- }
- }
-
- private OutlineNode getNodeByIndex(int idx) {
- return idx >= -1 && idx < this.NumNodes ? this.RootNode.getDescendantWithIndex(idx) : null;
- }
-
- private void reindex() {
- this.NumNodes = this.RootNode.reindexChildren(-1);
- }
-
- private void createVisibleList() {
- this.VisibleNodes = new Vector(this.NumNodes);
- this.RootNode.getVisibleDescendants(this.VisibleNodes);
- this.VisibleNodes.trimToSize();
- }
-
- void adjustScrolling() {
- int paneHeight = this.OutlinePane.bounds().height;
- if (this.NodeHeight == 0) {
- this.NodesInCanvas = 0;
- } else {
- this.NodesInCanvas = paneHeight / this.NodeHeight;
- }
-
- if (this.VisibleNodes.size() > this.NodesInCanvas && this.NodesInCanvas != 0) {
- this.VertScroll.enable();
- if (this.TopNode + this.NodesInCanvas >= this.VisibleNodes.size()) {
- this.TopNode = this.VisibleNodes.size() - this.NodesInCanvas;
- }
-
- int thumbSize;
- if (IS_BROKEN_SCROLL_BAR) {
- thumbSize = 0;
- } else {
- thumbSize = this.NodesInCanvas;
- }
-
- this.VertScroll.setValues(this.TopNode, thumbSize, 0, this.VisibleNodes.size() - this.NodesInCanvas);
- this.VertScroll.setPageIncrement(this.NodesInCanvas);
- } else {
- this.TopNode = 0;
- this.VertScroll.disable();
- }
-
- if (this.OutlinePane.TextFontMetrics != null) {
- int maxWidth = 0;
-
- for(int i = 0; i < this.VisibleNodes.size(); ++i) {
- OutlineNode nd = (OutlineNode)this.VisibleNodes.elementAt(i);
- String text = nd.getData().NodeTitle();
- int width = this.OutlinePane.TextFontMetrics.stringWidth(text);
- width += (nd.getDepth() + 1) * this.NodeHeight;
- if (nd.getData().NodeIcon() != null) {
- width += this.NodeHeight;
- }
-
- if (width > maxWidth) {
- maxWidth = width;
- }
- }
-
- if (maxWidth >= this.OutlinePane.bounds().width && this.NodeHeight != 0) {
- this.HorzScroll.enable();
- int scrWidth = this.OutlinePane.bounds().width / this.NodeHeight;
- maxWidth /= this.NodeHeight;
- int thumbSize;
- if (IS_BROKEN_SCROLL_BAR) {
- thumbSize = 0;
- } else {
- thumbSize = scrWidth;
- }
-
- this.HorzScroll.setValues(this.HorzScroll.getValue(), thumbSize, 0, maxWidth - scrWidth);
- this.HorzScroll.setPageIncrement(scrWidth);
- } else {
- this.HorzScroll.disable();
- }
- }
- }
-
- public int getNodeAtPos(int x, int y) {
- int listPos = y / this.NodeHeight + this.TopNode;
- return listPos >= this.VisibleNodes.size() ? -1 : ((OutlineNode)this.VisibleNodes.elementAt(listPos)).getIndex();
- }
-
- public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h) {
- return this.OutlinePane.imageUpdate(img, flags, x, y, w, h);
- }
-
- static {
- DEFAULT_BK_COLOR = Color.white;
- DEFAULT_FR_COLOR = Color.black;
- DEFAULT_SC_COLOR = Color.red;
- DEFAULT_SL_BK_COLOR = Color.blue.darker();
- DEFAULT_SL_FR_COLOR = Color.white;
- }
- }
-