f@0: /*
f@0: CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
f@0:
f@0: Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
f@0:
f@0: This program is free software: you can redistribute it and/or modify
f@0: it under the terms of the GNU General Public License as published by
f@0: the Free Software Foundation, either version 3 of the License, or
f@0: (at your option) any later version.
f@0:
f@0: This program is distributed in the hope that it will be useful,
f@0: but WITHOUT ANY WARRANTY; without even the implied warranty of
f@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@0: GNU General Public License for more details.
f@0:
f@0: You should have received a copy of the GNU General Public License
f@0: along with this program. If not, see .
f@0: */
f@0: package uk.ac.qmul.eecs.ccmi.gui;
f@0:
f@0: import java.awt.Frame;
f@0: import java.awt.GridBagLayout;
f@0: import java.awt.event.ActionEvent;
f@0: import java.awt.event.ActionListener;
f@0: import java.util.List;
f@0: import java.util.ResourceBundle;
f@0: import java.util.Set;
f@0:
f@0: import javax.swing.JButton;
f@0: import javax.swing.JCheckBox;
f@0: import javax.swing.JDialog;
f@0: import javax.swing.JLabel;
f@0: import javax.swing.JPanel;
f@0:
f@0: import uk.ac.qmul.eecs.ccmi.utils.GridBagUtilities;
f@0:
f@0: /**
f@0: * A dialog showing a list of checkboxes. By selecting the checkboxes the user can choose
f@0: * which modifiers are assigned to a property value.
f@0: */
f@0: @SuppressWarnings("serial")
f@0: public class ModifierEditorDialog extends JDialog {
f@0:
f@0: private ModifierEditorDialog(JDialog parent, List modifierTypes, Set modifierIndexes){
f@0: super(parent, resources.getString("dialog.modifier_editor.title"), true);
f@0: init(modifierTypes, modifierIndexes);
f@0: }
f@0:
f@0: private ModifierEditorDialog(Frame parent, List modifierTypes, Set modifierIndexes){
f@0: super(parent, resources.getString("dialog.modifier_editor.title"), true);
f@0: init(modifierTypes, modifierIndexes);
f@0: }
f@0:
f@0: private void init(List modifierTypes, Set modifierIndexes){
f@0: listenerManager = new ListenerManager();
f@0: createComponents();
f@0:
f@0: panel.setLayout(new GridBagLayout());
f@0:
f@0: checkBoxes = new JCheckBox[modifierTypes.size()];
f@0: GridBagUtilities gridBagUtils = new GridBagUtilities();
f@0: int i = 0;
f@0: for(String modifierType : modifierTypes){
f@0: panel.add(new JLabel(modifierType), gridBagUtils.label());
f@0: checkBoxes[i] = new JCheckBox();
f@0: if(modifierIndexes.contains(i))
f@0: checkBoxes[i].setSelected(true);
f@0: panel.add(checkBoxes[i],gridBagUtils.field());
f@0: i++;
f@0: }
f@0:
f@0: buttonPanel.add(okButton);
f@0: buttonPanel.add(cancelButton);
f@0: okButton.addActionListener(listenerManager);
f@0: cancelButton.addActionListener(listenerManager);
f@0: panel.add(buttonPanel,gridBagUtils.all());
f@0:
f@0: setContentPane(panel);
f@0: setResizable(false);
f@0: pack();
f@0: }
f@0:
f@0: /**
f@0: * Shows a dialog with the checkboxes for the user to tick.
f@0: *
f@0: * @param parent the parent JDialog this dialog will appear in front of
f@0: * @param modifierTypes a list of the modifier that will be shown to the user, each near a checkbox
f@0: * @param modifiers a set of modifiers indexes. The {@code modifierTypes} at the specified indexes will
f@0: * be shown as already ticked
f@0: *
f@0: * @return a reference to {@code modifiers} after it has been updated according to the user selections.
f@0: */
f@0: public static Set showDialog(JDialog parent, List modifierTypes, Set modifiers){
f@0: ModifierEditorDialog.modifiers = modifiers;
f@0: dialog = new ModifierEditorDialog(parent, modifierTypes, modifiers);
f@0: dialog.setLocationRelativeTo(parent);
f@0: dialog.setVisible(true);
f@0: return ModifierEditorDialog.modifiers;
f@0:
f@0: }
f@0:
f@0: /**
f@0: * Shows a dialog with the checkboxes for the user to tick.
f@0: *
f@0: * @param parent the parent Frame this dialog will appear in front of
f@0: * @param modifierTypes a list of the modifier that will be shown to the user, each near a checkbox
f@0: * @param modifiers a set of modifiers indexes. The {@code modifierTypes} at the specified indexes will
f@0: * be shown as already ticked
f@0: *
f@0: * @return a reference to {@code modifiers} after it has been updated according to the user selections.
f@0: */
f@0: public static Set showDialog(Frame parent, List modifierTypes, Set modifiers){
f@0: ModifierEditorDialog.modifiers = modifiers;
f@0: dialog = new ModifierEditorDialog(parent, modifierTypes, modifiers);
f@0: dialog.setLocationRelativeTo(parent);
f@0: dialog.setVisible(true);
f@0: return ModifierEditorDialog.modifiers;
f@0:
f@0: }
f@0:
f@0: private void createComponents(){
f@0: panel = new JPanel();
f@0: buttonPanel = new JPanel();
f@0: okButton = new JButton(resources.getString("dialog.ok_button"));
f@0: cancelButton = new JButton(resources.getString("dialog.cancel_button"));
f@0: }
f@0:
f@0: private JPanel panel;
f@0: private JPanel buttonPanel;
f@0: private JButton okButton;
f@0: private JButton cancelButton;
f@0: private JCheckBox[] checkBoxes;
f@0: private ListenerManager listenerManager;
f@0:
f@0: private static Set modifiers;
f@0: private static ModifierEditorDialog dialog;
f@0: private static ResourceBundle resources = ResourceBundle.getBundle(EditorFrame.class.getName());
f@0:
f@0: private class ListenerManager implements ActionListener {
f@0: @Override
f@0: public void actionPerformed(ActionEvent evt) {
f@0: Object source = evt.getSource();
f@0: if(source.equals(okButton)){
f@0: for(int i=0;i