summaryrefslogtreecommitdiff
path: root/src/main/java/org/cristalise/gui/tabs/CollectionPane.java
blob: c2dce76b292514bc8885de5246cd12229723bde5 (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
package org.cristalise.gui.tabs;
import java.awt.GridBagConstraints;
import java.util.StringTokenizer;

import javax.swing.JTabbedPane;

import org.cristalise.gui.tabs.collection.AggregationView;
import org.cristalise.gui.tabs.collection.CollectionView;
import org.cristalise.gui.tabs.collection.DependencyView;
import org.cristalise.kernel.collection.Aggregation;
import org.cristalise.kernel.collection.Collection;
import org.cristalise.kernel.collection.CollectionDescription;
import org.cristalise.kernel.collection.CollectionMember;
import org.cristalise.kernel.collection.Dependency;
import org.cristalise.kernel.entity.proxy.MemberSubscription;
import org.cristalise.kernel.entity.proxy.ProxyObserver;
import org.cristalise.kernel.persistency.ClusterStorage;
import org.cristalise.kernel.process.Gateway;
import org.cristalise.kernel.utils.Logger;

/**
 * @version $Revision: 1.36 $ $Date: 2005/10/06 06:51:15 $
 * @author $Author: abranson $
 */
public class CollectionPane extends ItemTabPane implements ProxyObserver<Collection<? extends CollectionMember>>
{
    JTabbedPane collTabs;

    public CollectionPane()
	{
		super("Collection", "Item Collection");
		createLayout();
	}

    @Override
	public void add(Collection<? extends CollectionMember> contents)
	{
        Logger.msg(5, "Got "+contents.getName()+": "+contents.getClass().getName());
        Logger.msg(7, "Looking for existing "+contents.getName());
		CollectionView<? extends CollectionMember> thisCollView = findTabForCollName(contents.getName());
        if (thisCollView == null){
            if (contents instanceof Aggregation) {
                AggregationView thisAggView = new AggregationView();
                thisAggView.setItem(sourceItem.getItem());
                thisAggView.setCollection((Aggregation)contents);
                thisCollView = thisAggView;
            }
            else if (contents instanceof Dependency) {
            	DependencyView thisDepView = new DependencyView();
            	thisDepView.setItem(sourceItem.getItem());
            	thisDepView.setCollection((Dependency)contents);
                thisCollView = thisDepView;
            }
            else {
                Logger.error("Collection type "+contents.getClass().getName()+" not known");
                return;
            }
            Logger.msg(3, "Adding new "+thisCollView.getClass().getName());
            collTabs.add(contents.getName()+(contents instanceof CollectionDescription?"*":""), thisCollView);
        }
	}

    @Override
	public void remove(String id)
	{

	}

    private CollectionView<? extends CollectionMember> findTabForCollName(String collName) {
        CollectionView<? extends CollectionMember> thisCollView = null;
        for (int i = 0; i < collTabs.getTabCount(); i++) {
            String tabName = collTabs.getTitleAt(i);
            if (tabName.equals(collName)) {
                thisCollView = (CollectionView<? extends CollectionMember>)collTabs.getComponentAt(i);
            }
        }
        return thisCollView;
    }

    protected void createLayout()
	{
		initPanel();
		// Add the collection tab pane
		getGridBagConstraints();
		c.gridx = 0;
		c.gridy = 1;
		c.fill = GridBagConstraints.BOTH;
		c.weighty = 2.0;
		collTabs = new JTabbedPane();
		gridbag.setConstraints(collTabs, c);
		add(collTabs);
	}

	@Override
	public void run()
	{
		Thread.currentThread().setName("Collection Loader");
		sourceItem.getItem().subscribe(new MemberSubscription<Collection<?>>(this, ClusterStorage.COLLECTION, false));
        try {
            String collNames = sourceItem.getItem().queryData(ClusterStorage.COLLECTION+"/all");
            StringTokenizer tok = new StringTokenizer(collNames, ",");
            while (tok.hasMoreTokens()) {
                Collection<?> thisLastColl = (Collection<?>) sourceItem.getItem().getObject(ClusterStorage.COLLECTION+"/"+tok.nextToken()+"/last");
                add(thisLastColl);
            }
        } catch (Exception e) {
        	Logger.error(e);
            Logger.msg(2, "Error loading collections");
        }
	}

	@Override
	public void reload()
	{
		Gateway.getStorage().clearCache(sourceItem.getItemPath(), ClusterStorage.COLLECTION);
        collTabs.removeAll();
		initForItem(sourceItem);
	}

	@Override
	public void control(String control, String msg) {
	}
}