source: src/main/java/weka/gui/ensembleLibraryEditor/LoadModelsPanel.java @ 24

Last change on this file since 24 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 13.8 KB
Line 
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 *    LoadModelsPanel.java
19 *    Copyright (C) 2006 Robert Jung
20 *
21 */
22
23package weka.gui.ensembleLibraryEditor;
24
25import weka.classifiers.EnsembleLibraryModel;
26import weka.classifiers.meta.ensembleSelection.EnsembleSelectionLibrary;
27import weka.classifiers.meta.ensembleSelection.EnsembleSelectionLibraryModel;
28import weka.gui.EnsembleSelectionLibraryEditor;
29
30import java.awt.BorderLayout;
31import java.awt.Dimension;
32import java.awt.GridBagConstraints;
33import java.awt.GridBagLayout;
34import java.awt.event.ActionEvent;
35import java.awt.event.ActionListener;
36import java.beans.PropertyChangeEvent;
37import java.beans.PropertyChangeListener;
38import java.io.File;
39import java.util.Iterator;
40
41import javax.swing.AbstractAction;
42import javax.swing.BorderFactory;
43import javax.swing.JButton;
44import javax.swing.JLabel;
45import javax.swing.JList;
46import javax.swing.JPanel;
47import javax.swing.JScrollPane;
48import javax.swing.JTabbedPane;
49import javax.swing.KeyStroke;
50import javax.swing.ListSelectionModel;
51import javax.swing.ToolTipManager;
52import javax.swing.event.ChangeEvent;
53import javax.swing.event.ChangeListener;
54
55/**
56 *
57 * @author  Robert Jung
58 * @version $Revision: 1.2 $
59 */
60public class LoadModelsPanel
61  extends JPanel
62  implements ActionListener, PropertyChangeListener, ChangeListener {
63 
64  /** for serialization */
65  private static final long serialVersionUID = -6961209378227736515L;
66
67  /**
68   * This is a reference to the main gui object that is responsible for
69   * displaying the model library. This panel will add models to the main
70   * panel through methods in this object.
71   */
72  private ListModelsPanel m_ListModelsPanel;
73 
74  /**
75   * This will display messages associated with model loading. Currently the
76   * number of models found and the number of data sets.
77   */
78  private JLabel m_LoadingLabel;
79 
80  /**
81   * This will display the current working Directory of the Ensemble Library.
82   */
83  private JLabel m_DirectoryLabel;
84 
85  /**
86   * This button will refresh the model list currently displayed in the case
87   * that either the working directory changed or the models stored in it
88   * changed.
89   */
90  private JButton m_RefreshListButton;
91 
92  /**
93   * This button will allow users to remove all of the models currently
94   * selected in the m_ModeList object
95   */
96  private JButton m_RemoveSelectedButton;
97 
98  /**
99   * This button will allow users to add all models currently in the model
100   * list to the model library in the ListModelsPanel. Note that this
101   * operation will exclude any models that had errors
102   */
103  private JButton m_AddAllButton;
104 
105  /**
106   * This button will add all of the models that had are currently selected in
107   * the model list.
108   */
109  private JButton m_AddSelectedButton;
110 
111  /**
112   * This object will store all of the models that were found in the library's
113   * current working directory. The ModelList class is JList list = new
114   * JList(listModel); a custom class in weka.gui that knows how to display
115   * library model objects in a JList
116   */
117  private ModelList m_ModelList;
118 
119  /**
120   * A reference to the main library object so that we can get the current
121   * working Directory.
122   */
123  private EnsembleSelectionLibrary m_Library;
124 
125  /**
126   * A reference to the libary editor that contains this panel so that we can
127   * see if we're selected or not.
128   */
129  private EnsembleSelectionLibraryEditor m_EnsembleLibraryEditor;
130 
131  /**
132   *
133   */
134  private boolean m_workingDirectoryChanged = false;
135 
136  /**
137   * This constructor simply stores the reference to the ListModelsPanel and
138   * build the user interface.
139   *
140   * @param listModelsPanel     the panel
141   * @param editor              the editor
142   */
143  public LoadModelsPanel(ListModelsPanel listModelsPanel,
144      EnsembleSelectionLibraryEditor editor) {
145   
146    m_ListModelsPanel = listModelsPanel;
147    m_EnsembleLibraryEditor = editor;
148    createLoadModelsPanel();
149  }
150 
151  /**
152   * This method is responsible for building the user interface.
153   */
154  private void createLoadModelsPanel() {
155    GridBagConstraints gbc = new GridBagConstraints();
156    setLayout(new GridBagLayout());
157   
158    m_RefreshListButton = new JButton("Reload List");
159    m_RefreshListButton.setToolTipText(
160        "Discover the set of models existing in the current working directory");
161    m_RefreshListButton.addActionListener(this);
162    gbc.weightx = 0;
163    gbc.fill = GridBagConstraints.HORIZONTAL;
164    gbc.gridx = 0;
165    gbc.gridy = 0;
166    gbc.anchor = GridBagConstraints.WEST;
167    gbc.gridwidth = 1;
168    add(m_RefreshListButton, gbc);
169   
170    m_DirectoryLabel = new JLabel("");
171    updateDirectoryLabel();
172    gbc.weightx = 1;
173    gbc.fill = GridBagConstraints.HORIZONTAL;
174    gbc.gridx = 0;
175    gbc.gridy = 1;
176    gbc.anchor = GridBagConstraints.WEST;
177    gbc.gridwidth = 3;
178    add(m_DirectoryLabel, gbc);
179   
180    m_LoadingLabel = new JLabel("");
181    gbc.weightx = 1;
182    gbc.fill = GridBagConstraints.HORIZONTAL;
183    gbc.gridx = 0;
184    gbc.gridy = 2;
185    gbc.anchor = GridBagConstraints.WEST;
186    gbc.gridwidth = 3;
187    add(m_LoadingLabel, gbc);
188   
189    JPanel modelListPanel = new JPanel();
190    modelListPanel.setBorder(
191        BorderFactory.createTitledBorder("Working Set of Loadable Models"));
192   
193    m_ModelList = new ModelList();
194   
195    m_ModelList.getInputMap().put(
196        KeyStroke.getKeyStroke("released DELETE"), "deleteSelected");
197    m_ModelList.getActionMap().put("deleteSelected",
198        new AbstractAction("deleteSelected") {
199     
200      private static final long serialVersionUID = 4557836979081722983L;
201     
202      public void actionPerformed(ActionEvent evt) {
203       
204        Object[] currentModels = m_ModelList.getSelectedValues();
205       
206        ModelList.SortedListModel dataModel = ((ModelList.SortedListModel) m_ModelList.getModel());
207       
208        for (int i = 0; i < currentModels.length; i++) {
209          dataModel.removeElement((EnsembleLibraryModel) currentModels[i]);
210        }
211       
212        // Shrink the selected range to the first index that was
213        // selected
214        int selected[] = new int[1];
215        selected[0] = m_ModelList.getSelectedIndices()[0];
216        m_ModelList.setSelectedIndices(selected);
217       
218      }
219    });
220   
221    m_ModelList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
222    m_ModelList.setLayoutOrientation(JList.VERTICAL);
223    // m_ModelList.setVisibleRowCount(12);
224   
225    ToolTipManager.sharedInstance().registerComponent(m_ModelList);
226   
227    JScrollPane listView = new JScrollPane(m_ModelList);
228    listView
229    .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
230    listView.setPreferredSize(new Dimension(150, 50));
231   
232    modelListPanel.setLayout(new BorderLayout());
233    modelListPanel.add(listView, BorderLayout.CENTER);
234   
235    gbc.weightx = 1;
236    gbc.weighty = 1;
237    gbc.fill = GridBagConstraints.BOTH;
238    gbc.gridx = 0;
239    gbc.gridy = 3;
240    gbc.gridwidth = 3;
241    gbc.anchor = GridBagConstraints.WEST;
242    add(modelListPanel, gbc);
243   
244    m_RemoveSelectedButton = new JButton("Remove Selected");
245    m_RemoveSelectedButton.setToolTipText(
246        "Remove all selected models from the above list");
247    m_RemoveSelectedButton.addActionListener(this);
248    gbc.weightx = 1;
249    gbc.weighty = 0;
250    gbc.fill = GridBagConstraints.HORIZONTAL;
251    gbc.gridx = 0;
252    gbc.gridy = 4;
253    gbc.anchor = GridBagConstraints.WEST;
254    gbc.gridwidth = 1;
255    add(m_RemoveSelectedButton, gbc);
256   
257    m_AddSelectedButton = new JButton("Add Selected");
258    m_AddSelectedButton.setToolTipText(
259        "Add selected models in the above list to the model library");
260    m_AddSelectedButton.addActionListener(this);
261    gbc.weightx = 1;
262    gbc.fill = GridBagConstraints.HORIZONTAL;
263    gbc.gridx = 1;
264    gbc.gridy = 4;
265    gbc.anchor = GridBagConstraints.WEST;
266    gbc.gridwidth = 1;
267    add(m_AddSelectedButton, gbc);
268   
269    m_AddAllButton = new JButton("Add All");
270    m_AddAllButton.setToolTipText(
271        "Add all models in the above list to the model library");
272    m_AddAllButton.addActionListener(this);
273    gbc.weightx = 1;
274    gbc.fill = GridBagConstraints.HORIZONTAL;
275    gbc.gridx = 2;
276    gbc.gridy = 4;
277    gbc.anchor = GridBagConstraints.WEST;
278    gbc.gridwidth = 1;
279    add(m_AddAllButton, gbc);
280  }
281 
282  /**
283   * Sets the library to use
284   *
285   * @param library     the library to use
286   */
287  public void setLibrary(EnsembleSelectionLibrary library) {
288    m_Library = library;
289    updateDirectoryLabel();
290    loadModels();
291  }
292 
293  /**
294   * This method will load all of the models found in the current working
295   * directory into the ModelList object
296   */
297  public void loadModels() {
298   
299    ModelList.SortedListModel dataModel = ((ModelList.SortedListModel) m_ModelList.getModel());
300   
301    int directoryCount = 0;
302    int modelCount = 0;
303   
304    dataModel.clear();
305   
306    File directory = m_Library.getWorkingDirectory();
307    File subDirectories[] = directory.listFiles();
308   
309    if (subDirectories != null) {
310     
311      for (int i = 0; i < subDirectories.length; i++) {
312       
313        if (subDirectories[i].isDirectory()
314            && subDirectories[i].getName().matches(
315            ".*_instances_.*")) {
316         
317          directoryCount++;
318         
319          File[] subDirectoryFiles = subDirectories[i].listFiles();
320         
321          for (int j = 0; j < subDirectoryFiles.length; j++) {
322           
323            if (subDirectoryFiles[j].getName().matches(".*.elm")) {
324             
325              EnsembleSelectionLibraryModel model = EnsembleSelectionLibraryModel
326              .loadModel(subDirectoryFiles[j].getPath());
327             
328              // get those Classifier[] objects garbage collected!
329              model.releaseModel();
330             
331              if (!dataModel.contains(model)) {
332               
333                modelCount++;
334                dataModel.add(model);
335              }
336            }
337          }
338        }
339      }
340    }
341   
342    updateLoadingLabel(modelCount, directoryCount);
343  }
344 
345  /**
346   * updates the "directory" label
347   */
348  public void updateDirectoryLabel() {
349    if (m_Library != null) {
350      m_DirectoryLabel.setText("Directory: "
351          + m_Library.getWorkingDirectory());
352    }
353  }
354 
355  /**
356   * updates the "loading" label
357   *
358   * @param modelCount          the number of models found
359   * @param directoryCount      the number of directories found
360   */
361  public void updateLoadingLabel(int modelCount, int directoryCount) {
362    m_LoadingLabel.setText(" " + modelCount
363        + " unique model descriptions found in " + directoryCount
364        + " directories");
365  }
366 
367  /**
368   * This will support the button triggered events for this panel.
369   *
370   * @param e           the event
371   */
372  public void actionPerformed(ActionEvent e) {
373   
374    ModelList.SortedListModel dataModel = ((ModelList.SortedListModel) m_ModelList.getModel());
375   
376    if (e.getSource() == m_RefreshListButton) {
377     
378      updateDirectoryLabel();
379      loadModels();
380     
381    } else if (e.getSource() == m_RemoveSelectedButton) {
382     
383      // here we simply get the list of models that are
384      // currently selected and ten remove them from the list
385     
386      Object[] currentModels = m_ModelList.getSelectedValues();
387     
388      for (int i = 0; i < currentModels.length; i++) {
389        dataModel.removeElement(currentModels[i]);
390      }
391     
392      // Shrink the selected range to the first index that was selected
393      if (m_ModelList.getSelectedIndices().length > 0) {
394        int selected[] = new int[1];
395        selected[0] = m_ModelList.getSelectedIndices()[0];
396        m_ModelList.setSelectedIndices(selected);
397      }
398     
399    } else if (e.getSource() == m_AddAllButton) {
400     
401      // here we just need to add all of the models to the
402      // ListModelsPanel object
403     
404      Iterator it = dataModel.iterator();
405     
406      while (it.hasNext()) {
407        EnsembleLibraryModel currentModel = (EnsembleLibraryModel) it
408        .next();
409       
410        m_ListModelsPanel.addModel(currentModel);
411      }
412     
413      dataModel.clear();
414     
415    } else if (e.getSource() == m_AddSelectedButton) {
416     
417      // here we simply get the list of models that are
418      // currently selected and ten remove them from the list
419     
420      Object[] currentModels = m_ModelList.getSelectedValues();
421     
422      for (int i = 0; i < currentModels.length; i++) {
423       
424        m_ListModelsPanel
425        .addModel((EnsembleLibraryModel) currentModels[i]);
426        dataModel.removeElement(currentModels[i]);
427      }
428    }
429  }
430 
431  /**
432   * This property change listener gets fired whenever the library's default
433   * working directory changes. Here we check to see if the user has currently
434   * selected the load models panel. If so then we immediately update the
435   * list. Otherwise, we cache this event in the workingDirectoryChanged field
436   * and reload the model list when the use switches back to this panel. This
437   * is taken care of by the next state changed listener
438   *
439   * @param evt         the event
440   */
441  public void propertyChange(PropertyChangeEvent evt) {
442   
443    if (m_EnsembleLibraryEditor.isLoadModelsPanelSelected()) {
444      updateDirectoryLabel();
445      loadModels();
446    } else {
447      m_workingDirectoryChanged = true;
448    }
449  }
450 
451  /**
452   * this listener event fires when the use switches back to this panel it
453   * checks to se if the working directory has changed since they were here
454   * last. If so then it updates the model list.
455   *
456   * @param e           the event
457   */
458  public void stateChanged(ChangeEvent e) {
459    JTabbedPane pane = (JTabbedPane) e.getSource();
460   
461    if (pane.getSelectedComponent().equals(this)
462        && m_workingDirectoryChanged) {
463     
464      updateDirectoryLabel();
465      loadModels();
466      m_workingDirectoryChanged = false;
467    }
468  }
469}
Note: See TracBrowser for help on using the repository browser.