summaryrefslogtreecommitdiff
path: root/source/com/c2kernel/graph/view/VertexPropertyPanel.java
blob: dd5fbd2ee31fb5fe139ea7224a3061e32beeb3db (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
235
236
237
238
239
240
241
242
243
244
245
package com.c2kernel.graph.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

import com.c2kernel.graph.event.EntireModelChangedEvent;
import com.c2kernel.graph.event.SelectionChangedEvent;
import com.c2kernel.graph.model.*;
import com.c2kernel.gui.tabs.EntityTabPane;
import com.c2kernel.utils.Language;
import com.c2kernel.utils.Logger;

/**************************************************************************
 *
 * $Revision: 1.4 $
 * $Date: 2005/09/09 12:19:28 $
 *
 * Copyright (C) 2003 CERN - European Organization for Nuclear Research
 * All rights reserved.
 **************************************************************************/

public class VertexPropertyPanel extends JPanel implements Observer, TableModelListener, ActionListener {

    private PropertyTableModel mPropertyModel;
    private PropertyTable mPropertyTable;
    private GraphModelManager mGraphModelManager;
    private boolean isEditable = false;
    GridBagLayout gridbag = new GridBagLayout();
    protected JLabel selObjName;
    protected JLabel selObjClass;
    JButton addPropButton;
    JButton delPropButton;
    Box newPropBox;
    private JTextField newPropName;
    private JComboBox<String> newPropType;
    String[] typeOptions = { "String", "Boolean", "Integer", "Float" };
    String[] typeInitVal = { "", "false", "0", "0.0"};
    SelectedVertexPanel mSelPanel;
    
    public VertexPropertyPanel() {
        super();
        setLayout(gridbag);
        mPropertyModel = new PropertyTableModel();
        mPropertyModel.addTableModelListener(this);
        mPropertyTable = new PropertyTable(mPropertyModel);
    }

    /**
    *
    */

    public void update(Observable o, Object arg) {
        Vertex[] selectedVertices = null;
        DirectedEdge selectedEdge = null;
        // If the selection has changed
        if (arg instanceof SelectionChangedEvent)
        {
            SelectionChangedEvent event = (SelectionChangedEvent) arg;
            selectedVertices = event.mSelection.mVertices;
            if (selectedVertices != null)
            {
                if (selectedVertices.length == 1)
                {
                    setVertex(selectedVertices[0]);
                    return;
                }
            }
            selectedEdge = event.mSelection.mEdge;
            if (selectedEdge != null)
            {
                setEdge(selectedEdge);
                return;
            }
        }
        if (arg instanceof SelectionChangedEvent || arg instanceof EntireModelChangedEvent){
            clear();
        }
    }
    

    public void tableChanged(TableModelEvent e) {
        if (mGraphModelManager!=null)
            mGraphModelManager.forceNotify();

    }
    
    public void setVertex(Vertex vert) {
        if (vert.getName().equals("domain"))
            selObjName.setText("Domain Workflow");
        else
            selObjName.setText(vert.getName());
        String className = vert.getClass().getName();
        selObjClass.setText(className.substring(className.lastIndexOf('.')+1));
        if (mSelPanel != null) mSelPanel.select(vert);
        if (vert instanceof GraphableVertex) {
            mPropertyModel.setMap(((GraphableVertex)vert).getProperties());
            addPropButton.setEnabled(isEditable);
            delPropButton.setEnabled(isEditable);
        }
    }
    
    public void setEdge(DirectedEdge edge) {
        selObjName.setText(edge.getName());
        String className = edge.getClass().getName();
        selObjClass.setText(className.substring(className.lastIndexOf('.')+1));
        if (edge instanceof GraphableEdge) {
            mPropertyModel.setMap(((GraphableEdge)edge).getProperties());
            addPropButton.setEnabled(isEditable);
            delPropButton.setEnabled(isEditable);
        }
        if (mSelPanel != null) mSelPanel.clear();
    }
    
    public void clear() {
        selObjName.setText("");
        selObjClass.setText("Nothing Selected");
        mPropertyModel.setMap(new HashMap<String, Object>());
        if (mSelPanel != null) mSelPanel.clear();
        addPropButton.setEnabled(false);
        delPropButton.setEnabled(false);
    }
    
    /**
     * @param isEditable The isEditable to set.
     */
    public void setEditable(boolean editable) {
        mPropertyModel.setEditable(editable);
        isEditable = editable;
        newPropBox.setVisible(editable);
    }
    
    public void setGraphModelManager(GraphModelManager manager) {
        mGraphModelManager = manager;
        manager.addObserver(this);
    }
    
    public void createLayout(SelectedVertexPanel selPanel)
    {
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1;
        c.weighty = 0;
        c.anchor = GridBagConstraints.NORTHWEST;
        c.ipadx = 5;
        c.ipady = 5;
        
        selObjName = new JLabel();
        selObjName.setFont(EntityTabPane.titleFont);
        gridbag.setConstraints(selObjName, c);
        add(selObjName); 
        
        c.gridy++;
        selObjClass = new JLabel();
        gridbag.setConstraints(selObjClass, c);
        add(selObjClass);
        
        c.gridy++;
        JLabel title = new JLabel("Properties");
        title.setFont(EntityTabPane.titleFont);
        gridbag.setConstraints(title, c);
        add(title);       
        
        c.gridy++;
        c.fill = GridBagConstraints.BOTH;
        c.weighty = 2;
        JScrollPane scroll = new JScrollPane(mPropertyTable);
        gridbag.setConstraints(scroll, c);
        add(scroll);
        
        newPropBox = Box.createHorizontalBox();
        newPropBox.add(new JLabel(Language.translate("New :")));
        newPropBox.add(Box.createHorizontalGlue());
        newPropName = new JTextField(15);
        newPropBox.add(newPropName);
        newPropType = new JComboBox<String>(typeOptions);
        newPropBox.add(newPropType);
        newPropBox.add(Box.createHorizontalStrut(1));
        addPropButton = new JButton("Add");
        addPropButton.setMargin(new Insets(0, 0, 0, 0));
        delPropButton = new JButton("Del");
        delPropButton.setMargin(new Insets(0, 0, 0, 0));
        addPropButton.addActionListener(this);
        delPropButton.addActionListener(this);
        newPropBox.add(addPropButton);
        newPropBox.add(delPropButton);
        
        c.gridy++;
        c.weighty=0;
        c.fill= GridBagConstraints.HORIZONTAL;
        gridbag.setConstraints(newPropBox, c);
        add(newPropBox);
        
        if (selPanel != null) {
            c.gridy++;
            mSelPanel = selPanel;
            gridbag.setConstraints(mSelPanel, c);
            add(mSelPanel);
        }
    }
    
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == addPropButton) {
            if (newPropName.getText().length() < 1) {
                JOptionPane.showMessageDialog(this, "Enter a name for the new property", "Cannot add property", JOptionPane.ERROR_MESSAGE);
                return;
            }
            if (mPropertyModel.sourceMap.containsKey(newPropName.getText())) {
                JOptionPane.showMessageDialog(this, "Property '"+newPropName.getText()+"' already exists.", "Cannot add property", JOptionPane.ERROR_MESSAGE);
                return;
            }
            if (mPropertyTable.getCellEditor() != null)
            	mPropertyTable.getCellEditor().stopCellEditing();

            try {
                Class<?> newPropClass = Class.forName("java.lang."+typeOptions[newPropType.getSelectedIndex()]);
                Class[] params = {String.class};
                Constructor init = newPropClass.getConstructor(params);
                Object[] initParams = { typeInitVal[newPropType.getSelectedIndex()] };
                mPropertyModel.addProperty(newPropName.getText(), init.newInstance(initParams));
            } catch (Exception ex) {
                Logger.exceptionDialog(ex);
            }
        }
        else if (e.getSource() == delPropButton) {
            int selrow = mPropertyTable.getSelectedRow();
            if (selrow == -1) {
                JOptionPane.showMessageDialog(this, "Select a property to remove", "Cannot delete property", JOptionPane.ERROR_MESSAGE);
                return;
            }
            mPropertyModel.delProperty(mPropertyModel.sortedNameList.get(selrow));
        }
    }
}