1 56 package org.objectstyle.cayenne.modeler.dialog.pref; 57 58 import java.awt.Component ; 59 import java.awt.event.ActionEvent ; 60 import java.awt.event.ActionListener ; 61 import java.io.File ; 62 import java.util.List ; 63 64 import javax.swing.JFileChooser ; 65 import javax.swing.filechooser.FileFilter ; 66 import javax.swing.table.AbstractTableModel ; 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 78 public class ClasspathPreferences extends CayenneController { 79 80 protected ClasspathPreferencesView view; 81 protected PreferenceEditor editor; 82 protected List 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 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 () { 110 111 public void actionPerformed(ActionEvent e) { 112 addClassDirectoryAction(); 113 } 114 }); 115 116 view.getRemoveEntryButton().addActionListener(new ActionListener () { 117 118 public void actionPerformed(ActionEvent e) { 119 removeEntryAction(); 120 } 121 }); 122 123 view.getAddJarButton().addActionListener(new ActionListener () { 124 125 public void actionPerformed(ActionEvent 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 filter, String title, int selectionMode) { 157 JFileChooser chooser = new JFileChooser (); 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 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 getLastDirectory().updateFromChooser(chooser); 179 180 int len = classPathEntries.size(); 181 String key = selected.getAbsolutePath(); 182 classPathEntries.add(editor.createDetail(getClassLoaderDomain(), key)); 183 tableModel.fireTableRowsInserted(len, len); 184 } 185 } 186 187 class ClasspathTableModel extends AbstractTableModel { 188 189 public int getColumnCount() { 190 return 1; 191 } 192 193 public int getRowCount() { 194 return classPathEntries.size(); 195 } 196 197 public Object getValueAt(int rowIndex, int columnIndex) { 198 PreferenceDetail preference = (PreferenceDetail) classPathEntries 199 .get(rowIndex); 200 return preference.getKey(); 201 } 202 203 public String getColumnName(int column) { 204 return "Custom ClassPath"; 205 } 206 } 207 208 } | Popular Tags |