view java/src/uk/ac/qmul/eecs/ccmi/gui/ModifierEditorDialog.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 source
/*  
 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.Frame;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import uk.ac.qmul.eecs.ccmi.utils.GridBagUtilities;

/**
 * A dialog showing a list of checkboxes. By selecting the checkboxes the user can choose
 * which modifiers are assigned to a property value. 
 */
@SuppressWarnings("serial")
public class ModifierEditorDialog extends JDialog {
	
	private ModifierEditorDialog(JDialog parent, List<String> modifierTypes, Set<Integer> modifierIndexes){
		super(parent, resources.getString("dialog.modifier_editor.title"), true);
		init(modifierTypes, modifierIndexes);
	}
	
	private ModifierEditorDialog(Frame parent, List<String> modifierTypes, Set<Integer> modifierIndexes){
		super(parent, resources.getString("dialog.modifier_editor.title"), true);
		init(modifierTypes, modifierIndexes);
	}
	
	private void init(List<String> modifierTypes, Set<Integer> modifierIndexes){	
		listenerManager = new ListenerManager();
		createComponents();
		
		panel.setLayout(new GridBagLayout());
		
		checkBoxes = new JCheckBox[modifierTypes.size()]; 
		GridBagUtilities gridBagUtils = new GridBagUtilities(); 
		int i = 0;
		for(String modifierType : modifierTypes){
			panel.add(new JLabel(modifierType), gridBagUtils.label());
			checkBoxes[i] = new JCheckBox();
			if(modifierIndexes.contains(i))
				checkBoxes[i].setSelected(true);
			panel.add(checkBoxes[i],gridBagUtils.field());
			i++;
		}
		
		buttonPanel.add(okButton);
		buttonPanel.add(cancelButton);
		okButton.addActionListener(listenerManager);
		cancelButton.addActionListener(listenerManager);
		panel.add(buttonPanel,gridBagUtils.all());
		
		setContentPane(panel);
		setResizable(false);
		pack();
	}
	
	/**
	 * Shows a dialog with the checkboxes for the user to tick. 
	 * 
	 * @param parent the parent JDialog this dialog will appear in front of
	 * @param modifierTypes a list of the modifier that will be shown to the user, each near a checkbox
	 * @param modifiers a set of modifiers indexes. The {@code modifierTypes} at the specified indexes will 
	 * be shown as already ticked  
	 * 
	 * @return a reference to {@code modifiers} after it has been updated according to the user selections. 
	 */
	public static Set<Integer> showDialog(JDialog parent, List<String> modifierTypes, Set<Integer> modifiers){
		ModifierEditorDialog.modifiers = modifiers;
		dialog = new ModifierEditorDialog(parent, modifierTypes, modifiers);
		dialog.setLocationRelativeTo(parent);
		dialog.setVisible(true);
		return ModifierEditorDialog.modifiers;
		
	}
	
	/**
	 * Shows a dialog with the checkboxes for the user to tick. 
	 * 
	 * @param parent the parent Frame this dialog will appear in front of
	 * @param modifierTypes a list of the modifier that will be shown to the user, each near a checkbox
	 * @param modifiers a set of modifiers indexes. The {@code modifierTypes} at the specified indexes will 
	 * be shown as already ticked  
	 * 
	 * @return a reference to {@code modifiers} after it has been updated according to the user selections. 
	 */
	public static Set<Integer> showDialog(Frame parent, List<String> modifierTypes, Set<Integer> modifiers){
		ModifierEditorDialog.modifiers = modifiers;
		dialog = new ModifierEditorDialog(parent, modifierTypes, modifiers);
		dialog.setLocationRelativeTo(parent);
		dialog.setVisible(true);
		return ModifierEditorDialog.modifiers;
		
	}
	
	private void createComponents(){
		panel = new JPanel();
		buttonPanel = new JPanel();
		okButton = new JButton(resources.getString("dialog.ok_button"));
		cancelButton = new JButton(resources.getString("dialog.cancel_button"));
	}
	
	private JPanel panel;
	private JPanel buttonPanel;
	private JButton okButton;
	private JButton cancelButton;
	private JCheckBox[] checkBoxes;
	private ListenerManager listenerManager; 
	
	private static Set<Integer> modifiers;
	private static ModifierEditorDialog dialog;
	private static ResourceBundle resources = ResourceBundle.getBundle(EditorFrame.class.getName());
	
	private class ListenerManager implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent evt) {
				Object source = evt.getSource();
				if(source.equals(okButton)){
					for(int i=0;i<checkBoxes.length;i++)
						if(checkBoxes[i].isSelected()) 
							modifiers.add(i);
						else 
							modifiers.remove(i);
					dispose();
				}else if(source.equals(cancelButton)){
					dispose();
				}
		}
	}

}