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.*;
014: import java.awt.event.ActionEvent;
015: import java.awt.event.ActionListener;
016: import java.io.File;
017: import java.util.HashMap;
018: import java.util.Iterator;
019:
020: import javax.swing.*;
021: import javax.swing.event.ListSelectionEvent;
022: import javax.swing.event.ListSelectionListener;
023: import javax.swing.table.AbstractTableModel;
024: import javax.swing.table.DefaultTableCellRenderer;
025:
026: import de.uka.ilkd.key.gui.KeYMediator;
027:
028: public class LibrariesConfiguration extends JDialog {
029:
030: private LibrariesSettings settingsFromFile;
031: private KeYMediator mediator = null;
032: private JButton remove, add, ok, cancel;
033:
034: private LibrariesTableModel tableModel;
035: private HashMap libToSel;
036: private JTable table;
037:
038: /** creates a new Libraries selection dialog
039: */
040: public LibrariesConfiguration(KeYMediator mediator,
041: LibrariesSettings settings) {
042: super (new JFrame(), "Libraries", true);
043: settingsFromFile = settings;
044: libToSel = settingsFromFile.getLibraries();
045:
046: // converts the HashMap to the arrays libraries and selected
047: int size = libToSel.keySet().size();
048: String[] l = new String[size];
049: boolean[] s = new boolean[size];
050:
051: Iterator it = libToSel.keySet().iterator();
052: int i = 0;
053: while (it.hasNext()) {
054: l[i] = (String) it.next();
055: i++;
056: }
057:
058: // sorts the libraries array
059: java.util.Arrays.sort(l, 0, size);
060:
061: for (int j = 0; j < size; j++) {
062: s[j] = ((Boolean) libToSel.get(l[j])).booleanValue();
063: }
064:
065: setMediator(mediator);
066: initizializeDialog(l, s);
067: setLocation(70, 70);
068: setSize(360, 200);
069: setVisible(false);
070: }
071:
072: /** sets the mediator to correspond with other gui elements
073: * @param mediator the KeYMediator
074: */
075: public void setMediator(KeYMediator mediator) {
076: this .mediator = mediator;
077: }
078:
079: private void pushSettings(String[] libraries, boolean[] selected) {
080: HashMap hm = new HashMap();
081: for (int i = 0; i < libraries.length; i++) {
082: hm.put(libraries[i], new Boolean(selected[i]));
083: }
084: settingsFromFile.setLibraries(hm);
085: }
086:
087: /** initializes the library configuration dialog */
088:
089: public void initizializeDialog(String[] libraries,
090: boolean[] selected) {
091: JPanel buttonPanel = new JPanel(new GridLayout(1, 4));
092: ok = new JButton("OK");
093:
094: ok.addActionListener(new ActionListener() {
095: public void actionPerformed(ActionEvent ae) {
096:
097: if (tableModel.settingsChanged()) {
098: int res = JOptionPane.showOptionDialog(
099: LibrariesConfiguration.this ,
100: "Your changes will become effective when "
101: + "the next problem is loaded.\n",
102: "Libraries Settings",
103: JOptionPane.DEFAULT_OPTION,
104: JOptionPane.QUESTION_MESSAGE, null,
105: new Object[] { "OK", "Cancel" }, "OK");
106: if (res == 0) {
107: pushSettings(tableModel.getLibraries(),
108: tableModel.getSelected());
109: }
110: }
111: dispose();
112: }
113: });
114: cancel = new JButton("Cancel");
115: cancel.addActionListener(new ActionListener() {
116: public void actionPerformed(ActionEvent ae) {
117: dispose();
118: }
119: });
120: ok.setSize(new Dimension(50, 25));
121: cancel.setSize(new Dimension(100, 50));
122:
123: add = new JButton("Add");
124: add.addActionListener(new ActionListener() {
125: public void actionPerformed(ActionEvent ae) {
126:
127: JFileChooser fileChooser = new JFileChooser(new File(
128: "."));
129: fileChooser
130: .setFileSelectionMode(JFileChooser.FILES_ONLY);
131: fileChooser
132: .setFileFilter(new javax.swing.filechooser.FileFilter() {
133: public boolean accept(File f) {
134: return f.isDirectory()
135: || f.toString()
136: .endsWith(".key");
137: }
138:
139: public String getDescription() {
140: return "KeY files";
141: }
142: });
143: fileChooser.setDialogTitle("Select a KeY library");
144: int result = fileChooser
145: .showOpenDialog(LibrariesConfiguration.this );
146: if (result == JFileChooser.APPROVE_OPTION) {
147: File file = fileChooser.getSelectedFile();
148: tableModel.addLibrary(file.toString(), false);
149: }
150: }
151: });
152:
153: remove = new JButton("Remove");
154: remove.setEnabled(false);
155: remove.addActionListener(new ActionListener() {
156: public void actionPerformed(ActionEvent ae) {
157: int row = table.getSelectedRow();
158: String file = (String) tableModel.getValueAt(row, 0);
159: String fileName = file;
160: if (file.startsWith(File.separator)) {
161: int i = file.lastIndexOf(File.separator);
162: if (i > -1)
163: fileName = file.substring(i + 1, file.length());
164: }
165:
166: int res = JOptionPane.showOptionDialog(
167: LibrariesConfiguration.this , "Delete Library "
168: + fileName + "?", "Delete Library",
169: JOptionPane.DEFAULT_OPTION,
170: JOptionPane.QUESTION_MESSAGE, null,
171: new Object[] { "OK", "Cancel" }, "OK");
172: if (res == 0) {
173: tableModel.removeLibrary(row);
174: }
175:
176: }
177: });
178:
179: buttonPanel.add(ok);
180: buttonPanel.add(cancel);
181: buttonPanel.add(add);
182: buttonPanel.add(remove);
183: buttonPanel.setSize(350, 100);
184:
185: tableModel = new LibrariesTableModel(libraries, selected);
186: table = new JTable(tableModel);
187: table.setDefaultRenderer(Object.class, new LibCellRenderer());
188: table
189: .setPreferredScrollableViewportSize(new Dimension(340,
190: 110));
191: table.setSize(300, 100);
192:
193: //Create the scroll pane and add the table to it.
194: JScrollPane scrollPane = new JScrollPane(table);
195: //tablePanel.add(scrollPane);
196:
197: DefaultListSelectionModel lsModel = new DefaultListSelectionModel();
198: lsModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
199:
200: lsModel.addListSelectionListener(new ListSelectionListener() {
201: public void valueChanged(ListSelectionEvent e) {
202: int row = table.getSelectedRow();
203: if (row == -1) {
204: remove.setEnabled(false);
205: return;
206: }
207: if (!tableModel.getLibraries()[row]
208: .startsWith(File.separator)) {
209: remove.setEnabled(false);
210: } else {
211: remove.setEnabled(true);
212: }
213: }
214: });
215:
216: table.setEditingColumn(1);
217: table.setSelectionModel(lsModel);
218: table.getColumnModel().getColumn(0).setPreferredWidth(250);
219: table.getColumnModel().getColumn(1).setPreferredWidth(50);
220:
221: getContentPane().setLayout(new FlowLayout());
222: getContentPane().add(scrollPane);
223: getContentPane().add(buttonPanel);
224:
225: }
226:
227: class LibrariesTableModel extends AbstractTableModel {
228: private String[] columnNames = { "Library Name", "Active" };
229:
230: String libraries[];
231: boolean sel[];
232: boolean changed = false;
233:
234: public LibrariesTableModel(String[] libs, boolean[] sel) {
235: this .libraries = libs;
236: this .sel = sel;
237: }
238:
239: public int getColumnCount() {
240: return 2;
241: }
242:
243: public int getRowCount() {
244: return libraries.length;
245: }
246:
247: public String getColumnName(int col) {
248: return columnNames[col];
249: }
250:
251: public Object getValueAt(int row, int col) {
252: if (col == 0)
253: return this .libraries[row];
254: else {
255: return new Boolean(sel[row]);
256: }
257: }
258:
259: public Class getColumnClass(int c) {
260: return getValueAt(0, c).getClass();
261: }
262:
263: public boolean isCellEditable(int row, int col) {
264: if (col < 1) {
265: return false;
266: } else {
267: return true;
268: }
269: }
270:
271: public void setValueAt(Object value, int row, int col) {
272: if (col == 0)
273: libraries[row] = ((String) value);
274: else
275: sel[row] = ((Boolean) value).booleanValue();
276: fireTableCellUpdated(row, col);
277: changed = true;
278: }
279:
280: public String[] getLibraries() {
281: return libraries;
282: }
283:
284: public boolean[] getSelected() {
285: return sel;
286: }
287:
288: public void addLibrary(String file, boolean selected) {
289: String[] newLib = new String[libraries.length + 1];
290: boolean[] newSel = new boolean[libraries.length + 1];
291: for (int i = 0; i < libraries.length; i++) {
292: newLib[i] = libraries[i];
293: newSel[i] = sel[i];
294: }
295: newLib[newLib.length - 1] = file;
296: newSel[newLib.length - 1] = selected;
297:
298: libraries = newLib;
299: sel = newSel;
300: this .fireTableRowsInserted(libraries.length - 1,
301: libraries.length - 1);
302: changed = true;
303: }
304:
305: public void removeLibrary(int a) {
306: String[] newLib = new String[libraries.length - 1];
307: boolean[] newSel = new boolean[libraries.length - 1];
308: for (int i = 0; i < a; i++) {
309: newLib[i] = libraries[i];
310: newSel[i] = sel[i];
311: }
312: if (a < libraries.length - 1)
313: for (int i = a; i < libraries.length - 1; i++) {
314: newLib[i] = libraries[i + 1];
315: newSel[i] = sel[i + 1];
316: }
317: libraries = newLib;
318: this .fireTableRowsDeleted(libraries.length - 1,
319: libraries.length - 1);
320: changed = true;
321: }
322:
323: public boolean settingsChanged() {
324: return changed;
325: }
326:
327: }
328:
329: public class LibCellRenderer extends DefaultTableCellRenderer {
330:
331: public String getFileName(String path) {
332: if (!path.startsWith(File.separator))
333: return path;
334: int i = path.lastIndexOf(File.separator);
335: if (i > -1)
336: return path.substring(i + 1, path.length());
337: return path;
338: }
339:
340: public Component getTableCellRendererComponent(JTable table,
341: Object col, boolean isSelected, boolean hasFocus,
342: int row, int column) {
343: Color grey = new Color(200, 200, 200);
344: Color white = new Color(255, 255, 255);
345: if (col instanceof Boolean) {
346: return super .getTableCellRendererComponent(table, col,
347: isSelected, hasFocus, row, column);
348: } else if (col instanceof String) {
349: String file = (String) col;
350: if (file.startsWith(File.separator))
351: setBackground(white);
352: else
353: setBackground(grey);
354:
355: return super.getTableCellRendererComponent(table,
356: getFileName(file), isSelected, hasFocus, row,
357: column);
358: } else
359: return super.getTableCellRendererComponent(table, col,
360: isSelected, hasFocus, row, column);
361: }
362: }
363: }
|