summaryrefslogtreecommitdiff
path: root/source/com/c2kernel/gui/data/Node.java
blob: b82ee92d63697caca18f0073c2e56c4a70b577f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package com.c2kernel.gui.data;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.tree.DefaultMutableTreeNode;

import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.gui.DynamicTreeBuilder;
import com.c2kernel.gui.EntityTabManager;
import com.c2kernel.lookup.AgentPath;
import com.c2kernel.lookup.Path;
import com.c2kernel.process.Gateway;
import com.c2kernel.utils.Language;
import com.c2kernel.utils.Logger;
import com.c2kernel.utils.Resource;

public abstract class Node implements Runnable {

    protected Path binding;
    protected DefaultMutableTreeNode treeNode;
    protected String name; // domain key
    protected int sysKey; // target item
    // attributes
    protected String type = "";
    protected Icon icon;
    protected boolean isExpandable = false;
    protected HashMap childNodes = new HashMap();
    protected ArrayList subscribers = new ArrayList();
    protected DynamicTreeBuilder loader = null;
    private boolean loaded = false;
    private String iconName;
    protected EntityTabManager desktop;
    static ImageIcon folder = Resource.getImageResource("folder.png");
    static ImageIcon emptyLeaf = Resource.getImageResource("leaf.png");

    public Node() {
    }
    
    protected void createTreeNode() {
        this.treeNode = new DefaultMutableTreeNode(this);
    }
    
    public Node(Path path, EntityTabManager desktop) {
        this.binding = path;
        this.desktop = desktop;
        this.sysKey = path.getSysKey();
        // get the name of this node (last path element)
        String[] pathComponents = path.getPath();
        if (pathComponents.length > 0)
            this.name = pathComponents[pathComponents.length-1];
        else
            this.name = Gateway.getProperty("Name");
    }
    
    public EntityTabManager getDesktop() {
        return desktop;
    }

    public Node newNode(Path path)
    {
        try {
            if (path.getEntity() instanceof AgentPath)
                return new NodeAgent(path, desktop);
            else
                return new NodeItem(path, desktop);
        } catch (ObjectNotFoundException ex) {
            return new NodeContext(path, desktop);
        }  

    }

    /** Inserts a tree builder as the first child of the node, so it can be opened in the tree
    */
    public void makeExpandable() {
        if (isExpandable) return;
        loader = new DynamicTreeBuilder(this.treeNode);
        this.treeNode.insert(loader.getTreeNode(),0);
        isExpandable = true;
    }


    public DefaultMutableTreeNode getTreeNode() {
        return treeNode;
    }

    public void setTreeNode(DefaultMutableTreeNode treeNode) {
        this.treeNode = treeNode;
        treeNode.setUserObject(this);
    }

    /** Subscription for loading node children.
      * Note this is separate from the itemproxy subscription as it included query of the naming service
      * and eventually should not require access to the item at all for higher performance */
    public void subscribeNode(NodeSubscriber target) {
        subscribers.add(target);
        if (loaded == false) {
            loaded = true;
            loadMore();
        }
        else {
            synchronized (childNodes) {
                Node newNode;
                for (Iterator nodes = childNodes.values().iterator(); nodes.hasNext();) {
                    newNode = (Node)nodes.next();
                    Logger.msg("subscribeNode target.add("+newNode.name+")");
                    target.add(newNode);
                }
            }
        }
    }
    
    public void loadMore() {
        Thread loading = new Thread(this);
        loading.start();
    }

    public void unsubscribeNode(NodeSubscriber target) {
        subscribers.remove(target);
    }

    public void add(Node newNode) {
        synchronized(childNodes) {
            childNodes.put(newNode.getPath(), newNode);
            for (Iterator e = subscribers.iterator(); e.hasNext();) {
                NodeSubscriber thisSub = (NodeSubscriber)e.next();

                thisSub.add(newNode);
            }
        }
    }
    
    public void remove(Path oldPath) {
        synchronized(childNodes) {
            childNodes.remove(oldPath);
            for (Iterator e = subscribers.iterator(); e.hasNext();) {
                NodeSubscriber thisSub = (NodeSubscriber)e.next();
                thisSub.remove(oldPath);
            }
        }
    }
    
    public void removeAllChildren() {
        synchronized(childNodes) {
            Path thisPath;
            while (childNodes.keySet().iterator().hasNext()) {
                remove((Path)childNodes.keySet().iterator().next());
            }
        }
    }

    public Node getChildNode(Path itsPath) {
        for (Iterator i = childNodes.keySet().iterator(); i.hasNext();) {
            Object next = i.next(); 
            if ( next.equals(itsPath) ) return (Node)childNodes.get(next);
        }
        return null;
    }

    // end of current batch
    public void end(boolean more) {
        for (Iterator e = subscribers.iterator(); e.hasNext();) {
            NodeSubscriber thisSub = (NodeSubscriber)e.next();
            thisSub.end(more);
        }
    }


    public void run() {
        Thread.currentThread().setName("Node Loader: "+name);
        loadChildren();
    }

    public abstract void loadChildren();
    
    public void refresh() {
        removeAllChildren();
        loadChildren();
    }
    
    // Getters and Setters

    public int getSysKey() { return sysKey; }
//  public void setSysKey( int sysKey ) { this.sysKey = sysKey; }

    public String getName() { return name; }
//  public void setName( String name ) { this.name = name; }

    public String getType() { return type; }
//  public void setType( String type ) { this.type = type; }

    public Path getPath() { return binding; }

    public DynamicTreeBuilder getTreeBuilder() { return loader; }
    
    public String toString() {
        if (this.name.length() > 0) {
            return this.name;
        }
        else { return "Cristal"; }
    }

    public Icon getIcon() {
        if (icon != null) return icon;
        return(isExpandable?folder:emptyLeaf);
    }

    public String getIconName() {
        return iconName;
    }
    
    public void setIcon(String icon) {
        iconName = icon;
        this.icon = Resource.getImageResource("typeicons/"+icon+"_16.png");
    }

	public JPopupMenu getPopupMenu() {
		JPopupMenu popup = new JPopupMenu();
		JMenuItem menuItem = new JMenuItem(Language.translate("Refresh"));
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (isExpandable) refresh();
			}
		});
		popup.add(menuItem);
		return popup;
	}
}