summaryrefslogtreecommitdiff
path: root/source/com/c2kernel/gui/EntityDetails.java
blob: b6c52452155d8a4b6bbe21a665dd70170fd767ea (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
package com.c2kernel.gui;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeListener;
import com.c2kernel.gui.data.NodeEntity;
import com.c2kernel.gui.tabs.EntityTabPane;
import com.c2kernel.utils.Language;
import com.c2kernel.utils.Logger;
import com.c2kernel.utils.Resource;

/**
 * The tab pane for each viewed item
 * @version $Revision: 1.38 $ $Date: 2005/06/27 15:16:14 $
 * @author  $Author: abranson $
 */
public class EntityDetails extends JPanel implements ChangeListener, Runnable {
    protected JTabbedPane myTabbedPane = new JTabbedPane(SwingConstants.BOTTOM);
    protected JPanel itemTitlePanel;
    private EntityTabManager desktopManager;
    protected NodeEntity myEntity;
    protected HashMap childPanes = new HashMap();
    protected String startTab;
    protected String startCommand = null;
    protected boolean initialized = false;

    public EntityDetails(NodeEntity thisItem) {
        super();
        startTab = MainFrame.getPref("DefaultStartTab", "Properties");
        myEntity = thisItem;
    }

    public void run() {
        Thread.currentThread().setName("Entity Pane Builder");
        EntityTabPane componentToAdd = null;
        setLayout(new BorderLayout());
		itemTitlePanel = getItemTitlePanel();
       	add(itemTitlePanel, BorderLayout.NORTH);
        add(myTabbedPane);

        // decide which tabs to create
        ArrayList requiredTabs = myEntity.getTabs();
        
        for (Iterator en = requiredTabs.iterator(); en.hasNext();) {
            String tabName = (String)en.next();
            if (tabName != null) {
                //create class instances and initialise
                Class myClass = null;
                //look up the required TabbedPane
                try {
                    myClass = Class.forName(this.getClass().getPackage().getName() + ".tabs." + tabName + "Pane");
                    Logger.msg(2, "ItemDetails.<init> - Creating ItemTabPane instance: " +
                        this.getClass().getPackage().getName() + ".tabs." + tabName + "Pane");
                    componentToAdd = (EntityTabPane)myClass.newInstance();
                } catch (ClassNotFoundException e) {
                    Logger.msg(2, "ItemDetails.<init> - No specialist tab found for " + tabName + ". Using default.");
                } catch (InstantiationException e) {
                    Logger.msg(0, "ItemDetails.<init> - Instantiation Error! " + e);
                } catch (IllegalAccessException e) {
                    Logger.msg(0, "ItemDetails.<init> - Illegal Method Access Error! Class was probably not a ItemTabPane: " + e);
                }                
                if (componentToAdd == null) componentToAdd = new EntityTabPane(tabName, null);
                componentToAdd.setParent(this);

                //adds the component to the panel
                childPanes.put(componentToAdd, new Boolean(false));

                int placement = myTabbedPane.getTabCount();
                if (tabName.equals("Properties")) // must be first
                    placement = 0;
                myTabbedPane.insertTab(componentToAdd.getTabName(), null, componentToAdd, null, placement);
            }
        }
        initialized = true;
        if (!(requiredTabs.contains(startTab))) {
            startTab = "Properties";
            startCommand = null;
        }
        runCommand(Language.translate(startTab), startCommand);
        myTabbedPane.setVisible(true);
        myTabbedPane.addChangeListener(this);
        validate();

    }

    public void stateChanged(javax.swing.event.ChangeEvent p1) {
        initialisePane((EntityTabPane)myTabbedPane.getSelectedComponent());
    }

    public void initialisePane(EntityTabPane pane) {
        Boolean isInit = (Boolean)childPanes.get(pane);
        if (isInit.booleanValue() == false) {
            Logger.msg(4,"Initialising "+pane.getTabName());
            pane.initForEntity(myEntity);
            childPanes.put(pane, new Boolean(true));
            validate();
        }
    }

    public EntityTabManager getDesktopManager() {
        return desktopManager;
    }

    public void setDesktopManager(EntityTabManager newDesktopManager) {
        desktopManager = newDesktopManager;
    }

    public JPanel getItemTitlePanel() {
        JPanel titlePanel = new JPanel();
        JComponent current;
        // Use gridbag layout for title
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        titlePanel.setLayout(gridbag);
        // Place Item Icon
        c.gridx = 0;
        c.gridy = 0;
        c.gridheight = GridBagConstraints.REMAINDER;
        c.anchor = GridBagConstraints.NORTH;
        c.ipadx = 5;
        c.ipady = 5;
        current = new JLabel(Resource.getImageResource("typeicons/"+myEntity.getIconName()+"_32.png"));
        gridbag.setConstraints(current, c);
        titlePanel.add(current);
        // Place Name/ID Label
        current = new JLabel(myEntity.getName() + " (" + myEntity.getSysKey() + ")");
        c.gridx = 1; c.gridy = 0; c.gridheight = 1;
        c.anchor = GridBagConstraints.NORTH; c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1.0; c.ipadx = 2; c.ipady = 2;
        current.setFont(new Font("Helvetica", Font.PLAIN, 18));
        gridbag.setConstraints(current, c);
        titlePanel.add(current);
        // Place Type Label
        current = new JLabel(myEntity.getType());
        c.gridx = 1; c.gridy = 2; c.gridheight = 1;
        c.anchor = GridBagConstraints.CENTER; c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1.0;
        current.setFont(new Font("Helvetica", Font.PLAIN, 12));
        gridbag.setConstraints(current, c);
        titlePanel.add(current);
        return titlePanel;
    }

    public void discardTabs() {
        myTabbedPane.removeChangeListener(this);
        myTabbedPane.removeAll();
        for (Iterator iter = childPanes.keySet().iterator(); iter.hasNext();) {
            EntityTabPane element = (EntityTabPane)iter.next();
            element.destroy();
            iter.remove();
        }
    }
    
    public int getSysKey()
    {
    	return myEntity.getSysKey();
    }
    
    public void closeTab() {
    	desktopManager.remove(myEntity.getSysKey());
    	Logger.msg(5,"Remove master Tab :"+myEntity.getType()+ " SysKey "+myEntity.getSysKey());
        myEntity.getEntity().dumpSubscriptions(0);
	}

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("close"))
			closeTab();
    }
    
    public void runCommand(String tab, String command) {
    	if (initialized) {
    		int tabIndex = findTab(tab);
            Logger.msg(3, "Running command "+tab+" "+command+" ("+tabIndex+")");
    		if (tabIndex == -1) {
                Logger.error("Tab "+tab+" not found for command "+command);
                return;
            } 
			EntityTabPane startPane = (EntityTabPane)myTabbedPane.getComponentAt(tabIndex);
			myTabbedPane.setSelectedIndex(tabIndex);
			initialisePane(startPane);
			if (command!= null) startPane.runCommand(command);
		}
		else
		{
            Logger.msg(3, "Storing command "+tab+" "+command+" until initialised.");
    		startTab = tab;
			startCommand = command;
		}
    }
    
    protected int findTab(String tabName) {
    	for (int i=0; i< myTabbedPane.getTabCount(); i++) {
            EntityTabPane thisPane = (EntityTabPane)myTabbedPane.getComponentAt(i);
    		if (thisPane.getTabName().equals(tabName))
				return i;
        }
		return -1;
    }
    

    public void refresh()
    {
    }
    /**
     *
     */
    protected void finalize() throws Throwable {
        Logger.msg(7, "EntityDetails "+myEntity.getSysKey()+" reaped");
        super.finalize();
    }

}