summaryrefslogtreecommitdiff
path: root/source/com/c2kernel/gui/tabs/PropertiesPane.java
blob: 5666ae96893a44068e635e9108798744784f0e0f (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
/*
 * StatusPane.java
 *
 * Created on March 20, 2001, 3:30 PM
 */

package com.c2kernel.gui.tabs;

/**
 * @author  abranson
 * @version
 */
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.*;

import com.c2kernel.entity.C2KLocalObject;
import com.c2kernel.entity.proxy.EntityProxyObserver;
import com.c2kernel.entity.proxy.ItemProxy;
import com.c2kernel.gui.MainFrame;
import com.c2kernel.gui.data.NodeAgent;
import com.c2kernel.persistency.ClusterStorage;
import com.c2kernel.process.Gateway;
import com.c2kernel.property.Property;
import com.c2kernel.utils.Language;
import com.c2kernel.utils.Logger;

/**
 * Pane to display all work orders that this agent can execute, and activate
 * them on request from the user. Subscribes to NodeItem for Property objects.
 * @version $Revision: 1.44 $ $Date: 2005/08/31 07:21:20 $
 * @author  $Author: abranson $
 */
public class PropertiesPane extends EntityTabPane implements EntityProxyObserver, ActionListener {

    Box propertyBox;
    boolean subbed = false;
    HashMap loadedProps = new HashMap();
    JLabel domTitle;
    DomainPathAdmin domAdmin;

    public PropertiesPane() {
        super("Properties", "Properties");
        initPanel();

        // Create box container for properties
        getGridBagConstraints();
        c.gridx = 0; c.gridy = 1;
        c.anchor = GridBagConstraints.NORTHWEST;
        c.fill = GridBagConstraints.NONE;
        c.weightx = 1.0; c.weighty = 2.0;
        propertyBox = Box.createVerticalBox();
        gridbag.setConstraints(propertyBox, c);
        add(propertyBox);
        if (MainFrame.isAdmin) { // edit dompath
            c.gridy++;
            c.fill = GridBagConstraints.NONE;
            c.weighty=0.0;
            domTitle = new JLabel("Domain Paths", titleIcon, SwingConstants.LEFT);
            domTitle.setFont(titleFont);
            domTitle.setForeground(headingColor);
            gridbag.setConstraints(domTitle, c);
            add(domTitle);
            
            c.gridy++;
            c.fill = GridBagConstraints.BOTH;
            c.weighty=1.0;
            domAdmin = new DomainPathAdmin();
            gridbag.setConstraints(domAdmin, c);
            add(domAdmin);           
            
            
            if ("true".equals(Gateway.getProperty("EnableItemErase"))) {
                c.gridy++;
                c.fill = GridBagConstraints.NONE;
                JButton eraseButton = new JButton(Language.translate("Erase!"));
                eraseButton.addActionListener(this);
                eraseButton.setActionCommand("Erase Item");
                gridbag.setConstraints(eraseButton, c);
                add(eraseButton);
            }
        }
    }

    public void reload() {
    	Gateway.getStorage().clearCache(sourceEntity.getSysKey(), ClusterStorage.PROPERTY);
        loadedProps = new HashMap();
        initForEntity(sourceEntity);
    }

    public void run() {
        Thread.currentThread().setName("Property Pane Builder");
        if (sourceEntity instanceof NodeAgent) {
            remove(domAdmin);
            remove(domTitle);
        }
        else if (domAdmin != null)
            domAdmin.setEntity((ItemProxy)sourceEntity.getEntity());
        propertyBox.removeAll();
        propertyBox.add(Box.createGlue());
		revalidate();
        sourceEntity.getEntity().subscribe(this, ClusterStorage.PROPERTY, true);    
          
    }
    /**
     *
     */
    public void add(C2KLocalObject contents) {
        if (!(contents instanceof Property)) return;
        Property newProp = (Property) contents;
        JLabel propLabel = (JLabel)loadedProps.get(newProp.getName());
        if (propLabel == null) { // new prop
            JPanel summaryPanel = new JPanel(new GridLayout(0,2));
            summaryPanel.add(new JLabel(Language.translate(newProp.getName()) + ":"));
            Box valueBox = Box.createHorizontalBox();
            propLabel = new JLabel(newProp.getValue());
            loadedProps.put(newProp.getName(), propLabel);
            valueBox.add(propLabel);
            if (MainFrame.isAdmin) {
                JButton editButton = new JButton("...");
                editButton.setMargin(new Insets(0,0,0,0));
                editButton.setActionCommand(newProp.getName());
                editButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e){
                        String oldVal = ((JLabel)loadedProps.get(e.getActionCommand())).getText();
                        String newVal = (String)JOptionPane.showInputDialog(null, "Enter new value for "+e.getActionCommand(), "Edit Property", 
                            JOptionPane.QUESTION_MESSAGE, null, null, oldVal);
                        if (newVal!=null && !(newVal.equals(oldVal))) {
                            try {
                                ((ItemProxy)sourceEntity.getEntity()).setProperty(MainFrame.userAgent, e.getActionCommand(), newVal);
                            } catch (Exception ex) {
                                Logger.exceptionDialog(ex);
                            }
                        }
                    }
                });
                valueBox.add(Box.createVerticalStrut(7));
                valueBox.add(editButton);

            }
            summaryPanel.add(valueBox);
            propertyBox.add(Box.createVerticalStrut(7));
            propertyBox.add(summaryPanel);
        }
        propLabel.setText(newProp.getValue());
        revalidate();
    }

    public void remove(String id) {
        String propName = id.substring(id.lastIndexOf("/")+1);
        JLabel propbox = (JLabel)loadedProps.get(propName);
        if (propbox!= null) propbox.setText("DELETED");   
        revalidate(); 
    }

    public void actionPerformed(ActionEvent e) {
        String[] params;
        String predefStep;
        
        if (JOptionPane.showConfirmDialog(this, 
                "Are you sure?", 
                e.getActionCommand(), 
                JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION)
            return;
        
        if (e.getActionCommand().equals("Erase Item")) {
            params = new String[0];
            predefStep = "Erase";
        }
        else
            return;

        try {
            MainFrame.userAgent.execute((ItemProxy)sourceEntity.getEntity(), predefStep, params);          
        } catch (Exception ex) {
            Logger.exceptionDialog(ex);
        }
    }

}