📄 genericobjectnode.java
字号:
} } } m_UsedPropertyIndexes = new Vector(); m_Editors = new PropertyEditor[m_Properties.length]; m_Values = new Object[m_Properties.length]; m_Names = new String[m_Properties.length]; m_TipTexts = new String[m_Properties.length]; boolean firstTip = true; for (int i = 0; i < m_Properties.length; i++) { // Don't display hidden or expert properties. if (m_Properties[i].isHidden() || m_Properties[i].isExpert()) { continue; } m_Names[i] = m_Properties[i].getDisplayName(); Class type = m_Properties[i].getPropertyType(); Method getter = m_Properties[i].getReadMethod(); Method setter = m_Properties[i].getWriteMethod(); // Only display read/write properties. if (getter == null || setter == null) { continue; } try { Object args[] = {}; Object value = getter.invoke(classifier, args); m_Values[i] = value; PropertyEditor editor = null; Class pec = m_Properties[i].getPropertyEditorClass(); if (pec != null) { try { editor = (PropertyEditor) pec.newInstance(); } catch (Exception ex) { // Drop through. } } if (editor == null) { editor = PropertyEditorManager.findEditor(type); } m_Editors[i] = editor; // If we can't edit this component, skip it. if (editor == null) { continue; } if (editor instanceof GenericObjectEditor) { ((GenericObjectEditor) editor).setClassType(type); } // Don't try to set null values: if (value == null) { continue; } editor.setValue(value); // now look for a TipText method for this property String tipName = m_Names[i] + "TipText"; for (int j = 0; j < m_Methods.length; j++) { String mname = m_Methods[j].getDisplayName(); Method meth = m_Methods[j].getMethod(); if (mname.equals(tipName)) { if (meth.getReturnType().equals(String.class)) { try { String tempTip = (String) (meth.invoke( classifier, args)); int ci = tempTip.indexOf('.'); if (ci < 0) { m_TipTexts[i] = tempTip; } else { m_TipTexts[i] = tempTip.substring(0, ci); } if (m_HelpText != null) { if (firstTip) { m_HelpText.append("OPTIONS\n"); firstTip = false; } m_HelpText.append(m_Names[i]).append(" -- "); m_HelpText.append(tempTip).append("\n\n"); } } catch (Exception ex) { } break; } } } //Here we update the usedPropertyIndexes variable so that //later on we will know which ones to look at. m_UsedPropertyIndexes.add(new Integer(i)); int currentCount = m_TreeModel.getChildCount(this); //Now we make a child node and add it to the tree underneath //this one PropertyNode newNode = new PropertyNode(m_Tree, m_ParentPanel, m_Names[i], m_TipTexts[i], m_Values[i], m_Editors[i]); m_TreeModel.insertNodeInto(newNode, this, currentCount); } catch (InvocationTargetException ex) { System.err.println("Skipping property " + m_Names[i] + " ; exception on target: " + ex.getTargetException()); ex.getTargetException().printStackTrace(); continue; } catch (Exception ex) { System.err.println("Skipping property " + m_Names[i] + " ; exception: " + ex); ex.printStackTrace(); continue; } } //Finally we tell the TreeModel to update itself so the changes //will be visible m_TreeModel.nodeStructureChanged(this); } /** * This method iterates over all of the child nodes of this * GenericObjectNode and requests the verious sets of values that the * user has presumably specified. Once these sets of values are * * @return a Vector consisting of all parameter combinations */ public Vector getValues() { Vector valuesVector = new Vector(); int childCount = m_TreeModel.getChildCount(this); //poll all child nodes for their values. for (int i = 0; i < childCount; i++) { PropertyNode currentChild = (PropertyNode) m_TreeModel.getChild( this, i); Vector v = currentChild.getAllValues(); valuesVector.add(v); } //Need to initialize the working set of paramter combinations m_WorkingSetCombinations = new Vector(); //obtain all combinations of the paremeters combineAllValues(new Vector(), valuesVector); /* //nice for initially debugging this - and there was a WHOLE lot of //that going on till this crazy idea finally worked. for (int i = 0; i < m_WorkingSetCombinations.size(); i++) { System.out.print("Combo "+i+": "); Vector current = (Vector)m_WorkingSetCombinations.get(i); for (int j = 0; j < current.size(); j++) { System.out.print(current.get(j)+"\t"); } System.out.print("\n"); } */ //Now the real work begins. Here we need to translate all of the values //received from the editors back into the actual class types that the //Weka classifiers will understand. for example, String values for //enumerated values need to be turned back into the SelectedTag objects //that classifiers understand. //This vector will hold all of the actual generic objects that are being //instantiated Vector newGenericObjects = new Vector(); for (int i = 0; i < m_WorkingSetCombinations.size(); i++) { Vector current = (Vector) m_WorkingSetCombinations.get(i); //create a new copy of this class. We will use this copy to test whether //the current set of parameters is valid. Object o = this.getUserObject(); Class c = o.getClass(); Object copy = null; try { copy = c.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } for (int j = 0; j < current.size(); j++) { Object[] args = new Object[1]; int index = ((Integer) m_UsedPropertyIndexes.get(j)).intValue(); PropertyDescriptor property = (PropertyDescriptor) m_Properties[index]; Method setter = property.getWriteMethod(); Class[] params = setter.getParameterTypes(); Object currentVal = current.get(j); //System.out.println(currentVal.getClass().toString()); //we gotta turn strings back into booleans if (params.length == 1 && params[0].toString().equals("boolean") && currentVal.getClass().toString().equals( "class java.lang.String")) { currentVal = new Boolean((String) current.get(j)); } //we gotta turn strings back into "Tags" if (params.length == 1 && params[0].toString().equals( "class weka.core.SelectedTag") && currentVal.getClass().toString().equals( "class java.lang.String")) { String tagString = (String) current.get(j); m_Editors[index].setAsText(tagString); currentVal = m_Editors[index].getValue(); } args[0] = currentVal; /* System.out.print("setterName: "+setter.getName()+ " editor class: "+m_Editors[index].getClass()+ " params: "); for (int k = 0; k < params.length; k++) System.out.print(params[k].toString()+" "); System.out.println(" value class: "+args[0].getClass().toString()); */ try { //we tell the setter for the current parameter to update the copy //with the current parameter value setter.invoke(copy, args); } catch (InvocationTargetException ex) { if (ex.getTargetException() instanceof PropertyVetoException) { String message = "WARNING: Vetoed; reason is: " + ex.getTargetException().getMessage(); System.err.println(message); Component jf; jf = m_ParentPanel.getRootPane(); JOptionPane.showMessageDialog(jf, message, "error", JOptionPane.WARNING_MESSAGE); if (jf instanceof JFrame) ((JFrame) jf).dispose(); } else { System.err.println(ex.getTargetException().getClass() .getName() + " while updating " + property.getName() + ": " + ex.getTargetException().getMessage()); Component jf; jf = m_ParentPanel.getRootPane(); JOptionPane.showMessageDialog(jf, ex .getTargetException().getClass().getName() + " while updating " + property.getName() + ":\n" + ex.getTargetException().getMessage(), "error", JOptionPane.WARNING_MESSAGE); if (jf instanceof JFrame) ((JFrame) jf).dispose(); } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } //At this point we have set all the parameters for this GenericObject //with a single combination that was generated from the //m_WorkingSetCombinations Vector and can add it to the collection that //will be returned newGenericObjects.add(copy); } return newGenericObjects; } /** This method is responsible for returning all possible values through * a recursive loop. * * When the recursion terminates that means that there are no more parameter * sets to branch out through so all we have to do is save the current Vector * in the working set of combinations. Otherwise we iterate through all * possible values left in the next set of parameter values and recursively * call this function. * * @param previouslySelected stores the values chosen in this branch of the recursion * @param remainingValues the sets of values left */ public void combineAllValues(Vector previouslySelected, Vector remainingValues) { if (remainingValues.isEmpty()) { m_WorkingSetCombinations.add(previouslySelected); return; } Vector currentSet = new Vector((Vector) remainingValues.get(0)); Vector tmpRemaining = new Vector(remainingValues); tmpRemaining.removeElementAt(0); for (int i = 0; i < currentSet.size(); i++) { Vector tmpPreviouslySelected = new Vector(previouslySelected); tmpPreviouslySelected.add(currentSet.get(i)); combineAllValues(tmpPreviouslySelected, tmpRemaining); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -