annotate java/src/uk/ac/qmul/eecs/ccmi/gui/ModifierEditorDialog.java @ 1:e3935c01cde2 tip

moved license of PdPersistenceManager to the beginning of the file
author Fiore Martin <f.martin@qmul.ac.uk>
date Tue, 08 Jul 2014 19:52:03 +0100
parents 78b7fc5391a2
children
rev   line source
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.Frame;
f@0 22 import java.awt.GridBagLayout;
f@0 23 import java.awt.event.ActionEvent;
f@0 24 import java.awt.event.ActionListener;
f@0 25 import java.util.List;
f@0 26 import java.util.ResourceBundle;
f@0 27 import java.util.Set;
f@0 28
f@0 29 import javax.swing.JButton;
f@0 30 import javax.swing.JCheckBox;
f@0 31 import javax.swing.JDialog;
f@0 32 import javax.swing.JLabel;
f@0 33 import javax.swing.JPanel;
f@0 34
f@0 35 import uk.ac.qmul.eecs.ccmi.utils.GridBagUtilities;
f@0 36
f@0 37 /**
f@0 38 * A dialog showing a list of checkboxes. By selecting the checkboxes the user can choose
f@0 39 * which modifiers are assigned to a property value.
f@0 40 */
f@0 41 @SuppressWarnings("serial")
f@0 42 public class ModifierEditorDialog extends JDialog {
f@0 43
f@0 44 private ModifierEditorDialog(JDialog parent, List<String> modifierTypes, Set<Integer> modifierIndexes){
f@0 45 super(parent, resources.getString("dialog.modifier_editor.title"), true);
f@0 46 init(modifierTypes, modifierIndexes);
f@0 47 }
f@0 48
f@0 49 private ModifierEditorDialog(Frame parent, List<String> modifierTypes, Set<Integer> modifierIndexes){
f@0 50 super(parent, resources.getString("dialog.modifier_editor.title"), true);
f@0 51 init(modifierTypes, modifierIndexes);
f@0 52 }
f@0 53
f@0 54 private void init(List<String> modifierTypes, Set<Integer> modifierIndexes){
f@0 55 listenerManager = new ListenerManager();
f@0 56 createComponents();
f@0 57
f@0 58 panel.setLayout(new GridBagLayout());
f@0 59
f@0 60 checkBoxes = new JCheckBox[modifierTypes.size()];
f@0 61 GridBagUtilities gridBagUtils = new GridBagUtilities();
f@0 62 int i = 0;
f@0 63 for(String modifierType : modifierTypes){
f@0 64 panel.add(new JLabel(modifierType), gridBagUtils.label());
f@0 65 checkBoxes[i] = new JCheckBox();
f@0 66 if(modifierIndexes.contains(i))
f@0 67 checkBoxes[i].setSelected(true);
f@0 68 panel.add(checkBoxes[i],gridBagUtils.field());
f@0 69 i++;
f@0 70 }
f@0 71
f@0 72 buttonPanel.add(okButton);
f@0 73 buttonPanel.add(cancelButton);
f@0 74 okButton.addActionListener(listenerManager);
f@0 75 cancelButton.addActionListener(listenerManager);
f@0 76 panel.add(buttonPanel,gridBagUtils.all());
f@0 77
f@0 78 setContentPane(panel);
f@0 79 setResizable(false);
f@0 80 pack();
f@0 81 }
f@0 82
f@0 83 /**
f@0 84 * Shows a dialog with the checkboxes for the user to tick.
f@0 85 *
f@0 86 * @param parent the parent JDialog this dialog will appear in front of
f@0 87 * @param modifierTypes a list of the modifier that will be shown to the user, each near a checkbox
f@0 88 * @param modifiers a set of modifiers indexes. The {@code modifierTypes} at the specified indexes will
f@0 89 * be shown as already ticked
f@0 90 *
f@0 91 * @return a reference to {@code modifiers} after it has been updated according to the user selections.
f@0 92 */
f@0 93 public static Set<Integer> showDialog(JDialog parent, List<String> modifierTypes, Set<Integer> modifiers){
f@0 94 ModifierEditorDialog.modifiers = modifiers;
f@0 95 dialog = new ModifierEditorDialog(parent, modifierTypes, modifiers);
f@0 96 dialog.setLocationRelativeTo(parent);
f@0 97 dialog.setVisible(true);
f@0 98 return ModifierEditorDialog.modifiers;
f@0 99
f@0 100 }
f@0 101
f@0 102 /**
f@0 103 * Shows a dialog with the checkboxes for the user to tick.
f@0 104 *
f@0 105 * @param parent the parent Frame this dialog will appear in front of
f@0 106 * @param modifierTypes a list of the modifier that will be shown to the user, each near a checkbox
f@0 107 * @param modifiers a set of modifiers indexes. The {@code modifierTypes} at the specified indexes will
f@0 108 * be shown as already ticked
f@0 109 *
f@0 110 * @return a reference to {@code modifiers} after it has been updated according to the user selections.
f@0 111 */
f@0 112 public static Set<Integer> showDialog(Frame parent, List<String> modifierTypes, Set<Integer> modifiers){
f@0 113 ModifierEditorDialog.modifiers = modifiers;
f@0 114 dialog = new ModifierEditorDialog(parent, modifierTypes, modifiers);
f@0 115 dialog.setLocationRelativeTo(parent);
f@0 116 dialog.setVisible(true);
f@0 117 return ModifierEditorDialog.modifiers;
f@0 118
f@0 119 }
f@0 120
f@0 121 private void createComponents(){
f@0 122 panel = new JPanel();
f@0 123 buttonPanel = new JPanel();
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 JButton okButton;
f@0 131 private JButton cancelButton;
f@0 132 private JCheckBox[] checkBoxes;
f@0 133 private ListenerManager listenerManager;
f@0 134
f@0 135 private static Set<Integer> modifiers;
f@0 136 private static ModifierEditorDialog dialog;
f@0 137 private static ResourceBundle resources = ResourceBundle.getBundle(EditorFrame.class.getName());
f@0 138
f@0 139 private class ListenerManager implements ActionListener {
f@0 140 @Override
f@0 141 public void actionPerformed(ActionEvent evt) {
f@0 142 Object source = evt.getSource();
f@0 143 if(source.equals(okButton)){
f@0 144 for(int i=0;i<checkBoxes.length;i++)
f@0 145 if(checkBoxes[i].isSelected())
f@0 146 modifiers.add(i);
f@0 147 else
f@0 148 modifiers.remove(i);
f@0 149 dispose();
f@0 150 }else if(source.equals(cancelButton)){
f@0 151 dispose();
f@0 152 }
f@0 153 }
f@0 154 }
f@0 155
f@0 156 }