KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > dialog > pref > ClasspathPreferences


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.modeler.dialog.pref;
57
58 import java.awt.Component JavaDoc;
59 import java.awt.event.ActionEvent JavaDoc;
60 import java.awt.event.ActionListener JavaDoc;
61 import java.io.File JavaDoc;
62 import java.util.List JavaDoc;
63
64 import javax.swing.JFileChooser JavaDoc;
65 import javax.swing.filechooser.FileFilter JavaDoc;
66 import javax.swing.table.AbstractTableModel JavaDoc;
67
68 import org.objectstyle.cayenne.modeler.FileClassLoadingService;
69 import org.objectstyle.cayenne.modeler.util.CayenneController;
70 import org.objectstyle.cayenne.modeler.util.FileFilters;
71 import org.objectstyle.cayenne.pref.Domain;
72 import org.objectstyle.cayenne.pref.PreferenceDetail;
73 import org.objectstyle.cayenne.pref.PreferenceEditor;
74
75 /**
76  * @author Andrei Adamchik
77  */

78 public class ClasspathPreferences extends CayenneController {
79
80     protected ClasspathPreferencesView view;
81     protected PreferenceEditor editor;
82     protected List JavaDoc classPathEntries;
83     protected ClasspathTableModel tableModel;
84
85     public ClasspathPreferences(PreferenceDialog parentController) {
86         super(parentController);
87
88         this.editor = parentController.getEditor();
89         this.view = new ClasspathPreferencesView();
90         this.classPathEntries = getClassLoaderDomain().getDetails();
91         this.tableModel = new ClasspathTableModel();
92
93         initBindings();
94     }
95
96     public Component JavaDoc getView() {
97         return view;
98     }
99
100     protected Domain getClassLoaderDomain() {
101         return editor
102                 .editableInstance(getApplication().getPreferenceDomain())
103                 .getSubdomain(FileClassLoadingService.class);
104     }
105
106     protected void initBindings() {
107         view.getTable().setModel(tableModel);
108
109         view.getAddDirButton().addActionListener(new ActionListener JavaDoc() {
110
111             public void actionPerformed(ActionEvent JavaDoc e) {
112                 addClassDirectoryAction();
113             }
114         });
115
116         view.getRemoveEntryButton().addActionListener(new ActionListener JavaDoc() {
117
118             public void actionPerformed(ActionEvent JavaDoc e) {
119                 removeEntryAction();
120             }
121         });
122
123         view.getAddJarButton().addActionListener(new ActionListener JavaDoc() {
124
125             public void actionPerformed(ActionEvent JavaDoc e) {
126                 addJarOrZipAction();
127             }
128         });
129     }
130
131     protected void addJarOrZipAction() {
132         chooseClassEntry(
133                 FileFilters.getClassArchiveFilter(),
134                 "Select JAR or ZIP File.",
135                 JFileChooser.FILES_ONLY);
136     }
137
138     protected void addClassDirectoryAction() {
139         chooseClassEntry(
140                 null,
141                 "Select Java Class Directory.",
142                 JFileChooser.DIRECTORIES_ONLY);
143     }
144
145     protected void removeEntryAction() {
146         int selected = view.getTable().getSelectedRow();
147         if (selected < 0) {
148             return;
149         }
150
151         PreferenceDetail selection = (PreferenceDetail) classPathEntries.remove(selected);
152         editor.deleteDetail(getClassLoaderDomain(), selection.getKey());
153         tableModel.fireTableRowsDeleted(selected, selected);
154     }
155
156     protected void chooseClassEntry(FileFilter JavaDoc filter, String JavaDoc title, int selectionMode) {
157         JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
158         chooser.setFileSelectionMode(selectionMode);
159         chooser.setDialogType(JFileChooser.OPEN_DIALOG);
160         chooser.setAcceptAllFileFilterUsed(true);
161
162         getLastDirectory().updateChooser(chooser);
163
164         if (filter != null) {
165             chooser.addChoosableFileFilter(filter);
166         }
167
168         chooser.setDialogTitle(title);
169
170         File JavaDoc selected = null;
171         int result = chooser.showOpenDialog(view);
172         if (result == JFileChooser.APPROVE_OPTION) {
173             selected = chooser.getSelectedFile();
174         }
175
176         if (selected != null) {
177             // store last dir in preferences
178
getLastDirectory().updateFromChooser(chooser);
179
180             int len = classPathEntries.size();
181             String JavaDoc key = selected.getAbsolutePath();
182             classPathEntries.add(editor.createDetail(getClassLoaderDomain(), key));
183             tableModel.fireTableRowsInserted(len, len);
184         }
185     }
186
187     class ClasspathTableModel extends AbstractTableModel JavaDoc {
188
189         public int getColumnCount() {
190             return 1;
191         }
192
193         public int getRowCount() {
194             return classPathEntries.size();
195         }
196
197         public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
198             PreferenceDetail preference = (PreferenceDetail) classPathEntries
199                     .get(rowIndex);
200             return preference.getKey();
201         }
202
203         public String JavaDoc getColumnName(int column) {
204             return "Custom ClassPath";
205         }
206     }
207
208 }
Popular Tags