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: package de.uka.ilkd.key.gui;
010:
011: import java.awt.Dimension;
012: import java.awt.FlowLayout;
013: import java.awt.event.ActionEvent;
014: import java.awt.event.ActionListener;
015: import java.util.Arrays;
016: import java.util.Comparator;
017:
018: import javax.swing.*;
019: import javax.swing.border.TitledBorder;
020:
021: import de.uka.ilkd.key.casetool.*;
022:
023: /**
024: * A dialog for selecting classes (including interfaces).
025: */
026: public class ClassSelectionDialog extends JDialog {
027:
028: private boolean successful = false;
029: private JList classList;
030:
031: /**
032: * Creates and displays a dialog box asking the user to make a choice from
033: * a set of classes.
034: * @param windowTitle Title for the dialog window.
035: * @param classesTitle Title for the list of available classes.
036: * @param modelClasses The available classes.
037: * @param allowMultipleSelection Whether multiple classes or a single class
038: * are to be selected
039: */
040: public ClassSelectionDialog(String dialogTitle,
041: String classesTitle, ListOfModelClass modelClasses,
042: ModelClass defaultClass, boolean allowMultipleSelection) {
043: super (new JFrame(), dialogTitle, true);
044:
045: //create type list
046: classList = new JList();
047: if (allowMultipleSelection) {
048: classList
049: .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
050: } else {
051: classList
052: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
053: }
054: ModelClass[] classesArray = modelClasses.toArray();
055: Arrays.sort(classesArray, new Comparator() {
056: public int compare(Object o1, Object o2) {
057: ModelClass mc1 = (ModelClass) o1;
058: ModelClass mc2 = (ModelClass) o2;
059: return mc1.getClassName().compareTo(mc2.getClassName());
060: }
061: });
062: classList.setListData(classesArray);
063:
064: //select default class
065: if (defaultClass != null) {
066: String defaultName = defaultClass.getFullClassName();
067: IteratorOfModelClass it = modelClasses.iterator();
068: while (it.hasNext()) {
069: ModelClass modelClass = it.next();
070: if (modelClass.getFullClassName().equals(defaultName)) {
071: classList.setSelectedValue(modelClass, true);
072: break;
073: }
074: }
075: }
076:
077: //create type scroll pane
078: JScrollPane typeScrollPane = new JScrollPane(classList);
079: typeScrollPane.setBorder(new TitledBorder(classesTitle));
080: Dimension typeScrollPaneDim = new Dimension(275, 300);
081: typeScrollPane.setPreferredSize(typeScrollPaneDim);
082: typeScrollPane.setMinimumSize(typeScrollPaneDim);
083: getContentPane().add(typeScrollPane);
084:
085: //create button panel
086: JPanel buttonPanel = new JPanel();
087: buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
088: getContentPane().add(buttonPanel);
089: Dimension buttonDim = new Dimension(80, 25);
090:
091: //create "ok" button
092: JButton okButton = new JButton("OK");
093: okButton.setPreferredSize(buttonDim);
094: okButton.setMinimumSize(buttonDim);
095: okButton.addActionListener(new ActionListener() {
096: public void actionPerformed(ActionEvent e) {
097: successful = true;
098: setVisible(false);
099: }
100: });
101: buttonPanel.add(okButton);
102: getRootPane().setDefaultButton(okButton);
103:
104: //create "cancel" button
105: JButton cancelButton = new JButton("Cancel");
106: cancelButton.setPreferredSize(buttonDim);
107: cancelButton.setMinimumSize(buttonDim);
108: cancelButton.addActionListener(new ActionListener() {
109: public void actionPerformed(ActionEvent e) {
110: setVisible(false);
111: }
112: });
113: buttonPanel.add(cancelButton);
114:
115: getContentPane().setLayout(
116: new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
117: pack();
118: setLocation(70, 70);
119: setVisible(true);
120: }
121:
122: public ClassSelectionDialog(String dialogTitle,
123: String classesTitle, ListOfModelClass modelClasses,
124: boolean allowMultipleSelection) {
125: this (dialogTitle, classesTitle, modelClasses, null,
126: allowMultipleSelection);
127: }
128:
129: /**
130: * Tells whether the user clicked "ok".
131: */
132: public boolean wasSuccessful() {
133: return successful;
134: }
135:
136: /**
137: * Returns the selected classes.
138: */
139: public ListOfModelClass getSelection() {
140: Object[] selectedClasses = classList.getSelectedValues();
141:
142: ListOfModelClass result = SLListOfModelClass.EMPTY_LIST;
143: for (int i = selectedClasses.length - 1; i >= 0; i--) {
144: result = result.prepend((UMLModelClass) selectedClasses[i]);
145: }
146:
147: return result;
148: }
149: }
|