f@0
|
1 /*
|
f@0
|
2 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
|
f@0
|
3
|
f@0
|
4 Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
|
f@0
|
5
|
f@0
|
6 This program is free software: you can redistribute it and/or modify
|
f@0
|
7 it under the terms of the GNU General Public License as published by
|
f@0
|
8 the Free Software Foundation, either version 3 of the License, or
|
f@0
|
9 (at your option) any later version.
|
f@0
|
10
|
f@0
|
11 This program is distributed in the hope that it will be useful,
|
f@0
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
f@0
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
f@0
|
14 GNU General Public License for more details.
|
f@0
|
15
|
f@0
|
16 You should have received a copy of the GNU General Public License
|
f@0
|
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
f@0
|
18 */
|
f@0
|
19 package uk.ac.qmul.eecs.ccmi.gui;
|
f@0
|
20
|
f@0
|
21 import java.awt.Dimension;
|
f@0
|
22 import java.awt.Frame;
|
f@0
|
23 import java.awt.GridBagLayout;
|
f@0
|
24 import java.awt.event.ActionEvent;
|
f@0
|
25 import java.awt.event.ActionListener;
|
f@0
|
26 import java.awt.event.WindowAdapter;
|
f@0
|
27 import java.awt.event.WindowEvent;
|
f@0
|
28 import java.util.List;
|
f@0
|
29 import java.util.ResourceBundle;
|
f@0
|
30 import java.util.Set;
|
f@0
|
31
|
f@0
|
32 import javax.swing.Box;
|
f@0
|
33 import javax.swing.BoxLayout;
|
f@0
|
34 import javax.swing.JButton;
|
f@0
|
35 import javax.swing.JDialog;
|
f@0
|
36 import javax.swing.JPanel;
|
f@0
|
37 import javax.swing.JScrollPane;
|
f@0
|
38 import javax.swing.JSeparator;
|
f@0
|
39 import javax.swing.JTable;
|
f@0
|
40
|
f@0
|
41 import uk.ac.qmul.eecs.ccmi.diagrammodel.NodeProperties;
|
f@0
|
42 import uk.ac.qmul.eecs.ccmi.diagrammodel.NodeProperties.Modifiers;
|
f@0
|
43 import uk.ac.qmul.eecs.ccmi.utils.GridBagUtilities;
|
f@0
|
44
|
f@0
|
45 /**
|
f@0
|
46 * A Dialog for editing the {@link NodeProperties} of a diagram node.
|
f@0
|
47 *
|
f@0
|
48 */
|
f@0
|
49 @SuppressWarnings("serial")
|
f@0
|
50 public class PropertyEditorDialog extends JDialog {
|
f@0
|
51
|
f@0
|
52 private PropertyEditorDialog(Frame parent){
|
f@0
|
53 super(parent, resources.getString("dialog.property_editor.title") , true);
|
f@0
|
54 setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
f@0
|
55
|
f@0
|
56 addWindowListener(new WindowAdapter(){
|
f@0
|
57 @Override
|
f@0
|
58 public void windowClosing(WindowEvent event){
|
f@0
|
59 result = null;
|
f@0
|
60 dispose();
|
f@0
|
61 }
|
f@0
|
62 });
|
f@0
|
63
|
f@0
|
64 listenerManager = new ListenerManager();
|
f@0
|
65
|
f@0
|
66 createComponents();
|
f@0
|
67 setContentPane(scrollPane);
|
f@0
|
68
|
f@0
|
69 panel.setLayout(new GridBagLayout());
|
f@0
|
70
|
f@0
|
71 GridBagUtilities gridBagUtils = new GridBagUtilities();
|
f@0
|
72 panel.add(topSeparator,gridBagUtils.all());
|
f@0
|
73
|
f@0
|
74 int i = 0;
|
f@0
|
75 for(String type : properties.getTypes()){
|
f@0
|
76 PropertyPanel propertyPanel = new PropertyPanel(type, properties.getValues(type), properties.getModifiers(type));
|
f@0
|
77 panel.add(propertyPanel,gridBagUtils.all());
|
f@0
|
78 propertyPanels[i++] = propertyPanel;
|
f@0
|
79 }
|
f@0
|
80
|
f@0
|
81 if(!properties.getTypes().isEmpty())
|
f@0
|
82 panel.add(bottomSeparator, gridBagUtils.all());
|
f@0
|
83 buttonPanel.add(okButton);
|
f@0
|
84 buttonPanel.add(cancelButton);
|
f@0
|
85 panel.add(buttonPanel, gridBagUtils.all());
|
f@0
|
86
|
f@0
|
87 okButton.addActionListener(listenerManager);
|
f@0
|
88 cancelButton.addActionListener(listenerManager);
|
f@0
|
89 pack();
|
f@0
|
90 }
|
f@0
|
91
|
f@0
|
92 /**
|
f@0
|
93 * A static method to show a {@code PropertyEditorDialog}. Via this dialog a user can
|
f@0
|
94 * specify the entries of a {@code NodeProperties} object and their modifiers, if any have been
|
f@0
|
95 * defined during the template creation.
|
f@0
|
96 *
|
f@0
|
97 * @param parent The parent {@code Frame} of the dialog
|
f@0
|
98 * @param properties an instance of {@code NodeProeprties} whose value will be shown in the dialog
|
f@0
|
99 * for the user to edit
|
f@0
|
100 * @return a reference to {@code properties} containing the new values entered by the user or
|
f@0
|
101 * {@code null} if the user presses the cancel button or closes the window.
|
f@0
|
102 */
|
f@0
|
103 public static NodeProperties showDialog(Frame parent, NodeProperties properties){
|
f@0
|
104 if(properties == null)
|
f@0
|
105 throw new IllegalArgumentException(resources.getString("dialog.property_editor.error.property_null"));
|
f@0
|
106 PropertyEditorDialog.properties = properties;
|
f@0
|
107 dialog = new PropertyEditorDialog(parent);
|
f@0
|
108 dialog.setLocationRelativeTo(parent);
|
f@0
|
109 dialog.setVisible(true);
|
f@0
|
110 return result;
|
f@0
|
111 }
|
f@0
|
112
|
f@0
|
113 private void createComponents(){
|
f@0
|
114 panel = new JPanel();
|
f@0
|
115 buttonPanel = new JPanel();
|
f@0
|
116 propertyPanels = new PropertyPanel[PropertyEditorDialog.properties.getTypes().size()];
|
f@0
|
117 scrollPane = new JScrollPane(
|
f@0
|
118 panel,
|
f@0
|
119 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
|
f@0
|
120 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
|
f@0
|
121 );
|
f@0
|
122 bottomSeparator = new JSeparator();
|
f@0
|
123 topSeparator = new JSeparator();
|
f@0
|
124 okButton = new JButton(resources.getString("dialog.ok_button"));
|
f@0
|
125 cancelButton = new JButton(resources.getString("dialog.cancel_button"));
|
f@0
|
126 }
|
f@0
|
127
|
f@0
|
128 private JPanel panel;
|
f@0
|
129 private JPanel buttonPanel;
|
f@0
|
130 private PropertyPanel[] propertyPanels;
|
f@0
|
131 private JScrollPane scrollPane;
|
f@0
|
132 private JSeparator topSeparator;
|
f@0
|
133 private JSeparator bottomSeparator;
|
f@0
|
134 private JButton okButton;
|
f@0
|
135 private JButton cancelButton;
|
f@0
|
136 private ListenerManager listenerManager;
|
f@0
|
137
|
f@0
|
138 private static PropertyEditorDialog dialog;
|
f@0
|
139 private static NodeProperties properties;
|
f@0
|
140 private static NodeProperties result;
|
f@0
|
141 private static ResourceBundle resources = ResourceBundle.getBundle(EditorFrame.class.getName());
|
f@0
|
142
|
f@0
|
143 private class PropertyPanel extends JPanel{
|
f@0
|
144 public PropertyPanel(String propertyType, List<String> values, final Modifiers modifiers ){
|
f@0
|
145 setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
|
f@0
|
146 model = new PropertyTableModel(propertyType, values, modifiers);
|
f@0
|
147 if(modifiers != null)
|
f@0
|
148 for(int i=0; i< values.size(); i++){
|
f@0
|
149 model.setIndexesAt(i, modifiers.getIndexes(i));
|
f@0
|
150 }
|
f@0
|
151 table = new JTable(model);
|
f@0
|
152 table.setPreferredScrollableViewportSize(new Dimension(250, 70));
|
f@0
|
153 table.setFillsViewportHeight(true);
|
f@0
|
154
|
f@0
|
155 add(new JScrollPane(table));
|
f@0
|
156 /* we can edit modifiers only if one or more modifier types have been defined for this property type */
|
f@0
|
157 if(!modifiers.isNull()){
|
f@0
|
158 editModifiers = new JButton(resources.getString("dialog.property_editor.edit_modifiers_button"));
|
f@0
|
159 editModifiers.setAlignmentX(RIGHT_ALIGNMENT);
|
f@0
|
160 add(editModifiers);
|
f@0
|
161 editModifiers.addActionListener(new ActionListener(){
|
f@0
|
162 @Override
|
f@0
|
163 public void actionPerformed(ActionEvent arg0) {
|
f@0
|
164 int row = table.getSelectedRow();
|
f@0
|
165 if((row == -1)||(row == model.getRowCount()-1))
|
f@0
|
166 return;
|
f@0
|
167 Set<Integer> indexes;
|
f@0
|
168
|
f@0
|
169 indexes = ModifierEditorDialog.showDialog(PropertyEditorDialog.this, modifiers.getTypes(), model.getIndexesAt(row));
|
f@0
|
170 model.setIndexesAt(row, indexes);
|
f@0
|
171 }
|
f@0
|
172 });
|
f@0
|
173 }
|
f@0
|
174 add(Box.createRigidArea(new Dimension(0,5)));
|
f@0
|
175 }
|
f@0
|
176
|
f@0
|
177 JTable table;
|
f@0
|
178 PropertyTableModel model;
|
f@0
|
179 JButton editModifiers;
|
f@0
|
180 }
|
f@0
|
181
|
f@0
|
182 private class ListenerManager implements ActionListener{
|
f@0
|
183 @Override
|
f@0
|
184 public void actionPerformed(ActionEvent evt) {
|
f@0
|
185 Object source = evt.getSource();
|
f@0
|
186 if(source.equals(okButton)){
|
f@0
|
187 for(int i=0; i<propertyPanels.length;i++){
|
f@0
|
188 PropertyTableModel model = propertyPanels[i].model;
|
f@0
|
189 properties.clear(model.getColumnName(0));
|
f@0
|
190 for(int j=0; j< model.getRowCount();j++){
|
f@0
|
191 String value = model.getValueAt(j, 0).toString().trim();
|
f@0
|
192 if(!value.equals("")){
|
f@0
|
193 properties.addValue(model.getColumnName(0),value , model.getIndexesAt(j));
|
f@0
|
194 }
|
f@0
|
195 }
|
f@0
|
196 }
|
f@0
|
197 result = properties;
|
f@0
|
198 dispose();
|
f@0
|
199 }else{
|
f@0
|
200 result = null;
|
f@0
|
201 dispose();
|
f@0
|
202 }
|
f@0
|
203 }
|
f@0
|
204 }
|
f@0
|
205
|
f@0
|
206 }
|