001: // This file is part of KeY - Integrated Deductive Software Design
002: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
003: // Universitaet Koblenz-Landau, Germany
004: // Chalmers University of Technology, Sweden
005: //
006: // The KeY system is protected by the GNU General Public License.
007: // See LICENSE.TXT for details.
008: //
009: //
010:
011: package de.uka.ilkd.key.gui.configuration;
012:
013: import java.awt.Dimension;
014: import java.awt.BorderLayout;
015: import java.awt.event.ActionEvent;
016: import java.awt.event.ActionListener;
017: import java.util.HashMap;
018: import java.util.Set;
019:
020: import javax.swing.*;
021: import javax.swing.border.TitledBorder;
022: import javax.swing.event.ListSelectionEvent;
023: import javax.swing.event.ListSelectionListener;
024:
025: public class ChoiceSelector extends JDialog {
026:
027: private ChoiceSettings settings;
028: private HashMap category2DefaultChoice;
029: private HashMap category2Choices;
030: private boolean changed = false;
031:
032: /** the JList with the categories of choices*/
033: private JList catList;
034: /** the JList with the choices for one category */
035: private JList choiceList;
036:
037: /** creates a new ChoiceSelector, using the <code>ChoiceSettings</code>
038: * from <code>settings</code> */
039: public ChoiceSelector(ChoiceSettings settings) {
040: super (new JFrame(), "Select Default Choices", true);
041: this .settings = settings;
042: category2DefaultChoice = settings.getDefaultChoices();
043: if (category2DefaultChoice.isEmpty()) {
044: JOptionPane.showConfirmDialog(ChoiceSelector.this ,
045: "There are no Taclet Options available as the rule-files "
046: + "have not been parsed yet!",
047: "No Options available", JOptionPane.DEFAULT_OPTION);
048: dispose();
049: } else {
050: category2Choices = settings.getChoices();
051: layoutChoiceSelector();
052: pack();
053: setLocation(70, 70);
054: setVisible(true);
055: }
056: }
057:
058: /** creates a new ChoiceSelector */
059: public ChoiceSelector() {
060: this (ProofSettings.DEFAULT_SETTINGS.getChoiceSettings());
061: }
062:
063: /** layout */
064: protected void layoutChoiceSelector() {
065: Object[] cats = category2DefaultChoice.keySet().toArray();
066: java.util.Arrays.sort(cats);
067: catList = new JList(cats);
068: catList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
069: catList.setSelectedIndex(0);
070: catList.addListSelectionListener(new ListSelectionListener() {
071: public void valueChanged(ListSelectionEvent e) {
072: setChoiceList();
073: }
074: });
075: choiceList = new JList(((Set) category2Choices.get(cats[0]))
076: .toArray());
077: choiceList
078: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
079: choiceList.setSelectedValue(
080: category2DefaultChoice.get(cats[0]), true);
081: choiceList
082: .addListSelectionListener(new ListSelectionListener() {
083: public void valueChanged(ListSelectionEvent e) {
084: setDefaultChoice((String) choiceList
085: .getSelectedValue());
086: }
087: });
088:
089: JScrollPane catListScroll = new JScrollPane(
090: JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
091: JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
092: catListScroll.getViewport().setView(catList);
093: catListScroll.setBorder(new TitledBorder("Category"));
094:
095: JScrollPane choiceScrollPane = new JScrollPane(
096: JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
097: JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
098: choiceScrollPane.getViewport().setView(choiceList);
099: choiceScrollPane.setBorder(new TitledBorder("Default Option"));
100:
101: JButton ok = new JButton("OK");
102: ok.addActionListener(new ActionListener() {
103: public void actionPerformed(ActionEvent e) {
104: if (changed) {
105: int res = JOptionPane.showOptionDialog(
106: ChoiceSelector.this ,
107: "Your changes will become effective when "
108: + "the next problem is loaded.\n",
109: "Taclet Option Settings",
110: JOptionPane.DEFAULT_OPTION,
111: JOptionPane.QUESTION_MESSAGE, null,
112: new Object[] { "OK", "Cancel" }, "OK");
113: if (res == 0) {
114: settings
115: .setDefaultChoices(category2DefaultChoice);
116: }
117: }
118: setVisible(false);
119: dispose();
120: }
121: });
122: JButton cancel = new JButton("Cancel");
123: cancel.addActionListener(new ActionListener() {
124: public void actionPerformed(ActionEvent e) {
125: setVisible(false);
126: dispose();
127: }
128: });
129: JPanel buttonPanel = new JPanel();
130: buttonPanel.add(ok);
131: buttonPanel.add(cancel);
132:
133: Dimension paneDim = new Dimension(300, 300);
134: choiceScrollPane.setPreferredSize(paneDim);
135: choiceScrollPane.setMinimumSize(paneDim);
136: paneDim = new Dimension(200, 300);
137: catListScroll.setPreferredSize(paneDim);
138: catListScroll.setMinimumSize(paneDim);
139:
140: JPanel listPanel = new JPanel();
141: listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.X_AXIS));
142: listPanel.add(catListScroll);
143: listPanel.add(choiceScrollPane);
144:
145: getContentPane().setLayout(new BorderLayout());
146: getContentPane().add(listPanel, BorderLayout.CENTER);
147: getContentPane().add(buttonPanel, BorderLayout.SOUTH);
148: }
149:
150: /** is called to set the selected choice in
151: * <code>category2DefaultChoice</code>*/
152: private void setDefaultChoice(String sel) {
153: String category = (String) catList.getSelectedValue();
154: if (sel != null) {
155: category2DefaultChoice.put(category, sel);
156: changed = true;
157: }
158: }
159:
160: /** is called if the selection of left list has changed, and causes the
161: * right one to display the possible choices for the category chosen on
162: * the left side
163: */
164: private void setChoiceList() {
165: String selection = (String) catList.getSelectedValue();
166: choiceList.setListData(((Set) category2Choices.get(selection))
167: .toArray());
168: choiceList.setSelectedValue(category2DefaultChoice
169: .get(selection), false);
170: }
171:
172: }
|