diff java/src/uk/ac/qmul/eecs/ccmi/gui/PropertyEditorDialog.java @ 0:78b7fc5391a2

first import, outcome of NIME 2014 hackaton
author Fiore Martin <f.martin@qmul.ac.uk>
date Tue, 08 Jul 2014 16:28:59 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/java/src/uk/ac/qmul/eecs/ccmi/gui/PropertyEditorDialog.java	Tue Jul 08 16:28:59 2014 +0100
@@ -0,0 +1,206 @@
+/*  
+ CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
+  
+ Copyright (C) 2011  Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+package uk.ac.qmul.eecs.ccmi.gui;
+
+import java.awt.Dimension;
+import java.awt.Frame;
+import java.awt.GridBagLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.util.List;
+import java.util.ResourceBundle;
+import java.util.Set;
+
+import javax.swing.Box;
+import javax.swing.BoxLayout;
+import javax.swing.JButton;
+import javax.swing.JDialog;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JSeparator;
+import javax.swing.JTable;
+
+import uk.ac.qmul.eecs.ccmi.diagrammodel.NodeProperties;
+import uk.ac.qmul.eecs.ccmi.diagrammodel.NodeProperties.Modifiers;
+import uk.ac.qmul.eecs.ccmi.utils.GridBagUtilities;
+
+/**
+ * A Dialog for editing the {@link NodeProperties} of a diagram node.  
+ *
+ */
+@SuppressWarnings("serial")
+public class PropertyEditorDialog extends JDialog {
+	
+	private PropertyEditorDialog(Frame parent){
+		super(parent, resources.getString("dialog.property_editor.title") , true);
+		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
+		
+		addWindowListener(new WindowAdapter(){
+			@Override
+			public void windowClosing(WindowEvent event){
+				result = null;
+				dispose();
+			}
+		});
+		
+		listenerManager = new ListenerManager();
+		
+		createComponents();
+		setContentPane(scrollPane);
+		
+		panel.setLayout(new GridBagLayout());
+		
+		GridBagUtilities gridBagUtils = new GridBagUtilities(); 
+		panel.add(topSeparator,gridBagUtils.all());
+		
+		int i = 0;
+		for(String type : properties.getTypes()){
+			PropertyPanel propertyPanel = new PropertyPanel(type, properties.getValues(type), properties.getModifiers(type));
+			panel.add(propertyPanel,gridBagUtils.all());
+			propertyPanels[i++] = propertyPanel;
+		}
+		
+		if(!properties.getTypes().isEmpty())
+			panel.add(bottomSeparator, gridBagUtils.all());
+		buttonPanel.add(okButton);
+		buttonPanel.add(cancelButton);
+		panel.add(buttonPanel, gridBagUtils.all());
+		
+		okButton.addActionListener(listenerManager);
+		cancelButton.addActionListener(listenerManager);
+		pack();
+	}
+	
+	/**
+	 * A static method to show a {@code PropertyEditorDialog}. Via this dialog a user can 
+	 * specify the entries of a {@code NodeProperties} object and their modifiers, if any have been 
+	 * defined during the template creation.  
+	 * 
+	 * @param parent The parent {@code Frame} of the dialog 
+	 * @param properties an instance of {@code NodeProeprties} whose value will be shown in the dialog 
+	 * for the user to edit
+	 * @return a reference to {@code properties} containing the new values entered by the user or 
+	 * {@code null} if the user presses the cancel button or closes the window. 
+	 */
+	public static NodeProperties showDialog(Frame parent, NodeProperties properties){
+		if(properties == null)
+			throw new IllegalArgumentException(resources.getString("dialog.property_editor.error.property_null"));		
+		PropertyEditorDialog.properties = properties;
+		dialog = new PropertyEditorDialog(parent);
+		dialog.setLocationRelativeTo(parent);
+		dialog.setVisible(true);
+		return result;
+	}
+	
+	private void createComponents(){
+		panel = new JPanel();
+		buttonPanel = new JPanel();
+		propertyPanels = new PropertyPanel[PropertyEditorDialog.properties.getTypes().size()];
+		scrollPane  = new JScrollPane(
+				panel,
+				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
+				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
+				);
+		bottomSeparator = new JSeparator();
+		topSeparator = new JSeparator();
+		okButton = new JButton(resources.getString("dialog.ok_button"));
+		cancelButton = new JButton(resources.getString("dialog.cancel_button"));
+	}
+	
+	private JPanel panel;
+	private JPanel buttonPanel;
+	private PropertyPanel[] propertyPanels;
+	private JScrollPane scrollPane;
+	private JSeparator topSeparator;
+	private JSeparator bottomSeparator;
+	private JButton okButton;
+	private JButton cancelButton;
+	private ListenerManager listenerManager;
+	
+	private static PropertyEditorDialog dialog;
+	private static NodeProperties properties;
+	private static NodeProperties result;
+	private static ResourceBundle resources = ResourceBundle.getBundle(EditorFrame.class.getName());
+	
+	private class PropertyPanel extends JPanel{
+		public PropertyPanel(String propertyType, List<String> values, final Modifiers modifiers ){
+			setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
+			model = new PropertyTableModel(propertyType, values, modifiers);
+			if(modifiers != null)
+				for(int i=0; i< values.size(); i++){
+					model.setIndexesAt(i, modifiers.getIndexes(i));
+				}
+			table = new JTable(model);
+			table.setPreferredScrollableViewportSize(new Dimension(250, 70));
+	        table.setFillsViewportHeight(true);
+	        
+			add(new JScrollPane(table));
+			/* we can edit modifiers only if one or more modifier types have been defined for this property type */
+			if(!modifiers.isNull()){
+				editModifiers = new JButton(resources.getString("dialog.property_editor.edit_modifiers_button"));
+				editModifiers.setAlignmentX(RIGHT_ALIGNMENT);
+				add(editModifiers);
+				editModifiers.addActionListener(new ActionListener(){
+					@Override
+					public void actionPerformed(ActionEvent arg0) {
+						int row = table.getSelectedRow();
+						if((row == -1)||(row == model.getRowCount()-1))
+							return;
+						Set<Integer> indexes;
+						
+						indexes = ModifierEditorDialog.showDialog(PropertyEditorDialog.this, modifiers.getTypes(), model.getIndexesAt(row));
+						model.setIndexesAt(row, indexes);
+					}
+				});
+			}
+			add(Box.createRigidArea(new Dimension(0,5)));
+		}
+		
+		JTable table;
+		PropertyTableModel model;
+		JButton editModifiers;
+	}
+	
+	private class ListenerManager implements ActionListener{
+		@Override
+		public void actionPerformed(ActionEvent evt) {
+			Object source = evt.getSource();
+			if(source.equals(okButton)){
+				for(int i=0; i<propertyPanels.length;i++){
+					PropertyTableModel model = propertyPanels[i].model;
+					properties.clear(model.getColumnName(0));
+					for(int j=0; j< model.getRowCount();j++){
+						String value = model.getValueAt(j, 0).toString().trim();
+						if(!value.equals("")){
+							properties.addValue(model.getColumnName(0),value , model.getIndexesAt(j));
+						}
+					}
+				}
+				result = properties;
+				dispose();
+			}else{
+				result = null;
+				dispose();
+			}
+		}
+ 	}
+
+}