| 1 | /* |
|---|
| 2 | * This program is free software; you can redistribute it and/or modify |
|---|
| 3 | * it under the terms of the GNU General Public License as published by |
|---|
| 4 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 5 | * (at your option) any later version. |
|---|
| 6 | * |
|---|
| 7 | * This program is distributed in the hope that it will be useful, |
|---|
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 10 | * GNU General Public License for more details. |
|---|
| 11 | * |
|---|
| 12 | * You should have received a copy of the GNU General Public License |
|---|
| 13 | * along with this program; if not, write to the Free Software |
|---|
| 14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | /* |
|---|
| 18 | * ListModelsPanel.java |
|---|
| 19 | * Copyright (C) 2006 Robert Jung |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | package weka.gui.ensembleLibraryEditor; |
|---|
| 24 | |
|---|
| 25 | import weka.classifiers.EnsembleLibrary; |
|---|
| 26 | import weka.classifiers.EnsembleLibraryModel; |
|---|
| 27 | |
|---|
| 28 | import java.awt.BorderLayout; |
|---|
| 29 | import java.awt.Dimension; |
|---|
| 30 | import java.awt.GridBagConstraints; |
|---|
| 31 | import java.awt.GridBagLayout; |
|---|
| 32 | import java.awt.event.ActionEvent; |
|---|
| 33 | import java.awt.event.ActionListener; |
|---|
| 34 | import java.io.File; |
|---|
| 35 | import java.util.Iterator; |
|---|
| 36 | import java.util.TreeSet; |
|---|
| 37 | |
|---|
| 38 | import javax.swing.AbstractAction; |
|---|
| 39 | import javax.swing.BorderFactory; |
|---|
| 40 | import javax.swing.JButton; |
|---|
| 41 | import javax.swing.JFileChooser; |
|---|
| 42 | import javax.swing.JList; |
|---|
| 43 | import javax.swing.JPanel; |
|---|
| 44 | import javax.swing.JScrollPane; |
|---|
| 45 | import javax.swing.KeyStroke; |
|---|
| 46 | import javax.swing.ListSelectionModel; |
|---|
| 47 | import javax.swing.filechooser.FileFilter; |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | * This class is responsible for creating the main panel in the library editor |
|---|
| 51 | * gui. It is responsible for displaying the entire list of models currently in |
|---|
| 52 | * the list. It is also responsible for allowing the user to save/load this list |
|---|
| 53 | * as a flat file and choosing the working directory for the library. |
|---|
| 54 | * |
|---|
| 55 | * @author Robert Jung (mrbobjung@gmail.com) |
|---|
| 56 | * @version $Revision: 1.2 $ |
|---|
| 57 | */ |
|---|
| 58 | public class ListModelsPanel |
|---|
| 59 | extends JPanel |
|---|
| 60 | implements ActionListener { |
|---|
| 61 | |
|---|
| 62 | /** for serialization */ |
|---|
| 63 | private static final long serialVersionUID = -1986253077944432252L; |
|---|
| 64 | |
|---|
| 65 | /** The library being edited */ |
|---|
| 66 | private EnsembleLibrary m_Library; |
|---|
| 67 | |
|---|
| 68 | /** The button for removing selected models */ |
|---|
| 69 | private JButton m_RemoveSelectedButton; |
|---|
| 70 | |
|---|
| 71 | /** The button for opening a model list from a file */ |
|---|
| 72 | private JButton m_OpenModelFileButton; |
|---|
| 73 | |
|---|
| 74 | /** The button for saving a model list to a file */ |
|---|
| 75 | private JButton m_SaveModelFileButton; |
|---|
| 76 | |
|---|
| 77 | /** The ModelList object that displays all currently selected models */ |
|---|
| 78 | private ModelList m_ModelList; |
|---|
| 79 | |
|---|
| 80 | /** The file chooser for the user to select model list files to save and load */ |
|---|
| 81 | private JFileChooser m_modelListChooser = new JFileChooser(new File( |
|---|
| 82 | System.getProperty("user.dir"))); |
|---|
| 83 | |
|---|
| 84 | /** |
|---|
| 85 | * Constructor to initialize library object and GUI |
|---|
| 86 | * |
|---|
| 87 | * @param library the library to use |
|---|
| 88 | */ |
|---|
| 89 | public ListModelsPanel(EnsembleLibrary library) { |
|---|
| 90 | m_Library = library; |
|---|
| 91 | createListModelsPanel(); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | /** |
|---|
| 95 | * this is necessay to set the Library object after initiialization |
|---|
| 96 | * |
|---|
| 97 | * @param library the library to use |
|---|
| 98 | */ |
|---|
| 99 | public void setLibrary(EnsembleLibrary library) { |
|---|
| 100 | |
|---|
| 101 | m_Library = library; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | /** |
|---|
| 105 | * Builds the GUI. |
|---|
| 106 | */ |
|---|
| 107 | private void createListModelsPanel() { |
|---|
| 108 | GridBagConstraints gbc = new GridBagConstraints(); |
|---|
| 109 | setLayout(new GridBagLayout()); |
|---|
| 110 | |
|---|
| 111 | JPanel modelListPanel = new JPanel(); |
|---|
| 112 | modelListPanel.setBorder( |
|---|
| 113 | BorderFactory.createTitledBorder("Currently Chosen Library Models")); |
|---|
| 114 | |
|---|
| 115 | m_ModelList = new ModelList(); |
|---|
| 116 | |
|---|
| 117 | m_ModelList.getInputMap().put( |
|---|
| 118 | KeyStroke.getKeyStroke("released DELETE"), "deleteSelected"); |
|---|
| 119 | m_ModelList.getActionMap().put("deleteSelected", |
|---|
| 120 | new AbstractAction("deleteSelected") { |
|---|
| 121 | |
|---|
| 122 | private static final long serialVersionUID = 8178827388328307805L; |
|---|
| 123 | |
|---|
| 124 | public void actionPerformed(ActionEvent evt) { |
|---|
| 125 | |
|---|
| 126 | Object[] currentModels = m_ModelList.getSelectedValues(); |
|---|
| 127 | |
|---|
| 128 | for (int i = 0; i < currentModels.length; i++) { |
|---|
| 129 | removeModel((EnsembleLibraryModel) currentModels[i]); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | // Shrink the selected range to the first index that was |
|---|
| 133 | // selected |
|---|
| 134 | int selected[] = new int[1]; |
|---|
| 135 | selected[0] = m_ModelList.getSelectedIndices()[0]; |
|---|
| 136 | m_ModelList.setSelectedIndices(selected); |
|---|
| 137 | } |
|---|
| 138 | }); |
|---|
| 139 | |
|---|
| 140 | m_ModelList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); |
|---|
| 141 | m_ModelList.setVisibleRowCount(-1); |
|---|
| 142 | m_ModelList.setLayoutOrientation(JList.VERTICAL); |
|---|
| 143 | JScrollPane listScroller = new JScrollPane(m_ModelList); |
|---|
| 144 | listScroller.setPreferredSize(new Dimension(150, 50)); |
|---|
| 145 | |
|---|
| 146 | modelListPanel.setLayout(new BorderLayout()); |
|---|
| 147 | modelListPanel.add(listScroller, BorderLayout.CENTER); |
|---|
| 148 | |
|---|
| 149 | gbc.fill = GridBagConstraints.BOTH; |
|---|
| 150 | gbc.weightx = 1; |
|---|
| 151 | gbc.weighty = 1; |
|---|
| 152 | gbc.gridx = 0; |
|---|
| 153 | gbc.gridy = 0; |
|---|
| 154 | gbc.anchor = GridBagConstraints.WEST; |
|---|
| 155 | gbc.gridwidth = 3; |
|---|
| 156 | add(modelListPanel, gbc); |
|---|
| 157 | |
|---|
| 158 | m_RemoveSelectedButton = new JButton("Remove Selected"); |
|---|
| 159 | m_RemoveSelectedButton.addActionListener(this); |
|---|
| 160 | m_RemoveSelectedButton.setToolTipText( |
|---|
| 161 | "Remove all currently selected models from the above list"); |
|---|
| 162 | gbc.fill = GridBagConstraints.HORIZONTAL; |
|---|
| 163 | gbc.gridx = 0; |
|---|
| 164 | gbc.gridy = 1; |
|---|
| 165 | gbc.anchor = GridBagConstraints.WEST; |
|---|
| 166 | gbc.weightx = 1; |
|---|
| 167 | gbc.weighty = 0; |
|---|
| 168 | gbc.gridwidth = 1; |
|---|
| 169 | add(m_RemoveSelectedButton, gbc); |
|---|
| 170 | |
|---|
| 171 | m_modelListChooser.setAcceptAllFileFilterUsed(false); |
|---|
| 172 | |
|---|
| 173 | XMLModelFileFilter xmlFilter = new XMLModelFileFilter(); |
|---|
| 174 | m_modelListChooser.addChoosableFileFilter(xmlFilter); |
|---|
| 175 | m_modelListChooser.addChoosableFileFilter(new FlatModelFileFilter()); |
|---|
| 176 | // set up the file chooser |
|---|
| 177 | m_modelListChooser.setFileFilter(xmlFilter); |
|---|
| 178 | m_modelListChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); |
|---|
| 179 | // create the buttons + field |
|---|
| 180 | m_OpenModelFileButton = new JButton("Open..."); |
|---|
| 181 | m_OpenModelFileButton.addActionListener(this); |
|---|
| 182 | m_OpenModelFileButton.setToolTipText( |
|---|
| 183 | "Import a model list file from the file system"); |
|---|
| 184 | gbc.fill = GridBagConstraints.HORIZONTAL; |
|---|
| 185 | gbc.gridx = 1; |
|---|
| 186 | gbc.gridy = 1; |
|---|
| 187 | gbc.anchor = GridBagConstraints.WEST; |
|---|
| 188 | gbc.gridwidth = 1; |
|---|
| 189 | gbc.weightx = 1; |
|---|
| 190 | gbc.weighty = 0; |
|---|
| 191 | add(m_OpenModelFileButton, gbc); |
|---|
| 192 | |
|---|
| 193 | m_SaveModelFileButton = new JButton("Save..."); |
|---|
| 194 | m_SaveModelFileButton.addActionListener(this); |
|---|
| 195 | m_SaveModelFileButton.setToolTipText( |
|---|
| 196 | "Save the current list of models to a file"); |
|---|
| 197 | gbc.fill = GridBagConstraints.HORIZONTAL; |
|---|
| 198 | gbc.gridx = 2; |
|---|
| 199 | gbc.gridy = 1; |
|---|
| 200 | gbc.anchor = GridBagConstraints.WEST; |
|---|
| 201 | gbc.gridwidth = 1; |
|---|
| 202 | gbc.weightx = 1; |
|---|
| 203 | gbc.weighty = 0; |
|---|
| 204 | add(m_SaveModelFileButton, gbc); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | /** |
|---|
| 208 | * returns the current library |
|---|
| 209 | * |
|---|
| 210 | * @return the current library |
|---|
| 211 | */ |
|---|
| 212 | public EnsembleLibrary getLibrary() { |
|---|
| 213 | return m_Library; |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | /** |
|---|
| 217 | * This handles all of the button events. Specifically the buttons |
|---|
| 218 | * associated with saving/loading model lists as well as removing models |
|---|
| 219 | * from the currently displayed list. |
|---|
| 220 | * |
|---|
| 221 | * @param e |
|---|
| 222 | * the action event that occured |
|---|
| 223 | */ |
|---|
| 224 | public void actionPerformed(ActionEvent e) { |
|---|
| 225 | |
|---|
| 226 | if (e.getSource() == m_OpenModelFileButton) { |
|---|
| 227 | |
|---|
| 228 | int returnVal = m_modelListChooser.showOpenDialog(this); |
|---|
| 229 | if (returnVal == JFileChooser.APPROVE_OPTION) { |
|---|
| 230 | |
|---|
| 231 | clearAll(); |
|---|
| 232 | |
|---|
| 233 | File selectedFile = m_modelListChooser.getSelectedFile(); |
|---|
| 234 | |
|---|
| 235 | // add .mlf extension if that was the file filter used |
|---|
| 236 | if ((m_modelListChooser.getFileFilter() instanceof FlatModelFileFilter)) { |
|---|
| 237 | if (!selectedFile.getName().endsWith( |
|---|
| 238 | EnsembleLibrary.FLAT_FILE_EXTENSION)) { |
|---|
| 239 | selectedFile = new File(selectedFile.getPath() |
|---|
| 240 | + EnsembleLibrary.FLAT_FILE_EXTENSION); |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | // otherwise use .model.xml file extension by default |
|---|
| 244 | } else { |
|---|
| 245 | if (!selectedFile.getName().endsWith( |
|---|
| 246 | EnsembleLibrary.XML_FILE_EXTENSION)) { |
|---|
| 247 | selectedFile = new File(selectedFile.getPath() |
|---|
| 248 | + EnsembleLibrary.XML_FILE_EXTENSION); |
|---|
| 249 | } |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | EnsembleLibrary.loadLibrary(selectedFile, this, m_Library); |
|---|
| 253 | |
|---|
| 254 | ModelList.SortedListModel dataModel = ((ModelList.SortedListModel) m_ModelList.getModel()); |
|---|
| 255 | |
|---|
| 256 | TreeSet models = m_Library.getModels(); |
|---|
| 257 | for (Iterator it = models.iterator(); it.hasNext();) { |
|---|
| 258 | addModel((EnsembleLibraryModel) it.next()); |
|---|
| 259 | // dataModel.add(it.next()); |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | } else if (e.getSource() == m_SaveModelFileButton) { |
|---|
| 265 | |
|---|
| 266 | int returnVal = m_modelListChooser.showSaveDialog(this); |
|---|
| 267 | if (returnVal == JFileChooser.APPROVE_OPTION) { |
|---|
| 268 | |
|---|
| 269 | File selectedFile = m_modelListChooser.getSelectedFile(); |
|---|
| 270 | |
|---|
| 271 | // add .mlf extension if that was the file filter used |
|---|
| 272 | if ((m_modelListChooser.getFileFilter() instanceof FlatModelFileFilter)) { |
|---|
| 273 | if (!selectedFile.getName().endsWith( |
|---|
| 274 | EnsembleLibrary.FLAT_FILE_EXTENSION)) { |
|---|
| 275 | selectedFile = new File(selectedFile.getPath() |
|---|
| 276 | + EnsembleLibrary.FLAT_FILE_EXTENSION); |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | // otherwise use .model.xml file extension by default |
|---|
| 280 | } else { |
|---|
| 281 | if (!selectedFile.getName().endsWith( |
|---|
| 282 | EnsembleLibrary.XML_FILE_EXTENSION)) { |
|---|
| 283 | selectedFile = new File(selectedFile.getPath() |
|---|
| 284 | + EnsembleLibrary.XML_FILE_EXTENSION); |
|---|
| 285 | } |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | EnsembleLibrary.saveLibrary(selectedFile, m_Library, this); |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | } else if (e.getSource() == m_RemoveSelectedButton) { |
|---|
| 292 | |
|---|
| 293 | // here we simply get the list of models that are |
|---|
| 294 | // currently selected and ten remove them from the list |
|---|
| 295 | |
|---|
| 296 | // ModelList.SortedListModel dataModel = |
|---|
| 297 | // ((ModelList.SortedListModel)m_ModelList.getModel()); |
|---|
| 298 | |
|---|
| 299 | Object[] currentModels = m_ModelList.getSelectedValues(); |
|---|
| 300 | |
|---|
| 301 | for (int i = 0; i < currentModels.length; i++) { |
|---|
| 302 | removeModel((EnsembleLibraryModel) currentModels[i]); |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | // Shrink the selected range to the first index that was selected |
|---|
| 306 | if (m_ModelList.getSelectedIndices().length > 0) { |
|---|
| 307 | int selected[] = new int[1]; |
|---|
| 308 | selected[0] = m_ModelList.getSelectedIndices()[0]; |
|---|
| 309 | m_ModelList.setSelectedIndices(selected); |
|---|
| 310 | } |
|---|
| 311 | } |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | /** |
|---|
| 315 | * This removes all the models from the current list in the GUI |
|---|
| 316 | */ |
|---|
| 317 | public void clearAll() { |
|---|
| 318 | ((ModelList.SortedListModel) m_ModelList.getModel()).clear(); |
|---|
| 319 | m_Library.clearModels(); |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | /** |
|---|
| 323 | * Adds a model to the current library |
|---|
| 324 | * |
|---|
| 325 | * @param model the model to add |
|---|
| 326 | */ |
|---|
| 327 | public void addModel(EnsembleLibraryModel model) { |
|---|
| 328 | |
|---|
| 329 | ((ModelList.SortedListModel) m_ModelList.getModel()).add(model); |
|---|
| 330 | m_Library.addModel(model); |
|---|
| 331 | |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | /** |
|---|
| 335 | * Removes a model to the current library |
|---|
| 336 | * |
|---|
| 337 | * @param model the model to remove |
|---|
| 338 | */ |
|---|
| 339 | public void removeModel(EnsembleLibraryModel model) { |
|---|
| 340 | ((ModelList.SortedListModel) m_ModelList.getModel()) |
|---|
| 341 | .removeElement(model); |
|---|
| 342 | m_Library.removeModel(model); |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | /** |
|---|
| 346 | * A helper class for filtering xml model files |
|---|
| 347 | */ |
|---|
| 348 | class XMLModelFileFilter |
|---|
| 349 | extends FileFilter { |
|---|
| 350 | |
|---|
| 351 | /** |
|---|
| 352 | * Whether the given file is accepted by this filter. |
|---|
| 353 | * |
|---|
| 354 | * @param file the file to check |
|---|
| 355 | * @return true if the file got accepted |
|---|
| 356 | */ |
|---|
| 357 | public boolean accept(File file) { |
|---|
| 358 | String filename = file.getName(); |
|---|
| 359 | return (filename.endsWith(EnsembleLibrary.XML_FILE_EXTENSION) || file |
|---|
| 360 | .isDirectory()); |
|---|
| 361 | } |
|---|
| 362 | |
|---|
| 363 | /** |
|---|
| 364 | * The description of this filter. |
|---|
| 365 | * |
|---|
| 366 | * @return the description |
|---|
| 367 | */ |
|---|
| 368 | public String getDescription() { |
|---|
| 369 | return "XML Library Files (*.model.xml)"; |
|---|
| 370 | } |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | /** |
|---|
| 374 | * A helper class for filtering flat model files |
|---|
| 375 | * |
|---|
| 376 | */ |
|---|
| 377 | class FlatModelFileFilter |
|---|
| 378 | extends FileFilter { |
|---|
| 379 | |
|---|
| 380 | /** |
|---|
| 381 | * Whether the given file is accepted by this filter. |
|---|
| 382 | * |
|---|
| 383 | * @param file the file to check |
|---|
| 384 | * @return true if the file got accepted |
|---|
| 385 | */ |
|---|
| 386 | public boolean accept(File file) { |
|---|
| 387 | String filename = file.getName(); |
|---|
| 388 | return (filename.endsWith(EnsembleLibrary.FLAT_FILE_EXTENSION) || file |
|---|
| 389 | .isDirectory()); |
|---|
| 390 | } |
|---|
| 391 | |
|---|
| 392 | /** |
|---|
| 393 | * The description of this filter. |
|---|
| 394 | * |
|---|
| 395 | * @return the description |
|---|
| 396 | */ |
|---|
| 397 | public String getDescription() { |
|---|
| 398 | return "Model List Files (*.mlf)"; |
|---|
| 399 | } |
|---|
| 400 | } |
|---|
| 401 | |
|---|
| 402 | } |
|---|