📄 addmodelspanel.java
字号:
"Remove all invalid (red) models from the above list"); m_RemoveInvalidButton.addActionListener(this); gbc.weightx = 0; gbc.fill = GridBagConstraints.NONE; gbc.gridx = 2; gbc.gridy = 1; gbc.anchor = GridBagConstraints.WEST; gbc.gridwidth = 1; //OK, this button was removed because we thought it was a waste //of space. Instead of removing invalid models, we just explicitly //prevent the user from adding them to the main list. I'm going to //leave the code in with this final "add" statement commented out //because we are still on the fence as to whether this is a good //idea //add(m_RemoveInvalidButton, gbc); m_ModelList = new ModelList(); m_ModelList.getInputMap().put( KeyStroke.getKeyStroke("released DELETE"), "deleteSelected"); m_ModelList.getActionMap().put("deleteSelected", new AbstractAction("deleteSelected") { public void actionPerformed(ActionEvent evt) { Object[] currentModels = m_ModelList.getSelectedValues(); ModelList.SortedListModel dataModel = ((ModelList.SortedListModel) m_ModelList.getModel()); for (int i = 0; i < currentModels.length; i++) { dataModel.removeElement((EnsembleLibraryModel) currentModels[i]); } //Shrink the selected range to the first index that was selected int selected[] = new int[1]; selected[0] = m_ModelList.getSelectedIndices()[0]; m_ModelList.setSelectedIndices(selected); } }); m_ModelList .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); m_ModelList.setLayoutOrientation(JList.VERTICAL); m_ModelList.setVisibleRowCount(-1); JPanel modelListPanel = new JPanel(); modelListPanel.setBorder( BorderFactory.createTitledBorder("Working Set of Newly Generated Models")); JScrollPane listView = new JScrollPane(m_ModelList); //listView.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); listView.setPreferredSize(new Dimension(150, 50)); modelListPanel.setLayout(new BorderLayout()); modelListPanel.add(listView, BorderLayout.CENTER); gbc.weightx = 1; gbc.weighty = 1; gbc.fill = GridBagConstraints.BOTH; gbc.gridx = 0; gbc.gridy = 2; gbc.gridwidth = 3; gbc.anchor = GridBagConstraints.WEST; add(modelListPanel, gbc); m_RemoveSelectedButton = new JButton("Remove Selected"); m_RemoveSelectedButton.setToolTipText("Remove all currently selected models from the above list"); m_RemoveSelectedButton.addActionListener(this); gbc.weightx = 1; gbc.weighty = 0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridx = 0; gbc.gridy = 3; gbc.anchor = GridBagConstraints.WEST; gbc.gridwidth = 1; add(m_RemoveSelectedButton, gbc); m_AddSelectedButton = new JButton("Add Selected"); m_AddSelectedButton.setToolTipText( "Add selected models in the above list to the model library"); m_AddSelectedButton.addActionListener(this); gbc.weightx = 1; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridx = 1; gbc.gridy = 3; gbc.anchor = GridBagConstraints.WEST; gbc.gridwidth = 1; add(m_AddSelectedButton, gbc); m_AddAllButton = new JButton("Add All"); m_AddAllButton.setToolTipText( "Add all models in the above list to the model library"); m_AddAllButton.addActionListener(this); gbc.weightx = 1; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridx = 2; gbc.gridy = 3; gbc.anchor = GridBagConstraints.WEST; gbc.gridwidth = 1; add(m_AddAllButton, gbc); } /** * This method necessarily seperates the process of building the * tree object from the rest of the GUI construction. In order to * prevent all kinds of strange garbage collection problems, we take * the conservative approach of gutting and rebuilding the JTree * every time a new classifier is chosen for the root node. * * @param classifier the classifier to build the tree for */ public void buildClassifierTree(Classifier classifier) { //This block sets up the root node of the tree. Note that //the constructor for the GenericObjectNode will take care //of creating all of the child nodes containing the node //properties GenericObjectEditor classifierEditor = new GenericObjectEditor(); classifierEditor.setClassType(Classifier.class); classifierEditor.setValue(classifier); GenericObjectNode rootNode = new GenericObjectNode(this, classifier, classifierEditor, "Current Classifier"); m_TreeModel = new DefaultTreeModel(rootNode); m_Tree = new JTree(m_TreeModel); rootNode.setTree(m_Tree); rootNode.updateTree(); m_Tree.setRootVisible(true); ModelTreeNodeRenderer renderer = new ModelTreeNodeRenderer(); m_Tree.setCellRenderer(renderer); m_Tree.setCellEditor(new ModelTreeNodeEditor(m_Tree)); m_Tree.setEditable(true); m_Tree.setVisibleRowCount(8); //ToolTipManager.sharedInstance().registerComponent(m_Tree); //This "tentatively seems to work better: m_Tree.setRowHeight(0); m_TreeView.setViewportView(m_Tree); } /** * This will support the button triggered events for this panel. * * @param e the event */ public void actionPerformed(ActionEvent e) { ModelList.SortedListModel dataModel = ((ModelList.SortedListModel) m_ModelList.getModel()); if (e.getSource() == m_GenerateButton) { //here we want to generate all permutations of the //options specified and then add each of them to the //model list panel Vector models = ((GenericObjectNode) m_TreeModel.getRoot()).getValues(); int total = models.size(); int invalid = 0; for (int i = 0; i < models.size(); i++) { Classifier classifier = (Classifier) models.get(i); //This method will invoke the classifier's setOptions //method to see if the current set of options was //valid. EnsembleLibraryModel model = m_ListModelsPanel.getLibrary().createModel(classifier); model.testOptions(); if (!model.getOptionsWereValid()) invalid++; dataModel.add(model); } //update the message text with model generation info String generateString = new String(" " + total + " models generated"); generateString += ", " + invalid + " had errors"; m_GenerateLabel.setText(generateString); } else if (e.getSource() == m_RemoveSelectedButton) { //here we simply get the list of models that are //currently selected and ten remove them from the list Object[] currentModels = m_ModelList.getSelectedValues(); for (int i = 0; i < currentModels.length; i++) { dataModel.removeElement(currentModels[i]); } //Shrink the selected range to the first index that was selected if (m_ModelList.getSelectedIndices().length > 0) { int selected[] = new int[1]; selected[0] = m_ModelList.getSelectedIndices()[0]; m_ModelList.setSelectedIndices(selected); } } else if (e.getSource() == m_RemoveInvalidButton) { //here we simply remove all the models that were not //valid Vector toRemove = new Vector(); for (int i = 0; i < dataModel.getSize(); i++) { EnsembleLibraryModel currentModel = (EnsembleLibraryModel) dataModel.getElementAt(i); if (!currentModel.getOptionsWereValid()) { toRemove.add(currentModel); } } for (int i = 0; i < toRemove.size(); i++) dataModel.removeElement(toRemove.get(i)); } else if (e.getSource() == m_AddAllButton) { //here we just need to add all of the models to the //ListModelsPanel object Iterator it = dataModel.iterator(); while (it.hasNext()) { EnsembleLibraryModel currentModel = (EnsembleLibraryModel) it.next(); if (currentModel.getOptionsWereValid()) { m_ListModelsPanel.addModel(currentModel); } } int size = dataModel.getSize(); for (int i = 0; i < size; i++) { dataModel.removeElement(dataModel.getElementAt(0)); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -