001 /* 002 * Copyright (C) 2008-2010 by Holger Arndt 003 * 004 * This file is part of the Universal Java Matrix Package (UJMP). 005 * See the NOTICE file distributed with this work for additional 006 * information regarding copyright ownership and licensing. 007 * 008 * UJMP is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation; either version 2 011 * of the License, or (at your option) any later version. 012 * 013 * UJMP is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public 019 * License along with UJMP; if not, write to the 020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 021 * Boston, MA 02110-1301 USA 022 */ 023 024 package org.ujmp.core.treematrix; 025 026 import java.util.ArrayList; 027 import java.util.Collection; 028 import java.util.List; 029 030 import javax.swing.event.TreeModelListener; 031 import javax.swing.tree.TreePath; 032 033 import org.ujmp.core.doublematrix.stub.AbstractSparseDoubleMatrix2D; 034 035 public abstract class AbstractTreeMatrix extends AbstractSparseDoubleMatrix2D implements TreeMatrix { 036 private static final long serialVersionUID = 7731771819651651188L; 037 038 public boolean contains(long... coordinates) { 039 return false; 040 } 041 042 public final boolean isChild(Object parent, Object child) { 043 return getChildren(parent).contains(child); 044 } 045 046 public final boolean isChild(int parentId, int childId) { 047 Object parent = getObject(parentId); 048 Object child = getObject(childId); 049 return isChild(parent, child); 050 } 051 052 @SuppressWarnings("unchecked") 053 public final Object getObject(int index) { 054 if (getObjectList() instanceof List) { 055 return ((List) getObjectList()).get(index); 056 } else { 057 // TODO: improve 058 return new ArrayList(getObjectList()).get(index); 059 } 060 } 061 062 063 public Object getParent(Object o) { 064 return getParentMap().get(o); 065 } 066 067 068 public void addChildren(Object parent, Collection<? extends Object> children) { 069 for (Object child : children) { 070 addChild(parent, child); 071 } 072 } 073 074 public final long[] getSize() { 075 return new long[] { getObjectList().size(), getObjectList().size() }; 076 } 077 078 public final double getDouble(int row, int column) { 079 return isChild(row, column) ? 1.0 : 0.0; 080 } 081 082 public final double getDouble(long row, long column) { 083 return getDouble((int) row, (int) column); 084 } 085 086 public final void addChild(Object parent, Object child) { 087 if (!getObjectList().contains(child)) { 088 getObjectList().add(child); 089 } 090 getChildren(parent).add(child); 091 getParentMap().put(child, parent); 092 notifyGUIObject(); 093 } 094 095 public final void removeChild(Object parent, Object child) { 096 getChildren(parent).remove(child); 097 Object oldParent = getParentMap().get(child); 098 if (parent.equals(oldParent)) { 099 getParentMap().remove(child); 100 } 101 notifyGUIObject(); 102 } 103 104 public final void setDouble(double value, long row, long column) { 105 setDouble(value, (int) row, (int) column); 106 } 107 108 public void setDouble(double value, int row, int column) { 109 Object parent = getObject(row); 110 Object child = getObject(column); 111 if (value == 0.0) { 112 removeChild(parent, child); 113 } else { 114 addChild(parent, child); 115 } 116 } 117 118 public void addTreeModelListener(TreeModelListener l) { 119 } 120 121 public final Object getChild(Object parent, int index) { 122 return getChildren(parent).get(index); 123 } 124 125 public final int getChildCount(Object parent) { 126 return getChildren(parent).size(); 127 } 128 129 public final int getIndexOfChild(Object parent, Object child) { 130 return getChildren(parent).indexOf(child); 131 } 132 133 public final boolean isLeaf(Object node) { 134 return getChildren(node).size() == 0; 135 } 136 137 public final void removeTreeModelListener(TreeModelListener l) { 138 } 139 140 public final void valueForPathChanged(TreePath path, Object newValue) { 141 } 142 143 public final int getNumberOfObjects() { 144 return getObjectList().size(); 145 } 146 147 public void addObject(Object o) { 148 getObjectList().add(o); 149 } 150 151 152 public final StorageType getStorageType() { 153 return StorageType.TREE; 154 } 155 156 }